Skip to content

Commit

Permalink
1.3.0
Browse files Browse the repository at this point in the history
- Add `StringReader.expect(String)`
- Add `StringReader.resetTo(String)`
- Fix `StringReader.readUntil`
  • Loading branch information
Juuxel committed Feb 15, 2020
1 parent 3c180f9 commit 7f414bb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

Using the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format.

## [1.3.0] - 2020-02-15
### Added
- `StringReader.expect(String)`
- Expects the passed string to be at the cursor.
- `StringReader.resetTo(String)`
- Changes the source and resets the cursor.

### Fixed
- `StringReader.readUntil()` moved the cursor when `inclusive = false` and the string was empty.

## [1.2.0] - 2020-02-13
### Added
- `StringReader.getRemaining()`
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "io.github.juuxel"
version = "1.2.0"
version = "1.3.0"

repositories {
jcenter()
Expand Down
45 changes: 40 additions & 5 deletions src/main/kotlin/juuxel/leafthrough/StringReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ package juuxel.leafthrough
* single chars.
*
* Inspired by the `StringReader` found in [Brigadier](/~https://github.com/Mojang/brigadier).
*
* @property source the source string to read from
*/
class StringReader(val source: String) {
class StringReader(source: String) {
/**
* The source string to read from.
* The source can be changed using [resetTo].
*/
var source: String = source
private set

/**
* The cursor represents the next character that will be read.
* Starts from zero.
Expand Down Expand Up @@ -115,6 +120,18 @@ class StringReader(val source: String) {
}
}

/**
* Expects the source to have [text] right after the cursor. If not, throws an [IllegalStateException].
* Moves the cursor forward.
*
* @since 1.3.0
*/
fun expect(text: String) {
for (character in text) {
expect(character, peek = false)
}
}

/**
* Reads until the [char] is encountered and returns the read characters.
*
Expand All @@ -129,8 +146,15 @@ class StringReader(val source: String) {
val builder = StringBuilder()

if (peek() == char) {
if (!peek) next()
return if (inclusive) "$char" else ""
return if (inclusive) {
if (!peek) {
next()
}

"$char"
} else {
""
}
}

var i = 0
Expand Down Expand Up @@ -191,5 +215,16 @@ class StringReader(val source: String) {
cursor = 0
}

/**
* Changes the source to [source] and resets the [cursor] to zero.
*
* @since 1.3.0
*/
fun resetTo(source: String) {
require(source.isNotEmpty()) { "The source must not be empty!" }
this.source = source
reset()
}

override fun toString() = "StringReader(source=$source, cursor=$cursor)"
}

0 comments on commit 7f414bb

Please sign in to comment.