Skip to content

Instantly share code, notes, and snippets.

@frostney
Last active December 12, 2015 08:49
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 frostney/4746769 to your computer and use it in GitHub Desktop.
Save frostney/4746769 to your computer and use it in GitHub Desktop.
StringBuilder class in CoffeeScript
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