Skip to content

Instantly share code, notes, and snippets.

@frostney
Last active December 10, 2015 16:38
Show Gist options
  • Save frostney/4461762 to your computer and use it in GitHub Desktop.
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/
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