wtf8.hpp (2088B)
1 #ifndef GUARD_EXASPERATINGLY_IMAGABLE_GUNLINE_RATFUCKS_3473 2 #define GUARD_EXASPERATINGLY_IMAGABLE_GUNLINE_RATFUCKS_3473 3 #pragma once 4 5 #include "libshit/nonowning_string.hpp" 6 7 #include <cstdint> 8 #include <string> 9 10 namespace Libshit 11 { 12 13 // Note: these functions will allocate bigger buffers than necessary, call 14 // shrink_to_fit if you intend to keep these strings around for a long time. 15 // Functions taking a `& out` will clear `out` but keep the allocation if it's 16 // large enough for a worst-case output. 17 18 #define LIBSHIT_GEN0(a_type, a_name, b_type, b_name) \ 19 void a_name##To##b_name(b_type& out, a_type in); \ 20 inline b_type a_name##To##b_name(a_type in) \ 21 { b_type res; a_name##To##b_name(res, in); return res; } 22 #define LIBSHIT_GEN(a_str, a_sv, a_name, b_str, b_sv, b_name) \ 23 LIBSHIT_GEN0(a_sv, a_name, b_str, b_name) \ 24 LIBSHIT_GEN0(b_sv, b_name, a_str, a_name) 25 26 LIBSHIT_GEN(std::string, StringView, Wtf8, 27 std::u16string, U16StringView, Wtf16) 28 LIBSHIT_GEN(std::string, StringView, Wtf8, 29 std::u16string, U16StringView, Wtf16LE) 30 31 LIBSHIT_GEN(std::string, StringView, Cesu8, 32 std::u16string, U16StringView, Wtf16) 33 LIBSHIT_GEN(std::string, StringView, Cesu8, 34 std::u16string, U16StringView, Wtf16LE) 35 36 LIBSHIT_GEN(std::string, StringView, Wtf8, 37 std::string, StringView, Cesu8) 38 39 // replaces invalid surrogate pairs with replacement char 40 LIBSHIT_GEN0(U16StringView, Utf16, std::string, Utf8) 41 LIBSHIT_GEN0(U16StringView, Utf16LE, std::string, Utf8) 42 43 // UTF-32 funs 44 LIBSHIT_GEN(std::string, StringView, Wtf8, 45 std::u32string, U32StringView, Wtf32) 46 LIBSHIT_GEN(std::string, StringView, Utf8, 47 std::u32string, U32StringView, Utf32) 48 49 // on windows, also support wchar_t 50 #if WCHAR_MAX == 65535 51 LIBSHIT_GEN0(StringView, Wtf8, std::wstring, Wtf16Wstr) 52 LIBSHIT_GEN0(WStringView, Wtf16, std::string, Wtf8) 53 LIBSHIT_GEN0(WStringView, Utf16, std::string, Utf8) 54 #endif 55 56 #undef LIBSHIT_GEN 57 #undef LIBSHIT_GEN0 58 } 59 60 #endif