Skip to content

Commit

Permalink
Internal symbols renamed to avoid potential naming conflicts with sta…
Browse files Browse the repository at this point in the history
…ndard headers.
  • Loading branch information
maxim2266 committed Oct 25, 2024
1 parent db8b3d1 commit 05fab47
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 54 deletions.
30 changes: 15 additions & 15 deletions str.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void str_free(const str s)
}

// version of str_free() for str_auto macro
void _str_free(const str* const ps)
void str_free_auto(const str* const ps)
{
if(ps)
str_free(*ps);
Expand Down Expand Up @@ -88,7 +88,7 @@ void str_swap(str* const s1, str* const s2)
}

// empty string
const char* const _empty_string = "";
const char* const str_empty_string = "";

// string comparison ---------------------------------------------------------------------
// compare two strings lexicographically
Expand Down Expand Up @@ -141,10 +141,10 @@ bool str_has_suffix(const str s, const str suffix)
// create a reference to the given range of chars
str str_ref_chars(const char* const s, const size_t n)
{
return (s && n > 0) ? ((str){ s, _ref_info(n) }) : str_null;
return (s && n > 0) ? ((str){ s, str_ref_info(n) }) : str_null;
}

str _str_ref_from_ptr(const char* const s)
str str_ref_from_ptr(const char* const s)
{
return s ? str_ref_chars(s, strlen(s)) : str_null;
}
Expand All @@ -161,7 +161,7 @@ str str_acquire_chars(const char* const s, const size_t n)
return str_null;
}

return (str){ s, _owner_info(n) };
return (str){ s, str_owner_info(n) };
}

// take ownership of the given C string
Expand All @@ -171,7 +171,7 @@ str str_acquire(const char* const s)
}

// allocate a copy of the given string
int _str_dup(str* const dest, const str s)
int str_dup_impl(str* const dest, const str s)
{
const size_t n = str_len(s);

Expand Down Expand Up @@ -311,7 +311,7 @@ size_t total_length(const str* src, size_t count)
}

// concatenate strings
int _str_cat_range(str* const dest, const str* src, size_t count)
int str_cat_range_impl(str* const dest, const str* src, size_t count)
{
if(!src)
{
Expand Down Expand Up @@ -344,7 +344,7 @@ int _str_cat_range(str* const dest, const str* src, size_t count)
}

// writing to file descriptor
int _str_cpy_to_fd(const int fd, const str s)
int str_cpy_to_fd(const int fd, const str s)
{
size_t n = str_len(s);
const void* p = str_ptr(s);
Expand All @@ -363,7 +363,7 @@ int _str_cpy_to_fd(const int fd, const str s)
}

// writing to byte stream
int _str_cpy_to_stream(FILE* const stream, const str s)
int str_cpy_to_stream(FILE* const stream, const str s)
{
const size_t n = str_len(s);

Expand Down Expand Up @@ -412,7 +412,7 @@ struct iovec* vec_append_nonempty(struct iovec* const pv, const str s)
return str_is_empty(s) ? pv : vec_append(pv, s);
}

int _str_cat_range_to_fd(const int fd, const str* src, size_t count)
int str_cat_range_to_fd(const int fd, const str* src, size_t count)
{
if(!src)
return 0;
Expand Down Expand Up @@ -440,7 +440,7 @@ int _str_cat_range_to_fd(const int fd, const str* src, size_t count)
return 0;
}

int _str_cat_range_to_stream(FILE* const stream, const str* src, size_t count)
int str_cat_range_to_stream(FILE* const stream, const str* src, size_t count)
{
if(!src)
return 0;
Expand All @@ -454,7 +454,7 @@ int _str_cat_range_to_stream(FILE* const stream, const str* src, size_t count)
}

// join strings
int _str_join_range(str* const dest, const str sep, const str* src, size_t count)
int str_join_range_impl(str* const dest, const str sep, const str* src, size_t count)
{
// test for simple cases
if(str_is_empty(sep))
Expand Down Expand Up @@ -487,7 +487,7 @@ int _str_join_range(str* const dest, const str sep, const str* src, size_t count
return 0;
}

int _str_join_range_to_fd(const int fd, const str sep, const str* src, size_t count)
int str_join_range_to_fd(const int fd, const str sep, const str* src, size_t count)
{
if(str_is_empty(sep))
return str_cat_range(fd, src, count);
Expand Down Expand Up @@ -523,7 +523,7 @@ int _str_join_range_to_fd(const int fd, const str sep, const str* src, size_t co
return 0;
}

int _str_join_range_to_stream(FILE* const stream, const str sep, const str* src, size_t count)
int str_join_range_to_stream(FILE* const stream, const str sep, const str* src, size_t count)
{
if(str_is_empty(sep))
return str_cat_range(stream, src, count);
Expand Down Expand Up @@ -656,7 +656,7 @@ size_t str_unique_range(str* const array, const size_t count)
// string iterator function
#ifdef __STDC_UTF_32__

char32_t _cp_iterator_next(_cp_iterator* const it)
char32_t str_cp_iterator_next(str_cp_iterator* const it)
{
if(it->curr >= it->end)
return CPI_END_OF_STRING;
Expand Down
78 changes: 39 additions & 39 deletions str.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ typedef struct
#define str_null ((str){ 0, 0 })

// helper macros
#define _ref_info(n) ((n) << 1)
#define _owner_info(n) (_ref_info(n) | 1)
#define str_ref_info(n) ((n) << 1)
#define str_owner_info(n) (str_ref_info(n) | 1)

// string properties ----------------------------------------------------------------------
// length of the string
Expand All @@ -62,9 +62,9 @@ size_t str_len(const str s) { return s.info >> 1; }
static inline
const char* str_ptr(const str s)
{
extern const char* const _empty_string;
extern const char* const str_empty_string;

return s.ptr ? s.ptr : _empty_string;
return s.ptr ? s.ptr : str_empty_string;
}

// end of the string
Expand All @@ -88,9 +88,9 @@ bool str_is_ref(const str s) { return !str_is_owner(s); }
void str_free(const str s);

// automatic cleanup
void _str_free(const str* const ps);
void str_free_auto(const str* const ps);

#define str_auto str __attribute__((cleanup(_str_free)))
#define str_auto str __attribute__((cleanup(str_free_auto)))

// string movements -----------------------------------------------------------------------
// free target string, then assign the new value to it
Expand Down Expand Up @@ -135,29 +135,29 @@ bool str_has_suffix(const str s, const str suffix);

// string composition ------------------------------------------------------------------
// implementation helpers
int _str_dup(str* const dest, const str s);
int _str_cpy_to_fd(const int fd, const str s);
int _str_cpy_to_stream(FILE* const stream, const str s);
int str_dup_impl(str* const dest, const str s);
int str_cpy_to_fd(const int fd, const str s);
int str_cpy_to_stream(FILE* const stream, const str s);

// copy string
#define str_cpy(dest, src) \
_Generic((dest), \
str*: _str_dup, \
int: _str_cpy_to_fd, \
FILE*: _str_cpy_to_stream \
str*: str_dup_impl, \
int: str_cpy_to_fd, \
FILE*: str_cpy_to_stream \
)((dest), (src))

// implementation helpers
int _str_cat_range(str* const dest, const str* src, size_t count);
int _str_cat_range_to_fd(const int fd, const str* src, size_t count);
int _str_cat_range_to_stream(FILE* const stream, const str* src, size_t count);
int str_cat_range_impl(str* const dest, const str* src, size_t count);
int str_cat_range_to_fd(const int fd, const str* src, size_t count);
int str_cat_range_to_stream(FILE* const stream, const str* src, size_t count);

// concatenate range of strings
#define str_cat_range(dest, src, count) \
_Generic((dest), \
str*: _str_cat_range, \
int: _str_cat_range_to_fd, \
FILE*: _str_cat_range_to_stream \
str*: str_cat_range_impl, \
int: str_cat_range_to_fd, \
FILE*: str_cat_range_to_stream \
)((dest), (src), (count))

// concatenate string arguments
Expand All @@ -168,16 +168,16 @@ int _str_cat_range_to_stream(FILE* const stream, const str* src, size_t count);
})

// implementation helpers
int _str_join_range(str* const dest, const str sep, const str* src, size_t count);
int _str_join_range_to_fd(const int fd, const str sep, const str* src, size_t count);
int _str_join_range_to_stream(FILE* const stream, const str sep, const str* src, size_t count);
int str_join_range_impl(str* const dest, const str sep, const str* src, size_t count);
int str_join_range_to_fd(const int fd, const str sep, const str* src, size_t count);
int str_join_range_to_stream(FILE* const stream, const str sep, const str* src, size_t count);

// join strings around the separator
#define str_join_range(dest, sep, src, count) \
_Generic((dest), \
str*: _str_join_range, \
int: _str_join_range_to_fd, \
FILE*: _str_join_range_to_stream \
str*: str_join_range_impl, \
int: str_join_range_to_fd, \
FILE*: str_join_range_to_stream \
)((dest), (sep), (src), (count))

// join string arguments around the separator
Expand All @@ -189,19 +189,19 @@ int _str_join_range_to_stream(FILE* const stream, const str sep, const str* src,

// constructors ----------------------------------------------------------------------------
// string reference from a string literal
#define str_lit(s) ((str){ "" s, _ref_info(sizeof(s) - 1) })
#define str_lit(s) ((str){ "" s, str_ref_info(sizeof(s) - 1) })

static inline
str _str_ref(const str s) { return (str){ s.ptr, s.info & ~(size_t)1 }; }
str str_ref_impl(const str s) { return (str){ s.ptr, s.info & ~(size_t)1 }; }

str _str_ref_from_ptr(const char* const s);
str str_ref_from_ptr(const char* const s);

// string reference from anything
#define str_ref(s) \
_Generic((s), \
str: _str_ref, \
char*: _str_ref_from_ptr, \
const char*: _str_ref_from_ptr \
str: str_ref_impl, \
char*: str_ref_from_ptr, \
const char*: str_ref_from_ptr \
)(s)

// create a reference to the given range of chars
Expand Down Expand Up @@ -246,34 +246,34 @@ size_t str_unique_range(str* const array, const size_t count);

// iterator
#define for_each_codepoint(var, src) \
_for_each_cp((var), (src), _CAT(__it_, __COUNTER__))
for_each_cp((var), (src), CAT1(inner_it_, __COUNTER__))

// iterator error codes
#define CPI_END_OF_STRING ((char32_t)-1)
#define CPI_ERR_INCOMPLETE_SEQ ((char32_t)-2)
#define CPI_ERR_INVALID_ENCODING ((char32_t)-3)

// implementation
#define _for_each_cp(var, src, it) \
for(_cp_iterator it = _make_cp_iterator(src); (var = _cp_iterator_next(&it)) <= 0x10FFFFu;)
#define for_each_cp(var, src, it) \
for(str_cp_iterator it = str_make_cp_iterator(src); (var = str_cp_iterator_next(&it)) <= 0x10FFFFu;)

#define _CAT(x, y) __CAT(x, y)
#define __CAT(x, y) x ## y
#define CAT1(x, y) CAT2(x, y)
#define CAT2(x, y) x ## y

typedef struct
{
const char* curr;
const char* const end;
mbstate_t state;
} _cp_iterator;
} str_cp_iterator;

static inline
_cp_iterator _make_cp_iterator(const str s)
str_cp_iterator str_make_cp_iterator(const str s)
{
return (_cp_iterator){ .curr = str_ptr(s), .end = str_end(s) };
return (str_cp_iterator){ .curr = str_ptr(s), .end = str_end(s) };
}

char32_t _cp_iterator_next(_cp_iterator* const it);
char32_t str_cp_iterator_next(str_cp_iterator* const it);

#endif // ifdef __STDC_UTF_32__

Expand Down

0 comments on commit 05fab47

Please sign in to comment.