Add Text.slice()

This commit is contained in:
Bruce Hill 2024-05-19 14:38:44 -04:00
parent b0d1daa0f3
commit 7dddfb71a0
3 changed files with 17 additions and 0 deletions

View File

@ -124,6 +124,21 @@ public uint32_t Text$hash(const CORD *cord)
return hash;
}
public CORD Text$slice(CORD text, int64_t first, int64_t length)
{
if (length == 0) return CORD_EMPTY;
int64_t len = CORD_len(text);
if (length < -len) return CORD_EMPTY;
if (first <= 0) first = len + first + 1;
if (first < 1) first = 1;
else if (first > len + 1) first = len + 1;
if (length < 0) length = len + length;
return CORD_substr(text, first - 1, length);
}
public CORD Text$upper(CORD str)
{
if (!str) return str;

View File

@ -23,6 +23,7 @@ CORD Text$quoted(CORD str, bool colorize);
int Text$compare(const CORD *x, const CORD *y);
bool Text$equal(const CORD *x, const CORD *y);
uint32_t Text$hash(const CORD *cord);
CORD Text$slice(CORD text, int64_t first, int64_t length);
CORD Text$upper(CORD str);
CORD Text$lower(CORD str);
CORD Text$title(CORD str);

View File

@ -158,6 +158,7 @@ env_t *new_compilation_unit(void)
#undef F
#undef C
{"Text", TEXT_TYPE, "Text_t", "$Text", TypedArray(ns_entry_t,
{"slice", "Text$slice", "func(text:Text, index:Int, length=Int.max)->Text"},
{"quoted", "Text$quoted", "func(text:Text, color=no)->Text"},
{"upper", "Text$upper", "func(text:Text)->Text"},
{"lower", "Text$lower", "func(text:Text)->Text"},