/
gutils_string.cpp
159 lines (134 loc) · 3.29 KB
/
gutils_string.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
*************************************************************
* @file gutils_string.cpp
* @brief Gutils string
*
*
*
* @author Gaspar Fernández <blakeyed@totaki.com>
* @version
* @date 02 oct 2013
* Changelog
*
*
*
*
*
*
*
*************************************************************/
#include "gutils_string.h"
#include <cstdlib>
namespace Gutils
{
ustring replace(ustring source, ustring fromStr, ustring toStr, int offset, int times)
{
int total = 0;
ustring::size_type pos=offset;
while ( ( (pos = source.find(fromStr, pos)) < Glib::ustring::npos) && ( (times==0) || (total++<times) ) )
{
source.replace(pos, fromStr.length(), toStr);
pos+=toStr.size();
}
return source;
}
ustring replace(ustring source, std::map<ustring,ustring>strMap, int offset, int times)
{
int total = 0;
ustring::size_type pos;
for (std::map<ustring, ustring>::iterator i=strMap.begin(); i!=strMap.end(); ++i)
{
ustring fromStr = i->first;
ustring toStr = i->second;
pos=offset;
while ( (pos = source.find(fromStr, pos)) < Glib::ustring::npos)
{
if ( (times!=0) && (total++>=times) )
return source; // Don't work anymore
source.replace(pos, fromStr.length(), toStr);
pos+=toStr.size();
}
}
return source;
}
ustring trimRight( ustring &str, ustring trimChars)
{
ustring::size_type pos = str.find_last_not_of( trimChars );
str.erase( pos + 1 );
return str;
}
ustring trimLeft( ustring &str, const ustring trimChars)
{
ustring::size_type pos = str.find_first_not_of( trimChars );
str.erase( 0, pos );
return str;
}
ustring trim( ustring &str, const ustring trimChars)
{
trimRight( str, trimChars );
trimLeft( str, trimChars );
return str;
}
ustring trimRightC( ustring &str, ustring trimChars)
{
ustring copy=str;
return trimRight(copy, trimChars);
}
ustring trimLeftC( ustring &str, const ustring trimChars)
{
ustring copy=str;
return trimLeft(copy, trimChars);
}
ustring trimC( ustring str, const ustring trimChars)
{
ustring copy=str;
trimRight(copy, trimChars);
trimLeft(copy, trimChars );
return copy;
}
ustring indentation(int level, ustring indentStr)
{
ustring retStr="";
for (unsigned i=0; i<level; ++i)
retStr+=indentStr;
return retStr;
}
int strtol(long &value, ustring str, int base)
{
const char *nptr = str.c_str();
char *eptr;
value = ::strtol(nptr, &eptr, base);
if (nptr==eptr)
return -1;
else if (eptr<(char*)nptr+str.length())
return 0;
else
return 1;
}
int strtod(double &value, ustring str)
{
const char *nptr = str.c_str();
char *eptr;
value = ::strtod(nptr, &eptr);
if (nptr==eptr)
return -1;
else if (eptr<(char*)nptr+str.length())
return 0;
else
return 1;
}
int strtol(long &value, ustring str, long &value2, ustring str2, int base)
{
const char *nptr = str.c_str();
const char *nptr2 = str.c_str();
char *eptr, *eptr2;
value = ::strtol(nptr, &eptr, base);
value2 = ::strtol(nptr2, &eptr2, base);
if ( (nptr==eptr) || (nptr2==eptr2) )
return -1;
else if ( (eptr<(char*)nptr+str.length()) && (eptr2<(char*)nptr2+str2.length()) )
return 0;
else
return 1;
}
};