From 54841f6294380c023f586598b672816f36a1adc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 2 Oct 2016 08:58:19 +0200 Subject: [PATCH] api: fix return of function string_match() when there are multiple masks in the string (issue #812) Some tests are added as well to test the multiple masks in the string. --- ChangeLog.adoc | 1 + src/core/wee-string.c | 16 ++++++++++++++-- tests/unit/core/test-string.cpp | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 267f645b4..8b740cfa3 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -36,6 +36,7 @@ Improvements:: Bug fixes:: * core, irc, xfer: refresh domain name and name server addresses before connection to servers (fix connection to servers after suspend mode) (issue #771) + * api: fix return of function string_match() when there are multiple masks in the string (issue #812) * api: fix crash in function network_connect_to() if address is NULL * api: fix connection to servers with hook_connect() on Windows 10 with Windows subsystem for Linux (issue #770) * api: fix crash in function string_split_command() when the separator is not a semicolon (issue #731) diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 1481e6361..f63d6f6f5 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -378,7 +378,7 @@ string_strcasestr (const char *string, const char *search) int string_match (const char *string, const char *mask, int case_sensitive) { - const char *ptr_string, *ptr_mask, *pos_word, *pos_end; + const char *ptr_string, *ptr_mask, *pos_word, *pos_word2, *pos_end; char *word; int wildcard, length_word; @@ -429,7 +429,10 @@ string_match (const char *string, const char *mask, int case_sensitive) /* check if the word is matching */ if (wildcard) { - /* search the word anywhere in the string (from current position) */ + /* + * search the word anywhere in the string (from current position), + * multiple times if needed + */ pos_word = (case_sensitive) ? strstr (ptr_string, word) : string_strcasestr (ptr_string, word); if (!pos_word) @@ -437,6 +440,15 @@ string_match (const char *string, const char *mask, int case_sensitive) free (word); return 0; } + while (1) + { + pos_word2 = (case_sensitive) ? + strstr (pos_word + length_word, word) : + string_strcasestr (pos_word + length_word, word); + if (!pos_word2) + break; + pos_word = pos_word2; + } ptr_string = pos_word + length_word; } else diff --git a/tests/unit/core/test-string.cpp b/tests/unit/core/test-string.cpp index fb1d06ddb..34c31793d 100644 --- a/tests/unit/core/test-string.cpp +++ b/tests/unit/core/test-string.cpp @@ -322,6 +322,24 @@ TEST(String, Match) LONGS_EQUAL(1, string_match ("test", "*es*", 1)); LONGS_EQUAL(1, string_match ("test", "*ES*", 0)); LONGS_EQUAL(0, string_match ("test", "*ES*", 1)); + LONGS_EQUAL(1, string_match ("TEST", "*es*", 0)); + LONGS_EQUAL(0, string_match ("TEST", "*es*", 1)); + LONGS_EQUAL(0, string_match ("aaba", "*aa", 0)); + LONGS_EQUAL(0, string_match ("aaba", "*aa", 1)); + LONGS_EQUAL(1, string_match ("abaa", "*aa", 0)); + LONGS_EQUAL(1, string_match ("abaa", "*aa", 1)); + LONGS_EQUAL(1, string_match ("aabaa", "*aa", 0)); + LONGS_EQUAL(1, string_match ("aabaa", "*aa", 1)); + LONGS_EQUAL(1, string_match ("aabaabaabaa", "*aa", 0)); + LONGS_EQUAL(1, string_match ("aabaabaabaa", "*aa", 1)); + LONGS_EQUAL(0, string_match ("abaa", "aa*", 0)); + LONGS_EQUAL(0, string_match ("abaa", "aa*", 1)); + LONGS_EQUAL(1, string_match ("aaba", "aa*", 0)); + LONGS_EQUAL(1, string_match ("aaba", "aa*", 1)); + LONGS_EQUAL(1, string_match ("aabaa", "aa*", 0)); + LONGS_EQUAL(1, string_match ("aabaa", "aa*", 1)); + LONGS_EQUAL(1, string_match ("aabaabaabaa", "aa*", 0)); + LONGS_EQUAL(1, string_match ("aabaabaabaa", "aa*", 1)); } /*