summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--benchmarks/misc/string_bench_STC.cpp4
-rw-r--r--docs/ccommon_api.md2
-rw-r--r--docs/cstr_api.md54
-rw-r--r--docs/csview_api.md12
-rw-r--r--examples/astar.c4
-rw-r--r--examples/cstr_match.c14
-rw-r--r--examples/demos.c2
-rw-r--r--examples/music_arc.c2
-rw-r--r--examples/regex1.c2
-rw-r--r--examples/sso_map.c2
-rw-r--r--examples/sso_substr.c2
-rw-r--r--include/stc/alt/cstr.h47
-rw-r--r--include/stc/cstr.h106
13 files changed, 128 insertions, 125 deletions
diff --git a/benchmarks/misc/string_bench_STC.cpp b/benchmarks/misc/string_bench_STC.cpp
index 648cb1f3..065b6dd7 100644
--- a/benchmarks/misc/string_bench_STC.cpp
+++ b/benchmarks/misc/string_bench_STC.cpp
@@ -101,7 +101,7 @@ void initShortStringVec(cvec_str* vs, cvec_sv* vsv)
c_foreach (i, cvec_str, *vs)
{
cvec_sv_push_back(vsv, cstr_sv(i.ref));
- num += cstr_size(*i.ref);
+ num += cstr_size(i.ref);
}
std::cout << "num strings: " << cvec_sv_size(vsv) << std::endl;
std::cout << "avg str len: " << num / (float)cvec_sv_size(vsv) << std::endl;
@@ -147,7 +147,7 @@ void initLongStringVec(cvec_str* vs, cvec_sv* vsv)
c_foreach (i, cvec_str, *vs)
{
cvec_sv_push_back(vsv, cstr_sv(i.ref));
- num += cstr_size(*i.ref);
+ num += cstr_size(i.ref);
}
std::cout << "num strings: " << cvec_sv_size(vsv) << std::endl;
std::cout << "avg str len: " << num / (float)cvec_sv_size(vsv) << std::endl;
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index 179e7398..6745317d 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -217,7 +217,7 @@ c_find_if(it, cvec_i, vec, *it.ref > 2);
if (it.ref) printf("%d\n", *it.ref);
// Search within a range:
-c_find_in(it, csmap_str, it1, it2, cstr_contains(*it.ref, "hello"));
+c_find_in(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello"));
if (it.ref) cmap_str_erase_at(&map, it);
```
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index fffec64c..f5414f1b 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -30,16 +30,16 @@ cstr cstr_clone(cstr s);
cstr* cstr_take(cstr* self, cstr s); // take ownership of s, i.e. don't drop s.
cstr cstr_move(cstr* self); // move string to caller, leave self empty
-void cstr_drop(cstr *self); // destructor
+void cstr_drop(cstr* self); // destructor
const char* cstr_str(const cstr* self); // cast to const char*
char* cstr_data(cstr* self); // cast to mutable char*
csview cstr_sv(const cstr* self); // cast to string view
cstr_buf cstr_buffer(cstr* self); // cast to mutable buffer (with capacity)
-size_t cstr_size(cstr s);
-size_t cstr_capacity(cstr s);
-bool cstr_empty(cstr s);
+size_t cstr_size(const cstr* self);
+size_t cstr_capacity(const cstr* self);
+bool cstr_empty(const cstr* self);
char* cstr_reserve(cstr* self, size_t capacity); // return pointer to buffer
void cstr_resize(cstr* self, size_t len, char fill);
@@ -57,7 +57,7 @@ char* cstr_append(cstr* self, const char* str);
char* cstr_append_n(cstr* self, const char* str, size_t n); // append n first bytes of str
char* cstr_append_sv(cstr* self, csview str);
char* cstr_append_s(cstr* self, cstr str);
-char* cstr_append_uninit(cstr *self, size_t len); // append len uninitialized bytes
+char* cstr_append_uninit(cstr* self, size_t len); // append len uninitialized bytes
void cstr_insert(cstr* self, size_t pos, const char* ins);
void cstr_insert_sv(cstr* self, size_t pos, csview ins);
@@ -72,19 +72,21 @@ void cstr_replace_at(cstr* self, size_t pos, size_t len, const char* rep
void cstr_replace_at_sv(cstr* self, size_t pos, size_t len, const csview repl);
void cstr_replace_at_s(cstr* self, size_t pos, size_t len, cstr repl);
-bool cstr_equals(cstr s, const char* str);
-bool cstr_equals_s(cstr s, cstr s2);
-size_t cstr_find(cstr s, const char* search);
-size_t cstr_find_at(cstr s, size_t pos, const char* search); // search from pos
-bool cstr_contains(cstr s, const char* search);
+bool cstr_equals(const cstr* self, const char* str);
+bool cstr_equals_s(const cstr* self, cstr s);
+bool cstr_equals_sv(const cstr* self, csview sv);
-bool cstr_starts_with(cstr s, const char* str);
-bool cstr_starts_with_sv(cstr s, csview sv);
-bool cstr_starts_with_s(cstr s, cstr s);
+size_t cstr_find(const cstr* self, const char* search);
+size_t cstr_find_at(const cstr* self, size_t pos, const char* search); // search from pos
+bool cstr_contains(const cstr* self, const char* search);
-bool cstr_ends_with(cstr s, const char* str);
-bool cstr_ends_with_sv(cstr s, csview sv);
-bool cstr_ends_with_s(cstr s, cstr s);
+bool cstr_starts_with(const cstr* self, const char* str);
+bool cstr_starts_with_sv(const cstr* self, csview sv);
+bool cstr_starts_with_s(const cstr* self, cstr s);
+
+bool cstr_ends_with(const cstr* self, const char* str);
+bool cstr_ends_with_sv(const cstr* self, csview sv);
+bool cstr_ends_with_s(const cstr* self, cstr s);
bool cstr_getline(cstr *self, FILE *stream); // cstr_getdelim(self, '\n', stream)
bool cstr_getdelim(cstr *self, int delim, FILE *stream); // does not append delim to result
@@ -92,9 +94,9 @@ bool cstr_getdelim(cstr *self, int delim, FILE *stream); // does no
#### UTF8 methods
```c
-size_t cstr_u8_size(cstr s); // number of utf8 codepoints
-size_t cstr_u8_size_n(cstr s, size_t nbytes); // utf8 size within n bytes
-size_t cstr_u8_to_pos(cstr s, size_t u8idx); // byte pos offset at utf8 codepoint index
+size_t cstr_u8_size(const cstr* self); // number of utf8 codepoints
+size_t cstr_u8_size_n(const cstr self, size_t nbytes); // utf8 size within n bytes
+size_t cstr_u8_to_pos(const cstr* self, size_t u8idx); // byte pos offset at utf8 codepoint index
const char* cstr_u8_at(const cstr* self, size_t u8idx); // char* position at utf8 codepoint index
csview cstr_u8_chr(const cstr* self, size_t u8idx); // get utf8 character as a csview
void cstr_u8_replace_at(cstr* self, size_t u8pos, size_t u8len, csview repl); // replace at utf8 indices
@@ -117,18 +119,18 @@ cstr cstr_toupper_sv(csview sv); // returns
void cstr_uppercase(cstr* self); // transform cstr to uppercase utf8
int cstr_icmp(const cstr* s1, const cstr* s2); // utf8 case-insensitive comparison
-bool cstr_iequals(cstr s, const char* str); // "
-bool cstr_istarts_with(cstr s, const char* str); // "
-bool cstr_iends_with(cstr s, const char* str); // "
+bool cstr_iequals(const cstr* self, const char* str); // "
+bool cstr_istarts_with(const cstr* self, const char* str); // "
+bool cstr_iends_with(const cstr* self, const char* str); // "
```
Note that all methods with arguments `(..., const char* str, size_t n)`, `n` must be within the range of `str` length.
#### Helper methods:
```c
-int cstr_cmp(const cstr *s1, const cstr *s2);
-bool cstr_eq(const cstr *s1, const cstr *s2);
-bool cstr_hash(const cstr *s);
+int cstr_cmp(const cstr* s1, const cstr* s2);
+bool cstr_eq(const cstr* s1, const cstr* s2);
+bool cstr_hash(const cstr* self);
char* c_strnstrn(const char* str, const char* search, size_t slen, size_t nlen);
```
@@ -155,7 +157,7 @@ char* c_strnstrn(const char* str, const char* search, size_t slen, size_t
int main() {
cstr s0 = cstr_new("Initialization without using strlen().");
- printf("%s\nLength: %" PRIuMAX "\n\n", cstr_str(&s0), cstr_size(s0));
+ printf("%s\nLength: %" PRIuMAX "\n\n", cstr_str(&s0), cstr_size(&s0));
cstr s1 = cstr_new("one-nine-three-seven-five.");
printf("%s\n", cstr_str(&s1));
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 1e2ed408..79d108a3 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -92,11 +92,11 @@ uint64_t csview_hash(const csview* x);
## Types
-| Type name | Type definition | Used to represent... |
-|:----------------|:------------------------------------------|:-------------------------|
-| `csview` | `struct { const char *str; size_t size }` | The string view type |
-| `csview_value` | `char` | The string element type |
-| `csview_iter` | `struct { csview_value *ref; }` | UTF8 iterator |
+| Type name | Type definition | Used to represent... |
+|:----------------|:-------------------------------------------|:-------------------------|
+| `csview` | `struct { const char *str; size_t size; }` | The string view type |
+| `csview_value` | `char` | The string element type |
+| `csview_iter` | `struct { csview_value *ref; }` | UTF8 iterator |
## Constants and macros
@@ -118,7 +118,7 @@ int main ()
// (quoting Alfred N. Whitehead)
csview sv1 = cstr_substr(&str1, 3, 5); // "think"
- size_t pos = cstr_find(str1, "live"); // position of "live" in str1
+ size_t pos = cstr_find(&str1, "live"); // position of "live" in str1
csview sv2 = cstr_substr(&str1, pos, 4); // get "live"
csview sv3 = cstr_slice(&str1, -8, -1); // get "details"
printf("%" c_PRIsv "%" c_PRIsv "%" c_PRIsv "\n",
diff --git a/examples/astar.c b/examples/astar.c
index 7d90bf35..cb498b11 100644
--- a/examples/astar.c
+++ b/examples/astar.c
@@ -37,7 +37,7 @@ point_equal(const point* a, const point* b)
point
point_from(const cstr* maze, const char* c, int width)
{
- int index = cstr_find(*maze, c);
+ int index = cstr_find(maze, c);
return point_init(index % width, index / width, width);
}
@@ -156,7 +156,7 @@ main(void)
"# # # # # # #\n"
"#########################################################################\n"), cstr_drop(&maze))
{
- int width = cstr_find(maze, "\n") + 1;
+ int width = cstr_find(&maze, "\n") + 1;
c_autovar (cdeq_point path = astar(&maze, width), cdeq_point_drop(&path))
{
c_foreach (it, cdeq_point, path) cstr_data(&maze)[point_index(it.ref)] = 'x';
diff --git a/examples/cstr_match.c b/examples/cstr_match.c
index 6927ed80..317ff986 100644
--- a/examples/cstr_match.c
+++ b/examples/cstr_match.c
@@ -5,18 +5,18 @@
int main()
{
c_autovar (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) {
- size_t pos = cstr_find_at(ss, 0, "brown");
+ size_t pos = cstr_find_at(&ss, 0, "brown");
printf("%" PRIuMAX " [%s]\n", pos, pos == cstr_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"));
+ 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_new("hell😀 w😀rl🐨");
csview ch1 = cstr_u8_chr(&s1, 7);
csview ch2 = cstr_u8_chr(&s1, 10);
- printf("%s\nsize: %" PRIuMAX ", %" PRIuMAX "\n", cstr_str(&s1), cstr_u8_size(s1), cstr_size(s1));
+ printf("%s\nsize: %" PRIuMAX ", %" PRIuMAX "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1));
printf("ch1: %.*s\n", c_ARGsv(ch1));
printf("ch2: %.*s\n", c_ARGsv(ch2));
}
diff --git a/examples/demos.c b/examples/demos.c
index 052c4e32..cd715d46 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -19,7 +19,7 @@ void stringdemo1()
cstr_take(&cs, cstr_from_fmt("%s *** %s", cstr_str(&cs), cstr_str(&cs)));
printf("%s.\n", cstr_str(&cs));
- printf("find \"four\": %s\n", cstr_str(&cs) + cstr_find(cs, "four"));
+ printf("find \"four\": %s\n", cstr_str(&cs) + cstr_find(&cs, "four"));
// reassign:
cstr_assign(&cs, "one two three four five six seven");
diff --git a/examples/music_arc.c b/examples/music_arc.c
index d933d125..02470b0f 100644
--- a/examples/music_arc.c
+++ b/examples/music_arc.c
@@ -40,7 +40,7 @@ void example3()
}) SongVec_emplace(&vec, *v);
c_foreach (s, SongVec, vec)
- if (!cstr_equals(s.ref->get->artist, "Bob Dylan"))
+ if (!cstr_equals(&s.ref->get->artist, "Bob Dylan"))
SongVec_push_back(&vec2, SongArc_clone(*s.ref));
SongVec_emplace(&vec2, Song_from("Michael Jackson", "Billie Jean"));
diff --git a/examples/regex1.c b/examples/regex1.c
index 5981e878..c8b3a4f5 100644
--- a/examples/regex1.c
+++ b/examples/regex1.c
@@ -19,7 +19,7 @@ int main(int argc, char* argv[])
cstr_getline(&input, stdin);
// Exit when the user inputs q
- if (cstr_equals(input, "q"))
+ if (cstr_equals(&input, "q"))
break;
if (cregex_is_match(cstr_str(&input), &float_expr))
diff --git a/examples/sso_map.c b/examples/sso_map.c
index a32a9a3d..43bcb40b 100644
--- a/examples/sso_map.c
+++ b/examples/sso_map.c
@@ -11,7 +11,7 @@ int main()
c_forpair (k, v, cmap_str, m)
printf("%s: '%s' Len=%" PRIuMAX ", Is long: %s\n",
- cstr_str(_.k), cstr_str(_.v), cstr_size(*_.v),
+ cstr_str(_.k), cstr_str(_.v), cstr_size(_.v),
cstr_is_long(_.v)?"true":"false");
}
}
diff --git a/examples/sso_substr.c b/examples/sso_substr.c
index b47512ea..098d9b4b 100644
--- a/examples/sso_substr.c
+++ b/examples/sso_substr.c
@@ -6,7 +6,7 @@ int main ()
{
cstr str = cstr_new("We think in generalities, but we live in details.");
csview sv1 = cstr_substr_ex(&str, 3, 5); // "think"
- size_t pos = cstr_find(str, "live"); // position of "live"
+ size_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_ARGsv(sv1), c_ARGsv(sv2), c_ARGsv(sv3));
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;