Last active
March 2, 2017 23:00
-
-
Save aziis98/2fbbbe60c8cc88c576524ab2b2365ab0 to your computer and use it in GitHub Desktop.
An alternative to the common java Scanner class in Kotlin
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 BasicScanner(val source: String, var carret: Int = 0) { | |
fun jumpAll(predicate: (Char) -> Boolean): String { | |
return StringBuilder().apply { | |
var current = seeNextChar() | |
while (predicate(current)) { | |
append(current) | |
carret++ | |
if (hasMore()) | |
current = seeNextChar() | |
else | |
break | |
} | |
}.toString() | |
} | |
fun seeNextChar(): Char { | |
return source[carret] | |
} | |
fun assertNext(string: String) { | |
assert(source.substring(carret, carret + string.length) == string) | |
carret += string.length | |
} | |
fun getUntil(terminator: String): String { | |
return source.substring(carret, source.indexOf(terminator, carret)).apply { | |
carret += length + terminator.length | |
} | |
} | |
fun isNext(string: String): Boolean { | |
if (source.length < carret + string.length) | |
return false | |
else | |
return source.substring(carret, carret + string.length) == string | |
} | |
fun hasMore(): Boolean { | |
return carret < source.length | |
} | |
override fun toString(): String { | |
return "BasicScanner(distanceFromEnd=${source.length - carret})" | |
} | |
} |
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
MIT License | |
Copyright (c) 2017 Antonio De Lucreziis | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment