From: "NARUSE, Yui" Date: 2010-01-11T16:32:21+09:00 Subject: [ruby-dev:40033] Re: [Feature #2571] 文字列のハミング距離 成瀬です。 パッチが誤っていました、これだけですね。 diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 14e23b8..116afa5 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -662,6 +662,7 @@ VALUE rb_str_associated(VALUE); void rb_str_setter(VALUE, ID, VALUE*); VALUE rb_str_intern(VALUE); VALUE rb_sym_to_s(VALUE); +long rb_str_strlen(VALUE); VALUE rb_str_length(VALUE); long rb_str_offset(VALUE, long); size_t rb_str_capacity(VALUE); diff --git a/string.c b/string.c index 2dcc330..78620b8 100644 --- a/string.c +++ b/string.c @@ -1079,6 +1079,12 @@ str_strlen(VALUE str, rb_encoding *enc) return n; } +long +rb_str_strlen(VALUE str) +{ + return str_strlen(str, STR_ENC_GET(str)); +} + /* * call-seq: * str.length => integer (2010/01/11 16:27), NARUSE, Yui wrote: > 成瀬です。 > > (2010/01/11 16:17), Yukihiro Matsumoto wrote: >> まつもと ゆきひろです >> >> In message "Re: [ruby-dev:40029] Re: [Feature #2571] 文字列のハミング距離" >> on Mon, 11 Jan 2010 15:53:55 +0900, "NARUSE, Yui" writes: >> >> |(2010/01/11 15:35), Kenta Murata wrote: >> |> ところで、拡張ライブラリにする際に、str_strlen を公開 API にしてもらいたいのですが、 >> |> それは可能でしょうか? rb_str_strlen になるのかな? >> | >> |名前がちょっと悩む所なんですがとりあえず、 >> >> 公開には賛成です。しかし、rb_str_strlen()とする時には、 >> >> str_strlen(str, STR_ENC_GET(str)) >> >> を返す1引数の関数にするべきでは。VALUEを与えているのに >> encodingも外から渡す必要はないでしょう。 > > 確かに、修正しました。 > > diff --git a/include/ruby/intern.h b/include/ruby/intern.h > index 14e23b8..116afa5 100644 > --- a/include/ruby/intern.h > +++ b/include/ruby/intern.h > @@ -662,6 +662,7 @@ VALUE rb_str_associated(VALUE); > void rb_str_setter(VALUE, ID, VALUE*); > VALUE rb_str_intern(VALUE); > VALUE rb_sym_to_s(VALUE); > +long rb_str_strlen(VALUE); > VALUE rb_str_length(VALUE); > long rb_str_offset(VALUE, long); > size_t rb_str_capacity(VALUE); > diff --git a/string.c b/string.c > index 2dcc330..a4a6122 100644 > --- a/string.c > +++ b/string.c > @@ -1079,6 +1079,12 @@ str_strlen(VALUE str, rb_encoding *enc) > return n; > } > > +long > +rb_str_strlen(VALUE str) > +{ > + return rb_str_strlen(str, STR_ENC_GET(str)); > +} > + > /* > * call-seq: > * str.length => integer > @@ -1092,7 +1098,7 @@ rb_str_length(VALUE str) > { > long len; > > - len = str_strlen(str, STR_ENC_GET(str)); > + len = rb_str_strlen(str, STR_ENC_GET(str)); > return LONG2NUM(len); > } > > @@ -1573,11 +1579,11 @@ rb_str_substr(VALUE str, long beg, long len) > goto sub; > } > else { > - beg += str_strlen(str, enc); > + beg += rb_str_strlen(str, enc); > if (beg < 0) return Qnil; > } > } > - else if (beg > 0 && beg > str_strlen(str, enc)) { > + else if (beg > 0 && beg > rb_str_strlen(str, enc)) { > return Qnil; > } > if (len == 0) { > @@ -2266,8 +2272,8 @@ rb_str_index(VALUE str, VALUE sub, long offset) > if (is_broken_string(sub)) { > return -1; > } > - len = str_strlen(str, enc); > - slen = str_strlen(sub, enc); > + len = rb_str_strlen(str, enc); > + slen = rb_str_strlen(sub, enc); > if (offset < 0) { > offset += len; > if (offset < 0) return -1; > @@ -2329,7 +2335,7 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str) > pos = 0; > } > if (pos < 0) { > - pos += str_strlen(str, STR_ENC_GET(str)); > + pos += rb_str_strlen(str, STR_ENC_GET(str)); > if (pos < 0) { > if (TYPE(sub) == T_REGEXP) { > rb_backref_set(Qnil); > @@ -2340,7 +2346,7 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str) > > switch (TYPE(sub)) { > case T_REGEXP: > - if (pos > str_strlen(str, STR_ENC_GET(str))) > + if (pos > rb_str_strlen(str, STR_ENC_GET(str))) > return Qnil; > pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos, > rb_enc_check(str, sub), single_byte_optimizable(str)); > @@ -2382,8 +2388,8 @@ rb_str_rindex(VALUE str, VALUE sub, long pos) > if (is_broken_string(sub)) { > return -1; > } > - len = str_strlen(str, enc); > - slen = str_strlen(sub, enc); > + len = rb_str_strlen(str, enc); > + slen = rb_str_strlen(sub, enc); > /* substring longer than string */ > if (len < slen) return -1; > if (len - pos < slen) { > @@ -2433,7 +2439,7 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str) > VALUE sub; > VALUE vpos; > rb_encoding *enc = STR_ENC_GET(str); > - long pos, len = str_strlen(str, enc); > + long pos, len = rb_str_strlen(str, enc); > > if (rb_scan_args(argc, argv, "11", &sub, &vpos) == 2) { > pos = NUM2LONG(vpos); > @@ -2981,7 +2987,7 @@ rb_str_aref(VALUE str, VALUE indx) > long beg, len; > VALUE tmp; > > - len = str_strlen(str, STR_ENC_GET(str)); > + len = rb_str_strlen(str, STR_ENC_GET(str)); > switch (rb_range_beg_len(indx, &beg, &len, len, 0)) { > case Qfalse: > break; > @@ -3136,7 +3142,7 @@ rb_str_splice(VALUE str, long beg, long len, VALUE val) > > StringValue(val); > enc = rb_enc_check(str, val); > - slen = str_strlen(str, enc); > + slen = rb_str_strlen(str, enc); > > if (slen < beg) { > out_of_range: > @@ -3232,14 +3238,14 @@ rb_str_aset(VALUE str, VALUE indx, VALUE val) > rb_raise(rb_eIndexError, "string not matched"); > } > beg = rb_str_sublen(str, beg); > - rb_str_splice(str, beg, str_strlen(indx, 0), val); > + rb_str_splice(str, beg, rb_str_strlen(indx, 0), val); > return val; > > default: > /* check if indx is Range */ > { > long beg, len; > - if (rb_range_beg_len(indx, &beg, &len, str_strlen(str, 0), 2)) { > + if (rb_range_beg_len(indx, &beg, &len, rb_str_strlen(str, 0), 2)) { > rb_str_splice(str, beg, len, val); > return val; > } > @@ -6605,13 +6611,13 @@ rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag) > enc = rb_enc_check(str, pad); > f = RSTRING_PTR(pad); > flen = RSTRING_LEN(pad); > - fclen = str_strlen(pad, enc); > + fclen = rb_str_strlen(pad, enc); > singlebyte = single_byte_optimizable(pad); > if (flen == 0 || fclen == 0) { > rb_raise(rb_eArgError, "zero width padding"); > } > } > - len = str_strlen(str, enc); > + len = rb_str_strlen(str, enc); > if (width < 0 || len >= width) return rb_str_dup(str); > n = width - len; > llen = (jflag == 'l') ? 0 : ((jflag == 'r') ? n : n/2); > @@ -6829,7 +6835,7 @@ rb_str_rpartition(VALUE str, VALUE sep) > } > return rb_ary_new3(3, rb_str_substr(str, 0, pos), > sep, > - rb_str_substr(str,pos+str_strlen(sep,STR_ENC_GET(sep)),RSTRING_LEN(str))); > + rb_str_substr(str,pos+rb_str_strlen(sep,STR_ENC_GET(sep)),RSTRING_LEN(str))); > } > > /* > -- NARUSE, Yui