aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-02 23:13:02 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-02 23:13:02 -0400
commit6e4be93848ebea9f3fb921bed453f8ab74327c15 (patch)
tree822a33522ccb55c89f8d297ac5ed909169b4b872
parent6d7e09bf1801c2fe183df17cc67017a6d3d8513b (diff)
Add Text:join() and tests
-rw-r--r--builtins/text.c11
-rw-r--r--builtins/text.h25
-rw-r--r--test/text.tm12
3 files changed, 24 insertions, 24 deletions
diff --git a/builtins/text.c b/builtins/text.c
index a1a98a00..df90b382 100644
--- a/builtins/text.c
+++ b/builtins/text.c
@@ -1449,6 +1449,17 @@ public array_t Text$split(Text_t text, Text_t pattern)
return chunks;
}
+public Text_t Text$join(Text_t glue, array_t pieces)
+{
+ if (pieces.length == 0) return (Text_t){.length=0};
+
+ Text_t result = *(Text_t*)pieces.data;
+ for (int64_t i = 1; i < pieces.length; i++) {
+ result = Text$concat(result, glue, *(Text_t*)(pieces.data + i*pieces.stride));
+ }
+ return result;
+}
+
public Text_t Text$format(const char *fmt, ...)
{
va_list args;
diff --git a/builtins/text.h b/builtins/text.h
index cca18a2c..9f187a37 100644
--- a/builtins/text.h
+++ b/builtins/text.h
@@ -17,30 +17,6 @@ typedef struct {
int32_t index;
} find_result_t;
-// CORD Text$as_text(const void *str, bool colorize, const TypeInfo *info);
-// 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$upper(CORD str);
-// // CORD Text$lower(CORD str);
-// // CORD Text$title(CORD str);
-// bool Text$has(CORD str, CORD target, Where_t where);
-// CORD Text$without(CORD str, CORD target, Where_t where);
-// CORD Text$trimmed(CORD str, CORD skip, Where_t where);
-// find_result_t Text$find(CORD str, CORD pat);
-// CORD Text$replace(CORD text, CORD pat, CORD replacement, Int_t limit);
-// array_t Text$split(CORD str, CORD split);
-// CORD Text$join(CORD glue, array_t pieces);
-// array_t Text$clusters(CORD text);
-// array_t Text$codepoints(CORD text);
-// array_t Text$bytes(CORD text);
-// Int_t Text$num_clusters(CORD text);
-// Int_t Text$num_codepoints(CORD text);
-// Int_t Text$num_bytes(CORD text);
-// array_t Text$character_names(CORD text);
-// CORD Text$read_line(CORD prompt);
-
int printf_text(FILE *stream, const struct printf_info *info, const void *const args[]);
int printf_text_size(const struct printf_info *info, size_t n, int argtypes[n], int sizes[n]);
@@ -73,6 +49,7 @@ 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);
array_t Text$lines(Text_t text);
+Text_t Text$join(Text_t glue, array_t pieces);
extern const TypeInfo $Text;
diff --git a/test/text.tm b/test/text.tm
index 0955ffd2..4dd3531c 100644
--- a/test/text.tm
+++ b/test/text.tm
@@ -140,3 +140,15 @@ func main():
>> "abc":split("")
= ["a", "b", "c"]
+
+ >> ", ":join(["one", "two", "three"])
+ = "one, two, three"
+
+ >> "":join(["one", "two", "three"])
+ = "onetwothree"
+
+ >> "+":join(["one"])
+ = "one"
+
+ >> "+":join([:Text])
+ = ""