dropOneOf and takeOneOf

This commit is contained in:
2024-12-12 14:39:46 -06:00
parent 4ddf68e3db
commit 2e0991d2d2

View File

@@ -287,6 +287,22 @@ class Stream {
return Some(toReturn); 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>) { public function dropWhileOneOf(options:Array<String>) {
_dropWhileOneOf(options, false); _dropWhileOneOf(options, false);
} }
@@ -295,7 +311,7 @@ class Stream {
return _dropWhileOneOf(options, true); 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 taken = "";
var lengths = [for (option in options) option.length => true]; var lengths = [for (option in options) option.length => true];
@@ -307,7 +323,7 @@ class Stream {
for (length => _ in lengths) { for (length => _ in lengths) {
var sample = content.substr(0, length); var sample = content.substr(0, length);
if (optsMap.exists(sample)) { if (optsMap.exists(sample)) {
nextIs = true; nextIs = !justOnce && true;
if (take) { if (take) {
taken += sample; taken += sample;
} }