1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
#ifndef SOCI_UNICODE_H_INCLUDED
#define SOCI_UNICODE_H_INCLUDED
#include "soci/error.h"
#include <string>
#include <wchar.h>
// Define SOCI_WCHAR_T_IS_UTF32 if wchar_t is wider than 16 bits (e.g., on Unix/Linux)
#if WCHAR_MAX > 0xFFFFu
#define SOCI_WCHAR_T_IS_UTF32
#endif
namespace soci
{
namespace details
{
#if defined(SOCI_WCHAR_T_IS_UTF32)
static_assert(sizeof(wchar_t) == sizeof(char32_t), "wchar_t must be 32 bits");
inline char32_t const* wide_to_char_type(std::wstring const& ws)
{
return reinterpret_cast<char32_t const*>(ws.data());
}
#else
static_assert(sizeof(wchar_t) == sizeof(char16_t), "wchar_t must be 16 bits");
inline char16_t const* wide_to_char_type(std::wstring const& ws)
{
return reinterpret_cast<char16_t const*>(ws.data());
}
#endif
inline void throw_if_too_small(std::size_t required, std::size_t available)
{
if (required > available)
throw soci_error("Output buffer is too small");
}
/**
Check if the given string is a valid UTF-8 encoded string.
Throws soci_error if the string is not a valid UTF-8 string.
@param utf8 The string of length @a len.
@param len The length of the string.
*/
void SOCI_DECL ensure_valid_utf8(char const* utf8, std::size_t len);
/// @overload
inline void ensure_valid_utf8(std::string const& utf8)
{
ensure_valid_utf8(utf8.data(), utf8.size());
}
/**
Check if a given string is a valid UTF-16 encoded string.
Throws soci_error if the string is not a valid UTF-16 string.
@param s The UTF-16 string to check.
@param len The length of the string in characters.
*/
void SOCI_DECL ensure_valid_utf16(char16_t const* s, std::size_t len);
/// @overload
inline void ensure_valid_utf16(std::u16string const& utf16)
{
ensure_valid_utf16(utf16.data(), utf16.size());
}
/**
Check if a given string is a valid UTF-32 encoded string.
Throws soci_error if the string is not a valid UTF-32 string.
@param utf32 The input UTF-32 string.
@return True if the input string is valid, false otherwise.
*/
void SOCI_DECL ensure_valid_utf32(char32_t const* s, std::size_t len);
/// @overload
inline void ensure_valid_utf32(std::u32string const& utf32)
{
ensure_valid_utf32(utf32.data(), utf32.size());
}
/**
Convert a UTF-8 encoded string to a UTF-16 encoded string.
The input string must be a valid UTF-8 encoded string of the given length
(not necessarily NUL-terminated). The output buffer must either contain
enough space to store @a len16 characters or be @c nullptr to just compute
the length required for conversion (in which case @a len16 is ignored).
@param utf8 The input UTF-8 encoded string.
@param len8 The length of the input string.
@param out16 The output buffer or nullptr to just compute the required
length.
@param len16 The length of the output buffer if it is non-null.
@return The length of the UTF-16 output.
@throws soci_error if the input string contains invalid UTF-8 encoding or
if the required length is greater than @a len16 when @a out16 is not @c
nullptr.
*/
std::size_t SOCI_DECL
utf8_to_utf16(char const* utf8, std::size_t len8,
char16_t* out16, std::size_t len16);
/// @overload
inline std::u16string utf8_to_utf16(char const* s, std::size_t len)
{
auto const len16 = utf8_to_utf16(s, len, nullptr, 0);
std::u16string utf16(len16, u'\0');
utf8_to_utf16(s, len, const_cast<char16_t*>(utf16.data()), len16);
return utf16;
}
/// @overload
inline std::u16string utf8_to_utf16(std::string const& utf8)
{
return utf8_to_utf16(utf8.data(), utf8.size());
}
/// @overload
inline std::u16string utf8_to_utf16(char const* s)
{
return utf8_to_utf16(s, std::char_traits<char>::length(s));
}
/**
Convert a UTF-16 encoded string to a UTF-8 encoded string.
Semantics of this function are the same as for utf8_to_utf16(), see its
documentation for more details.
*/
std::size_t SOCI_DECL
utf16_to_utf8(char16_t const* utf16, std::size_t len16,
char* out8, std::size_t len8);
/// @overload
inline std::string utf16_to_utf8(char16_t const* s, std::size_t len)
{
auto const len8 = utf16_to_utf8(s, len, nullptr, 0);
std::string utf8(len8, '\0');
utf16_to_utf8(s, len, const_cast<char*>(utf8.data()), len8);
return utf8;
}
/// @overload
inline std::string utf16_to_utf8(std::u16string const& utf16)
{
return utf16_to_utf8(utf16.data(), utf16.size());
}
/// @overload
inline std::string utf16_to_utf8(char16_t const* s)
{
return utf16_to_utf8(s, std::char_traits<char16_t>::length(s));
}
/**
Convert a UTF-16 encoded string to a UTF-32 encoded string.
Semantics of this function are the same as for utf8_to_utf16(), see its
documentation for more details.
*/
std::size_t SOCI_DECL
utf16_to_utf32(char16_t const* utf16, std::size_t len16,
char32_t* out32, std::size_t len32);
/// @overload
inline std::u32string utf16_to_utf32(char16_t const* s, std::size_t len)
{
auto const len32 = utf16_to_utf32(s, len, nullptr, 0);
std::u32string utf32(len32, U'\0');
utf16_to_utf32(s, len, const_cast<char32_t*>(utf32.data()), len32);
return utf32;
}
/// @overload
inline std::u32string utf16_to_utf32(std::u16string const& utf16)
{
return utf16_to_utf32(utf16.data(), utf16.size());
}
/// @overload
inline std::u32string utf16_to_utf32(char16_t const* s)
{
return utf16_to_utf32(s, std::char_traits<char16_t>::length(s));
}
/**
Convert a UTF-32 encoded string to a UTF-16 encoded string.
Semantics of this function are the same as for utf8_to_utf16(), see its
documentation for more details.
*/
std::size_t SOCI_DECL
utf32_to_utf16(char32_t const* utf32, std::size_t len32,
char16_t* out16, std::size_t len16);
/// @overload
inline std::u16string utf32_to_utf16(char32_t const* utf32, std::size_t len)
{
auto const len16 = utf32_to_utf16(utf32, len, nullptr, 0);
std::u16string utf16(len16, u'\0');
utf32_to_utf16(utf32, len, const_cast<char16_t*>(utf16.data()), len16);
return utf16;
}
/// @overload
inline std::u16string utf32_to_utf16(std::u32string const& utf32)
{
return utf32_to_utf16(utf32.data(), utf32.size());
}
/// @overload
inline std::u16string utf32_to_utf16(char32_t const* s)
{
return utf32_to_utf16(s, std::char_traits<char32_t>::length(s));
}
/**
Convert a UTF-8 encoded string to a UTF-32 encoded string.
Semantics of this function are the same as for utf8_to_utf16(), see its
documentation for more details.
*/
std::size_t SOCI_DECL
utf8_to_utf32(char const* utf8, std::size_t len8,
char32_t* out32, std::size_t len32);
/// @overload
inline std::u32string utf8_to_utf32(char const* utf8, std::size_t len)
{
auto const len32 = utf8_to_utf32(utf8, len, nullptr, 0);
std::u32string utf32(len32, U'\0');
utf8_to_utf32(utf8, len, const_cast<char32_t*>(utf32.data()), len32);
return utf32;
}
/// @overload
inline std::u32string utf8_to_utf32(std::string const& utf8)
{
return utf8_to_utf32(utf8.data(), utf8.size());
}
/// @overload
inline std::u32string utf8_to_utf32(char const* s)
{
return utf8_to_utf32(s, std::char_traits<char>::length(s));
}
/**
Convert a UTF-32 encoded string to a UTF-8 encoded string.
Semantics of this function are the same as for utf8_to_utf16(), see its
documentation for more details.
*/
std::size_t SOCI_DECL
utf32_to_utf8(char32_t const* utf32, std::size_t len32,
char* out8, std::size_t len8);
/// @overload
inline std::string utf32_to_utf8(char32_t const* s, std::size_t len)
{
auto const len8 = utf32_to_utf8(s, len, nullptr, 0);
std::string utf8(len8, '\0');
utf32_to_utf8(s, len, const_cast<char*>(utf8.data()), len8);
return utf8;
}
/// @overload
inline std::string utf32_to_utf8(std::u32string const& utf32)
{
return utf32_to_utf8(utf32.data(), utf32.size());
}
/// @overload
inline std::string utf32_to_utf8(char32_t const* s)
{
return utf32_to_utf8(s, std::char_traits<char32_t>::length(s));
}
/**
Convert a UTF-8 encoded string to a wide string (wstring).
This is equivalent to either utf8_to_utf32() or utf8_to_utf16() depending
on the platform.
@param utf8 The input UTF-8 encoded string.
@return The wide string.
*/
inline std::wstring utf8_to_wide(char const* s, std::size_t len)
{
#if defined(SOCI_WCHAR_T_IS_UTF32)
auto const wlen = utf8_to_utf32(s, len, nullptr, 0);
std::wstring ws(wlen, u'\0');
utf8_to_utf32(s, len, const_cast<char32_t*>(wide_to_char_type(ws)), wlen);
return ws;
#else // !SOCI_WCHAR_T_IS_UTF32
auto const wlen = utf8_to_utf16(s, len, nullptr, 0);
std::wstring ws(wlen, u'\0');
utf8_to_utf16(s, len, const_cast<char16_t*>(wide_to_char_type(ws)), wlen);
return ws;
#endif // SOCI_WCHAR_T_IS_UTF32
}
/// @overload
inline std::wstring utf8_to_wide(std::string const& utf8)
{
return utf8_to_wide(utf8.data(), utf8.size());
}
/**
Convert a wide string (wstring) to a UTF-8 encoded string.
This is equivalent to either utf32_to_utf8() or utf16_to_utf8() depending
on the platform.
@param ws The wide string.
@return std::string The UTF-8 encoded string.
*/
inline std::string wide_to_utf8(std::wstring const& ws)
{
#if defined(SOCI_WCHAR_T_IS_UTF32)
return utf32_to_utf8(wide_to_char_type(ws), ws.size());
#else // !SOCI_WCHAR_T_IS_UTF32
return utf16_to_utf8(wide_to_char_type(ws), ws.size());
#endif // SOCI_WCHAR_T_IS_UTF32
}
/**
Convert a UTF-16 encoded string to a wide string (wstring).
This is equivalent to either utf16_to_utf32() or direct copy depending on
the platform.
@param s The UTF-16 encoded string.
@param len The length of the input string.
@return The wide string.
@throws soci_error if the input string contains invalid UTF-16 encoding.
*/
inline std::wstring utf16_to_wide(char16_t const* s, std::size_t len)
{
#if defined(SOCI_WCHAR_T_IS_UTF32)
// Convert UTF-16 to UTF-32 which is used by wstring.
auto const wlen = utf16_to_utf32(s, len, nullptr, 0);
std::wstring ws(wlen, L'\0');
utf16_to_utf32(s, len,
const_cast<char32_t*>(wide_to_char_type(ws)), wlen);
return ws;
#else // !SOCI_WCHAR_T_IS_UTF32
// Perform validation even though it's already UTF-16
ensure_valid_utf16(s, len);
wchar_t const* ws = reinterpret_cast<wchar_t const*>(s);
return std::wstring(ws, ws + len);
#endif // SOCI_WCHAR_T_IS_UTF32
}
/// @overload
inline std::wstring utf16_to_wide(char16_t const* s)
{
return utf16_to_wide(s, std::char_traits<char16_t>::length(s));
}
/// @overload
inline std::wstring utf16_to_wide(std::u16string const& utf16)
{
return utf16_to_wide(utf16.data(), utf16.size());
}
/**
Convert a wide string (wstring) to a UTF-16 encoded string.
This is equivalent to either utf32_to_utf16() or direct copy depending on
the platform.
@param ws The wide string.
@param out The output buffer or nullptr to just compute the required length.
@param len The output buffer length in characters (ignored if @a out is @c
nullptr).
@return The length of the UTF-16 output.
@throws soci_error if the input string contains invalid wide characters or
if the output buffer is too small when @a out is not @c nullptr.
*/
inline
std::size_t wide_to_utf16(std::wstring const& ws, char16_t* out, std::size_t len)
{
#if defined(SOCI_WCHAR_T_IS_UTF32)
// Convert UTF-32 string to UTF-16.
return utf32_to_utf16(wide_to_char_type(ws), ws.length(), out, len);
#else // !SOCI_WCHAR_T_IS_UTF32
// It's already in UTF-16, just copy, but check that it's valid and that we
// have enough space -- or just return the length if not asked to copy.
auto const wlen = ws.length();
if (out)
{
throw_if_too_small(wlen, len);
ensure_valid_utf16(wide_to_char_type(ws), wlen);
std::memcpy(out, ws.data(), wlen * sizeof(wchar_t));
}
return wlen;
#endif // SOCI_WCHAR_T_IS_UTF32
}
/// @overload
inline std::u16string wide_to_utf16(std::wstring const& ws)
{
auto const wlen = wide_to_utf16(ws, nullptr, 0);
std::u16string utf16(wlen, u'\0');
wide_to_utf16(ws, const_cast<char16_t*>(utf16.data()), wlen);
return utf16;
}
} // namespace details
} // namespace soci
#endif // SOCI_UNICODE_H_INCLUDED
|