summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-12 21:54:53 +0200
committerTyge Løvset <[email protected]>2022-08-12 21:54:53 +0200
commitccb63f1abbae18657708dd8ea38e0892aa0fc48a (patch)
treef64f432f79ce1552951e2a88d80824af964878b6 /include
parent5f8a7951996728f6e91ef9ae2e904ce51ac0c883 (diff)
downloadSTC-modified-ccb63f1abbae18657708dd8ea38e0892aa0fc48a.tar.gz
STC-modified-ccb63f1abbae18657708dd8ea38e0892aa0fc48a.zip
cstr V4: Changed cstr functions to take pointers to self, not values. This is consistent with the rest of the containers. csview will still use values, as it is designed to be passed by value.
Diffstat (limited to 'include')
-rw-r--r--include/stc/alt/cstr.h47
-rw-r--r--include/stc/cstr.h106
2 files changed, 77 insertions, 76 deletions
diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h
index c669c620..a2233432 100644
--- a/include/stc/alt/cstr.h
+++ b/include/stc/alt/cstr.h
@@ -56,8 +56,8 @@ STC_API cstr* cstr_append_n(cstr* self, const char* str, size_t n);
STC_API cstr cstr_replace_sv(csview str, csview find, csview repl, unsigned count);
STC_DEF void cstr_replace_at_sv(cstr* self, const size_t pos, size_t len, csview repl);
STC_API void cstr_erase_n(cstr* self, size_t pos, size_t n);
-STC_API size_t cstr_find(cstr s, const char* needle);
-STC_API size_t cstr_find_at(cstr s, size_t pos, const char* needle);
+STC_API size_t cstr_find(const cstr* self, const char* needle);
+STC_API size_t cstr_find_at(const cstr* self, size_t pos, const char* needle);
STC_API bool cstr_getdelim(cstr *self, int delim, FILE *stream);
STC_INLINE cstr cstr_init() { return cstr_null; }
@@ -70,7 +70,7 @@ STC_INLINE csview cstr_sv(const cstr* self)
STC_INLINE cstr cstr_from(const char* str)
{ return cstr_from_n(str, strlen(str)); }
STC_INLINE char* cstr_data(cstr* self) { return self->str; }
-STC_INLINE size_t cstr_size(cstr s) { return _cstr_p(&s)->size; }
+STC_INLINE size_t cstr_size(const cstr* self) { return _cstr_p(self)->size; }
STC_INLINE size_t cstr_capacity(cstr s) { return _cstr_p(&s)->cap; }
STC_INLINE bool cstr_empty(cstr s) { return _cstr_p(&s)->size == 0; }
STC_INLINE void cstr_drop(cstr* self)
@@ -106,12 +106,12 @@ STC_INLINE void cstr_erase(cstr* self, const size_t pos)
STC_INLINE char* cstr_front(cstr* self) { return self->str; }
STC_INLINE char* cstr_back(cstr* self)
{ return self->str + _cstr_p(self)->size - 1; }
-STC_INLINE bool cstr_equals(cstr s, const char* str)
- { return strcmp(s.str, str) == 0; }
-STC_INLINE bool cstr_equals_s(cstr s1, cstr s2)
- { return strcmp(s1.str, s2.str) == 0; }
-STC_INLINE bool cstr_contains(cstr s, const char* needle)
- { return strstr(s.str, needle) != NULL; }
+STC_INLINE bool cstr_equals(const cstr* self, const char* str)
+ { return strcmp(self->str, str) == 0; }
+STC_INLINE bool cstr_equals_s(const cstr* self, cstr s)
+ { return strcmp(self->str, s.str) == 0; }
+STC_INLINE bool cstr_contains(const cstr* self, const char* needle)
+ { return strstr(self->str, needle) != NULL; }
STC_INLINE bool cstr_getline(cstr *self, FILE *stream)
{ return cstr_getdelim(self, '\n', stream); }
@@ -133,7 +133,7 @@ STC_INLINE cstr cstr_with_size(const size_t len, const char fill) {
}
STC_INLINE char* cstr_append_uninit(cstr *self, size_t n) {
- size_t len = cstr_size(*self); char* d;
+ size_t len = cstr_size(self); char* d;
if (!(d = cstr_reserve(self, len + n))) return NULL;
_cstr_p(self)->size += n;
return d + len;
@@ -152,14 +152,15 @@ STC_INLINE cstr cstr_move(cstr* self) {
return tmp;
}
-STC_INLINE bool cstr_starts_with(cstr s, const char* sub) {
- while (*sub && *s.str == *sub) ++s.str, ++sub;
+STC_INLINE bool cstr_starts_with(const cstr* self, const char* sub) {
+ const char* p = self->str;
+ while (*sub && *p == *sub) ++p, ++sub;
return *sub == 0;
}
-STC_INLINE bool cstr_ends_with(cstr s, const char* sub) {
- const size_t n = strlen(sub), sz = _cstr_p(&s)->size;
- return n <= sz && !memcmp(s.str + sz - n, sub, n);
+STC_INLINE bool cstr_ends_with(const cstr* self, const char* sub) {
+ const size_t n = strlen(sub), sz = _cstr_p(self)->size;
+ return n <= sz && !memcmp(self->str + sz - n, sub, n);
}
STC_INLINE int c_strncasecmp(const char* s1, const char* s2, size_t nmax) {
@@ -309,7 +310,7 @@ STC_INLINE void _cstr_internal_move(cstr* self, const size_t pos1, const size_t
STC_DEF void
cstr_replace_at_sv(cstr* self, const size_t pos, size_t len, csview repl) {
- const size_t sz = cstr_size(*self);
+ const size_t sz = cstr_size(self);
if (len > sz - pos) len = sz - pos;
c_autobuf (xstr, char, repl.size) {
memcpy(xstr, repl.str, repl.size);
@@ -366,16 +367,16 @@ cstr_getdelim(cstr *self, const int delim, FILE *fp) {
}
STC_DEF size_t
-cstr_find(cstr s, const char* needle) {
- char* res = strstr(s.str, needle);
- return res ? res - s.str : cstr_npos;
+cstr_find(const cstr* self, const char* needle) {
+ char* res = strstr(self->str, needle);
+ return res ? res - self->str : cstr_npos;
}
STC_DEF size_t
-cstr_find_at(cstr s, const size_t pos, const char* needle) {
- if (pos > _cstr_p(&s)->size) return cstr_npos;
- char* res = strstr(s.str + pos, needle);
- return res ? res - s.str : cstr_npos;
+cstr_find_at(const cstr* self, const size_t pos, const char* needle) {
+ if (pos > _cstr_p(self)->size) return cstr_npos;
+ char* res = strstr(self->str + pos, needle);
+ return res ? res - self->str : cstr_npos;
}
#endif
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 7af54c58..0728b110 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -80,7 +80,7 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2);
STC_API char* cstr_reserve(cstr* self, size_t cap);
STC_API void cstr_shrink_to_fit(cstr* self);
STC_API void cstr_resize(cstr* self, size_t size, char value);
-STC_API size_t cstr_find_at(cstr s, size_t pos, const char* search);
+STC_API size_t cstr_find_at(const cstr* self, size_t pos, const char* search);
STC_API char* cstr_assign_n(cstr* self, const char* str, size_t len);
STC_API char* cstr_append_n(cstr* self, const char* str, size_t len);
STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp);
@@ -161,14 +161,14 @@ STC_INLINE char* cstr_data(cstr* self)
STC_INLINE const char* cstr_str(const cstr* self)
{ return SSO_CALL(self, data(self)); }
-STC_INLINE bool cstr_empty(cstr s)
- { return s.sml.last == cstr_s_cap; }
+STC_INLINE bool cstr_empty(const cstr* self)
+ { return self->sml.last == cstr_s_cap; }
-STC_INLINE size_t cstr_size(cstr s)
- { return SSO_CALL(&s, size(&s)); }
+STC_INLINE size_t cstr_size(const cstr* self)
+ { return SSO_CALL(self, size(self)); }
-STC_INLINE size_t cstr_capacity(cstr s)
- { return cstr_is_long(&s) ? cstr_l_cap(&s) : cstr_s_cap; }
+STC_INLINE size_t cstr_capacity(const cstr* self)
+ { return cstr_is_long(self) ? cstr_l_cap(self) : cstr_s_cap; }
// utf8 methods defined in/depending on src/utf8code.c:
@@ -200,11 +200,11 @@ STC_INLINE bool cstr_valid_utf8(const cstr* self)
// other utf8
-STC_INLINE size_t cstr_u8_size(cstr s)
- { return utf8_size(cstr_str(&s)); }
+STC_INLINE size_t cstr_u8_size(const cstr* self)
+ { return utf8_size(cstr_str(self)); }
-STC_INLINE size_t cstr_u8_size_n(cstr s, size_t nbytes)
- { return utf8_size_n(cstr_str(&s), nbytes); }
+STC_INLINE size_t cstr_u8_size_n(const cstr* self, size_t nbytes)
+ { return utf8_size_n(cstr_str(self), nbytes); }
STC_INLINE size_t cstr_u8_to_pos(const cstr* self, size_t u8idx)
{ return utf8_pos(cstr_str(self), u8idx); }
@@ -242,7 +242,7 @@ STC_INLINE void cstr_clear(cstr* self)
{ _cstr_set_size(self, 0); }
STC_INLINE char* cstr_append_uninit(cstr *self, size_t len) {
- size_t sz = cstr_size(*self);
+ size_t sz = cstr_size(self);
char* d = cstr_reserve(self, sz + len);
if (!d) return NULL;
_cstr_set_size(self, sz + len);
@@ -261,75 +261,75 @@ STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2) {
}
-STC_INLINE bool cstr_equals(cstr s1, const char* str)
- { return !strcmp(cstr_str(&s1), str); }
+STC_INLINE bool cstr_equals(const cstr* self, const char* str)
+ { return !strcmp(cstr_str(self), str); }
-STC_INLINE bool cstr_equals_sv(cstr s, csview sv)
- { return sv.size == cstr_size(s) && !memcmp(cstr_str(&s), sv.str, sv.size); }
+STC_INLINE bool cstr_equals_sv(const cstr* self, csview sv)
+ { return sv.size == cstr_size(self) && !memcmp(cstr_str(self), sv.str, sv.size); }
-STC_INLINE bool cstr_equals_s(cstr s1, cstr s2)
- { return !cstr_cmp(&s1, &s2); }
+STC_INLINE bool cstr_equals_s(const cstr* self, cstr s)
+ { return !cstr_cmp(self, &s); }
-STC_INLINE bool cstr_iequals(cstr s1, const char* str)
- { return !utf8_icmp(cstr_str(&s1), str); }
+STC_INLINE bool cstr_iequals(const cstr* self, const char* str)
+ { return !utf8_icmp(cstr_str(self), str); }
-STC_INLINE size_t cstr_find(cstr s, const char* search) {
- const char *str = cstr_str(&s), *res = strstr((char*)str, search);
+STC_INLINE size_t cstr_find(const cstr* self, const char* search) {
+ const char *str = cstr_str(self), *res = strstr((char*)str, search);
return res ? res - str : cstr_npos;
}
-STC_API size_t cstr_find_sv(cstr s, csview search);
+STC_API size_t cstr_find_sv(const cstr* self, csview search);
-STC_INLINE size_t cstr_find_s(cstr s, cstr search)
- { return cstr_find(s, cstr_str(&search)); }
+STC_INLINE size_t cstr_find_s(const cstr* self, cstr search)
+ { return cstr_find(self, cstr_str(&search)); }
-STC_INLINE bool cstr_contains(cstr s, const char* search)
- { return strstr(cstr_data(&s), search) != NULL; }
+STC_INLINE bool cstr_contains(const cstr* self, const char* search)
+ { return strstr((char*)cstr_str(self), search) != NULL; }
-STC_INLINE bool cstr_contains_sv(cstr s, csview search)
- { return cstr_find_sv(s, search) != cstr_npos; }
+STC_INLINE bool cstr_contains_sv(const cstr* self, csview search)
+ { return cstr_find_sv(self, search) != cstr_npos; }
-STC_INLINE bool cstr_contains_s(cstr s, cstr search)
- { return strstr(cstr_data(&s), cstr_str(&search)) != NULL; }
+STC_INLINE bool cstr_contains_s(const cstr* self, cstr search)
+ { return strstr((char*)cstr_str(self), cstr_str(&search)) != NULL; }
-STC_INLINE bool cstr_starts_with_sv(cstr s, csview sub) {
- if (sub.size > cstr_size(s)) return false;
- return !memcmp(cstr_str(&s), sub.str, sub.size);
+STC_INLINE bool cstr_starts_with_sv(const cstr* self, csview sub) {
+ if (sub.size > cstr_size(self)) return false;
+ return !memcmp(cstr_str(self), sub.str, sub.size);
}
-STC_INLINE bool cstr_starts_with(cstr s, const char* sub) {
- const char* str = cstr_str(&s);
+STC_INLINE bool cstr_starts_with(const cstr* self, const char* sub) {
+ const char* str = cstr_str(self);
while (*sub && *str == *sub) ++str, ++sub;
return !*sub;
}
-STC_INLINE bool cstr_starts_with_s(cstr s, cstr sub)
- { return cstr_starts_with_sv(s, cstr_sv(&sub)); }
+STC_INLINE bool cstr_starts_with_s(const cstr* self, cstr sub)
+ { return cstr_starts_with_sv(self, cstr_sv(&sub)); }
-STC_INLINE bool cstr_istarts_with(cstr s, const char* sub) {
- csview sv = cstr_sv(&s);
+STC_INLINE bool cstr_istarts_with(const cstr* self, const char* sub) {
+ csview sv = cstr_sv(self);
size_t len = strlen(sub);
return len <= sv.size && !utf8_icmp_sv(sv, c_sv(sub, len));
}
-STC_INLINE bool cstr_ends_with_sv(cstr s, csview sub) {
- csview sv = cstr_sv(&s);
+STC_INLINE bool cstr_ends_with_sv(const cstr* self, csview sub) {
+ csview sv = cstr_sv(self);
if (sub.size > sv.size) return false;
return !memcmp(sv.str + sv.size - sub.size, sub.str, sub.size);
}
-STC_INLINE bool cstr_ends_with_s(cstr s, cstr sub)
- { return cstr_ends_with_sv(s, cstr_sv(&sub)); }
+STC_INLINE bool cstr_ends_with_s(const cstr* self, cstr sub)
+ { return cstr_ends_with_sv(self, cstr_sv(&sub)); }
-STC_INLINE bool cstr_ends_with(cstr s, const char* sub)
- { return cstr_ends_with_sv(s, c_sv(sub, strlen(sub))); }
+STC_INLINE bool cstr_ends_with(const cstr* self, const char* sub)
+ { return cstr_ends_with_sv(self, c_sv(sub, strlen(sub))); }
-STC_INLINE bool cstr_iends_with(cstr s, const char* sub) {
- csview sv = cstr_sv(&s);
+STC_INLINE bool cstr_iends_with(const cstr* self, const char* sub) {
+ csview sv = cstr_sv(self);
size_t n = strlen(sub);
return n <= sv.size && !utf8_icmp(sv.str + sv.size - n, sub);
}
@@ -402,9 +402,9 @@ STC_DEF uint64_t cstr_hash(const cstr *self) {
return c_fasthash(sv.str, sv.size);
}
-STC_DEF size_t cstr_find_sv(cstr s, csview search) {
- char* res = c_strnstrn(cstr_str(&s), search.str, cstr_size(s), search.size);
- return res ? res - cstr_str(&s) : cstr_npos;
+STC_DEF size_t cstr_find_sv(const cstr* self, csview search) {
+ char* res = c_strnstrn(cstr_str(self), search.str, cstr_size(self), search.size);
+ return res ? res - cstr_str(self) : cstr_npos;
}
STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) {
@@ -474,8 +474,8 @@ STC_DEF void cstr_resize(cstr* self, const size_t size, const char value) {
_cstr_set_size(self, size);
}
-STC_DEF size_t cstr_find_at(cstr s, const size_t pos, const char* search) {
- csview sv = cstr_sv(&s);
+STC_DEF size_t cstr_find_at(const cstr* self, const size_t pos, const char* search) {
+ csview sv = cstr_sv(self);
if (pos > sv.size) return cstr_npos;
const char* res = strstr((char*)sv.str + pos, search);
return res ? res - sv.str : cstr_npos;