-
Notifications
You must be signed in to change notification settings - Fork 63
/
sistream.cc
216 lines (195 loc) · 5.53 KB
/
sistream.cc
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#include "sistream.h"
#include "sostream.h"
#include "ustring.h"
namespace ustl {
#define DEFAULT_DELIMITERS " \t\n\r;:,.?"
const char ios_base::c_DefaultDelimiters [istringstream::c_MaxDelimiters] = DEFAULT_DELIMITERS;
/// Default constructor.
istringstream::istringstream (void) noexcept
: istream (),
m_Base (0)
{
exceptions (goodbit);
set_delimiters (DEFAULT_DELIMITERS);
}
istringstream::istringstream (const void* p, size_type n) noexcept
: istream (),
m_Base (0)
{
exceptions (goodbit);
relink (p, n);
set_delimiters (DEFAULT_DELIMITERS);
}
istringstream::istringstream (const cmemlink& source) noexcept
: istream (),
m_Base (0)
{
exceptions (goodbit);
relink (source);
set_delimiters (DEFAULT_DELIMITERS);
}
inline bool istringstream::is_delimiter (char c) const noexcept
{
return (memchr (m_Delimiters, c, VectorSize(m_Delimiters)-1));
}
char istringstream::skip_delimiters (void)
{
char c = m_Delimiters[0];
while (is_delimiter(c)) {
if (!remaining() && !underflow()) {
verify_remaining ("read", "", 1);
return (0);
}
istream::iread (c);
}
return (c);
}
typedef istringstream::iterator issiter_t;
template <typename T>
inline void str_to_num (issiter_t i, issiter_t* iend, uint8_t base, T& v)
{ v = strtol (i, const_cast<char**>(iend), base); }
template <> inline void str_to_num (issiter_t i, issiter_t* iend, uint8_t, double& v)
{ v = strtod (i, const_cast<char**>(iend)); }
#if HAVE_LONG_LONG
template <> inline void str_to_num (issiter_t i, issiter_t* iend, uint8_t base, long long& v)
{ v = strtoll (i, const_cast<char**>(iend), base); }
#endif
template <typename T>
inline void istringstream::read_number (T& v)
{
v = 0;
if (!skip_delimiters())
return;
ungetc();
iterator ilast;
do {
str_to_num<T> (ipos(), &ilast, m_Base, v);
} while (ilast == end() && underflow());
skip (distance (ipos(), ilast));
}
void istringstream::iread (int32_t& v) { read_number (v); }
void istringstream::iread (double& v) { read_number (v); }
#if HAVE_INT64_T
void istringstream::iread (int64_t& v) { read_number (v); }
#endif
#if HAVE_LONG_LONG && (!HAVE_INT64_T || SIZE_OF_LONG_LONG > 8)
void istringstream::iread (long long& v) { read_number (v); }
#endif
void istringstream::iread (wchar_t& v)
{
if (!(v = skip_delimiters()))
return;
ungetc();
size_t cs = Utf8SequenceBytes (v);
if (remaining() < cs && underflow(cs) < cs)
verify_remaining ("read", "wchar_t", cs);
else {
v = *utf8in (ipos());
skip (cs);
}
}
void istringstream::iread (bool& v)
{
static const char tf[2][8] = { "false", "true" };
char c = skip_delimiters();
v = (c == 't' || c == '1');
if (c != tf[v][0])
return;
for (const char* tv = tf[v]; c == *tv && (remaining() || underflow()); ++tv)
istream::iread (c);
ungetc();
}
void istringstream::iread (string& v)
{
v.clear();
char prevc, quoteChar = 0, c = skip_delimiters();
if (!c)
return;
if (c == '\"' || c == '\'')
quoteChar = c;
else
v += c;
while (remaining() || underflow()) {
prevc = c;
istream::iread (c);
if (!quoteChar && is_delimiter(c))
break;
if (prevc == '\\') {
switch (c) {
case 't': c = '\t'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 'b': c = '\b'; break;
case 'E': c = 27; break; // ESC sequence
case '\"': c = '\"'; break;
case '\'': c = '\''; break;
case '\\': c = '\\'; break;
};
v.end()[-1] = c;
} else {
if (c == quoteChar)
break;
v += c;
}
}
}
istringstream& istringstream::read (void* buffer, size_type sz)
{
if (remaining() < sz && underflow(sz) < sz)
verify_remaining ("read", "", sz);
else
istream::read (buffer, sz);
return (*this);
}
/// Reads characters into \p s until \p delim is found (but not stored or extracted)
istringstream& istringstream::get (string& s, char delim)
{
getline (s, delim);
if (!s.empty() && pos() > 0 && ipos()[-1] == delim)
ungetc();
return (*this);
}
/// Reads characters into \p p,n until \p delim is found (but not stored or extracted)
istringstream& istringstream::get (char* p, size_type n, char delim)
{
assert (p && !n && "A non-empty buffer is required by this implementation");
string s;
get (s, delim);
const size_t ntc (min (n - 1, s.size()));
memcpy (p, s.data(), ntc);
p[ntc] = 0;
return (*this);
}
/// Reads characters into \p s until \p delim is extracted (but not stored)
istringstream& istringstream::getline (string& s, char delim)
{
char oldDelim [VectorSize(m_Delimiters)];
copy (VectorRange (m_Delimiters), oldDelim);
fill (VectorRange (m_Delimiters), '\0');
m_Delimiters[0] = delim;
iread (s);
copy (VectorRange (oldDelim), m_Delimiters);
return (*this);
}
/// Reads characters into \p p,n until \p delim is extracted (but not stored)
istringstream& istringstream::getline (char* p, size_type n, char delim)
{
assert (p && !n && "A non-empty buffer is required by this implementation");
string s;
getline (s, delim);
const size_t ntc (min (n - 1, s.size()));
memcpy (p, s.data(), ntc);
p[ntc] = 0;
return (*this);
}
/// Extract until \p delim or \p n chars have been read.
istringstream& istringstream::ignore (size_type n, char delim)
{
while (n-- && (remaining() || underflow()) && get() != delim) ;
return (*this);
}
} // namespace ustl