Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/BLI_string.h7
-rw-r--r--source/blender/blenlib/intern/string.c33
-rw-r--r--source/blender/blenlib/tests/BLI_string_test.cc10
3 files changed, 50 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index d1fab065959..4968b4ee159 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -133,6 +133,13 @@ size_t BLI_str_partition_ex(const char *str,
const char **suf,
const bool from_right) ATTR_NONNULL(1, 3, 4, 5);
+int BLI_string_max_possible_word_count(const int str_len);
+bool BLI_string_has_word_prefix(const char *haystack, const char *needle, size_t needle_len);
+bool BLI_string_all_words_matched(const char *name,
+ const char *str,
+ int (*words)[2],
+ const int words_len);
+
int BLI_string_find_split_words(const char *str,
const size_t len,
const char delim,
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index 755637ac274..02d12d2df9b 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -524,6 +524,39 @@ char *BLI_strcasestr(const char *s, const char *find)
return ((char *)s);
}
+int BLI_string_max_possible_word_count(const int str_len)
+{
+ return (str_len / 2) + 1;
+}
+
+bool BLI_string_has_word_prefix(const char *haystack, const char *needle, size_t needle_len)
+{
+ const char *match = BLI_strncasestr(haystack, needle, needle_len);
+ if (match) {
+ if ((match == haystack) || (*(match - 1) == ' ') || ispunct(*(match - 1))) {
+ return true;
+ }
+ return BLI_string_has_word_prefix(match + 1, needle, needle_len);
+ }
+ return false;
+}
+
+bool BLI_string_all_words_matched(const char *name,
+ const char *str,
+ int (*words)[2],
+ const int words_len)
+{
+ int index;
+ for (index = 0; index < words_len; index++) {
+ if (!BLI_string_has_word_prefix(name, str + words[index][0], (size_t)words[index][1])) {
+ break;
+ }
+ }
+ const bool all_words_matched = (index == words_len);
+
+ return all_words_matched;
+}
+
/**
* Variation of #BLI_strcasestr with string length limited to \a len
*/
diff --git a/source/blender/blenlib/tests/BLI_string_test.cc b/source/blender/blenlib/tests/BLI_string_test.cc
index 1760b7966e3..a5fd3e31c31 100644
--- a/source/blender/blenlib/tests/BLI_string_test.cc
+++ b/source/blender/blenlib/tests/BLI_string_test.cc
@@ -570,6 +570,16 @@ TEST(string, StringStrncasestr)
EXPECT_EQ(res, (void *)NULL);
}
+/* BLI_string_max_possible_word_count */
+TEST(string, StringMaxPossibleWordCount)
+{
+ EXPECT_EQ(BLI_string_max_possible_word_count(0), 1);
+ EXPECT_EQ(BLI_string_max_possible_word_count(1), 1);
+ EXPECT_EQ(BLI_string_max_possible_word_count(2), 2);
+ EXPECT_EQ(BLI_string_max_possible_word_count(3), 2);
+ EXPECT_EQ(BLI_string_max_possible_word_count(10), 6);
+}
+
/* BLI_string_is_decimal */
TEST(string, StrIsDecimal)
{