Module: Fzy

Defined in:
lib/fzy.rb,
lib/fzy/version.rb,
ext/fzy/fzy.c

Defined Under Namespace

Classes: Error, Match

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.has_match?(needle, haystack) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
# File 'ext/fzy/fzy.c', line 6

static VALUE
rb_has_match(VALUE _, VALUE needle, VALUE haystack) {
  const char *needle_str = StringValueCStr(needle);
  const char *haystack_str = StringValueCStr(haystack);
  int match = has_match(needle_str, haystack_str);

  return match ? Qtrue : Qfalse;
}

.match(needle, haystack) ⇒ Object



22
23
24
25
26
# File 'lib/fzy.rb', line 22

def self.match(needle, haystack)
  if has_match?(needle, haystack)
    Match.new(needle, haystack)
  end
end

.positions(needle, haystack) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'ext/fzy/fzy.c', line 24

static VALUE
rb_positions(VALUE _, VALUE needle, VALUE haystack) {
  const char *needle_str = StringValueCStr(needle);
  const char *haystack_str = StringValueCStr(haystack);
  size_t size = RSTRING_LEN(needle);
  VALUE arr = rb_ary_new2(size);

  size_t *positions = xcalloc(size, sizeof(size_t));
  match_positions(needle_str, haystack_str, positions);

  for (size_t i = 0; i < size; i++) {
    rb_ary_push(arr, ULL2NUM(positions[i]));
  }

  xfree(positions);
  return arr;
}

.score(needle, haystack) ⇒ Object



15
16
17
18
19
20
21
22
# File 'ext/fzy/fzy.c', line 15

static VALUE
rb_score(VALUE _, VALUE needle, VALUE haystack) {
  const char *needle_str = StringValueCStr(needle);
  const char *haystack_str = StringValueCStr(haystack);
  double score = match(needle_str, haystack_str);

  return DBL2NUM(score);
}