irccd  3.0.3
unicode.hpp
1 /*
2  * unicode.hpp -- UTF-8 to UTF-32 conversions and various operations
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_JS_UNICODE_HPP
20 #define IRCCD_JS_UNICODE_HPP
21 
29 #include <stdexcept>
30 #include <string>
31 #include <string_view>
32 
36 namespace irccd::unicode {
37 
44 void encode(char32_t point, char res[5]) noexcept;
45 
52 void decode(char32_t& c, const char* res) noexcept;
53 
64 auto nbytes_utf8(char c) noexcept -> int;
65 
72 auto nbytes_point(char32_t point) noexcept -> int;
73 
81 auto length(std::string_view str) -> unsigned;
82 
93 template <typename Func>
94 void for_each(std::string_view str, Func function)
95 {
96  for (size_t i = 0; i < str.size(); ) {
97  char32_t point = 0;
98  int size = nbytes_utf8(str[i]);
99 
100  if (size < 0)
101  throw std::invalid_argument("invalid sequence");
102 
103  decode(point, str.data() + i);
104  function(point);
105 
106  i += size;
107  }
108 }
109 
117 auto to_utf8(std::u32string_view array) -> std::string;
118 
126 auto to_utf32(std::string_view str) -> std::u32string;
127 
134 auto isspace(char32_t c) noexcept -> bool;
135 
142 auto isdigit(char32_t c) noexcept -> bool;
143 
150 auto isalpha(char32_t c) noexcept -> bool;
151 
158 auto isupper(char32_t c) noexcept -> bool;
159 
166 auto islower(char32_t c) noexcept -> bool;
167 
174 auto istitle(char32_t c) noexcept -> bool;
175 
182 auto toupper(char32_t c) noexcept -> char32_t;
183 
190 auto tolower(char32_t c) noexcept -> char32_t;
191 
198 auto totitle(char32_t c) noexcept -> char32_t;
199 
206 auto toupper(std::u32string_view str) -> std::u32string;
207 
215 auto toupper(std::string_view str) -> std::string;
216 
223 auto tolower(std::u32string_view str) -> std::u32string;
224 
232 auto tolower(std::string_view str) -> std::string;
233 
234 } // !irccd::unicode
235 
236 #endif // !IRCCD_JS_UNICODE_HPP
irccd::unicode::nbytes_point
auto nbytes_point(char32_t point) noexcept -> int
irccd::unicode::isupper
auto isupper(char32_t c) noexcept -> bool
irccd::unicode::isdigit
auto isdigit(char32_t c) noexcept -> bool
irccd::unicode::istitle
auto istitle(char32_t c) noexcept -> bool
irccd::unicode::isspace
auto isspace(char32_t c) noexcept -> bool
irccd::unicode::isalpha
auto isalpha(char32_t c) noexcept -> bool
irccd::unicode::length
auto length(std::string_view str) -> unsigned
irccd::unicode::totitle
auto totitle(char32_t c) noexcept -> char32_t
irccd::unicode::to_utf32
auto to_utf32(std::string_view str) -> std::u32string
irccd::unicode::decode
void decode(char32_t &c, const char *res) noexcept
irccd::unicode::toupper
auto toupper(char32_t c) noexcept -> char32_t
irccd::unicode::tolower
auto tolower(char32_t c) noexcept -> char32_t
irccd::unicode::nbytes_utf8
auto nbytes_utf8(char c) noexcept -> int
irccd::unicode::to_utf8
auto to_utf8(std::u32string_view array) -> std::string
irccd::unicode::encode
void encode(char32_t point, char res[5]) noexcept
irccd::unicode::islower
auto islower(char32_t c) noexcept -> bool
irccd::unicode
Unicode namespace.
Definition: unicode.hpp:36
irccd::unicode::for_each
void for_each(std::string_view str, Func function)
Definition: unicode.hpp:94