Prelude.substr allow negative indices

This commit is contained in:
2021-11-29 15:54:39 -07:00
parent b57f399b2f
commit 1f39bcb788
2 changed files with 11 additions and 0 deletions

View File

@@ -111,6 +111,7 @@ class Kiss {
"zipThrow" => Symbol("Prelude.zipThrow"),
"joinPath" => Symbol("Prelude.joinPath"),
"readDirectory" => Symbol("Prelude.readDirectory"),
"substr" => Symbol("Prelude.substr")
],
fieldList: [],
fieldDict: new Map(),

View File

@@ -632,6 +632,16 @@ class Prelude {
}
#end
public static function substr(str:String, startIdx:Int, ?endIdx:Int) {
function negIdx(idx) {
return if (idx < 0) str.length + idx else idx;
}
if (endIdx == null) endIdx = str.length;
return str.substr(negIdx(startIdx), negIdx(endIdx));
}
public static var newLine = "\n";
public static var backSlash = "\\";
}