From 47190a51588341d0ab715398ded6a480bc4fa95a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 14 Oct 2020 23:14:02 +0200 Subject: Renamed cstr() constructor to cstr_from(), cstr_from() to cstr_from_fmt(). --- README.md | 10 +++++----- examples/README.md | 2 +- examples/advanced.c | 2 +- examples/complex.c | 2 +- examples/demos.c | 6 +++--- examples/inits.c | 6 +++--- examples/phonebook.c | 4 ++-- examples/ptr.c | 2 +- examples/replace.c | 2 +- examples/share_ptr.c | 8 ++++---- examples/share_ptr2.c | 6 +++--- examples/words.c | 2 +- stc/carray.h | 12 ++++++------ stc/clist.h | 2 +- stc/cmap.h | 6 +++--- stc/cptr.h | 4 ++-- stc/cstr.h | 6 +++--- stc/cvec.h | 2 +- 18 files changed, 42 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index af7cdf8e..047b5a68 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,8 @@ using_cvec(u, User, User_del, User_compare); int main(void) { cvec_u vec = cvec_u_init(); - cvec_u_push_back(&vec, (User) {cstr("admin"), 0}); // cstr() allocates string memory - cvec_u_push_back(&vec, (User) {cstr("usera"), 1}); + cvec_u_push_back(&vec, (User) {cstr_from("admin"), 0}); // cstr_from() allocates string memory + cvec_u_push_back(&vec, (User) {cstr_from("usera"), 1}); c_foreach (i, cvec_u, vec) printf("%s: %d\n", i.val->name.str, i.val->id); cvec_u_del(&vec); // free everything @@ -229,7 +229,7 @@ int main() { cstr_assign(&names.data[1], "Jake"); // replace "Joe". // Use push_back() rather than emplace_back() when adding a cstr_t type: - cstr_t tmp = cstr_from("%d elements so far", cvec_str_size(names)); + cstr_t tmp = cstr_from_fmt("%d elements so far", cvec_str_size(names)); cvec_str_push_back(&names, tmp); // tmp is moved to names, do not del() it. printf("%s\n", names.data[1].str); // Access the second element @@ -249,7 +249,7 @@ item: 2 elements so far #include int main() { - cstr_t s1 = cstr("one-nine-three-seven-five"); + cstr_t s1 = cstr_from("one-nine-three-seven-five"); printf("%s.\n", s1.str); cstr_insert(&s1, 3, "-two"); @@ -266,7 +266,7 @@ int main() { cstr_append(&s1, " eight"); printf("append: %s\n", s1.str); - cstr_t full_path = cstr_from("%s/%s.%s", "directory", "filename", "ext"); + cstr_t full_path = cstr_from_fmt("%s/%s.%s", "directory", "filename", "ext"); printf("%s\n", full_path.str); c_del(cstr, &s1, &full_path); diff --git a/examples/README.md b/examples/README.md index ba6a7cfb..4c24d32a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -47,7 +47,7 @@ static inline VikingRaw viking_toRaw(Viking* vk) { VikingRaw vw = {vk->name.str, vk->country.str}; return vw; } static inline Viking viking_fromRaw(VikingRaw vw) { // note: parameter is by value - Viking vk = {cstr(vw.name), cstr(vw.country)}; return vk; + Viking vk = {cstr_from(vw.name), cstr_from(vw.country)}; return vk; } ``` With this in place, we use the full using_cmap() macro to define {Viking -> int} hash map type: diff --git a/examples/advanced.c b/examples/advanced.c index 8c3564f3..e9e0002b 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -47,7 +47,7 @@ VikingVw viking_toVw(Viking* vk) { VikingVw vw = {vk->name.str, vk->country.str}; return vw; } Viking viking_fromVw(VikingVw vw) { - Viking vk = {cstr(vw.name), cstr(vw.country)}; return vk; + Viking vk = {cstr_from(vw.name), cstr_from(vw.country)}; return vk; } // Using the full using_cmap() macro to define [Viking -> int] hash map type: diff --git a/examples/complex.c b/examples/complex.c index 7cde293c..c9704d73 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -17,7 +17,7 @@ int main() { cmap_s myMap = cmap_INIT; { // Construct. - carray2f table = carray2f_make(ydim, xdim, 0.f); + carray2f table = carray2f_init(ydim, xdim, 0.f); printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table)); clist_y tableList = clist_INIT; // Put in some data. diff --git a/examples/demos.c b/examples/demos.c index 1955d229..18b0f1ef 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -8,7 +8,7 @@ void stringdemo1() { printf("\nSTRINGDEMO1\n"); - cstr_t cs = cstr("one-nine-three-seven-five"); + cstr_t cs = cstr_from("one-nine-three-seven-five"); printf("%s.\n", cs.str); cstr_insert(&cs, 3, "-two"); @@ -20,7 +20,7 @@ void stringdemo1() cstr_replace(&cs, cstr_find(&cs, "seven"), 5, "four"); printf("%s.\n", cs.str); - cstr_take(&cs, cstr_from("%s *** %s", cs.str, cs.str)); + cstr_take(&cs, cstr_from_fmt("%s *** %s", cs.str, cs.str)); printf("%s.\n", cs.str); printf("find \"four\": %s\n", cs.str + cstr_find(&cs, "four")); @@ -177,7 +177,7 @@ using_carray(f, float); void arraydemo1() { printf("\nARRAYDEMO1\n"); - carray3f a3 = carray3f_make(30, 20, 10, 0.0f); + carray3f a3 = carray3f_init(30, 20, 10, 0.0f); *carray3f_at(&a3, 5, 4, 3) = 10.2f; // a3[5][4][3] carray2f a2 = carray3f_at1(&a3, 5); // sub-array reference: a2 = a3[5] carray1f a1 = carray3f_at2(&a3, 5, 4); // sub-array reference: a1 = a3[5][4] diff --git a/examples/inits.c b/examples/inits.c index ea4c61bc..3565c189 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -47,9 +47,9 @@ int main(void) int year = 2020; cmap_id idnames = cmap_INIT; c_push_items(&idnames, cmap_id, { - {100, cstr("Hello")}, - {110, cstr("World")}, - {120, cstr_from("Howdy, -%d-", year)}, + {100, cstr_from("Hello")}, + {110, cstr_from("World")}, + {120, cstr_from_fmt("Howdy, -%d-", year)}, }); c_foreach (i, cmap_id, idnames) diff --git a/examples/phonebook.c b/examples/phonebook.c index cb7130c1..c7a02e74 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -47,8 +47,8 @@ int main(int argc, char **argv) printf("Phone book:\n"); print_phone_book(phone_book); - c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr("(551) 396-1880")); - c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr("(551) 396-1990")); + c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1880")); + c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1990")); printf("\nPhone book after adding Zak Byers:\n"); print_phone_book(phone_book); diff --git a/examples/ptr.c b/examples/ptr.c index dc0e5341..3ffb9292 100644 --- a/examples/ptr.c +++ b/examples/ptr.c @@ -5,7 +5,7 @@ typedef struct { cstr_t name, last; } Person; Person* Person_make(Person* p, const char* name, const char* last) { - p->name = cstr(name), p->last = cstr(last); + p->name = cstr_from(name), p->last = cstr_from(last); return p; } void Person_del(Person* p) { diff --git a/examples/replace.c b/examples/replace.c index 5c5192e2..53e916fc 100644 --- a/examples/replace.c +++ b/examples/replace.c @@ -11,7 +11,7 @@ int main () // replace signatures used in the same order as described above: // Ustring positions: 0123456789*123456789*12345 - cstr_t s = cstr(base); // "this is a test string." + cstr_t s = cstr_from(base); // "this is a test string." cstr_t m = cstr_clone(s); cstr_append(&m, m.str); diff --git a/examples/share_ptr.c b/examples/share_ptr.c index 28f1975c..5dd52d19 100644 --- a/examples/share_ptr.c +++ b/examples/share_ptr.c @@ -7,7 +7,7 @@ typedef struct { cstr_t name, last; } Person; Person* Person_make(Person* p, const char* name, const char* last) { - p->name = cstr(name), p->last = cstr(last); + p->name = cstr_from(name), p->last = cstr_from(last); return p; } void Person_del(Person* p) { @@ -27,15 +27,15 @@ int main() { clist_pe queue = clist_pe_init(); cvec_pe vec = cvec_pe_init(); - csptr_pe joe = csptr_pe_make((Person) {cstr("Joe"), cstr("Jordan")}); + csptr_pe joe = csptr_pe_make((Person) {cstr_from("Joe"), cstr_from("Jordan")}); clist_pe_push_back(&queue, csptr_pe_share(joe)); cvec_pe_push_back(&vec, csptr_pe_share(joe)); puts("Push 10:"); c_forrange (i, 10) { csptr_pe p = csptr_pe_from(c_new(Person)); - p.get->name = cstr_from("Name %d", (i * 7) % 10); - p.get->last = cstr_from("Last %d", (i * 7) % 10); + p.get->name = cstr_from_fmt("Name %d", (i * 7) % 10); + p.get->last = cstr_from_fmt("Last %d", (i * 7) % 10); clist_pe_push_back(&queue, p); cvec_pe_push_back(&vec, csptr_pe_share(p)); // Don't forget to share! } diff --git a/examples/share_ptr2.c b/examples/share_ptr2.c index 4c589ad1..d4b536ac 100644 --- a/examples/share_ptr2.c +++ b/examples/share_ptr2.c @@ -11,7 +11,7 @@ Person* Person_from(Person* p, cstr_t name, cstr_t last) { return p; } Person* Person_make(Person* p, const char* name, const char* last) { - p->name = cstr(name), p->last = cstr(last); + p->name = cstr_from(name), p->last = cstr_from(last); return p; } void Person_del(Person* p) { @@ -30,8 +30,8 @@ int main() { // c_try_emplace: The last argument is completely ignored if key already exist in map, so no memory leak happens! c_forrange (i, 20) { // When i>9, all key will exist, so value arg is not executed. c_try_emplace(&map, cmap_pe, (i * 7) % 10, - csptr_pe_from(Person_from(c_new(Person), cstr_from("Name %d", (i * 7) % 10), - cstr_from("Last %d", (i * 9) % 10)))); + csptr_pe_from(Person_from(c_new(Person), cstr_from_fmt("Name %d", (i * 7) % 10), + cstr_from_fmt("Last %d", (i * 9) % 10)))); } c_try_emplace(&map, cmap_pe, 11, csptr_pe_from(Person_make(c_new(Person), "Hello", "World!"))); diff --git a/examples/words.c b/examples/words.c index f95c36eb..fb8b9b8c 100644 --- a/examples/words.c +++ b/examples/words.c @@ -16,7 +16,7 @@ int main1() "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); - clist_str_push_back(&lwords, cstr_from("%f", 123897.0 / 23.0)); + clist_str_push_back(&lwords, cstr_from_fmt("%f", 123897.0 / 23.0)); c_foreach (w, clist_str, lwords) printf("%s\n", w.val->str); puts(""); diff --git a/stc/carray.h b/stc/carray.h index 05ddda37..5df7add8 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -119,14 +119,14 @@ _carray3_size(const size_t* zdim) {return zdim[0] * zdim[-1] * zdim[-2];} using_carray_common(3, X, Value, valueDestroy) \ \ STC_INLINE carray1##X \ - carray1##X##_make(size_t xdim, Value val) { \ + carray1##X##_init(size_t xdim, Value val) { \ Value* m = c_new_2(Value, xdim); \ for (size_t i=0; iname = cstr(name), p->last = cstr(last); + p->name = cstr_from(name), p->last = cstr_from(last); return p; } void Person_del(Person* p) { @@ -100,7 +100,7 @@ int main() { typedef struct { cstr_t name, last; } Person; Person* Person_make(Person* p, const char* name, const char* last) { - p->name = cstr(name), p->last = cstr(last); + p->name = cstr_from(name), p->last = cstr_from(last); return p; } void Person_del(Person* p) { diff --git a/stc/cstr.h b/stc/cstr.h index 7031cc63..79a204d4 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -44,7 +44,7 @@ static cstr_t cstr_INIT = {(char* ) &_cstr_nullrep[2]}; STC_API cstr_t cstr_n(const char* str, size_t len); STC_API cstr_t -cstr_from(const char* fmt, ...); +cstr_from_fmt(const char* fmt, ...); STC_API size_t cstr_reserve(cstr_t* self, size_t cap); STC_API void @@ -91,7 +91,7 @@ cstr_with_size(size_t len, char fill) { return s; } STC_INLINE cstr_t -cstr(const char* str) { +cstr_from(const char* str) { return cstr_n(str, strlen(str)); } @@ -246,7 +246,7 @@ cstr_n(const char* str, size_t len) { } STC_API cstr_t -cstr_from(const char* fmt, ...) { +cstr_from_fmt(const char* fmt, ...) { #if defined(__clang__) # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wdeprecated-declarations" diff --git a/stc/cvec.h b/stc/cvec.h index 037b33ab..16c344a3 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -40,7 +40,7 @@ #define using_cvec_4(X, Value, valueDestroy, valueCompare) \ using_cvec_7(X, Value, valueDestroy, valueCompare, Value, c_default_to_raw, c_default_from_raw) #define using_cvec_str() \ - using_cvec_7(str, cstr_t, cstr_del, cstr_compare_raw, const char*, cstr_to_raw, cstr) + using_cvec_7(str, cstr_t, cstr_del, cstr_compare_raw, const char*, cstr_to_raw, cstr_from) #define using_cvec_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) \ -- cgit v1.2.3