diff options
author | Campbell Barton <ideasman42@gmail.com> | 2016-03-23 10:45:32 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2016-03-23 10:51:29 +0300 |
commit | 80a7efdc1d395f44a14e7bf1085a5d1b3fe45547 (patch) | |
tree | 7c7dc860c64ee1ad933350e67c80d9a36a970637 /source/blender/blenlib | |
parent | b0a7e77700535a0fb021505f1920322d633926d4 (diff) |
UI: multi word filtering in search menu
D1080 by @rockets, with own improvements to tests
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_string.h | 6 | ||||
-rw-r--r-- | source/blender/blenlib/intern/string.c | 66 |
2 files changed, 72 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 82780394ce7..d137806c575 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -74,6 +74,7 @@ size_t BLI_str_format_int_grouped(char dst[16], int num) ATTR_NONNULL(); int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); char *BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +char *BLI_strncasestr(const char *s, const char *find, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); int BLI_natstrcmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); @@ -97,6 +98,11 @@ size_t BLI_str_partition_ex( const char *str, const char *end, const char delim[], const char **sep, const char **suf, const bool from_right) ATTR_NONNULL(1, 3, 4, 5); +int BLI_string_find_split_words( + const char *str, const size_t len, + const char delim, int r_words[][2], int words_max) + ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index aad4649344f..2f67b0e57a3 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -525,6 +525,26 @@ char *BLI_strcasestr(const char *s, const char *find) return ((char *) s); } +/** + * Variation of #BLI_strcasestr with string length limited to \a len + */ +char *BLI_strncasestr(const char *s, const char *find, size_t len) +{ + register char c, sc; + + if ((c = *find++) != 0) { + c = tolower(c); + do { + do { + if ((sc = *s++) == 0) + return (NULL); + sc = tolower(sc); + } while (sc != c); + } while (BLI_strncasecmp(s, find, len - 1) != 0); + s--; + } + return ((char *)s); +} int BLI_strcasecmp(const char *s1, const char *s2) { @@ -962,3 +982,49 @@ size_t BLI_str_format_int_grouped(char dst[16], int num) return (size_t)(p_dst - dst); } + +/** + * Find the ranges needed to split \a str into its individual words. + * + * \param str: The string to search for words. + * \param len: Size of the string to search. + * \param delim: Character to use as a delimiter. + * \param r_words: Info about the words found. Set to [index, len] pairs. + * \param words_max: Max number of words to find + * \return The number of words found in \a str + */ +int BLI_string_find_split_words( + const char *str, const size_t len, + const char delim, int r_words[][2], int words_max) +{ + int n = 0, i; + bool charsearch = true; + + /* Skip leading spaces */ + for (i = 0; (i < len) && (str[i] != '\0'); i++) { + if (str[i] != delim) { + break; + } + } + + for (; (i < len) && (str[i] != '\0') && (n < words_max); i++) { + if ((str[i] != delim) && (charsearch == true)) { + r_words[n][0] = i; + charsearch = false; + } + else { + if ((str[i] == delim) && (charsearch == false)) { + r_words[n][1] = i - r_words[n][0]; + n++; + charsearch = true; + } + } + } + + if (charsearch == false) { + r_words[n][1] = i - r_words[n][0]; + n++; + } + + return n; +} |