+
+ inline do |builder|
+ add_ambiguity_macro(builder)
+
+ # C method for determining if two strings of equal length match
+ # given a maximum allowed mismatches and allowing for IUPAC
+ # ambiguity codes. Returns true if match, else false.
+ builder.c %{
+ VALUE match_C(
+ VALUE _string1, // String 1
+ VALUE _string2, // String 2
+ VALUE _offset1, // Offset 1
+ VALUE _offset2, // Offset 2
+ VALUE _length, // String length
+ VALUE _max_mismatch // Maximum mismatches
+ )
+ {
+ char *string1 = StringValuePtr(_string1);
+ char *string2 = StringValuePtr(_string2);
+ unsigned int offset1 = FIX2UINT(_offset1);
+ unsigned int offset2 = FIX2UINT(_offset2);
+ unsigned int length = FIX2UINT(_length);
+ unsigned int max_mismatch = FIX2UINT(_max_mismatch);
+
+ unsigned int max_match = length - max_mismatch;
+ unsigned int match = 0;
+ unsigned int mismatch = 0;
+ unsigned int i = 0;
+
+ for (i = 0; i < length; i++)
+ {
+ if (MATCH(string1[i + offset1], string2[i + offset2]))
+ {
+ match++;
+
+ if (match >= max_match) {
+ return Qtrue;
+ }
+ }
+ else
+ {
+ mismatch++;
+
+ if (mismatch > max_mismatch) {
+ return Qfalse;
+ }
+ }
+ }
+
+ return Qfalse;
+ }
+ }
+ end