kblib 0.2.3
General utilities library for modern C++
icu.h
Go to the documentation of this file.
1/* *****************************************************************************
2 * kblib is a general utility library for C++14 and C++17, intended to provide
3 * performant high-level abstractions and more expressive ways to do simple
4 * things.
5 *
6 * Copyright (c) 2021 killerbee
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * ****************************************************************************/
21
31#ifndef KBLIB_ICU_H
32#define KBLIB_ICU_H
33
34#include "tdecl.h"
35
36#include <ostream>
37#include <string>
38#include <typeinfo>
39#include <unicode/unistr.h>
40
41namespace KBLIB_NS {
42
51template <typename string = std::string>
52auto toUTF8(const icu::UnicodeString& s) -> string {
53 string res;
54 return s.toUTF8String(res);
55}
56
66template <typename string>
67auto fromUTF8(string s) -> icu::UnicodeString {
69}
70
77template <typename string = std::u32string>
78auto toUTF32(const icu::UnicodeString& s) -> string {
79 string res(s.countChar32(), '\0');
80 UErrorCode ec{U_ZERO_ERROR};
81 s.toUTF32(&res[0], res.size(), ec);
82 if (U_FAILURE(ec)) {
83 // silence warnings about ec not being a temporary
84 throw UErrorCode{ec};
85 }
86 return res;
87}
88
98template <typename string>
99auto fromUTF32(string s) -> icu::UnicodeString {
100 return icu::UnicodeString::fromUTF32(s.data(), s.length());
101}
102
107namespace icu_str_ops {
108
117 inline auto operator<<(std::ostream& os, const icu::UnicodeString& str)
118 -> std::ostream& {
119 return os << toUTF8(str);
120 }
121
126 inline auto operator+(std::string lhs, const icu::UnicodeString& str)
127 -> std::string {
128 return str.toUTF8String(lhs);
129 }
130
134 inline auto operator+(icu::UnicodeString lhs, const std::string& rhs)
135 -> icu::UnicodeString {
136 return lhs += fromUTF8(rhs);
137 }
138} // namespace icu_str_ops
139
148template <typename T>
149auto fromStr(const icu::UnicodeString& val, const char* type = typeid(T).name())
150 -> T {
151 return fromStr<T>(toUTF8(val), type);
152}
153
154} // namespace KBLIB_NS
155
156#endif // KBLIB_ICU_H
auto operator+(icu::UnicodeString lhs, const std::string &rhs) -> icu::UnicodeString
Definition: icu.h:134
auto operator<<(std::ostream &os, const icu::UnicodeString &str) -> std::ostream &
Provides a transcoding stream insertion operator for UnicodeStrings.
Definition: icu.h:117
auto toUTF32(const icu::UnicodeString &s) -> string
Converts a UnicodeString to UTF-32.
Definition: icu.h:78
auto toUTF8(const icu::UnicodeString &s) -> string
Convert a UnicodeString to a UTF-8 string.
Definition: icu.h:52
auto fromStr(const icu::UnicodeString &val, const char *type=typeid(T).name()) -> T
Reencodes val to UTF-8 and then converts it to T using the primary overload.
Definition: icu.h:149
auto fromUTF8(string s) -> icu::UnicodeString
Convert a UTF-8 string into a UnicodeString.
Definition: icu.h:67
auto fromUTF32(string s) -> icu::UnicodeString
Converts a UTF-32 string into a UnicodeString.
Definition: icu.h:99
Provides macros and basic templates used by the rest of kblib.
#define KBLIB_NS
Definition: tdecl.h:113