Mercurial > code
comparison tools/mkunicode/Unicode-after.cpp @ 395:b78d6d8f2872
Unicode: remove class, use namespace
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 28 Sep 2015 15:55:46 +0200 |
parents | 7fe8d4094983 |
children | f083259de5e6 |
comparison
equal
deleted
inserted
replaced
394:fdceef4be88b | 395:b78d6d8f2872 |
---|---|
1 void Unicode::encode(char32_t c, char res[5]) noexcept | 1 void encode(char32_t c, char res[5]) noexcept |
2 { | 2 { |
3 switch (nbytesPoint(c)) { | 3 switch (nbytesPoint(c)) { |
4 case 1: | 4 case 1: |
5 res[0] = c; | 5 res[0] = c; |
6 res[1] = '\0'; | 6 res[1] = '\0'; |
26 default: | 26 default: |
27 break; | 27 break; |
28 } | 28 } |
29 } | 29 } |
30 | 30 |
31 void Unicode::decode(char32_t &c, const char *res) noexcept | 31 void decode(char32_t &c, const char *res) noexcept |
32 { | 32 { |
33 c = 0; | 33 c = 0; |
34 | 34 |
35 switch (nbytesUtf8(res[0])) { | 35 switch (nbytesUtf8(res[0])) { |
36 case 1: | 36 case 1: |
53 default: | 53 default: |
54 break; | 54 break; |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 int Unicode::nbytesUtf8(char c) noexcept | 58 int nbytesUtf8(char c) noexcept |
59 { | 59 { |
60 if ((c & 0xE0) == 0xC0) | 60 if ((c & 0xE0) == 0xC0) |
61 return 2; | 61 return 2; |
62 if ((c & 0xF0) == 0xE0) | 62 if ((c & 0xF0) == 0xE0) |
63 return 3; | 63 return 3; |
65 return 4; | 65 return 4; |
66 | 66 |
67 return 1; | 67 return 1; |
68 } | 68 } |
69 | 69 |
70 int Unicode::nbytesPoint(char32_t c) noexcept | 70 int nbytesPoint(char32_t c) noexcept |
71 { | 71 { |
72 if (c <= 0x7F) | 72 if (c <= 0x7F) |
73 return 1; | 73 return 1; |
74 if (c <= 0x7FF) | 74 if (c <= 0x7FF) |
75 return 2; | 75 return 2; |
79 return 4; | 79 return 4; |
80 | 80 |
81 return -1; | 81 return -1; |
82 } | 82 } |
83 | 83 |
84 int Unicode::length(const std::string &str) | 84 int length(const std::string &str) |
85 { | 85 { |
86 int total = 0; | 86 int total = 0; |
87 | 87 |
88 forEach(str, [&] (char32_t) { | 88 forEach(str, [&] (char32_t) { |
89 ++ total; | 89 ++ total; |
90 }); | 90 }); |
91 | 91 |
92 return total; | 92 return total; |
93 } | 93 } |
94 | 94 |
95 std::string Unicode::toUtf8(const std::u32string &array) | 95 std::string toUtf8(const std::u32string &array) |
96 { | 96 { |
97 std::string res; | 97 std::string res; |
98 | 98 |
99 for (size_t i = 0; i < array.size(); ++i) { | 99 for (size_t i = 0; i < array.size(); ++i) { |
100 char tmp[5]; | 100 char tmp[5]; |
109 } | 109 } |
110 | 110 |
111 return res; | 111 return res; |
112 } | 112 } |
113 | 113 |
114 std::u32string Unicode::toUtf32(const std::string &str) | 114 std::u32string toUtf32(const std::string &str) |
115 { | 115 { |
116 std::u32string res; | 116 std::u32string res; |
117 | 117 |
118 forEach(str, [&] (char32_t code) { | 118 forEach(str, [&] (char32_t code) { |
119 res.push_back(code); | 119 res.push_back(code); |
120 }); | 120 }); |
121 | 121 |
122 return res; | 122 return res; |
123 } | 123 } |
124 | |
125 } // !unicode |