|
| 1 | +#include "../NativeString.h" |
| 2 | +#include <iostream> |
| 3 | +#include <sstream> |
| 4 | +#include <cwchar> |
| 5 | + |
| 6 | +#include <iterator> |
| 7 | +#include <cstdint> |
| 8 | +#include "utf8.h" |
| 9 | + |
| 10 | +namespace die { |
| 11 | + |
| 12 | +NativeString::Encoding const NativeString::nativeEncoding = NativeString::utf8; |
| 13 | + |
| 14 | +template<typename IT> |
| 15 | +std::wstring utf8toUtf16(IT beg, IT end) |
| 16 | +{ |
| 17 | + std::wstring result; |
| 18 | + utf8::utf8to16(beg,end,std::back_inserter(result)); |
| 19 | + return result; |
| 20 | +} |
| 21 | + |
| 22 | +template<typename IT> |
| 23 | +std::string utf16toUtf8(IT beg, IT end) |
| 24 | +{ |
| 25 | + std::string result; |
| 26 | + utf8::utf16to8(beg,end,std::back_inserter(result)); |
| 27 | + return result; |
| 28 | +} |
| 29 | + |
| 30 | +std::string encodeToUTF8(NativeString::Encoding encoding, std::string const & strEncoded) |
| 31 | +{ |
| 32 | + if( strEncoded.empty() ) return std::string(); |
| 33 | + |
| 34 | + switch(encoding) { |
| 35 | + case NativeString::utf16: |
| 36 | + return utf16toUtf8( |
| 37 | + reinterpret_cast<std::uint16_t const *>(&strEncoded[0]), |
| 38 | + reinterpret_cast<std::uint16_t const *>(&strEncoded[strEncoded.size()])); |
| 39 | + case NativeString::utf8: |
| 40 | + return strEncoded; |
| 41 | + default: |
| 42 | + std::cerr << "unsupported encoding" << std::endl; |
| 43 | + return std::string(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +NativeString::NativeString(): |
| 48 | + str() |
| 49 | +{} |
| 50 | + |
| 51 | +NativeString::~NativeString() |
| 52 | +{ |
| 53 | + using std::string; |
| 54 | + str.~string(); |
| 55 | +} |
| 56 | + |
| 57 | +NativeString::NativeString(NativeString const & other): |
| 58 | + str(other.str) |
| 59 | +{} |
| 60 | + |
| 61 | +NativeString & NativeString::operator=(NativeString const & other) |
| 62 | +{ |
| 63 | + str = other.str; |
| 64 | + return *this; |
| 65 | +} |
| 66 | + |
| 67 | +NativeString::NativeString(std::string const & strUTF8): |
| 68 | + str(strUTF8) |
| 69 | +{} |
| 70 | + |
| 71 | +NativeString::NativeString(char const * strUTF8): |
| 72 | + str(strUTF8) |
| 73 | +{} |
| 74 | + |
| 75 | +NativeString::NativeString(char const * strUTF8, size_t size): |
| 76 | + str(strUTF8,size) |
| 77 | +{} |
| 78 | + |
| 79 | +NativeString::NativeString(NativeString::Encoding encoding, std::string const & strEncoded): |
| 80 | + str(encodeToUTF8(encoding,strEncoded)) |
| 81 | +{} |
| 82 | + |
| 83 | +std::string NativeString::toUTF8() const |
| 84 | +{ |
| 85 | + return str; |
| 86 | +} |
| 87 | + |
| 88 | +NativeString::NativeString(std::wstring const & strUTF16): |
| 89 | + str(utf16toUtf8(strUTF16.begin(),strUTF16.end())) |
| 90 | +{} |
| 91 | + |
| 92 | +NativeString::NativeString(wchar_t const * strUTF16): |
| 93 | + str(utf16toUtf8(strUTF16,strUTF16 + std::wcslen(strUTF16))) |
| 94 | +{} |
| 95 | + |
| 96 | +std::wstring NativeString::toUTF16() const |
| 97 | +{ |
| 98 | + return utf8toUtf16(str.begin(),str.end()); |
| 99 | +} |
| 100 | + |
| 101 | +template<typename T> |
| 102 | +T lexical_cast(std::string const & str) |
| 103 | +{ |
| 104 | + T result = T(); |
| 105 | + std::basic_istringstream<char> iss(str); |
| 106 | + iss >> result; |
| 107 | + return result; |
| 108 | +} |
| 109 | + |
| 110 | +int NativeString::toInt() const |
| 111 | +{ |
| 112 | + return lexical_cast<int>(str); |
| 113 | +} |
| 114 | + |
| 115 | +float NativeString::toFloat() const |
| 116 | +{ |
| 117 | + return lexical_cast<float>(str); |
| 118 | +} |
| 119 | + |
| 120 | +double NativeString::toDouble() const |
| 121 | +{ |
| 122 | + return lexical_cast<double>(str); |
| 123 | +} |
| 124 | + |
| 125 | +template<typename T> |
| 126 | +NativeString toStringT(T value) |
| 127 | +{ |
| 128 | + std::basic_ostringstream<char> oss; |
| 129 | + oss << value; |
| 130 | + return NativeString(oss.str()); |
| 131 | +} |
| 132 | + |
| 133 | +NativeString NativeString::toString(int value) |
| 134 | +{ |
| 135 | + return toStringT(value); |
| 136 | +} |
| 137 | + |
| 138 | +NativeString NativeString::toString(float value) |
| 139 | +{ |
| 140 | + return toStringT(value); |
| 141 | +} |
| 142 | + |
| 143 | +NativeString NativeString::toString(double value) |
| 144 | +{ |
| 145 | + return toStringT(value); |
| 146 | +} |
| 147 | + |
| 148 | +bool NativeString::empty() const |
| 149 | +{ |
| 150 | + return str.empty(); |
| 151 | +} |
| 152 | + |
| 153 | +NativeString & NativeString::operator+=(NativeString const & other) |
| 154 | +{ |
| 155 | + str += other.str; |
| 156 | + return *this; |
| 157 | +} |
| 158 | + |
| 159 | +bool NativeString::operator==(NativeString const & other) const |
| 160 | +{ |
| 161 | + return str == other.str; |
| 162 | +} |
| 163 | + |
| 164 | +bool NativeString::operator!=(NativeString const & other) const |
| 165 | +{ |
| 166 | + return str != other.str; |
| 167 | +} |
| 168 | + |
| 169 | +bool operator<(NativeString const & a, NativeString const & b) |
| 170 | +{ |
| 171 | + return a.str < b.str; |
| 172 | +} |
| 173 | + |
| 174 | +} |
| 175 | + |
0 commit comments