Skip to content

Commit

Permalink
impl partial consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jun 28, 2024
1 parent 5d88181 commit 966c831
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ fun interface ArgumentConsumer {

infix fun untilFalse(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor ->
var current = processor.next()
val list = arrayListOf<String>()

if (current != null) {
list.add(current)
}
var fullString = current ?: ""

while (current != null && predicate(processor, current)) {
while (current != null) {
current = processor.next()
if (current != null) {
list.add(current)

if (current == null) {
return@ArgumentConsumer null
}

fullString += " $current"

if (!predicate(processor, fullString)) {
return@ArgumentConsumer fullString
}
}
list.joinToString(" ")
null
}

infix fun until(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor ->
infix fun until(predicate: (ArgumentProcessor, String) -> Boolean) = untilFalse { p, s -> !predicate(p, s) }

infix fun untilFalsePartial(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor ->
var current = processor.next()
var fullString = current ?: ""

Expand All @@ -38,13 +43,15 @@ fun interface ArgumentConsumer {

fullString += " $current"

if (predicate(processor, fullString)) {
if (!predicate(processor, current)) {
return@ArgumentConsumer fullString
}
}
null
}

infix fun untilPartial(predicate: (ArgumentProcessor, String) -> Boolean) = untilFalsePartial { p, s -> !predicate(p, s) }

fun remaining() = untilFalse { processor, _ -> processor.pointer < processor.args.size }

infix fun variable(amount: Int): ArgumentConsumer {
Expand Down

0 comments on commit 966c831

Please sign in to comment.