Rename separator_get_first and word_get_first ##refactor (#3559)

This commit is contained in:
Daniel 2023-06-11 13:38:42 +02:00 committed by GitHub
parent d0426f9edd
commit 70a0244e04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -155,7 +155,7 @@ static inline const char *rz_str_word_get_next0(const char *str) {
return str + strlen(str) + 1;
}
RZ_API const char *rz_str_word_get0(const char *str, int idx);
RZ_API char *rz_str_word_get_first(const char *string);
RZ_API RZ_OWN char *rz_str_skip_separator_chars(RZ_NONNULL const char *string);
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str);
RZ_API void rz_str_trim_char(RZ_NONNULL RZ_INOUT char *str, const char c);
RZ_API char *rz_str_trim_dup(const char *str);

View file

@ -618,35 +618,43 @@ RZ_API int rz_str_char_count(const char *string, char ch) {
return count;
}
static const char *separator_get_first(const char *text) {
static const char *skip_non_separator_chars(const char *text) {
rz_return_val_if_fail(text, NULL);
for (; *text && !IS_SEPARATOR(*text); text++)
;
;
return text;
}
static const char *word_get_first(const char *text) {
static const char *skip_separator_chars(const char *text) {
rz_return_val_if_fail(text, NULL);
for (; *text && IS_SEPARATOR(*text); text++)
;
;
return text;
}
RZ_API char *rz_str_word_get_first(const char *text) {
return strdup(word_get_first(text));
/**
* \brief Skips over separator characters and moves to the first non-separator character in the string.
* \param text The string to process.
* \return A pointer to the first non-separator character in the string.
*
* This function iterates through the given string and skips over any separator characters until it reaches the first non-separator character.
*/
RZ_API RZ_OWN char *rz_str_skip_separator_chars(RZ_NONNULL const char *text) {
rz_return_val_if_fail(text, NULL);
return strdup(skip_separator_chars(text));
}
// Counts the number of words (separated by separator characters: newlines, tabs,
// return, space). See rz_util.h for more details of the IS_SEPARATOR macro.
RZ_API int rz_str_word_count(const char *string) {
int word;
const char *text = word_get_first(string);
const char *text = skip_separator_chars(string);
for (word = 0; *text; word++) {
text = separator_get_first(text);
text = word_get_first(text);
text = skip_non_separator_chars(text);
text = skip_separator_chars(text);
}
return word;