aboutsummaryrefslogtreecommitdiff
path: root/builtins/util.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-02 18:47:39 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-02 18:47:39 -0400
commit61e482f6f36aee6f72392a6188f2ec5c858b88fd (patch)
treebea4123fcc62dd834405ae89ce9fe260e90a0023 /builtins/util.c
parentf0f8f218703ebb4512b3cd3f9e06b86a7d9861b0 (diff)
Initial WIP first past
Diffstat (limited to 'builtins/util.c')
-rw-r--r--builtins/util.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/builtins/util.c b/builtins/util.c
index d4f3cd31..7fca15e3 100644
--- a/builtins/util.c
+++ b/builtins/util.c
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
+#include "text.h"
#include "util.h"
public bool USE_COLOR;
@@ -67,4 +68,54 @@ public CORD CORD_asprintf(CORD fmt, ...)
return c;
}
+public CORD CORD_quoted(CORD str)
+{
+ CORD quoted = "\"";
+ CORD_pos i;
+ CORD_FOR(i, str) {
+ char c = CORD_pos_fetch(i);
+ switch (c) {
+ case '\a': quoted = CORD_cat(quoted, "\\a"); break;
+ case '\b': quoted = CORD_cat(quoted, "\\b"); break;
+ case '\x1b': quoted = CORD_cat(quoted, "\\e"); break;
+ case '\f': quoted = CORD_cat(quoted, "\\f"); break;
+ case '\n': quoted = CORD_cat(quoted, "\\n"); break;
+ case '\r': quoted = CORD_cat(quoted, "\\r"); break;
+ case '\t': quoted = CORD_cat(quoted, "\\t"); break;
+ case '\v': quoted = CORD_cat(quoted, "\\v"); break;
+ case '"': quoted = CORD_cat(quoted, "\\\""); break;
+ case '\\': quoted = CORD_cat(quoted, "\\\\"); break;
+ case '\x00' ... '\x06': case '\x0E' ... '\x1A':
+ case '\x1C' ... '\x1F': case '\x7F' ... '\x7F':
+ CORD_sprintf(&quoted, "%r\\x%02X", quoted, c);
+ break;
+ default: quoted = CORD_cat_char(quoted, c); break;
+ }
+ }
+ quoted = CORD_cat_char(quoted, '"');
+ return quoted;
+}
+
+public CORD CORD_replace(CORD c, CORD to_replace, CORD replacement)
+{
+ size_t len = CORD_len(c);
+ size_t replaced_len = CORD_len(to_replace);
+ size_t pos = 0;
+ CORD ret = CORD_EMPTY;
+ while (pos < len) {
+ size_t found = CORD_str(c, pos, to_replace);
+ if (found == CORD_NOT_FOUND) {
+ if (pos < len-1)
+ ret = CORD_cat(ret, CORD_substr(c, pos, len));
+ return ret;
+ }
+ if (found > pos)
+ ret = CORD_cat(ret, CORD_substr(c, pos, found-pos));
+ ret = CORD_cat(ret, replacement);
+ pos = found + replaced_len;
+ }
+ return ret;
+}
+
+
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0