Module: FuzzyMatcher

Defined in:
lib/fuzzy_matcher.rb,
lib/fuzzy_matcher/version.rb

Defined Under Namespace

Modules: FuzzyBinding Classes: Error

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.find(source, targets) ⇒ Array<String>

Note:

This method possibly is not thread safe.

Note:

This method is case sensitive. For case insensitive matching, downcase targets/source or use a case insensitive matcher (#find_fold)

find() will return a list of strings in targets that fuzzy matches source.

Examples:

require 'fast_fuzzy_matcher'
FuzzyMatch.find("whl", ["cartwheel", "foobar", "wheel", "baz"])
=> ["cartwheel", "wheel"]

Parameters:

  • source (String)

    The string to match against.

  • targets (Array<String>)

    The strings to match.

Returns:

  • (Array<String>)

    The strings in targets that fuzzy match source.

See Also:

  • for the implementation of this method.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fuzzy_matcher.rb', line 26

def self.find(source, targets)
  pointers = targets.map { |t| FFI::MemoryPointer.from_string(t) }
  targets_ptr = FFI::MemoryPointer.new(:pointer, targets.size)
  targets_ptr.write_array_of_pointer(pointers)

  result_ptr = FuzzyBinding.Find(source, targets_ptr, targets.size)

  return [] if result_ptr.null?

  pointers_array = result_ptr.read_array_of_pointer(targets.size)

  result_array = pointers_array.each_with_object([]) do |ptr, arr|
    if ptr && !ptr.null?
      value = ptr.read_string_to_null
      arr << value unless value.nil? || value == ""
    end
  end

  FuzzyBinding.free_cstrings(result_ptr, targets.size)

  FFI::MemoryPointer.new(:pointer).write_pointer(result_ptr).free

  result_array
end