Created
July 22, 2011 00:46
-
-
Save tastycode/1098589 to your computer and use it in GitHub Desktop.
Levenshtein C extension Draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mkmf' | |
dir_config('string'); | |
create_makefile('string'); | |
~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ruby.h> | |
#include <string.h> | |
VALUE method_levenshtein(cls,strb) | |
{ | |
VALUE stra = cls; | |
if (FIX2INT(rb_str_length(stra))>FIX2INT(rb_str_length(strb))) { | |
VALUE strt = stra; | |
stra = strb; | |
strb = strt; | |
} | |
int distance = FIX2INT(rb_str_length(strb))-FIX2INT(rb_str_length(stra)); | |
char* cstra = StringValuePtr(stra); | |
char* cstrb = StringValuePtr(strb); | |
int i=0; | |
for (i=0;i<strlen(cstra);i++) { | |
if (cstrb[i]==cstra[i]) continue; | |
distance++; | |
} | |
return INT2FIX(distance); | |
} | |
void Init_string() { | |
VALUE String = rb_define_class("String",rb_cObject); | |
rb_define_method(String,"levenshtein",method_levenshtein,1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment