diff options
| -rw-r--r-- | examples/demos.c | 4 | ||||
| -rw-r--r-- | stc/cstr.h | 26 |
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");
@@ -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: */
|
