Add method for getting a length-based string as Text

This commit is contained in:
Bruce Hill 2024-09-04 03:02:30 -04:00
parent c3f8b40c84
commit e0aca8fa72
2 changed files with 9 additions and 3 deletions

View File

@ -400,13 +400,13 @@ Text_t text_from_u32(uint32_t *codepoints, int64_t num_codepoints, bool normaliz
return ret;
}
public Text_t Text$from_str(const char *str)
public Text_t Text$from_strn(const char *str, size_t len)
{
int64_t ascii_span = 0;
while (str[ascii_span] && isascii(str[ascii_span]))
for (size_t i = 0; i < len && isascii(str[i]); i++)
ascii_span++;
if (str[ascii_span] == '\0') { // All ASCII
if (ascii_span == (int64_t)len) { // All ASCII
Text_t ret = {.length=ascii_span};
if (ascii_span <= 8) {
ret.tag = TEXT_SHORT_ASCII;
@ -427,6 +427,11 @@ public Text_t Text$from_str(const char *str)
}
}
public Text_t Text$from_str(const char *str)
{
return Text$from_strn(str, strlen(str));
}
static void u8_buf_append(Text_t text, char **buf, int64_t *capacity, int64_t *i)
{
switch (text.tag) {

View File

@ -23,6 +23,7 @@ Text_t Text$_concat(int n, Text_t items[n]);
#define Texts(...) Text$concat(__VA_ARGS__)
Text_t Text$slice(Text_t text, Int_t first_int, Int_t last_int);
Text_t Text$from_str(const char *str);
Text_t Text$from_strn(const char *str, size_t len);
uint64_t Text$hash(Text_t *text);
int32_t Text$compare(const Text_t *a, const Text_t *b);
bool Text$equal(const Text_t *a, const Text_t *b);