1 #include <mbgl/util/platform.hpp>
2 #include <libnu/unaccent.h>
3 #include <unaccent.hpp>
4
5 #include <cstring>
6 #include <sstream>
7
8 namespace mbgl { namespace platform {
9
unaccent(const std::string & str)10 std::string unaccent(const std::string& str)
11 {
12 std::stringstream output;
13 char const *itr = str.c_str(), *nitr;
14 char const *end = itr + str.length();
15 char lo[5] = { 0 };
16
17 for (; itr < end; itr = nitr)
18 {
19 uint32_t code_point = 0;
20 char const* buf = nullptr;
21
22 nitr = _nu_tounaccent(itr, end, nu_utf8_read, &code_point, &buf, nullptr);
23 if (buf != nullptr)
24 {
25 do
26 {
27 buf = NU_CASEMAP_DECODING_FUNCTION(buf, &code_point);
28 if (code_point == 0) break;
29 output.write(lo, nu_utf8_write(code_point, lo) - lo);
30 }
31 while (code_point != 0);
32 }
33 else
34 {
35 output.write(itr, nitr - itr);
36 }
37 }
38
39 return output.str();
40 }
41
42 } // namespace platform
43 } // namespace mbgl
44