-
Notifications
You must be signed in to change notification settings - Fork 43
/
strutil.cpp
166 lines (151 loc) · 4.79 KB
/
strutil.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
160
161
162
163
164
165
166
#include <stdarg.h>
#include "strutil.h"
namespace strutil {
template<> char *strsep(char **strp, const char *sep)
{
char *tok, *s;
if (!strp || !(tok = *strp))
return 0;
if ((s = std::strpbrk(tok, sep))) {
*s = 0;
*strp = s + 1;
} else
*strp = 0;
return tok;
}
template<> wchar_t *strsep(wchar_t **strp, const wchar_t *sep)
{
wchar_t *tok, *s;
if (!strp || !(tok = *strp))
return 0;
if ((s = std::wcspbrk(tok, sep))) {
*s = 0;
*strp = s + 1;
} else
*strp = 0;
return tok;
}
std::wstring &m2w(std::wstring &dst, const char *src, size_t srclen,
const std::codecvt<wchar_t, char, std::mbstate_t> &cvt)
{
typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt_t;
wchar_t buffer[0x100];
std::mbstate_t state = { 0 };
const char *pend = src + srclen, *pnext = src;
wchar_t *pwbegin = buffer,
*pwend = buffer + sizeof(buffer)/sizeof(buffer[0]),
*pwnext = pwbegin;
dst.clear();
while (src < pend) {
if (cvt.in(state, src, pend, pnext,
pwbegin, pwend, pwnext) == cvt_t::error)
throw std::runtime_error("conversion failed");
dst.append(pwbegin, pwnext - pwbegin);
pwnext = pwbegin;
src = pnext;
}
return dst;
}
std::string &w2m(std::string &dst, const wchar_t *src, size_t srclen,
const std::codecvt<wchar_t, char, std::mbstate_t> &cvt)
{
typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt_t;
char buffer[0x100];
std::mbstate_t state = { 0 };
const wchar_t *pwend = src + srclen, *pwnext = src;
char *pbegin = buffer,
*pend = buffer + sizeof(buffer),
*pnext = pbegin;
dst.clear();
while (src < pwend) {
if (cvt.out(state, src, pwend, pwnext,
pbegin, pend, pnext) == cvt_t::error)
throw std::runtime_error("conversion failed");
dst.append(pbegin, pnext - pbegin);
pnext = pbegin;
src = pwnext;
}
return dst;
}
#if defined(_MSC_VER) || defined(__MINGW32__)
#ifndef vsnprintf
#define vsnprintf _vsnprintf
#endif
#endif
std::string format(const char *fmt, ...)
{
va_list args;
std::vector<char> buffer(128);
va_start(args, fmt);
int rc = vsnprintf(&buffer[0], buffer.size(), fmt, args);
va_end(args);
if (rc >= 0 && rc < static_cast<int>(buffer.size()))
return std::string(&buffer[0], &buffer[rc]);
#if defined(_MSC_VER) || defined(__MINGW32__)
va_start(args, fmt);
rc = _vscprintf(fmt, args);
va_end(args);
if (rc < 0) {
// format failed
return "";
}
#endif
buffer.resize(rc + 1);
va_start(args, fmt);
rc = vsnprintf(&buffer[0], buffer.size(), fmt, args);
va_end(args);
return std::string(&buffer[0], &buffer[rc]);
}
#if defined(_MSC_VER) || defined(__MINGW32__)
std::wstring format(const wchar_t *fmt, ...)
{
va_list args;
va_start(args, fmt);
int rc = _vscwprintf(fmt, args);
va_end(args);
std::vector<wchar_t> buffer(rc + 1);
va_start(args, fmt);
rc = _vsnwprintf(&buffer[0], buffer.size(), fmt, args);
va_end(args);
return std::wstring(&buffer[0], &buffer[rc]);
}
#endif
/*
* NUMBER ::= [0-9]+
* TERM ::= NUMBER | NUMBER"-"NUMBER
* RANGES ::= TERM | RANGES","TERM
*/
bool parse_numeric_ranges(const wchar_t *s, std::vector<int> *nums,
int vmin, int vmax)
{
enum { NUMBER, TERM };
wchar_t *end;
std::vector<int> result;
int n, state = NUMBER;
do {
n = wcstoul(s, &end, 10);
if (end == s || n < vmin || n > vmax)
return false;
if (state == NUMBER)
result.push_back(n);
else if (result.back() > n)
return false;
else {
/* XXX: can consume HUGE memory depending on vmin and vmax */
for (int k = result.back() + 1; k <= n; ++k)
result.push_back(k);
}
if (*end == L',')
state = NUMBER;
else if (*end == L'-') {
if (state == TERM)
return false;
else
state = TERM;
} else if (*end)
return false;
} while (*end && (s = end + 1));
nums->swap(result);
return true;
}
}