Mercurial > libunicode
diff generator/make-unicode/unicode-after.cpp @ 2:84765c6f4872
New style
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 02 Feb 2017 18:07:27 +0100 |
parents | f94206b2e05e |
children | d9d3406c1250 |
line wrap: on
line diff
--- a/generator/make-unicode/unicode-after.cpp Mon Jan 02 20:43:34 2017 +0100 +++ b/generator/make-unicode/unicode-after.cpp Thu Feb 02 18:07:27 2017 +0100 @@ -1,6 +1,6 @@ void encode(char32_t c, char res[5]) noexcept { - switch (nbytesPoint(c)) { + switch (nbytes_point(c)) { case 1: res[0] = static_cast<char>(c); res[1] = '\0'; @@ -28,11 +28,11 @@ } } -void decode(char32_t &c, const char *res) noexcept +void decode(char32_t& c, const char* res) noexcept { c = 0; - switch (nbytesUtf8(res[0])) { + switch (nbytes_utf8(res[0])) { case 1: c = res[0]; break; @@ -55,55 +55,64 @@ } } -int nbytesUtf8(char c) noexcept +int nbytes_utf8(char c) noexcept { - if (static_cast<unsigned char>(c) <= 127) + if (static_cast<unsigned char>(c) <= 127) { return 1; - if ((c & 0xE0) == 0xC0) + } + if ((c & 0xE0) == 0xC0) { return 2; - if ((c & 0xF0) == 0xE0) + } + if ((c & 0xF0) == 0xE0) { return 3; - if ((c & 0xF8) == 0xF0) + } + if ((c & 0xF8) == 0xF0) { return 4; + } return -1; } -int nbytesPoint(char32_t c) noexcept +int nbytes_point(char32_t c) noexcept { - if (c <= 0x7F) + if (c <= 0x7F) { return 1; - if (c <= 0x7FF) + } + if (c <= 0x7FF) { return 2; - if (c <= 0xFFFF) + } + if (c <= 0xFFFF) { return 3; - if (c <= 0x1FFFFF) + } + if (c <= 0x1FFFFF) { return 4; + } return -1; } -unsigned length(const std::string &str) +unsigned length(const std::string& str) { unsigned total = 0; - forEach(str, [&] (char32_t) { + for_each(str, [&] (char32_t) { ++ total; }); return total; } -std::string toUtf8(const std::u32string &array) +std::string to_utf8(const std::u32string& array) { std::string res; for (size_t i = 0; i < array.size(); ++i) { char tmp[5]; - int size = nbytesPoint(array[i]); + int size = nbytes_point(array[i]); - if (size < 0) + if (size < 0) { throw std::invalid_argument("invalid sequence"); + } encode(array[i], tmp); res.insert(res.length(), tmp); @@ -112,11 +121,11 @@ return res; } -std::u32string toUtf32(const std::string &str) +std::u32string to_utf32(const std::string& str) { std::u32string res; - forEach(str, [&] (char32_t code) { + for_each(str, [&] (char32_t code) { res.push_back(code); });