summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/strings
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/strings')
-rw-r--r--misc/examples/strings/cstr_match.c26
-rw-r--r--misc/examples/strings/replace.c36
-rw-r--r--misc/examples/strings/splitstr.c21
-rw-r--r--misc/examples/strings/sso_map.c19
-rw-r--r--misc/examples/strings/sso_substr.c21
-rw-r--r--misc/examples/strings/sview_split.c20
-rw-r--r--misc/examples/strings/utf8replace_c.c25
-rw-r--r--misc/examples/strings/utf8replace_rs.rs22
8 files changed, 190 insertions, 0 deletions
diff --git a/misc/examples/strings/cstr_match.c b/misc/examples/strings/cstr_match.c
new file mode 100644
index 00000000..80013019
--- /dev/null
+++ b/misc/examples/strings/cstr_match.c
@@ -0,0 +1,26 @@
+#define i_implement
+#include <stc/cstr.h>
+#include <stc/csview.h>
+#include <stdio.h>
+
+int main(void)
+{
+ cstr ss = cstr_from("The quick brown fox jumps over the lazy dog.JPG");
+
+ intptr_t pos = cstr_find_at(&ss, 0, "brown");
+ printf("%" c_ZI " [%s]\n", pos, pos == c_NPOS ? "<NULL>" : cstr_str(&ss) + pos);
+ printf("equals: %d\n", cstr_equals(&ss, "The quick brown fox jumps over the lazy dog.JPG"));
+ printf("contains: %d\n", cstr_contains(&ss, "umps ove"));
+ printf("starts_with: %d\n", cstr_starts_with(&ss, "The quick brown"));
+ printf("ends_with: %d\n", cstr_ends_with(&ss, ".jpg"));
+ printf("ends_with: %d\n", cstr_ends_with(&ss, ".JPG"));
+
+ cstr s1 = cstr_lit("hell😀 w😀rl🐨");
+ csview ch1 = cstr_u8_chr(&s1, 7);
+ csview ch2 = cstr_u8_chr(&s1, 10);
+ printf("%s\nsize: %" c_ZI ", %" c_ZI "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1));
+ printf("ch1: %.*s\n", c_SV(ch1));
+ printf("ch2: %.*s\n", c_SV(ch2));
+
+ c_drop(cstr, &ss, &s1);
+}
diff --git a/misc/examples/strings/replace.c b/misc/examples/strings/replace.c
new file mode 100644
index 00000000..59a56bf7
--- /dev/null
+++ b/misc/examples/strings/replace.c
@@ -0,0 +1,36 @@
+#define i_implement
+#include <stc/cstr.h>
+
+int main(void)
+{
+ const char *base = "this is a test string.";
+ const char *s2 = "n example";
+ const char *s3 = "sample phrase";
+
+ // replace signatures used in the same order as described above:
+
+ // Ustring positions: 0123456789*123456789*12345
+ cstr s = cstr_from(base); // "this is a test string."
+ cstr m = cstr_clone(s);
+
+ cstr_append(&m, cstr_str(&m));
+ cstr_append(&m, cstr_str(&m));
+ printf("%s\n", cstr_str(&m));
+
+ cstr_replace_at(&s, 9, 5, s2); // "this is an example string." (1)
+ printf("(1) %s\n", cstr_str(&s));
+
+ cstr_replace_at_sv(&s, 19, 6, c_sv(s3+7, 6)); // "this is an example phrase." (2)
+ printf("(2) %s\n", cstr_str(&s));
+
+ cstr_replace_at(&s, 8, 10, "just a"); // "this is just a phrase." (3)
+ printf("(3) %s\n", cstr_str(&s));
+
+ cstr_replace_at_sv(&s, 8, 6, c_sv("a shorty", 7)); // "this is a short phrase." (4)
+ printf("(4) %s\n", cstr_str(&s));
+
+ cstr_replace_at(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5)
+ printf("(5) %s\n", cstr_str(&s));
+
+ c_drop(cstr, &s, &m);
+}
diff --git a/misc/examples/strings/splitstr.c b/misc/examples/strings/splitstr.c
new file mode 100644
index 00000000..ef7ed174
--- /dev/null
+++ b/misc/examples/strings/splitstr.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#define i_import // cstr + utf8 functions
+#include <stc/cregex.h>
+#define i_implement
+#include <stc/csview.h>
+
+int main(void)
+{
+ puts("Split with c_fortoken (csview):");
+
+ c_fortoken (i, "Hello World C99!", " ")
+ printf("'%.*s'\n", c_SV(i.token));
+
+ puts("\nSplit with c_formatch (regex):");
+
+ cregex re = cregex_from("[^ ]+");
+ c_formatch (i, &re, " Hello World C99! ")
+ printf("'%.*s'\n", c_SV(i.match[0]));
+
+ cregex_drop(&re);
+}
diff --git a/misc/examples/strings/sso_map.c b/misc/examples/strings/sso_map.c
new file mode 100644
index 00000000..4f84b651
--- /dev/null
+++ b/misc/examples/strings/sso_map.c
@@ -0,0 +1,19 @@
+#define i_implement
+#include <stc/cstr.h>
+#define i_key_str
+#define i_val_str
+#include <stc/cmap.h>
+
+int main(void)
+{
+ cmap_str m = {0};
+ cmap_str_emplace(&m, "Test short", "This is a short string");
+ cmap_str_emplace(&m, "Test long ", "This is a longer string");
+
+ c_forpair (k, v, cmap_str, m)
+ printf("%s: '%s' Len=%" c_ZI ", Is long: %s\n",
+ cstr_str(_.k), cstr_str(_.v), cstr_size(_.v),
+ cstr_is_long(_.v) ? "true" : "false");
+
+ cmap_str_drop(&m);
+}
diff --git a/misc/examples/strings/sso_substr.c b/misc/examples/strings/sso_substr.c
new file mode 100644
index 00000000..70d34440
--- /dev/null
+++ b/misc/examples/strings/sso_substr.c
@@ -0,0 +1,21 @@
+#define i_implement
+#include <stc/cstr.h>
+#define i_implement
+#include <stc/csview.h>
+
+int main(void)
+{
+ cstr str = cstr_from("We think in generalities, but we live in details.");
+ csview sv1 = cstr_substr_ex(&str, 3, 5); // "think"
+ intptr_t pos = cstr_find(&str, "live"); // position of "live"
+ csview sv2 = cstr_substr_ex(&str, pos, 4); // "live"
+ csview sv3 = cstr_slice_ex(&str, -8, -1); // "details"
+ printf("%.*s, %.*s, %.*s\n", c_SV(sv1), c_SV(sv2), c_SV(sv3));
+
+ cstr_assign(&str, "apples are green or red");
+ cstr s2 = cstr_from_sv(cstr_substr_ex(&str, -3, 3)); // "red"
+ cstr s3 = cstr_from_sv(cstr_substr_ex(&str, 0, 6)); // "apples"
+ printf("%s %s: %d, %d\n", cstr_str(&s2), cstr_str(&s3),
+ cstr_is_long(&str), cstr_is_long(&s2));
+ c_drop (cstr, &str, &s2, &s3);
+}
diff --git a/misc/examples/strings/sview_split.c b/misc/examples/strings/sview_split.c
new file mode 100644
index 00000000..ac275da0
--- /dev/null
+++ b/misc/examples/strings/sview_split.c
@@ -0,0 +1,20 @@
+#define i_implement
+#include <stc/cstr.h>
+#define i_implement
+#include <stc/csview.h>
+
+int main(void)
+{
+ // No memory allocations or string length calculations!
+ const csview date = c_sv("2021/03/12");
+ intptr_t pos = 0;
+ const csview year = csview_token(date, "/", &pos);
+ const csview month = csview_token(date, "/", &pos);
+ const csview day = csview_token(date, "/", &pos);
+
+ printf("%.*s, %.*s, %.*s\n", c_SV(year), c_SV(month), c_SV(day));
+
+ cstr y = cstr_from_sv(year), m = cstr_from_sv(month), d = cstr_from_sv(day);
+ printf("%s, %s, %s\n", cstr_str(&y), cstr_str(&m), cstr_str(&d));
+ c_drop(cstr, &y, &m, &d);
+}
diff --git a/misc/examples/strings/utf8replace_c.c b/misc/examples/strings/utf8replace_c.c
new file mode 100644
index 00000000..317313b0
--- /dev/null
+++ b/misc/examples/strings/utf8replace_c.c
@@ -0,0 +1,25 @@
+#define i_implement
+#include <stc/cstr.h>
+
+int main(void)
+{
+ cstr hello = cstr_lit("hell😀 w😀rld");
+ printf("%s\n", cstr_str(&hello));
+
+ /* replace second smiley at utf8 codepoint pos 7 */
+ cstr_u8_replace_at(&hello,
+ cstr_u8_to_pos(&hello, 7),
+ 1,
+ c_sv("🐨")
+ );
+ printf("%s\n", cstr_str(&hello));
+
+ c_foreach (c, cstr, hello)
+ printf("%.*s,", c_SV(c.chr));
+
+ cstr str = cstr_lit("scooby, dooby doo");
+ cstr_replace(&str, "oo", "00");
+ printf("\n%s\n", cstr_str(&str));
+
+ c_drop(cstr, &hello, &str);
+}
diff --git a/misc/examples/strings/utf8replace_rs.rs b/misc/examples/strings/utf8replace_rs.rs
new file mode 100644
index 00000000..8b163b4e
--- /dev/null
+++ b/misc/examples/strings/utf8replace_rs.rs
@@ -0,0 +1,22 @@
+pub fn main() {
+ let mut hello = String::from("hell😀 w😀rld");
+ println!("{}", hello);
+
+ /* replace second smiley at utf8 codepoint pos 7 */
+ hello.replace_range(
+ hello
+ .char_indices()
+ .nth(7)
+ .map(|(pos, ch)| (pos..pos + ch.len_utf8()))
+ .unwrap(),
+ "🐨",
+ );
+ println!("{}", hello);
+
+ for c in hello.chars() {
+ print!("{},", c);
+ }
+
+ let str = "If you find the time, you will find the winner";
+ println!("\n{}", str.replace("find", "match"));
+}