summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/corestr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/corestr.cpp')
-rw-r--r--src/lib/util/corestr.cpp78
1 files changed, 25 insertions, 53 deletions
diff --git a/src/lib/util/corestr.cpp b/src/lib/util/corestr.cpp
index e55bc63908e..586f56c1aa3 100644
--- a/src/lib/util/corestr.cpp
+++ b/src/lib/util/corestr.cpp
@@ -63,70 +63,42 @@ int core_strnicmp(const char *s1, const char *s2, size_t n)
/*-------------------------------------------------
core_strwildcmp - case-insensitive wildcard
- string compare (up to 16 characters at the
- moment)
+ string compare
-------------------------------------------------*/
-int core_strwildcmp(const char *sp1, const char *sp2)
+int core_strwildcmp(std::string_view s1, std::string_view s2)
{
- char s1[17], s2[17];
- size_t i, l1, l2;
- char *p;
-
- //assert(strlen(sp1) < 16);
- //assert(strlen(sp2) < 16);
-
- if (sp1[0] == 0) strcpy(s1, "*");
- else { strncpy(s1, sp1, 16); s1[16] = 0; }
-
- if (sp2[0] == 0) strcpy(s2, "*");
- else { strncpy(s2, sp2, 16); s2[16] = 0; }
-
- p = strchr(s1, '*');
- if (p)
- {
- for (i = p - s1; i < 16; i++) s1[i] = '?';
- s1[16] = 0;
- }
-
- p = strchr(s2, '*');
- if (p)
- {
- for (i = p - s2; i < 16; i++) s2[i] = '?';
- s2[16] = 0;
- }
-
- l1 = strlen(s1);
- if (l1 < 16)
+ // slight tweak of core_stricmp() logic
+ auto s1_iter = s1.begin();
+ auto s2_iter = s2.begin();
+ while (true)
{
- for (i = l1 + 1; i < 16; i++) s1[i] = ' ';
- s1[16] = 0;
- }
+ if ((s1.end() != s1_iter && *s1_iter == '*')
+ || (s2.end() != s2_iter && *s2_iter == '*'))
+ return 0;
- l2 = strlen(s2);
- if (l2 < 16)
- {
- for (i = l2 + 1; i < 16; i++) s2[i] = ' ';
- s2[16] = 0;
- }
+ if (s1.end() == s1_iter)
+ return (s2.end() == s2_iter) ? 0 : -1;
+ else if (s2.end() == s2_iter)
+ return 1;
- for (i = 0; i < 16; i++)
- {
- if (s1[i] == '?' && s2[i] != '?') s1[i] = s2[i];
- if (s2[i] == '?' && s1[i] != '?') s2[i] = s1[i];
+ const int c1 = tolower(uint8_t(*s1_iter++));
+ const int c2 = tolower(uint8_t(*s2_iter++));
+ const int diff = (c1 != '?' && c2 != '?')
+ ? c1 - c2
+ : 0;
+ if (diff)
+ return diff;
}
-
- return core_stricmp(s1, s2);
}
-bool core_iswildstr(const char *sp)
+bool core_iswildstr(std::string_view s)
{
- for ( ; sp && *sp; sp++)
+ auto iter = std::find_if(s.begin(), s.end(), [](char c)
{
- if (('?' == *sp) || ('*' == *sp))
- return true;
- }
- return false;
+ return c == '?' || c == '*';
+ });
+ return iter != s.end();
}