summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-05 09:07:29 +0200
committerTyge Løvset <[email protected]>2022-09-05 09:07:29 +0200
commitfc559ef64840fe54c0136f064cbfb2d3c055b692 (patch)
tree3935236a6ff3f7cfc8db4e0b5cb96e3203197bbc
parent491a8cba3ee55a1200fc2de8ef3807dd8436f829 (diff)
downloadSTC-modified-fc559ef64840fe54c0136f064cbfb2d3c055b692.tar.gz
STC-modified-fc559ef64840fe54c0136f064cbfb2d3c055b692.zip
Added cstr_append_fmt().
-rw-r--r--docs/cstr_api.md1
-rw-r--r--examples/box2.c8
-rw-r--r--examples/new_sptr.c12
-rw-r--r--include/stc/cstr.h19
4 files changed, 25 insertions, 15 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index 273f4de4..3d9aae85 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -57,6 +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);
+int cstr_append_fmt(const char* fmt, ...); // printf() formatting
char* cstr_append_uninit(cstr* self, size_t len); // append len uninitialized bytes
void cstr_insert(cstr* self, size_t pos, const char* ins);
diff --git a/examples/box2.c b/examples/box2.c
index 75f9970f..28b4acff 100644
--- a/examples/box2.c
+++ b/examples/box2.c
@@ -35,7 +35,7 @@ Point origin(void) {
cbox_Point boxed_origin(void) {
// Allocate this point on the heap, and return a pointer to it
- return cbox_Point_make((Point){ .x=0.0, .y=0.0 });
+ return cbox_Point_from((Point){ .x=0.0, .y=0.0 });
}
@@ -53,16 +53,16 @@ int main(void) {
c_auto (cbox_BoxPoint, box_in_a_box)
{
// Heap allocated rectangle
- boxed_rectangle = cbox_Rectangle_make((Rectangle){
+ boxed_rectangle = cbox_Rectangle_from((Rectangle){
.top_left = origin(),
.bottom_right = (Point){ .x=3.0, .y=-4.0 }
});
// The output of functions can be boxed
- boxed_point = cbox_Point_make(origin());
+ boxed_point = cbox_Point_from(origin());
// Double indirection
- box_in_a_box = cbox_BoxPoint_make(boxed_origin());
+ box_in_a_box = cbox_BoxPoint_from(boxed_origin());
printf("Point occupies %" PRIuMAX " bytes on the stack\n",
sizeof(point));
diff --git a/examples/new_sptr.c b/examples/new_sptr.c
index 71222799..e3431d8a 100644
--- a/examples/new_sptr.c
+++ b/examples/new_sptr.c
@@ -2,7 +2,7 @@
struct Person { cstr name, last; } typedef Person;
-Person Person_new(const char* name, const char* last) {
+Person Person_from(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
Person Person_clone(Person p) {
@@ -32,18 +32,18 @@ int main(void) {
c_auto (carc_person, p, q, r, s)
{
puts("Ex1");
- p = carc_person_make(Person_new("John", "Smiths"));
+ p = carc_person_from(Person_from("John", "Smiths"));
q = carc_person_clone(p);
r = carc_person_clone(p);
- s = carc_person_make(Person_clone(*p.get)); // deep copy
+ s = carc_person_from(Person_clone(*p.get)); // deep copy
printf("%s %s. uses: %lu\n", cstr_str(&r.get->name), cstr_str(&s.get->last), *p.use_count);
}
c_auto (cstack_iptr, stk) {
puts("Ex2");
- cstack_iptr_push(&stk, SPtr_make(10));
- cstack_iptr_push(&stk, SPtr_make(20));
- cstack_iptr_push(&stk, SPtr_make(30));
+ cstack_iptr_push(&stk, SPtr_from(10));
+ cstack_iptr_push(&stk, SPtr_from(20));
+ cstack_iptr_push(&stk, SPtr_from(30));
cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_top(&stk)));
cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_begin(&stk).ref));
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index fe253c71..983c3d4b 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -87,6 +87,7 @@ STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp);
STC_API void cstr_erase(cstr* self, size_t pos, size_t len);
STC_API void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len);
STC_API cstr cstr_from_fmt(const char* fmt, ...);
+STC_API int cstr_append_fmt(cstr* self, const char* fmt, ...);
STC_API int cstr_printf(cstr* self, const char* fmt, ...);
STC_API void cstr_replace(cstr* self, const char* search, const char* repl, unsigned count);
STC_API cstr cstr_replace_sv(csview sv, csview search, csview repl, unsigned count);
@@ -563,13 +564,13 @@ STC_DEF void cstr_u8_erase(cstr* self, const size_t bytepos, const size_t u8len)
# pragma warning(disable: 4996)
#endif
-STC_DEF int cstr_vfmt(cstr* self, const char* fmt, va_list args) {
+STC_DEF int cstr_vfmt(cstr* self, size_t start, const char* fmt, va_list args) {
va_list args2;
va_copy(args2, args);
const int n = vsnprintf(NULL, (size_t)0, fmt, args);
- vsprintf(cstr_reserve(self, n), fmt, args2);
+ vsprintf(cstr_reserve(self, start + n) + start, fmt, args2);
va_end(args2);
- _cstr_set_size(self, n);
+ _cstr_set_size(self, start + n);
return n;
}
#if defined(__clang__)
@@ -582,16 +583,24 @@ STC_DEF cstr cstr_from_fmt(const char* fmt, ...) {
cstr s = cstr_null;
va_list args;
va_start(args, fmt);
- cstr_vfmt(&s, fmt, args);
+ cstr_vfmt(&s, 0, fmt, args);
va_end(args);
return s;
}
+STC_DEF int cstr_append_fmt(cstr* self, const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ const int n = cstr_vfmt(self, cstr_size(self), fmt, args);
+ va_end(args);
+ return n;
+}
+
/* NB! self-data in args is UB */
STC_DEF int cstr_printf(cstr* self, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
- const int n = cstr_vfmt(self, fmt, args);
+ const int n = cstr_vfmt(self, 0, fmt, args);
va_end(args);
return n;
}