aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-03 16:26:10 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-03 16:26:10 -0500
commit74c86176cb2bc68f7a3c7b9c65d93184c8b7d959 (patch)
treeb9a873366583261d059afb89cd3634d6e91b7cc6
parent32f27b6206c542d53383654fffff27a3fcdbc168 (diff)
Tweaks and improvements to built-in functions
-rw-r--r--builtins/string.c42
-rw-r--r--builtins/string.h18
-rw-r--r--environment.c48
3 files changed, 66 insertions, 42 deletions
diff --git a/builtins/string.c b/builtins/string.c
index c8aaf90d..c28aa510 100644
--- a/builtins/string.c
+++ b/builtins/string.c
@@ -106,7 +106,7 @@ public uint32_t Str__hash(CORD *cord)
return hash;
}
-public CORD Str__uppercased(CORD str)
+public CORD Str__upper(CORD str)
{
if (!str) return str;
size_t len = strlen(str) + 1;
@@ -114,7 +114,7 @@ public CORD Str__uppercased(CORD str)
return (CORD)u8_toupper((const uint8_t*)str, len-1, uc_locale_language(), NULL, dest, &len);
}
-public CORD Str__lowercased(CORD str)
+public CORD Str__lower(CORD str)
{
if (!str) return str;
size_t len = strlen(str) + 1;
@@ -122,7 +122,7 @@ public CORD Str__lowercased(CORD str)
return (CORD)u8_tolower((const uint8_t*)str, len-1, uc_locale_language(), NULL, dest, &len);
}
-public CORD Str__titlecased(CORD str)
+public CORD Str__title(CORD str)
{
if (!str) return str;
size_t len = strlen(str) + 1;
@@ -192,12 +192,6 @@ public CORD Str__trimmed(CORD str, CORD skip, where_e where)
}
}
-public CORD Str__slice(CORD str, int64_t first, int64_t stride, int64_t length)
-{
- if (stride != 1) errx(1, "Slicing with non-1 stride is not supported");
- return CORD_substr(str, first-1, length);
-}
-
public find_result_t Str__find(CORD str, CORD pat)
{
if (!pat) return (find_result_t){.status=FIND_SUCCESS, .index=1};
@@ -218,31 +212,31 @@ public CORD Str__replace(CORD text, CORD pat, CORD replacement, int64_t limit)
return CORD_cat(ret, CORD_substr(text, pos, SIZE_MAX));
}
-public Str_Array_t Str__split(CORD str, CORD split)
+public array_t Str__split(CORD str, CORD split)
{
- if (!str) return (Str_Array_t){.stride=sizeof(CORD)};
- Str_Array_t strings = {.stride=sizeof(CORD)};
+ if (!str) return (array_t){.data=GC_MALLOC(sizeof(CORD)), .atomic=1, .length=1, .stride=sizeof(CORD)};
+ array_t strings = {.stride=sizeof(CORD), .atomic=1};
int64_t capacity = 0;
const uint8_t *ustr = (uint8_t*)CORD_to_const_char_star(str);
const uint8_t *usplit = (uint8_t*)CORD_to_const_char_star(split);
for (int64_t i = 0; ; ) {
- i += u8_strcspn(ustr + i, usplit);
- size_t span = u8_strspn(ustr + i, usplit);
- if (span > 0) {
- CORD chunk = CORD_substr((CORD)ustr, i, i+span);
- if (capacity <= 0)
- strings.data = GC_REALLOC(strings.data, sizeof(CORD)*(capacity += 10));
- strings.data[strings.length++] = chunk;
- i += span;
- } else {
- break;
- }
+ size_t non_split = u8_strcspn(ustr + i, usplit);
+ CORD chunk = CORD_substr((CORD)ustr, i, non_split);
+ if (capacity <= 0)
+ strings.data = GC_REALLOC(strings.data, sizeof(CORD)*(capacity += 10));
+ ((CORD*)strings.data)[strings.length++] = chunk;
+
+ i += non_split;
+
+ size_t split = u8_strspn(ustr + i, usplit);
+ if (split == 0) break;
+ i += split;
}
return strings;
}
-public CORD Str__join(CORD glue, Str_Array_t pieces)
+public CORD Str__join(CORD glue, array_t pieces)
{
if (pieces.length == 0) return CORD_EMPTY;
diff --git a/builtins/string.h b/builtins/string.h
index a4bb6f2b..c24de72d 100644
--- a/builtins/string.h
+++ b/builtins/string.h
@@ -8,13 +8,6 @@
#define String_t CORD
#define Str_t CORD
-typedef struct {
- CORD *data;
- unsigned long int length:42;
- unsigned short int free:4, cow:1, atomic:1;
- short int stride:16;
-} Str_Array_t;
-
typedef enum { WHERE_ANYWHERE, WHERE_START, WHERE_END } where_e;
typedef struct {
@@ -27,17 +20,16 @@ CORD Str__quoted(CORD str, bool colorize);
int Str__compare(CORD *x, CORD *y);
bool Str__equal(CORD *x, CORD *y);
uint32_t Str__hash(CORD *cord);
-CORD Str__uppercased(CORD str);
-CORD Str__lowercased(CORD str);
-CORD Str__titlecased(CORD str);
+CORD Str__upper(CORD str);
+CORD Str__lower(CORD str);
+CORD Str__title(CORD str);
bool Str__has(CORD str, CORD target, where_e where);
CORD Str__without(CORD str, CORD target, where_e where);
CORD Str__trimmed(CORD str, CORD skip, where_e where);
-CORD Str__slice(CORD str, int64_t first, int64_t stride, int64_t length);
find_result_t Str__find(CORD str, CORD pat);
CORD Str__replace(CORD text, CORD pat, CORD replacement, int64_t limit);
-Str_Array_t Str__split(CORD str, CORD split);
-CORD Str__join(CORD glue, Str_Array_t pieces);
+array_t Str__split(CORD str, CORD split);
+CORD Str__join(CORD glue, array_t pieces);
extern const TypeInfo Str;
diff --git a/environment.c b/environment.c
index 7729c3d1..1145e029 100644
--- a/environment.c
+++ b/environment.c
@@ -68,15 +68,53 @@ env_t *new_compilation_unit(void)
{"bits", "Int__bits", "func(x:Int)->[Bool]"},
{"abs", "Int__abs", "func(i:Int)->Int"},
{"min", "Int__min", "Int"},
- {"max", "Int__max", "Int"}
+ {"max", "Int__max", "Int"},
+ )},
+ {"Int32", Type(IntType, .bits=32), "Int32_t", "Int32", $TypedArray(ns_entry_t,
+ {"format", "Int32__format", "func(i:Int32, digits=0)->Str"},
+ {"hex", "Int32__hex", "func(i:Int32, digits=0, uppercase=yes, prefix=yes)->Str"},
+ {"octal", "Int32__octal", "func(i:Int32, digits=0, prefix=yes)->Str"},
+ {"random", "Int32__random", "func(min=0, max=0xffffffff)->Int32"},
+ {"bits", "Int32__bits", "func(x:Int32)->[Bool]"},
+ {"abs", "Int32__abs", "func(i:Int32)->Int32"},
+ {"min", "Int32__min", "Int32"},
+ {"max", "Int32__max", "Int32"},
+ )},
+ {"Int16", Type(IntType, .bits=16), "Int16_t", "Int16", $TypedArray(ns_entry_t,
+ {"format", "Int16__format", "func(i:Int16, digits=0)->Str"},
+ {"hex", "Int16__hex", "func(i:Int16, digits=0, uppercase=yes, prefix=yes)->Str"},
+ {"octal", "Int16__octal", "func(i:Int16, digits=0, prefix=yes)->Str"},
+ {"random", "Int16__random", "func(min=0, max=0xffffffff)->Int16"},
+ {"bits", "Int16__bits", "func(x:Int16)->[Bool]"},
+ {"abs", "Int16__abs", "func(i:Int16)->Int16"},
+ {"min", "Int16__min", "Int16"},
+ {"max", "Int16__max", "Int16"},
+ )},
+ {"Int8", Type(IntType, .bits=8), "Int8_t", "Int8", $TypedArray(ns_entry_t,
+ {"format", "Int8__format", "func(i:Int8, digits=0)->Str"},
+ {"hex", "Int8__hex", "func(i:Int8, digits=0, uppercase=yes, prefix=yes)->Str"},
+ {"octal", "Int8__octal", "func(i:Int8, digits=0, prefix=yes)->Str"},
+ {"random", "Int8__random", "func(min=0, max=0xffffffff)->Int8"},
+ {"bits", "Int8__bits", "func(x:Int8)->[Bool]"},
+ {"abs", "Int8__abs", "func(i:Int8)->Int8"},
+ {"min", "Int8__min", "Int8"},
+ {"max", "Int8__max", "Int8"},
)},
- {"Int32", Type(IntType, .bits=32), "Int32_t", "Int32", {}},
- {"Int16", Type(IntType, .bits=16), "Int16_t", "Int16", {}},
- {"Int8", Type(IntType, .bits=8), "Int8_t", "Int8", {}},
{"Num", Type(NumType, .bits=64), "Num_t", "Num", {}},
{"Num32", Type(NumType, .bits=32), "Num32_t", "Num32", {}},
{"Str", Type(StringType), "Str_t", "Str", $TypedArray(ns_entry_t,
- {"quoted", "Str__quoted", "func(s:Str, color=no)->Str"}
+ {"quoted", "Str__quoted", "func(s:Str, color=no)->Str"},
+ {"upper", "Str__upper", "func(s:Str)->Str"},
+ {"lower", "Str__lower", "func(s:Str)->Str"},
+ {"title", "Str__title", "func(s:Str)->Str"},
+ // {"has", "Str__has", "func(s:Str, target:Str, where=ANYWHERE)->Bool"},
+ // {"without", "Str__without", "func(s:Str, target:Str, where=ANYWHERE)->Str"},
+ // {"trimmed", "Str__without", "func(s:Str, skip:Str, where=ANYWHERE)->Str"},
+ {"title", "Str__title", "func(s:Str)->Str"},
+ // {"find", "Str__find", "func(s:Str, pattern:Str)->FindResult"},
+ {"replace", "Str__replace", "func(s:Str, pattern:Str, replacement:Str, limit=Int.max)->Str"},
+ {"split", "Str__split", "func(s:Str, split:Str)->[Str]"},
+ {"join", "Str__join", "func(glue:Str, pieces:[Str])->Str"},
)},
};