-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstringc.h
More file actions
199 lines (156 loc) · 3.58 KB
/
stringc.h
File metadata and controls
199 lines (156 loc) · 3.58 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef __ROV_STRINGC__H
#define __ROV_STRINGC__H
/***************************************************************************
*
* Project libmjpeg
*
* Copyright (C) 2014-2015, Changer, <dingjinchengyx@163.com>.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include <string>
#include <stdlib.h>
#include <algorithm> // std::reverse
#include <time.h>
typedef std::string stringc;
inline void trim(stringc& s) {
int i = 0;
while (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n') {
i++;
}
s = s.substr(i);
i = s.size() - 1;
while (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n'){
i--;
}
s = s.substr(0, i + 1);
}
inline bool isDigit(const stringc& s) {
const char *p = s.c_str();
int len = s.size();
if (len == 1 && *p == '0')
return true;
if (*p == '-')
p++;
if (*p > '9' || *p < '1')
return false;
p++;
while (*p) {
if (*p > '9' || *p < '0')
return false;
p++;
}
return true;
}
inline int toDigit(const stringc& s) {
int i = 0;
const char* p = s.c_str();
if (*p == '-')p++;
while (*p) {
i *= 10;
i += *p - '0';
p++;
}
if (s[0] == '-')
i *= -1;
return i;
}
inline void formatRandomString(stringc& s) {
char buf[32]{0};
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep);
sprintf(buf, "%d%d%d%d%d%d", (1900 + p->tm_year), (1 + p->tm_mon),
p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
s.clear();
s.append(buf);
}
inline bool isHexDigit(const stringc& s){
auto p = s.c_str();
int len = s.size();
if (len == 1 && *p == '0')
return true;
if (!((*p >= '1'&&*p <= '9') || (*p >= 'a'&&*p <= 'f') || (*p >= 'A'&&*p <= 'F')))
return false;
p++;
while (*p) {
if (!((*p >= '0'&&*p <= '9') || (*p >= 'a'&&*p <= 'f') || (*p >= 'A'&&*p <= 'F')))
return false;
p++;
}
return true;
}
inline void append_f32(stringc& s,float f) {
char tmp[32]{ 0 };
sprintf(tmp, "%.2f", f);
s.append(tmp);
}
inline bool _check_is_digital(const char* digit) {
const char *p = digit;
int len = strlen(p);
if (len == 1 && *p == '0')
return true;
if (*p == '-')
p++;
if (*p > '9' || *p < '1')
return false;
p++;
while (*p) {
if (*p > '9' || *p < '0')
return false;
p++;
}
return true;
}
inline bool _check_is_ip(const stringc& s) {
if (s.size() > 15)
return false;
char buf[16],*p = buf;
int dot_num = 4,num;
memset(buf, '.', 16);
s.copy(buf, 15);
while (dot_num)
{
char* h = p;
while (*p != '.')p++;
if (p - h > 3)
return false;
*p = 0;
if (!_check_is_digital(h))
return false;
num = atoi(h);
if (num < 0 || num > 255)
return false;
dot_num--;
p++;
}
return true;
}
inline std::string stringc_itoa(int value, int base) {
std::string buf;
// check that the base if valid
if (base < 2 || base > 16) return buf;
enum { kMaxDigits = 35 };
buf.reserve(kMaxDigits); // Pre-allocate enough space.
int quotient = value;
// Translating number to string with base:
do {
buf += "0123456789abcdef"[abs(quotient % base)];
quotient /= base;
} while (quotient);
// Append the negative sign
if (value < 0) buf += '-';
std::reverse(buf.begin(), buf.end());
return buf;
}
#endif