Skip to content

Commit

Permalink
string changes
Browse files Browse the repository at this point in the history
  • Loading branch information
uecker committed Jun 1, 2024
1 parent 922d3cc commit 89da896
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ https://godbolt.org/z/EzejPPM35
Example 3 (strings)
-------------------

https://godbolt.org/z/ooWj88dsY
https://godbolt.org/z/zeYv54dTr

string *a = string_init("abc");
string *b = string_printf("%d", 3);
string *c = string_concat(string_view(a), string_view(b));
string *c = string_concat(a, b);

printf("%s\n", string_cstr(c));

Expand Down
30 changes: 24 additions & 6 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdarg.h>

#include "array.h"
#include "string.h"


Expand All @@ -33,16 +34,21 @@ string* string_init(const char* c)
}


string* string_dup(const string_view x)
string* strview_dup(const strview x)
{
return string_init0(string_length(&x), string_cstr(&x));
}

void string_append(string** a, const string_view b)
string* string_dup(const string *x)
{
return string_init0(string_length(x), string_cstr(x));
}

void string_append_view(string **a, const strview b)
{
ssize_t blen = string_length(&b);

string_priv* x = STRING_UNWRAP(*a);
string_priv *x = STRING_UNWRAP(*a);
ssize_t alen = string_length(x);
vec_realloc(char, &x, alen + blen + 1);

Expand All @@ -55,12 +61,17 @@ void string_append(string** a, const string_view b)
err:
}

string* string_concat(const string_view a, const string_view b)
void string_append(string **a, const string *b)
{
string_append_view(a, string_view((string*)b));
}

string* strview_concat(const strview a, const strview b)
{
ssize_t alen = string_length(&a);
ssize_t blen = string_length(&b);

string_priv* x = vec_alloc_n(char, alen + blen + 1);
string_priv *x = vec_alloc_n(char, alen + blen + 1);

if (NULL == x)
goto err;
Expand All @@ -73,6 +84,13 @@ string* string_concat(const string_view a, const string_view b)
return (string*)x;
}


string* string_concat(const string *a, const string *b)
{
return strview_concat(string_view((string*)a), string_view((string*)b));
}


string* string_printf(const char* fmt, ...)
{
va_list ap;
Expand All @@ -83,7 +101,7 @@ string* string_printf(const char* fmt, ...)

va_end(ap);

string_priv* s = NULL;
string_priv *s = NULL;

if (len < 0)
goto err;
Expand Down
21 changes: 12 additions & 9 deletions src/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct string string;
typedef vec(char) string;
typedef string string_priv;
#endif
typedef view(char) string_view;
typedef view(char) strview;

#define string_check(x) \
({ \
Expand All @@ -38,8 +38,8 @@ typedef view(char) string_view;
const string_priv*: __y, \
string*: (string_priv*)__y, \
const string*: (const string_priv*)__y, \
string_view*: __y, \
const string_view*: __y); \
strview*: __y, \
const strview*: __y); \
})
#else
#define STRING_UNWRAP(x) (x)
Expand All @@ -50,9 +50,9 @@ typedef view(char) string_view;
#define string_length(x) (vec_length(STRING_UNWRAP(x)) - 1)


inline string* string_alloc(void)
inline string *string_alloc(void)
{
string_priv* s = vec_alloc(char);
string_priv *s = vec_alloc(char);

if (NULL == s)
goto err;
Expand All @@ -67,11 +67,14 @@ extern string* string_init(const char* c);

#define STRING(x) (string_init(x))

extern string* string_dup(const string_view x);
extern string* string_concat(const string_view a, const string_view b);
extern void string_append(string** a, const string_view b);
extern string* string_printf(const char* fmt, ...);
extern string *string_dup(const string *s);
extern string *string_concat(const string *a, const string *b);
extern void string_append(string **a, const string *b);
extern string *string_printf(const char* fmt, ...);

extern void string_append_view(string **a, const strview b);
extern string *strview_dup(const strview v);
extern string *strview_concat(const strview a, const strview b);

#endif // __STRING_H

4 changes: 2 additions & 2 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ int main()
string* ss = NULL_CHECK(string_alloc());

for (int i = 0; i < (int)vec_length(s); i++)
string_append(&ss, string_view(vec_access(string_ptr, s, vec_length(s) - 1 - i)));
string_append_view(&ss, string_view(vec_access(string_ptr, s, vec_length(s) - 1 - i)));

const string* s2 = ss;
const char* x = string_cstr(s2);
Expand Down Expand Up @@ -155,7 +155,7 @@ int main()

printf("%s\n", string_cstr(s3));

string_view v3 = string_view(s3);
strview v3 = string_view(s3);

printf("%s\n", string_cstr(&v3));

Expand Down

0 comments on commit 89da896

Please sign in to comment.