Skip to content

Instantly share code, notes, and snippets.

@documentcloud
Created January 12, 2010 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save documentcloud/275399 to your computer and use it in GitHub Desktop.
Save documentcloud/275399 to your computer and use it in GitHub Desktop.
def scan_for(matcher, text)
scanner = StringScanner.new(text)
last_byte, last_char = 0, 0
while scanner.scan_until(matcher)
byte = scanner.pos - scanner.matched.length
char = last_char + text[last_byte..byte].unpack('U*').length - 1
length = scanner.matched.unpack('U*').length
last_byte, last_char = byte, char
# Do something with the "char" offset and "length"...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment