Skip to content

Commit

Permalink
Updated docs to use cstr_str(&s) instead of s.str
Browse files Browse the repository at this point in the history
  • Loading branch information
tylov committed Apr 24, 2022
1 parent 81b541b commit 1ebbd36
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 42 deletions.
10 changes: 5 additions & 5 deletions benchmarks/misc/string_bench_STC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void initShortStringVec(cvec_str* vs, cvec_sv* vsv)
cvec_str_clear(vs);
cvec_sv_clear(vsv);

*vs = read_file("names.txt");
cvec_str_copy(vs, read_file("names.txt"));
/*
cvec_str_emplace_back(vs, "Susan");
cvec_str_emplace_back(vs, "Jason");
Expand Down Expand Up @@ -110,11 +110,11 @@ void initLongStringVec(cvec_str* vs, cvec_sv* vsv)
cvec_str_clear(vs);
cvec_sv_clear(vsv);

*vs = read_file("names.txt");
cvec_str_copy(vs, read_file("names.txt"));
c_foreach (i, cvec_str, *vs) {
cstr_append_s(i.ref, *i.ref);
cstr_append_s(i.ref, *i.ref);
cstr_append_s(i.ref, *i.ref);
cstr_append_s(i.ref, *i.ref);
cstr_append_s(i.ref, *i.ref);
cstr_append_s(i.ref, *i.ref);
}
/*
cvec_str_emplace_back(vs, "Susan Susan Susan Susan Susan Susan");
Expand Down
4 changes: 2 additions & 2 deletions docs/carc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ int main()
puts("STACKS");
c_foreach (i, Stack, stack) {
c_forpair (name, year, Map, *i.ref->get)
printf(" %s:%d", _.name.str, _.year);
printf(" %s:%d", cstr_str(&_.name), _.year);
puts("");
}

puts("LIST");
c_foreach (i, List, list) {
c_forpair (name, year, Map, *i.ref->get)
printf(" %s:%d", _.name.str, _.year);
printf(" %s:%d", cstr_str(&_.name), _.year);
puts("");
}
}
Expand Down
12 changes: 6 additions & 6 deletions docs/ccommon_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ c_autovar (FILE* f = fopen(fname, "rb"), fclose(f))
c_autovar (cstr s = cstr_new("Hello"), cstr_drop(&s))
{
cstr_append(&s, " world");
printf("%s\n", s.str);
printf("%s\n", cstr_str(&s));
}

c_auto (cstr, s1, s2)
Expand All @@ -42,19 +42,19 @@ c_auto (cstr, s1, s2)
cstr_append(&s2, "Cool");
cstr_append(&s2, " stuff");

printf("%s %s\n", s1.str, s2.str);
printf("%s %s\n", cstr_str(&s1), cstr_str(&s2));
}

MyData data;
c_autoscope (mydata_init(&data), mydata_destroy(&data))
{
printf("%s\n", mydata.name.str);
printf("%s\n", cstr_str(&mydata.name));
}

cstr s1 = cstr_new("Hello"), s2 = cstr_new("world");
c_autodefer (cstr_drop(&s1), cstr_drop(&s2))
{
printf("%s %s\n", s1.str, s2.str);
printf("%s %s\n", cstr_str(&s1), cstr_str(&s2));
}
```
**Example**: Load each line of a text file into a vector of strings:
Expand All @@ -73,15 +73,15 @@ cvec_str readFile(const char* name)
c_autovar (FILE* fp = fopen(name, "r"), fclose(fp))
c_autovar (cstr line = cstr_null, cstr_drop(&line))
while (cstr_getline(&line, fp))
cvec_str_emplace_back(&vec, line.str);
cvec_str_emplace_back(&vec, cstr_str(&line));
return vec;
}
int main()
{
c_autovar (cvec_str x = readFile(__FILE__), cvec_str_drop(&x))
c_foreach (i, cvec_str, x)
printf("%s\n", i.ref->str);
printf("%s\n", cstr_str(i.ref));
}
```
### The checkauto utility program (for RAII)
Expand Down
12 changes: 6 additions & 6 deletions docs/cmap_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int main()

// Iterate and print keys and values of unordered map
c_foreach (n, cmap_str, u) {
printf("Key:[%s] Value:[%s]\n", n.ref->first.str, n.ref->second.str);
printf("Key:[%s] Value:[%s]\n", cstr_str(&n.ref->first), cstr_str(&n.ref->second));
}

// Add two new entries to the unordered map
Expand Down Expand Up @@ -182,7 +182,7 @@ int main()
cmap_id_emplace(&idnames, 100, "Green");

c_foreach (i, cmap_id, idnames)
printf("%d: %s\n", i.ref->first, i.ref->second.str);
printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second));
}
}
```
Expand Down Expand Up @@ -279,7 +279,7 @@ static inline int Viking_cmp(const Viking* a, const Viking* b) {
}

static inline uint32_t Viking_hash(const Viking* a, int ignored) {
return c_strhash(a->name.str) ^ (c_strhash(a->country.str) >> 15);
return c_strhash(cstr_str(&a->name)) ^ (c_strhash(cstr_str(&a->country)) >> 15);
}

static inline Viking Viking_clone(Viking v) {
Expand Down Expand Up @@ -319,7 +319,7 @@ int main()

// Print the status of the vikings.
c_forpair (viking, hp, Vikings, vikings) {
printf("%s of %s has %d hp\n", _.viking.name.str, _.viking.country.str, _.hp);
printf("%s of %s has %d hp\n", cstr_str(&_.viking.name), cstr_str(&_.viking.country), _.hp);
}
}
}
Expand Down Expand Up @@ -369,7 +369,7 @@ static inline Viking Viking_from(RViking raw) {
return (Viking){cstr_from(raw.name), cstr_from(raw.country)};
}
static inline RViking Viking_toraw(const Viking* vk) {
return (RViking){vk->name.str, vk->country.str};
return (RViking){cstr_str(&vk->name), cstr_str(&vk->country)};
}
// With this in place, we define the Viking => int hash map type:
Expand Down Expand Up @@ -399,7 +399,7 @@ int main()
if (v) v->second += 3; // add 3 hp points to Einar
c_forpair (viking, health, Vikings, vikings) {
printf("%s of %s has %d hp\n", _.viking.name.str, _.viking.country.str, _.health);
printf("%s of %s has %d hp\n", cstr_str(&_.viking.name), cstr_str(&_.viking.country), _.health);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/crandom_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int main()
size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / N);
if (n > 0) {
cstr_resize(&bar, n, '*');
printf("%4d %s\n", i.ref->first, bar.str);
printf("%4d %s\n", i.ref->first, cstr_str(&bar));
}
}
// Cleanup
Expand Down
6 changes: 3 additions & 3 deletions docs/cset_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ int main ()

fifth = cset_str_clone(second);
c_foreach (i, cset_str, third)
cset_str_emplace(&fifth, i.ref->str);
cset_str_emplace(&fifth, cstr_str(i.ref));
c_foreach (i, cset_str, fourth)
cset_str_emplace(&fifth, i.ref->str);
cset_str_emplace(&fifth, cstr_str(i.ref));
}
printf("fifth contains:\n\n");
c_foreach (i, cset_str, fifth)
printf("%s\n", i.ref->str);
printf("%s\n", cstr_str(i.ref));
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/csmap_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int main()
// Iterate and print keys and values of sorted map
c_foreach (i, csmap_str, colors) {
printf("Key:[%s] Value:[%s]\n", i.ref->first.str, i.ref->second.str);
printf("Key:[%s] Value:[%s]\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second));
}
// Add two new entries to the sorted map
Expand Down Expand Up @@ -163,7 +163,7 @@ int main()
csmap_id_emplace(&idnames, 100, "Green");

c_foreach (i, csmap_id, idnames)
printf("%d: %s\n", i.ref->first, i.ref->second.str);
printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second));
}
}
```
Expand Down
6 changes: 3 additions & 3 deletions docs/csset_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ c_auto (csset_str, fifth)

fifth = csset_str_clone(second);
c_foreach (i, csset_str, third)
csset_str_emplace(&fifth, i.ref->str);
csset_str_emplace(&fifth, cstr_str(i.ref));
c_foreach (i, csset_str, fourth)
csset_str_emplace(&fifth, i.ref->str);
csset_str_emplace(&fifth, cstr_str(i.ref));
}
printf("fifth contains:\n\n");
c_foreach (i, csset_str, fifth)
printf("%s\n", i.ref->str);
printf("%s\n", cstr_str(i.ref));
}
}
```
Expand Down
18 changes: 9 additions & 9 deletions docs/cstr_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ cstr* cstr_take(cstr* self, cstr s); // take th
cstr cstr_move(cstr* self); // move string to caller, leave empty string
void cstr_drop(cstr *self); // destructor

const char* cstr_str(const cstr* self); // self->str
char* cstr_data(cstr* self); // self->str
const char* cstr_str(const cstr* self); // access to const char*
char* cstr_data(cstr* self); // access to char*
size_t cstr_size(cstr s);
size_t cstr_length(cstr s);
size_t cstr_capacity(cstr s);
Expand Down Expand Up @@ -110,27 +110,27 @@ int c_strncasecmp(const char* str1, const char* str2, size_t n);

int main() {
cstr s0 = cstr_new("Initialization without using strlen().");
printf("%s\nLength: %" PRIuMAX "\n\n", s0.str, 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", s1.str);
printf("%s\n", cstr_str(&s1));

cstr_insert(&s1, 3, "-two");
printf("%s\n", s1.str);
printf("%s\n", cstr_str(&s1));

cstr_erase_n(&s1, 7, 5); // -nine
printf("%s\n", s1.str);
printf("%s\n", cstr_str(&s1));

cstr_replace(&s1, cstr_find(s1, "seven"), 5, "four");
printf("%s\n", s1.str);
printf("%s\n", cstr_str(&s1));

// reassign:
cstr_assign(&s1, "one two three four five six seven");
cstr_append(&s1, " eight");
printf("append: %s\n", s1.str);
printf("append: %s\n", cstr_str(&s1));

cstr full_path = cstr_from_fmt("%s/%s.%s", "directory", "filename", "ext");
printf("%s\n", full_path.str);
printf("%s\n", cstr_str(&full_path));

c_drop(cstr, &s0, &s1, &full_path);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/csview_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ int main()

c_autovar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_drop(&v))
c_foreach (i, cvec_str, v)
printf("[%s]\n", i.ref->str);
printf("[%s]\n", cstr_str(i.ref));
}
```
Output:
Expand Down
8 changes: 4 additions & 4 deletions docs/cvec_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ int main() {
// emplace() will not compile if adding a new cstr type. Use push_back():
cvec_str_push(&names, tmp); // tmp is moved to names, do not drop() it.

printf("%s\n", names.data[1].str); // Access the second element
printf("%s\n", cstr_str(&names.data[1])); // Access the second element

c_foreach (i, cvec_str, names)
printf("item: %s\n", i.ref->str);
printf("item: %s\n", cstr_str(i.ref));
cvec_str_drop(&names);
}
```
Expand All @@ -187,7 +187,7 @@ typedef struct {
} User;

int User_cmp(const User* a, const User* b) {
int c = strcmp(a->name.str, b->name.str);
int c = strcmp(cstr_str(&a->name), cstr_str(&b->name));
return c != 0 ? c : a->id - b->id;
}
void User_drop(User* self) {
Expand All @@ -214,7 +214,7 @@ int main(void) {

cvec_u vec2 = cvec_u_clone(vec);
c_foreach (i, cvec_u, vec2)
printf("%s: %d\n", i.ref->name.str, i.ref->id);
printf("%s: %d\n", cstr_str(&i.ref->name), i.ref->id);

c_drop(cvec_u, &vec, &vec2); // cleanup
}
Expand Down

0 comments on commit 1ebbd36

Please sign in to comment.