Class: Squib::EmbeddingUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/squib/graphics/embedding_utils.rb

Class Method Summary collapse

Class Method Details

.indices(str, keys) ⇒ Object

Given a string and a bunch of keys, give us back a mapping of those keys to where those keys start, and where they end (in ranges)

See the spec for expected outputs



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/squib/graphics/embedding_utils.rb', line 8

def self.indices(str, keys)
  map = {}
  keys.each do |key|
    map[key] ||= []
    start = 0
    while true
      idx = str.index(key, start)
      if idx.nil?
        break; # done searching
      else
        idx_bytes = str[0..idx].bytesize - 1
        map[key] << (idx_bytes..(idx_bytes + key.size))
        start = idx + 1
      end
    end
  end
  return map
end