Add base::utf8_icmp()

This commit is contained in:
David Capello 2014-09-21 16:55:54 -03:00
parent 64b8b8ef82
commit 432dd33281
4 changed files with 52 additions and 3 deletions

View File

@ -25,6 +25,7 @@
#include "app/modules/gfx.h"
#include "app/thumbnail_generator.h"
#include "app/ui/skin/skin_theme.h"
#include "base/string.h"
#include "she/font.h"
#include "she/surface.h"
#include "ui/ui.h"
@ -261,9 +262,7 @@ bool FileList::onProcessMessage(Message* msg)
for (i=MAX(select, 0); i<bottom; ++i, ++link) {
IFileItem* fi = *link;
if (strnicmp(fi->getDisplayName().c_str(),
m_isearch.c_str(),
chrs) == 0) {
if (base::utf8_icmp(fi->getDisplayName(), m_isearch, chrs) == 0) {
select = i;
break;
}

View File

@ -177,4 +177,32 @@ int utf8_length(const std::string& utf8string)
return c;
}
int utf8_icmp(const std::string& a, const std::string& b, int n)
{
utf8_const_iterator a_it(a.begin());
utf8_const_iterator a_end(a.end());
utf8_const_iterator b_it(b.begin());
utf8_const_iterator b_end(b.end());
int i = 0;
for (; (n == 0 || i < n) && a_it != a_end && b_it != b_end; ++a_it, ++b_it, ++i) {
int a_chr = std::tolower(*a_it);
int b_chr = std::tolower(*b_it);
if (a_chr < b_chr)
return -1;
else if (a_chr > b_chr)
return 1;
}
if (n > 0 && i == n)
return 0;
else if (a_it == a_end && b_it == b_end)
return 0;
else if (a_it == a_end)
return -1;
else
return 1;
}
} // namespace base

View File

@ -20,6 +20,7 @@ namespace base {
std::wstring from_utf8(const std::string& utf8string);
int utf8_length(const std::string& utf8string);
int utf8_icmp(const std::string& a, const std::string& b, int n = 0);
template<typename SubIterator>
class utf8_iteratorT : public std::iterator<std::forward_iterator_tag,

View File

@ -66,6 +66,27 @@ TEST(String, Utf8Iterator)
ASSERT_TRUE((utf8_iterator(d.begin())+1) == utf8_iterator(d.end()));
}
TEST(String, Utf8ICmp)
{
EXPECT_EQ(-1, utf8_icmp("a", "b"));
EXPECT_EQ(-1, utf8_icmp("a", "b", 1));
EXPECT_EQ(-1, utf8_icmp("a", "b", 2));
EXPECT_EQ(-1, utf8_icmp("a", "aa"));
EXPECT_EQ(-1, utf8_icmp("A", "aa", 3));
EXPECT_EQ(-1, utf8_icmp("a", "ab"));
EXPECT_EQ(0, utf8_icmp("AaE", "aae"));
EXPECT_EQ(0, utf8_icmp("AaE", "aae", 3));
EXPECT_EQ(0, utf8_icmp("a", "aa", 1));
EXPECT_EQ(0, utf8_icmp("a", "ab", 1));
EXPECT_EQ(1, utf8_icmp("aaa", "Aa", 3));
EXPECT_EQ(1, utf8_icmp("Bb", "b"));
EXPECT_EQ(1, utf8_icmp("z", "b"));
EXPECT_EQ(1, utf8_icmp("z", "b", 1));
EXPECT_EQ(1, utf8_icmp("z", "b", 2));
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);