Last active
December 10, 2015 16:38
-
-
Save frostney/4461762 to your computer and use it in GitHub Desktop.
Simple StringReader class in CoffeeScript which can be used to extract specific tokens from a bigger string. Inspired by http://brandoncodes.wordpress.com/2012/04/07/add-some-syntactical-sugar-to-your-coffeescript/
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
class StringReader | |
buffer = "" | |
constructor: (@input) -> | |
@reset() | |
readUntil: (character, omitCharacter = false) -> | |
loop | |
break if @currentIndex is @input.length | |
if character.length is 1 | |
break if @input[@currentIndex] is character | |
else | |
skip = true | |
counter = 0 | |
for c in character | |
unless @input[@currentIndex + counter] is c | |
skip = false | |
break | |
counter++ | |
break if skip | |
buffer += @input[@currentIndex] | |
@currentIndex++ | |
@currentIndex += character.length if (omitCharacter) | |
buffer | |
clearBuffer: -> buffer = "" | |
reset: -> | |
@currentIndex = 0 | |
@clearBuffer() | |
end: -> @currentIndex is @input.length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment