Refacto function rz_str_word_count (librz/util/str.c)

This commit is contained in:
Alexis Ehret 2021-02-06 17:00:42 +01:00 committed by Riccardo Schirone
parent df62070922
commit c1ce1bae6c

View file

@ -541,23 +541,37 @@ RZ_API int rz_str_char_count(const char *string, char ch) {
return count;
}
static const char *separator_get_first(const char *text) {
for (; *text && !IS_SEPARATOR(*text); text++)
;
;
return text;
}
static const char *word_get_first(const char *text) {
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));
}
// 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) {
const char *text, *tmp;
int word;
const char *text = word_get_first(string);
for (text = string; *text && IS_SEPARATOR(*text); text++) {
;
}
for (word = 0; *text; word++) {
for (; *text && !IS_SEPARATOR(*text); text++) {
;
}
for (tmp = text; *text && IS_SEPARATOR(*text); text++) {
;
}
text = separator_get_first(text);
text = word_get_first(text);
}
return word;
}
@ -809,13 +823,6 @@ RZ_API int rz_str_ccpy(char *dst, char *src, int ch) {
return i;
}
RZ_API char *rz_str_word_get_first(const char *text) {
for (; *text && IS_SEPARATOR(*text); text++) {
;
}
return strdup(text);
}
RZ_API char *rz_str_ndup(const char *ptr, int len) {
if (len < 0) {
return NULL;