Created
January 12, 2010 17:43
-
-
Save documentcloud/275399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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