Ruby中用于"String #include?"的算法

Joh*_*ich 10 ruby algorithm search substring

有人能够确定哪个算法用于包含?Ruby中的方法?例如

"helloworld".include?("hello")
Run Code Online (Sandbox Code Playgroud)

rdv*_*ijk 11

作为浮雕陈述在他的回答,String#include电话rb_str_index.根据这篇文章,这个函数依次调用rb_memsearch,它实现了 Rabin-Karp字符串搜索算法.ruby-forum.com


Jör*_*tag 7

Ruby语言规范没有规定任何特定的算法.每个实现都可以使用他们想要的算法.

例如,在Rubinius中,String#include?调用String#find_string:

def include?(needle)
  if needle.kind_of? Fixnum
    needle = needle % 256
    str_needle = needle.chr
  else
    str_needle = StringValue(needle)
  end

  !!find_string(str_needle, 0)
end
Run Code Online (Sandbox Code Playgroud)

String#find_string反过来通过string_index原语实现:

def find_string(pattern, start)
  Rubinius.primitive :string_index
  raise PrimitiveFailure, "String#find_string failed"
end
Run Code Online (Sandbox Code Playgroud)

string_index原语由实现rubinius::String::index功能:

// Rubinius.primitive :string_index
Fixnum* index(STATE, String* pattern, Fixnum* start);
Run Code Online (Sandbox Code Playgroud)

rubinius::String::index:

Fixnum* String::index(STATE, String* pattern, Fixnum* start) {
  native_int total = size();
  native_int match_size = pattern->size();

  if(start->to_native() < 0) {
    Exception::argument_error(state, "negative start given");
  }

  switch(match_size) {
  case 0:
    return start;
  case 1:
    {
      uint8_t* buf = byte_address();
      uint8_t matcher = pattern->byte_address()[0];

      for(native_int pos = start->to_native(); pos < total; pos++) {
        if(buf[pos] == matcher) return Fixnum::from(pos);
      }
    }
    return nil<Fixnum>();
  default:
    {
      uint8_t* buf = byte_address();
      uint8_t* matcher = pattern->byte_address();

      uint8_t* last = buf + (total - match_size);
      uint8_t* pos = buf + start->to_native();

      while(pos <= last) {
        // Checking *pos directly then also checking memcmp is an
        // optimization. It's about 10x faster than just calling memcmp
        // everytime.
        if(*pos == *matcher &&
            memcmp(pos, matcher, match_size) == 0) {
          return Fixnum::from(pos - buf);
        }
        pos++;
      }
    }
    return nil<Fixnum>();
  }
}
Run Code Online (Sandbox Code Playgroud)


emb*_*oss 6

这是实际执行String#include?:

static VALUE
rb_str_include(VALUE str, VALUE arg)
{
    long i;

    StringValue(arg);
    i = rb_str_index(str, arg, 0);

    if (i == -1) return Qfalse;
    return Qtrue;
}
Run Code Online (Sandbox Code Playgroud)

因此,使用的实际算法可以在rb_str_index中找到.

  • 其中反过来使用`rb_memsearch`,它实现了[Karp Rabin](http://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_string_search_algorithm)算法(根据[this post](http:// www) .ruby-forum.com /主题/ 87830)). (5认同)