summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-11-16 11:27:52 +0100
committerTyge Løvset <[email protected]>2020-11-16 11:27:52 +0100
commit56e3c5c17c55aab4f83e7f2d9f74288d5043bf6f (patch)
treed1bdb465beb6b708450944f8b2091f2a8b29e6e3
parent360c948953ee0b7313592371f537ec4bf1f1a127 (diff)
downloadSTC-modified-56e3c5c17c55aab4f83e7f2d9f74288d5043bf6f.tar.gz
STC-modified-56e3c5c17c55aab4f83e7f2d9f74288d5043bf6f.zip
Added cstr_contains(), cstr_begins_with(), cstr_ends_with(), and changed cstr_find*() to take a cstr_t value instead of pointer, because it returns an int position only, not iter reference.
-rw-r--r--examples/demos.c4
-rw-r--r--stc/cstr.h26
2 files changed, 22 insertions, 8 deletions
diff --git a/examples/demos.c b/examples/demos.c
index 4c7e6a01..e6649af6 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -17,13 +17,13 @@ void stringdemo1()
cstr_erase(&cs, 7, 5); // -nine
c_print(1, "{}.\n", cs.str);
- cstr_replace(&cs, cstr_find(&cs, "seven"), 5, "four");
+ cstr_replace(&cs, cstr_find(cs, "seven"), 5, "four");
c_print(1, "{}.\n", cs.str);
cstr_take(&cs, cstr_from_fmt("%s *** %s", cs.str, cs.str));
c_print(1, "{}.\n", cs.str);
- c_print(1, "find \"four\": {}\n", cs.str + cstr_find(&cs, "four"));
+ c_print(1, "find \"four\": {}\n", cs.str + cstr_find(cs, "four"));
// reassign:
cstr_assign(&cs, "one two three four five six seven");
diff --git a/stc/cstr.h b/stc/cstr.h
index 94b902a1..1ceb892f 100644
--- a/stc/cstr.h
+++ b/stc/cstr.h
@@ -183,14 +183,28 @@ cstr_compare(const cstr_t *s1, const cstr_t *s2) {
return strcmp(s1->str, s2->str);
}
STC_INLINE size_t
-cstr_find_n(const cstr_t* s, const char* needle, size_t pos, size_t n) {
- char* res = c_strnstr(s->str + pos, needle, n);
- return res ? res - s->str : cstr_NPOS;
+cstr_find_n(cstr_t s, const char* needle, size_t pos, size_t n) {
+ char* res = c_strnstr(s.str + pos, needle, n);
+ return res ? res - s.str : cstr_NPOS;
}
STC_INLINE size_t
-cstr_find(const cstr_t* s, const char* needle) {
- char* res = strstr(s->str, needle);
- return res ? res - s->str : cstr_NPOS;
+cstr_find(cstr_t s, const char* needle) {
+ char* res = strstr(s.str, needle);
+ return res ? res - s.str : cstr_NPOS;
+}
+
+STC_INLINE bool
+cstr_contains(const cstr_t s, const char* needle) {
+ return strstr(s.str, needle) != NULL;
+}
+STC_INLINE bool
+cstr_begins_with(const cstr_t s, const char* needle) {
+ return strncmp(s.str, needle, strlen(needle)) == 0;
+}
+STC_INLINE bool
+cstr_ends_with(const cstr_t s, const char* needle) {
+ size_t n = strlen(needle), sz = cstr_size(s);
+ return n <= sz ? strcmp(s.str + sz - n, needle) == 0 : false;
}
/* cvec/cmap API functions: */