Skip to content

Commit 5bb4158

Browse files
committed
added toInt() method
1 parent 5b2863f commit 5bb4158

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/NativeString.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class NativeString {
2828
std::string toUTF8() const;
2929
std::wstring toUTF16() const;
3030

31+
int toInt() const;
32+
3133
bool empty() const;
3234
NativeString & operator+=(NativeString const & other);
3335
bool operator==(NativeString const & other) const;

src/win32/NativeString.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../NativeString.h"
22
#include <iostream>
3+
#include <sstream>
34
#include <windows.h>
45

56
namespace die {
@@ -13,7 +14,7 @@ std::wstring utf8_to_ws(char const * text, int size)
1314
int total = MultiByteToWideChar(CP_UTF8,0,text,size,0,0);
1415
std::wstring converted(total,L'\0');
1516
if( MultiByteToWideChar(CP_UTF8,0,text,size,&converted[0],total) == 0 ) {
16-
std::cerr << "error converting string\n";
17+
std::cerr << "error converting string" << std::endl;
1718
}
1819
return converted;
1920
}
@@ -27,6 +28,7 @@ std::wstring utf8_to_ws(char const * text)
2728

2829
std::wstring utf8_to_ws(std::string const & text)
2930
{
31+
if( text.empty() ) return std::wstring();
3032
return utf8_to_ws(text.c_str(),text.size());
3133
}
3234

@@ -38,7 +40,7 @@ std::wstring encodedToWs(NativeString::Encoding encoding, std::string const & st
3840
case NativeString::utf8:
3941
return utf8_to_ws(strEncoded);
4042
default:
41-
std::cerr << "unsupported encoding\n";
43+
std::cerr << "unsupported encoding" << std::endl;
4244
return std::wstring();
4345
}
4446
}
@@ -96,6 +98,20 @@ std::wstring NativeString::toUTF16() const
9698
return wstr;
9799
}
98100

101+
template<typename T>
102+
T lexical_cast(std::wstring const & wstr)
103+
{
104+
T result = T();
105+
std::basic_istringstream<wchar_t> iss(wstr);
106+
iss >> result;
107+
return result;
108+
}
109+
110+
int NativeString::toInt() const
111+
{
112+
return lexical_cast<int>(wstr);
113+
}
114+
99115
bool NativeString::empty() const
100116
{
101117
return wstr.empty();

0 commit comments

Comments
 (0)