aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-11-04 01:17:47 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-11-04 01:17:47 -0500
commitaabc0a3cff685e31f2492c977c6562d8e0ef8ebc (patch)
tree518e70f81407c02978c69f80be6f9872dabaa295 /stdlib
parentb69d14b89492919dc5c1669d2c569ee3baf1bbb0 (diff)
Update text API to use optional returns when applicable
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/text.c15
-rw-r--r--stdlib/text.h9
2 files changed, 13 insertions, 11 deletions
diff --git a/stdlib/text.c b/stdlib/text.c
index e8ecc786..92c5df48 100644
--- a/stdlib/text.c
+++ b/stdlib/text.c
@@ -607,7 +607,7 @@ Text_t text_from_u32(ucs4_t *codepoints, int64_t num_codepoints, bool normalize)
return ret;
}
-public Text_t Text$from_strn(const char *str, size_t len)
+public OptionalText_t Text$from_strn(const char *str, size_t len)
{
int64_t ascii_span = 0;
for (size_t i = 0; i < len && isascii(str[i]); i++)
@@ -626,7 +626,7 @@ public Text_t Text$from_strn(const char *str, size_t len)
return ret;
} else {
if (u8_check((uint8_t*)str, len) != NULL)
- return Text("");
+ return NULL_TEXT;
ucs4_t buf[128];
size_t length = sizeof(buf)/sizeof(buf[0]);
@@ -638,7 +638,7 @@ public Text_t Text$from_strn(const char *str, size_t len)
}
}
-public Text_t Text$from_str(const char *str)
+public OptionalText_t Text$from_str(const char *str)
{
return str ? Text$from_strn(str, strlen(str)) : Text("");
}
@@ -1270,20 +1270,21 @@ public Text_t Text$from_codepoints(Array_t codepoints)
return text_from_u32(codepoints.data, codepoints.length, true);
}
-public Text_t Text$from_codepoint_names(Array_t codepoint_names)
+public OptionalText_t Text$from_codepoint_names(Array_t codepoint_names)
{
Array_t codepoints = {};
for (int64_t i = 0; i < codepoint_names.length; i++) {
Text_t *name = ((Text_t*)(codepoint_names.data + i*codepoint_names.stride));
const char *name_str = Text$as_c_string(*name);
ucs4_t codepoint = unicode_name_character(name_str);
- if (codepoint != UNINAME_INVALID)
- Array$insert(&codepoints, &codepoint, I_small(0), sizeof(ucs4_t));
+ if (codepoint == UNINAME_INVALID)
+ return NULL_TEXT;
+ Array$insert(&codepoints, &codepoint, I_small(0), sizeof(ucs4_t));
}
return Text$from_codepoints(codepoints);
}
-public Text_t Text$from_bytes(Array_t bytes)
+public OptionalText_t Text$from_bytes(Array_t bytes)
{
if (bytes.stride != sizeof(int8_t))
Array$compact(&bytes, sizeof(int8_t));
diff --git a/stdlib/text.h b/stdlib/text.h
index bcdccfeb..45aa00ca 100644
--- a/stdlib/text.h
+++ b/stdlib/text.h
@@ -9,6 +9,7 @@
#include "datatypes.h"
#include "integers.h"
+#include "optionals.h"
#include "util.h"
typedef struct {
@@ -27,8 +28,8 @@ Text_t Text$_concat(int n, Text_t items[n]);
#define Text$concat(...) Text$_concat(sizeof((Text_t[]){__VA_ARGS__})/sizeof(Text_t), (Text_t[]){__VA_ARGS__})
#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);
+OptionalText_t Text$from_str(const char *str);
+OptionalText_t Text$from_strn(const char *str, size_t len);
PUREFUNC uint64_t Text$hash(Text_t *text);
PUREFUNC int32_t Text$compare(const Text_t *a, const Text_t *b);
PUREFUNC bool Text$equal(const Text_t *a, const Text_t *b);
@@ -49,8 +50,8 @@ Array_t Text$utf32_codepoints(Text_t text);
Array_t Text$utf8_bytes(Text_t text);
Array_t Text$codepoint_names(Text_t text);
Text_t Text$from_codepoints(Array_t codepoints);
-Text_t Text$from_codepoint_names(Array_t codepoint_names);
-Text_t Text$from_bytes(Array_t bytes);
+OptionalText_t Text$from_codepoint_names(Array_t codepoint_names);
+OptionalText_t Text$from_bytes(Array_t bytes);
Array_t Text$lines(Text_t text);
Text_t Text$join(Text_t glue, Array_t pieces);
Text_t Text$repeat(Text_t text, Int_t count);