Last active
December 12, 2015 08:49
-
-
Save frostney/4746769 to your computer and use it in GitHub Desktop.
StringBuilder class in CoffeeScript
This file contains hidden or 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
do (root = if exports? and module.exports? then module.exports else @) -> | |
class root.StringReader | |
constructor: (@input) -> | |
@buffer = "" | |
@reset() | |
readUntil: (character, omitCharacter = false, notFoundFn) -> | |
loop | |
if @end() | |
notFoundFn(@buffer) if notFoundFn | |
break | |
if character.length is 1 | |
break if @input[@currentIndex] is character | |
else | |
skip = true | |
counter = 0 | |
for c in character when @input[@currentIndex + counter] isnt 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 >= @input.length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment