diff options
| author | Tyge Løvset <[email protected]> | 2022-12-20 12:17:32 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-12-20 12:17:32 +0100 |
| commit | 1778629d92849af84239c4b8c1940ed76fd87b8a (patch) | |
| tree | cbe600a280aee2405836c66413e234de071bb231 | |
| parent | 21817cae767d72e6007150b639f9365e35502173 (diff) | |
| download | STC-modified-1778629d92849af84239c4b8c1940ed76fd87b8a.tar.gz STC-modified-1778629d92849af84239c4b8c1940ed76fd87b8a.zip | |
Renamed (reverted) cstr_new(lit) => cstr_lit(lit). Old name is deprecated (supported for now).
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 8 | ||||
| -rw-r--r-- | docs/cmap_api.md | 10 | ||||
| -rw-r--r-- | docs/cstr_api.md | 6 | ||||
| -rw-r--r-- | docs/csview_api.md | 13 | ||||
| -rw-r--r-- | docs/cvec_api.md | 6 | ||||
| -rw-r--r-- | examples/astar.c | 2 | ||||
| -rw-r--r-- | examples/books.c | 4 | ||||
| -rw-r--r-- | examples/city.c | 8 | ||||
| -rw-r--r-- | examples/csmap_erase.c | 10 | ||||
| -rw-r--r-- | examples/csmap_insert.c | 2 | ||||
| -rw-r--r-- | examples/cstr_match.c | 4 | ||||
| -rw-r--r-- | examples/demos.c | 2 | ||||
| -rw-r--r-- | examples/inits.c | 2 | ||||
| -rw-r--r-- | examples/sso_substr.c | 2 | ||||
| -rw-r--r-- | examples/utf8replace_c.c | 2 | ||||
| -rw-r--r-- | include/stc/alt/cstr.h | 2 | ||||
| -rw-r--r-- | include/stc/cstr.h | 4 |
18 files changed, 50 insertions, 47 deletions
@@ -371,7 +371,7 @@ and non-emplace methods: #include <stc/cvec.h> // vector of string (cstr) ... c_auto (cvec_str, vec) // declare and call cvec_str_init() and defer cvec_str_drop(&vec) -c_autodrop (cstr, s, cstr_new("a string literal")) // like c_auto without auto default init. +c_autodrop (cstr, s, cstr_lit("a string literal")) // like c_auto without auto default init. { const char* hello = "Hello"; cvec_str_push_back(&vec, cstr_from(hello); // construct and add string from const char* @@ -396,9 +396,9 @@ cmap_str_emplace(&map, "Hello", "again"); // No cstr was constructed because "Hello" was already in the map. cmap_str_emplace_or_assign(&map, "Hello", "there"); -// Only cstr_new("there") constructed. "world" was destructed and replaced. +// Only cstr_lit("there") constructed. "world" was destructed and replaced. -cmap_str_insert(&map, cstr_new("Hello"), cstr_new("you")); +cmap_str_insert(&map, cstr_lit("Hello"), cstr_lit("you")); // Two cstr's constructed outside call, but both destructed by insert // because "Hello" existed. No mem-leak but less efficient. @@ -520,7 +520,7 @@ Memory efficiency - Renamed: type **csptr** to [**carc**](docs/carc_api.md) (atomic reference counted) smart pointer. - Renamed: ***i_key_csptr*** / ***i_val_csptr*** to `i_keyboxed` / `i_valboxed` for specifying **carc** and **cbox** values in containers. - Renamed: *csptr_X_make()* to `carc_X_from()`. -- Renamed: *cstr_lit()* to `cstr_new(literal)`, and *cstr_assign_fmt()* to `cstr_printf()`. +- Renamed: *cstr_lit()* to `cstr_lit(literal)`, and *cstr_assign_fmt()* to `cstr_printf()`. - Renamed: *c_default_fromraw()* to `c_default_from()`. - Changed: the [**c_apply**](docs/ccommon_api.md) macros API. - Replaced: *csview_first_token()* and *csview_next_token()* with one function: `csview_token()`. @@ -541,7 +541,7 @@ Replace (whole word + match case): - `i_keydel` ⟶ `i_keydrop` - `i_valdel` ⟶ `i_valdrop` - `i_cnt` ⟶ `i_type` -- `cstr_lit` ⟶ `cstr_new` +- `cstr_lit` ⟶ `cstr_lit` - `i_key_sptr` ⟶ `i_keyboxed` - `i_val_sptr` ⟶ `i_valboxed` diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 8f887626..2c6db85f 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -32,7 +32,7 @@ c_with (FILE* fp = fopen(fname, "rb"), fclose(fp)) } } -c_with (cstr str = cstr_new("Hello"), cstr_drop(&str)) +c_with (cstr str = cstr_lit("Hello"), cstr_drop(&str)) { cstr_append(&str, " world"); printf("%s\n", cstr_str(&str)); @@ -50,7 +50,7 @@ c_auto (cstr, s1, s2) printf("%s %s\n", cstr_str(&s1), cstr_str(&s2)); } -c_autodrop (cstr, str, cstr_new("Hello")) +c_autodrop (cstr, str, cstr_lit("Hello")) { cstr_append(&str, " world"); printf("%s\n", cstr_str(&str)); @@ -64,7 +64,7 @@ c_scope (pthread_mutex_lock(&mut), pthread_mutex_unlock(&mut)) } // `c_defer` executes the expressions when leaving scope. -cstr s1 = cstr_new("Hello"), s2 = cstr_new("world"); +cstr s1 = cstr_lit("Hello"), s2 = cstr_lit("world"); c_defer (cstr_drop(&s1), cstr_drop(&s2)) { printf("%s %s\n", cstr_str(&s1), cstr_str(&s2)); @@ -330,7 +330,7 @@ c_free(pnt); int* array = c_alloc_n (int, 100); c_free(array); -cstr a = cstr_new("Hello"), b = cstr_new("World"); +cstr a = cstr_lit("Hello"), b = cstr_lit("World"); c_drop(cstr, &a, &b); ``` diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 315d2062..520d9046 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -316,13 +316,13 @@ int main() // Use a HashMap to store the vikings' health points. c_auto (Vikings, vikings) // uses Vikings_init(), Vikings_drop() { - Vikings_insert(&vikings, (Viking){cstr_new("Einar"), cstr_new("Norway")}, 25); - Vikings_insert(&vikings, (Viking){cstr_new("Olaf"), cstr_new("Denmark")}, 24); - Vikings_insert(&vikings, (Viking){cstr_new("Harald"), cstr_new("Iceland")}, 12); - Vikings_insert(&vikings, (Viking){cstr_new("Einar"), cstr_new("Denmark")}, 21); + Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Norway")}, 25); + Vikings_insert(&vikings, (Viking){cstr_lit("Olaf"), cstr_lit("Denmark")}, 24); + Vikings_insert(&vikings, (Viking){cstr_lit("Harald"), cstr_lit("Iceland")}, 12); + Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Denmark")}, 21); c_auto (Viking, lookup) { - lookup = (Viking){cstr_new("Einar"), cstr_new("Norway")}; + lookup = (Viking){cstr_lit("Einar"), cstr_lit("Norway")}; printf("Lookup: Einar of Norway has %d hp\n\n", *Vikings_at(&vikings, lookup)); } diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 1f80e8e4..0f9589d9 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -19,7 +19,7 @@ All cstr definitions and prototypes are available by including a single header f ## Methods ```c cstr cstr_init(void); // constructor; same as cstr_NULL. -cstr cstr_new(const char literal_only[]); // cstr from literal; no strlen() call. +cstr cstr_lit(const char literal_only[]); // cstr from literal; no strlen() call. cstr cstr_from(const char* str); // constructor using strlen() cstr cstr_from_n(const char* str, size_t n); // constructor with n first bytes of str cstr cstr_from_sv(csview sv); // construct cstr from csview @@ -160,10 +160,10 @@ char* cstrnstrn(const char* str, const char* search, size_t slen, size_t #include <stc/cstr.h> int main() { - cstr s0 = cstr_new("Initialization without using strlen()."); + cstr s0 = cstr_lit("Initialization without using strlen()."); printf("%s\nLength: %" c_ZU "\n\n", cstr_str(&s0), cstr_size(&s0)); - cstr s1 = cstr_new("one-nine-three-seven-five."); + cstr s1 = cstr_lit("one-nine-three-seven-five."); printf("%s\n", cstr_str(&s1)); cstr_insert(&s1, 3, "-two"); diff --git a/docs/csview_api.md b/docs/csview_api.md index 8d288783..4851152a 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -27,13 +27,14 @@ All csview definitions and prototypes are available by including a single header ```c csview c_SV(const char literal_only[]); // construct from literal, no strlen() -csview c_SV(const char* str, size_t n); // shorthand for csview_from_n() +csview c_SV(const char* str, size_t n); // construct from str and length n +csview csview_lit(const char literal_only[]); // alias for c_SV(lit) csview csview_from(const char* str); // construct from const char* -csview csview_from_n(const char* str); // construct from const char* and len -void csview_clear(csview* self); +csview csview_from_n(const char* str, size_t n); // alias for c_SV(str, n) size_t csview_size(csview sv); bool csview_empty(csview sv); +void csview_clear(csview* self); bool csview_equals(csview sv, csview sv2); size_t csview_find(csview sv, const char* str); @@ -120,7 +121,7 @@ uint64_t csview_hash(const csview* x); int main () { - cstr str1 = cstr_new("We think in generalities, but we live in details."); + cstr str1 = cstr_lit("We think in generalities, but we live in details."); // (quoting Alfred N. Whitehead) csview sv1 = cstr_substr(&str1, 3, 5); // "think" @@ -129,7 +130,7 @@ int main () csview sv3 = cstr_slice(&str1, -8, -1); // get "details" printf("%.*s %.*s %.*s\n", c_ARGSV(sv1), c_ARGSV(sv2), c_ARGSV(sv3)); - cstr s1 = cstr_new("Apples are red"); + cstr s1 = cstr_lit("Apples are red"); cstr s2 = cstr_from_sv(cstr_substr(&s1, -3, 3)); // "red" cstr s3 = cstr_from_sv(cstr_substr(&s1, 0, 6)); // "Apples" printf("%s %s\n", cstr_str(&s2), cstr_str(&s3)); @@ -151,7 +152,7 @@ red Apples int main() { c_auto (cstr, s1) { - s1 = cstr_new("hell😀 w😀rld"); + s1 = cstr_lit("hell😀 w😀rld"); cstr_u8_replace(&s1, cstr_find(&s1, "😀rld"), 1, c_SV("ø")); printf("%s\n", cstr_str(&s1)); diff --git a/docs/cvec_api.md b/docs/cvec_api.md index d19607c6..9b2614af 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -214,9 +214,9 @@ User User_clone(User user) { int main(void) { UVec vec = UVec_init(); - UVec_push(&vec, (User){cstr_new("mary"), 0}); - UVec_push(&vec, (User){cstr_new("joe"), 1}); - UVec_push(&vec, (User){cstr_new("admin"), 2}); + UVec_push(&vec, (User){cstr_lit("mary"), 0}); + UVec_push(&vec, (User){cstr_lit("joe"), 1}); + UVec_push(&vec, (User){cstr_lit("admin"), 2}); UVec vec2 = UVec_clone(vec); diff --git a/examples/astar.c b/examples/astar.c index 4d9f2469..d4a821f9 100644 --- a/examples/astar.c +++ b/examples/astar.c @@ -131,7 +131,7 @@ astar(cstr* maze, int width) int main(void) { - c_with (cstr maze = cstr_new( + c_with (cstr maze = cstr_lit( "#########################################################################\n" "# # # # # # #\n" "# # ######### # ##### ######### ##### ##### ##### # ! #\n" diff --git a/examples/books.c b/examples/books.c index bfced28f..b6067d81 100644 --- a/examples/books.c +++ b/examples/books.c @@ -24,8 +24,8 @@ int main() "Very enjoyable" ); cmap_str_insert(&book_reviews, - cstr_new("The Adventures of Sherlock Holmes"), - cstr_new("Eye lyked it alot.") + cstr_lit("The Adventures of Sherlock Holmes"), + cstr_lit("Eye lyked it alot.") ); // Check for a specific one. diff --git a/examples/city.c b/examples/city.c index 0996fe9b..8557c6cf 100644 --- a/examples/city.c +++ b/examples/city.c @@ -54,10 +54,10 @@ int main(void) c_auto (CityMap, map) { c_forlist (i, City, { - {cstr_new("New York"), cstr_new("US"), 4.3f, 23.2f, 9000000}, - {cstr_new("Paris"), cstr_new("France"), 4.3f, 23.2f, 9000000}, - {cstr_new("Berlin"), cstr_new("Germany"), 4.3f, 23.2f, 9000000}, - {cstr_new("London"), cstr_new("UK"), 4.3f, 23.2f, 9000000}, + {cstr_lit("New York"), cstr_lit("US"), 4.3f, 23.2f, 9000000}, + {cstr_lit("Paris"), cstr_lit("France"), 4.3f, 23.2f, 9000000}, + {cstr_lit("Berlin"), cstr_lit("Germany"), 4.3f, 23.2f, 9000000}, + {cstr_lit("London"), cstr_lit("UK"), 4.3f, 23.2f, 9000000}, }) Cities_emplace(&cities, *i.ref); // NB. creates smart pointers! Cities_sort(&cities); diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index 97f45779..48d8ceef 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -20,11 +20,11 @@ int main() c_auto (mymap, m1) { // Fill in some data to test with, one at a time - mymap_insert(&m1, 1, cstr_new("A")); - mymap_insert(&m1, 2, cstr_new("B")); - mymap_insert(&m1, 3, cstr_new("C")); - mymap_insert(&m1, 4, cstr_new("D")); - mymap_insert(&m1, 5, cstr_new("E")); + mymap_insert(&m1, 1, cstr_lit("A")); + mymap_insert(&m1, 2, cstr_lit("B")); + mymap_insert(&m1, 3, cstr_lit("C")); + mymap_insert(&m1, 4, cstr_lit("D")); + mymap_insert(&m1, 5, cstr_lit("E")); puts("Starting data of map m1 is:"); printmap(m1); diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c index 5b18d691..8f777fc6 100644 --- a/examples/csmap_insert.c +++ b/examples/csmap_insert.c @@ -85,7 +85,7 @@ int main() // The templatized versions move-constructing elements c_auto (csmap_istr, m3) { - csmap_istr_value ip1 = {475, cstr_new("blue")}, ip2 = {510, cstr_new("green")}; + csmap_istr_value ip1 = {475, cstr_lit("blue")}, ip2 = {510, cstr_lit("green")}; // single element csmap_istr_insert(&m3, ip1.first, cstr_move(&ip1.second)); diff --git a/examples/cstr_match.c b/examples/cstr_match.c index 5ba09ed9..286ba505 100644 --- a/examples/cstr_match.c +++ b/examples/cstr_match.c @@ -4,7 +4,7 @@ int main() { - c_with (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { + c_with (cstr ss = cstr_lit("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { size_t pos = cstr_find_at(&ss, 0, "brown"); printf("%" c_ZU " [%s]\n", pos, pos == c_NPOS ? "<NULL>" : cstr_str(&ss) + pos); printf("equals: %d\n", cstr_equals(&ss, "The quick brown fox jumps over the lazy dog.JPG")); @@ -13,7 +13,7 @@ int main() 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🐨"); + cstr s1 = cstr_lit("hell😀 w😀rl🐨"); csview ch1 = cstr_u8_chr(&s1, 7); csview ch2 = cstr_u8_chr(&s1, 10); printf("%s\nsize: %" c_ZU ", %" c_ZU "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1)); diff --git a/examples/demos.c b/examples/demos.c index 2795b478..fc3771cb 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -3,7 +3,7 @@ void stringdemo1() { printf("\nSTRINGDEMO1\n"); - c_with (cstr cs = cstr_new("one-nine-three-seven-five"), cstr_drop(&cs)) + c_with (cstr cs = cstr_lit("one-nine-three-seven-five"), cstr_drop(&cs)) { printf("%s.\n", cstr_str(&cs)); diff --git a/examples/inits.c b/examples/inits.c index 324168cf..021a3e0a 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -56,7 +56,7 @@ int main(void) int year = 2020; c_auto (cmap_id, idnames) { cmap_id_emplace(&idnames, 100, "Hello"); - cmap_id_insert(&idnames, 110, cstr_new("World")); + cmap_id_insert(&idnames, 110, cstr_lit("World")); cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year)); c_foreach (i, cmap_id, idnames) diff --git a/examples/sso_substr.c b/examples/sso_substr.c index ea10c411..be372a8d 100644 --- a/examples/sso_substr.c +++ b/examples/sso_substr.c @@ -4,7 +4,7 @@ int main () { - cstr str = cstr_new("We think in generalities, but we live in details."); + cstr str = cstr_lit("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" csview sv2 = cstr_substr_ex(&str, pos, 4); // "live" diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c index 35e6b8ef..22a5c990 100644 --- a/examples/utf8replace_c.c +++ b/examples/utf8replace_c.c @@ -4,7 +4,7 @@ int main() { c_auto (cstr, hello, upper) { - hello = cstr_new("hell😀 w😀rld"); + hello = cstr_lit("hell😀 w😀rld"); printf("%s\n", cstr_str(&hello)); /* replace second smiley at utf8 codepoint pos 7 */ diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index 37eaa318..ecb9c9fb 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -68,7 +68,7 @@ STC_INLINE const char* cstr_str(const cstr* self) { return self->str; } #define cstr_toraw(self) (self)->str STC_INLINE csview cstr_sv(const cstr* self) { return c_INIT(csview){self->str, _cstr_p(self)->size}; } -#define cstr_new(literal) \ +#define cstr_lit(literal) \ cstr_from_n(literal, c_strlen_lit(literal)) STC_INLINE cstr cstr_from(const char* str) { return cstr_from_n(str, strlen(str)); } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index a0dd1adc..6f774135 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -70,9 +70,11 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2); /**************************** PUBLIC API **********************************/ -#define cstr_new(literal) cstr_from_n(literal, c_strlen_lit(literal)) +#define cstr_lit(literal) cstr_from_n(literal, c_strlen_lit(literal)) #define cstr_NULL (c_INIT(cstr){{{0}, 0}}) #define cstr_toraw(self) cstr_str(self) +#define cstr_new(lit) cstr_lit(lit) /* [deprecated] */ +#define cstr_null cstr_NULL /* [deprecated] */ STC_API char* cstr_reserve(cstr* self, size_t cap); STC_API void cstr_shrink_to_fit(cstr* self); |
