Skip to content

Commit

Permalink
dropOneOf and takeOneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
NQNStudios committed Dec 12, 2024
1 parent 4ddf68e commit 2e0991d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/kiss/Stream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ class Stream {
return Some(toReturn);
}

public function dropOneOf(options:Array<String>) {
switch(_dropWhileOneOf(options, true, true)) {
case None:
error(this, 'expected to drop one of ${options}');
default:
}
}

public function takeOneOf(options:Array<String>) {
return _dropWhileOneOf(options, true, true);
}

function _dropOneOf(options:Array<String>, take:Bool) {
_dropWhileOneOf(options, take, true);
}

public function dropWhileOneOf(options:Array<String>) {
_dropWhileOneOf(options, false);
}
Expand All @@ -295,7 +311,7 @@ class Stream {
return _dropWhileOneOf(options, true);
}

function _dropWhileOneOf(options:Array<String>, take:Bool):Option<String> {
function _dropWhileOneOf(options:Array<String>, take:Bool, justOnce=false):Option<String> {
var taken = "";

var lengths = [for (option in options) option.length => true];
Expand All @@ -307,7 +323,7 @@ class Stream {
for (length => _ in lengths) {
var sample = content.substr(0, length);
if (optsMap.exists(sample)) {
nextIs = true;
nextIs = !justOnce && true;
if (take) {
taken += sample;
}
Expand Down

0 comments on commit 2e0991d

Please sign in to comment.