-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversion.cpp
69 lines (58 loc) · 1.56 KB
/
Conversion.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
#include "conversion.h"
#define WHITE_SPACE_CHARACTERS _T(" \t\n\v\f\r ")
int Conversion::ToInt(const wstring& _buff)
{
return _tcstol(_buff.c_str(), nullptr, 10);
}
wstring Conversion::LeftTrimString(wstring _str, const wstring& _val)
{
wstring::size_type pos = _str.find_first_not_of(_val);
if (pos != wstring::npos)
_str.erase(0, pos);
else
_str.erase(_str.begin(), _str.end());
return _str;
}
wstring Conversion::RightTrimString(wstring _str, const wstring& _val)
{
wstring::size_type pos = _str.find_last_not_of(_val);
if (pos != wstring::npos)
_str.erase(pos + 1);
else
_str.erase(_str.begin(), _str.end());
return _str;
}
wstring Conversion::TrimString(wstring _str, const wstring& _val)
{
wstring::size_type pos = _str.find_last_not_of(_val);
if (pos != wstring::npos)
{
_str.erase(pos + 1);
pos = _str.find_first_not_of(_val);
if (pos != wstring::npos)
_str.erase(0, pos);
}
else
_str.erase(_str.begin(), _str.end());
return _str;
}
wstring Conversion::TrimWhiteChar(const wstring& _val)
{
return TrimString(_val, WHITE_SPACE_CHARACTERS);
}
wstring Conversion::ToLower(wstring _val)
{
transform(_val.begin(), _val.end(), _val.begin(), [](wchar_t c) { return towlower(c); });
return _val;
}
void Conversion::StringReplaceAll(wstring& _mess, const wstring& _oldStr, const wstring& _newStr)
{
const size_t oldLen = _oldStr.length();
const size_t newLen = _newStr.length();
size_t position = 0;
while ((position = _mess.find(_oldStr, position)) != string::npos)
{
_mess.replace(position, oldLen, _newStr);
position += newLen;
}
}