Skip to content

Commit c0331c4

Browse files
committed
posix build tree
1 parent 66dc4a0 commit c0331c4

File tree

7 files changed

+1094
-0
lines changed

7 files changed

+1094
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Debug/

src/posix/NativeString.cpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+

src/posix/TODO

Whitespace-only changes.

src/posix/utf8.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2006 Nemanja Trifunovic
2+
3+
/*
4+
Permission is hereby granted, free of charge, to any person or organization
5+
obtaining a copy of the software and accompanying documentation covered by
6+
this license (the "Software") to use, reproduce, display, distribute,
7+
execute, and transmit the Software, and to prepare derivative works of the
8+
Software, and to permit third-parties to whom the Software is furnished to
9+
do so, all subject to the following:
10+
11+
The copyright notices in the Software and this entire statement, including
12+
the above license grant, this restriction and the following disclaimer,
13+
must be included in all copies of the Software, in whole or in part, and
14+
all derivative works of the Software, unless such copies or derivative
15+
works are solely in the form of machine-executable object code generated by
16+
a source language processor.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21+
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22+
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24+
DEALINGS IN THE SOFTWARE.
25+
*/
26+
27+
28+
#ifndef UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731
29+
#define UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731
30+
31+
#include "utf8/checked.h"
32+
#include "utf8/unchecked.h"
33+
34+
#endif // header guard

0 commit comments

Comments
 (0)