summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-05-18 10:04:53 +0200
committerTyge Løvset <[email protected]>2021-05-18 10:04:53 +0200
commit60a127bb9a7180d4d30c2b7186456cc1226e812a (patch)
tree615bc0f176dbd22af7d9a269c02793eeceab45cf
parent915ccf1d811ae9a0469cddd8f2dc6370e835e24b (diff)
downloadSTC-modified-60a127bb9a7180d4d30c2b7186456cc1226e812a.tar.gz
STC-modified-60a127bb9a7180d4d30c2b7186456cc1226e812a.zip
Internal updates in cstr. Minor API change in carray carrayNX_with_value() => carrayNX_with_values(). Docs/examples improvements.
-rw-r--r--docs/carray_api.md4
-rw-r--r--docs/cstr_api.md22
-rw-r--r--examples/complex.c2
-rw-r--r--examples/crandom_ex.c2
-rw-r--r--examples/demos.c2
-rw-r--r--examples/ex_gauss1.c10
-rw-r--r--examples/ex_gauss2.c2
-rw-r--r--stc/carray.h8
-rw-r--r--stc/cstr.h80
9 files changed, 62 insertions, 70 deletions
diff --git a/docs/carray_api.md b/docs/carray_api.md
index 51688fd3..3ae293c3 100644
--- a/docs/carray_api.md
+++ b/docs/carray_api.md
@@ -29,7 +29,7 @@ be replaced by `i` in all of the following documentation.
```c
carray2X carray2X_init(size_t xdim, size_t ydim);
-carray2X carray2X_with_value(size_t xdim, size_t ydim, Value val);
+carray2X carray2X_with_values(size_t xdim, size_t ydim, Value val);
carray2X carray2X_with_storage(size_t xdim, size_t ydim, Value* array);
carray2X carray2X_clone(carray2X arr);
Value* carray2X_release(carray2X* self); // release storage (not freed)
@@ -45,7 +45,7 @@ void carray2X_next(carray2X_iter_t* it);
```
```c
carray3X carray3X_init(size_t xdim, size_t ydim, size_t zdim);
-carray3X carray3X_with_value(size_t xdim, size_t ydim, size_t zdim, Value val);
+carray3X carray3X_with_values(size_t xdim, size_t ydim, size_t zdim, Value val);
carray3X carray3X_with_storage(size_t xdim, size_t ydim, size_t zdim, Value* array);
carray3X carray3X_clone(carray3X arr);
Value* carray3X_release(carray3X* self); // release storage (not freed)
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index 308c54ab..a2b23a70 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -15,12 +15,12 @@ All cstr definitions and prototypes are available by including a single header f
## Methods
```c
-cstr cstr_init(void); // constructor
-cstr cstr_new(const char literal_only[]); // constructor
+cstr cstr_init(void); // constructor; same as cstr_null.
+cstr cstr_new(const char literal_only[]); // cstr from literal; no strlen().
+cstr cstr_from(const char* str); // constructor using strlen()
+cstr cstr_from_n(const char* str, size_t n); // constructor with specified length
cstr cstr_with_capacity(size_t cap);
cstr cstr_with_size(size_t len, char fill); // repeat fill len times
-cstr cstr_from(const char* str);
-cstr cstr_from_n(const char* str, size_t n);
cstr cstr_from_fmt(const char* fmt, ...); // printf() formatting
cstr cstr_clone(cstr s);
@@ -62,14 +62,14 @@ int cstr_compare(const cstr *s1, const cstr *s2);
bool cstr_equals(cstr s, const char* str);
bool cstr_equals_s(cstr s, cstr s2);
size_t cstr_find(cstr s, const char* substr);
-size_t cstr_find_n(cstr s, const char* substr, size_t pos, size_t n);
-bool cstr_contains(cstr s, const char* substr);
+size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax);
+bool cstr_contains(cstr s, const char* needle);
bool cstr_begins_with(cstr s, const char* substr);
bool cstr_ends_with(cstr s, const char* substr);
bool cstr_iequals(cstr s, const char* str); // prefix i = case-insensitive
-size_t cstr_ifind_n(cstr s, const char* substr, size_t pos, size_t n);
-bool cstr_icontains(cstr s, const char* substr);
+size_t cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t nmax);
+bool cstr_icontains(cstr s, const char* needle);
bool cstr_ibegins_with(cstr s, const char* substr);
bool cstr_iends_with(cstr s, const char* substr);
@@ -96,8 +96,8 @@ int c_rawstr_compare(const char** x, const char** y);
bool c_rawstr_equals(const char** x, const char** y);
uint64_t c_rawstr_hash(const char* const* x, size_t ignored);
int c_strncasecmp(const char* str1, const char* str2, size_t n);
-char* c_strnstr(const char* str, const char* needle, size_t n);
-char* c_strncasestr(const char* str, const char* needle, size_t n);
+char* c_strnstrn(const char* str, const char* needle, size_t slen, size_t nmax);
+char* c_strncasestrn(const char* str, const char* needle, size_t slen, size_t nmax);
```
## Types
@@ -113,7 +113,7 @@ char* c_strncasestr(const char* str, const char* needle, size_t n);
| Name | Value |
|:------------------|:------------------|
| `cstr_npos` | `((size_t) -1)` |
-| `cstr_null | cstr null value |
+| `cstr_null` | cstr null value |
## Example
```c
diff --git a/examples/complex.c b/examples/complex.c
index 13c36d97..c6390d52 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -22,7 +22,7 @@ int main() {
cmap_map myMap = cmap_map_init();
cmap_lst listMap = cmap_lst_init();
clist_arr tableList = clist_arr_init();
- carray2f arr2 = carray2f_with_value(xdim, ydim, 1.f);
+ carray2f arr2 = carray2f_with_values(xdim, ydim, 1.f);
printf("arr2 size: %zu x %zu\n", arr2.xdim, arr2.ydim);
diff --git a/examples/crandom_ex.c b/examples/crandom_ex.c
index 4fa1d487..1d1691af 100644
--- a/examples/crandom_ex.c
+++ b/examples/crandom_ex.c
@@ -13,7 +13,7 @@ int main()
uint64_t sum = 0;
- stc64_normalf_t dist2 = stc64_normalf_init(R / 2.0, R / 6.0);
+ stc64_normalf_t dist2 = stc64_normalf_init((float)R / 2.0, (float)R / 6.0);
size_t N2 = 10000000;
int hist[R] = {0};
sum = 0;
diff --git a/examples/demos.c b/examples/demos.c
index 3502f5f1..e63d5660 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -184,7 +184,7 @@ using_carray3(f, float);
void arraydemo1()
{
printf("\nARRAYDEMO1\n");
- carray3f arr3 = carray3f_with_value(30, 20, 10, 0.0f);
+ carray3f arr3 = carray3f_with_values(30, 20, 10, 0.0f);
arr3.data[5][4][3] = 10.2f;
float **arr2 = arr3.data[5];
float *arr1 = arr3.data[5][4];
diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c
index 75b96482..5613d2b3 100644
--- a/examples/ex_gauss1.c
+++ b/examples/ex_gauss1.c
@@ -10,12 +10,12 @@
using_cmap(i, int, size_t);
// Declare int vector with map entries that can be sorted by map keys.
-struct mapval {int first; size_t second;};
-static int compare(struct mapval *a, struct mapval *b) {
+c_struct (mapval) {int first; size_t second;};
+static int compare(mapval *a, mapval *b) {
return c_default_compare(&a->first, &b->first);
}
-using_cvec(e, struct mapval, compare);
+using_cvec(e, mapval, compare);
int main()
{
@@ -39,13 +39,13 @@ int main()
// Transfer map to vec and sort it by map keys.
cvec_e vhist = cvec_e_init();
c_foreach (i, cmap_i, mhist)
- cvec_e_push_back(&vhist, (struct mapval){i.ref->first, i.ref->second});
+ cvec_e_push_back(&vhist, c_make(mapval){i.ref->first, i.ref->second});
cvec_e_sort(&vhist);
// Print the gaussian bar chart
cstr bar = cstr_init();
c_foreach (i, cvec_e, vhist) {
- size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / N);
+ size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
printf("%4d %s\n", i.ref->first, bar.str);
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c
index 60195e7d..bd2f41bd 100644
--- a/examples/ex_gauss2.c
+++ b/examples/ex_gauss2.c
@@ -30,7 +30,7 @@ int main()
// Print the gaussian bar chart
cstr bar = cstr_init();
c_foreach (i, csmap_i, mhist) {
- size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / N);
+ size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
printf("%4d %s\n", i.ref->first, bar.str);
diff --git a/stc/carray.h b/stc/carray.h
index a8f02a95..8bfe6fe1 100644
--- a/stc/carray.h
+++ b/stc/carray.h
@@ -67,7 +67,7 @@ int main() {
typedef struct { CX##_value_t **data; size_t xdim, ydim; } CX; \
typedef struct { CX##_value_t *ref; } CX##_iter_t; \
\
- STC_API CX CX##_with_value(size_t xdim, size_t ydim, Value value); \
+ STC_API CX CX##_with_values(size_t xdim, size_t ydim, Value value); \
STC_API CX CX##_with_storage(size_t xdim, size_t ydim, CX##_value_t* storage); \
STC_API CX CX##_clone(CX src); \
\
@@ -111,7 +111,7 @@ int main() {
typedef struct { CX##_value_t ***data; size_t xdim, ydim, zdim; } CX; \
typedef struct { CX##_value_t *ref; } CX##_iter_t; \
\
- STC_API CX CX##_with_value(size_t xdim, size_t ydim, size_t zdim, Value value); \
+ STC_API CX CX##_with_values(size_t xdim, size_t ydim, size_t zdim, Value value); \
STC_API CX CX##_with_storage(size_t xdim, size_t ydim, size_t zdim, CX##_value_t* storage); \
STC_API CX CX##_clone(CX src); \
\
@@ -154,7 +154,7 @@ int main() {
return _arr; \
} \
\
- STC_DEF CX CX##_with_value(size_t xdim, size_t ydim, Value value) { \
+ STC_DEF CX CX##_with_values(size_t xdim, size_t ydim, Value value) { \
CX _arr = CX##_init(xdim, ydim); \
for (CX##_value_t* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p) \
*p = value; \
@@ -189,7 +189,7 @@ int main() {
return _arr; \
} \
\
- STC_DEF CX CX##_with_value(size_t xdim, size_t ydim, size_t zdim, Value value) { \
+ STC_DEF CX CX##_with_values(size_t xdim, size_t ydim, size_t zdim, Value value) { \
CX _arr = CX##_init(xdim, ydim, zdim); \
for (CX##_value_t* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p) \
*p = value; \
diff --git a/stc/cstr.h b/stc/cstr.h
index 302bdfec..4497d1a9 100644
--- a/stc/cstr.h
+++ b/stc/cstr.h
@@ -58,12 +58,12 @@ STC_API void cstr_replace_n(cstr* self, size_t pos, size_t len, const
STC_API size_t cstr_replace_all(cstr* self, const char* find, const char* replace);
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_n(cstr s, const char* needle, size_t pos, size_t n);
-STC_API size_t cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t n);
+STC_API size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax);
+STC_API size_t cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t nmax);
STC_API bool cstr_getdelim(cstr *self, int delim, FILE *stream);
-STC_API int c_strncasecmp(const char* s1, const char* s2, size_t n);
-STC_API char* c_strnstr(const char* s, const char* needle, size_t nmax);
-STC_API char* c_strncasestr(const char* s, const char* needle, size_t nmax);
+STC_API int c_strncasecmp(const char* s1, const char* s2, size_t nmax);
+STC_API char* c_strnstrn(const char* s, const char* needle, size_t slen, size_t nmax);
+STC_API char* c_strncasestrn(const char* s, const char* needle, size_t slen, size_t nmax);
STC_INLINE cstr cstr_init() { return cstr_null; }
#define cstr_new(literal) \
@@ -112,16 +112,16 @@ STC_INLINE cstr_iter_t cstr_begin(cstr* self)
STC_INLINE cstr_iter_t cstr_end(cstr* self)
{ return c_make(cstr_iter_t){self->str + _cstr_rep(self)->size}; }
STC_INLINE void cstr_next(cstr_iter_t* it) {++it->ref; }
-STC_INLINE bool cstr_equals(cstr s1, const char* str)
- { return strcmp(s1.str, str) == 0; }
+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_iequals(cstr s1, const char* str)
- { return c_strncasecmp(s1.str, str, cstr_npos) == 0; }
+STC_INLINE bool cstr_iequals(cstr s, const char* str)
+ { return c_strncasecmp(s.str, str, cstr_npos) == 0; }
STC_INLINE bool cstr_contains(cstr s, const char* needle)
{ return strstr(s.str, needle) != NULL; }
STC_INLINE bool cstr_icontains(cstr s, const char* needle)
- { return c_strncasestr(s.str, needle, cstr_npos) != NULL; }
+ { return c_strncasestrn(s.str, needle, cstr_size(s), cstr_npos) != NULL; }
STC_INLINE bool cstr_getline(cstr *self, FILE *stream)
{ return cstr_getdelim(self, '\n', stream); }
@@ -362,58 +362,50 @@ cstr_find(cstr s, const char* needle) {
}
STC_DEF size_t
-cstr_find_n(cstr s, const char* needle, size_t pos, size_t n) {
+cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax) {
if (pos > _cstr_rep(&s)->size) return cstr_npos;
- char* res = c_strnstr(s.str + pos, needle, n);
+ char* res = c_strnstrn(s.str + pos, needle, _cstr_rep(&s)->size - pos, nmax);
return res ? res - s.str : cstr_npos;
}
STC_DEF size_t
-cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t n) {
+cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t nmax) {
if (pos > _cstr_rep(&s)->size) return cstr_npos;
- char* res = c_strncasestr(s.str + pos, needle, n);
+ char* res = c_strncasestrn(s.str + pos, needle, _cstr_rep(&s)->size - pos, nmax);
return res ? res - s.str : cstr_npos;
}
STC_DEF int
-c_strncasecmp(const char* s1, const char* s2, size_t n) {
+c_strncasecmp(const char* s1, const char* s2, size_t nmax) {
int ret = 0;
- while (n-- && (ret = tolower(*s1++) - tolower(*s2)) == 0 && *s2++) ;
+ while (nmax-- && (ret = tolower(*s1++) - tolower(*s2)) == 0 && *s2++) ;
return ret;
}
STC_DEF char*
-c_strnstr(const char* s, const char* needle, size_t nmax) {
- ptrdiff_t sum = 0;
- const char *t = s, *p = needle;
- while (*p && nmax--) {
- if (!*t) return NULL;
- sum += *t++ - *p++;
- }
- nmax = t - s;
- for (;;) {
- if (sum == 0 && memcmp(s, needle, nmax) == 0)
- return (char *) s;
- if (!*t) return NULL;
- sum += *t++ - *s++;
- }
+c_strnstrn(const char *s, const char *needle, size_t slen, size_t nmax) {
+ size_t n = strlen(needle);
+ if (nmax < n) n = nmax;
+ if (n > slen) return NULL;
+ slen -= n;
+ do {
+ if (*s == *needle && !memcmp(s, needle, n)) return (char *)s;
+ ++s;
+ } while (slen--);
+ return NULL;
}
STC_DEF char*
-c_strncasestr(const char* s, const char* needle, size_t nmax) {
- ptrdiff_t sum = 0;
- const char *t = s, *p = needle;
- while (*p && nmax--) {
- if (!*t) return NULL;
- sum += tolower(*t++) - tolower(*p++);
- }
- nmax = t - s;
- for (;;) {
- if (sum == 0 && c_strncasecmp(s, needle, nmax) == 0)
- return (char *) s;
- if (!*t) return NULL;
- sum += tolower(*t++) - tolower(*s++);
- }
+c_strncasestrn(const char *s, const char *needle, size_t slen, size_t nmax) {
+ size_t n = strlen(needle);
+ if (nmax < n) n = nmax;
+ if (n > slen) return NULL;
+ int c = tolower(*needle); slen -= n;
+ do {
+ if (tolower(*s) == c && !c_strncasecmp(s, needle, n)) return (char *)s;
+ ++s;
+ } while (slen--);
+ return NULL;
}
#endif