-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwstringfunc.cpp
96 lines (81 loc) · 3.34 KB
/
wstringfunc.cpp
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
/********************************************************************************
Copyright (C) 2014 Append Huang <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
********************************************************************************/
#include <windows.h>
#include <cwctype>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include "wstringfunc.h"
//split from http://stackoverflow.com/questions/236129/how-to-split-a-wstring-in-c ;
std::vector<std::wstring> &split(const std::wstring &s, wchar_t delim, unsigned int maximum, std::vector<std::wstring> &elems) {
std::wstringstream ss(s);
std::wstring item;
while (elems.size() < maximum-1) {
if(std::getline(ss, item, delim)) {
if( (item.length()==0)) continue;
elems.push_back(item);
}
else break;
}
if ( (unsigned int)ss.tellg() < s.length()) {
unsigned int i = (unsigned int)ss.tellg();
while(i<s.length()) {
if(s[i]==delim) i++;
else {elems.push_back(s.substr(i)); break;}
}
}
return elems;
}
std::vector<std::wstring> split(const std::wstring &s, wchar_t delim, unsigned int maximum) {
std::vector<std::wstring> elems;
split(s, delim, maximum, elems);
return elems;
}
std::wstring replaceFirst(std::wstring &s,
std::wstring toReplace,
std::wstring replaceWith) {
return(s.replace(s.find(toReplace), toReplace.length(), replaceWith));
}
std::wstring ToUpperString (std::wstring &s)
{ std::transform (s.begin(), s.end(), s.begin(), towupper); return s;}
std::string ToUpperString (std::string &s)
{ std::transform (s.begin(), s.end(), s.begin(), toupper); return s;}
std::wstring ToLowerString (std::wstring &s)
{ std::transform (s.begin(), s.end(), s.begin(), towlower); return s;}
std::string ToLowerString (std::string &s)
{ std::transform (s.begin(), s.end(), s.begin(), tolower); return s;}
std::string to_utf8(const wchar_t* buffer, int len) {
int nChars = WideCharToMultiByte(CP_UTF8, 0, buffer, len, NULL, 0, NULL, NULL);
if (nChars == 0) return "";
std::string newbuffer;
newbuffer.resize(nChars);
WideCharToMultiByte(CP_UTF8, 0, buffer, len, const_cast<char*>(newbuffer.c_str()), nChars, NULL, NULL);
return newbuffer;
}
std::string to_utf8(const std::wstring& str){
return to_utf8(str.c_str(), (int)str.size());
}
std::wstring from_utf8(const char* buffer, int len){
int nChars = MultiByteToWideChar(CP_UTF8, 0, buffer, len, NULL, 0);
if (nChars == 0) return L"";
std::wstring newbuffer;
newbuffer.resize(nChars);
MultiByteToWideChar(CP_UTF8, 0, buffer, len, const_cast<wchar_t*>(newbuffer.c_str()), nChars);
return newbuffer;
}
std::wstring from_utf8(const std::string& str){
return from_utf8(str.c_str(), (int)str.size());
}