Refacto function rz_str_word_count (librz/util/str.c)
This commit is contained in:
parent
df62070922
commit
c1ce1bae6c
1 changed files with 24 additions and 17 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue