This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
string-format.cc
339 lines (319 loc) · 7.7 KB
/
string-format.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Copyright © 2009-2017 Jakub Wilk <[email protected]>
* Copyright © 2009 Mateusz Turcza
*
* pdf2djvu is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* pdf2djvu is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "string-format.hh"
#include <climits>
#include <iomanip>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "i18n.hh"
#include "string-printf.hh"
namespace string_format
{
class StaticChunk : public Chunk
{
protected:
std::string value;
public:
StaticChunk(const std::string &value)
: value(value)
{ }
virtual ~StaticChunk()
{ }
virtual void format(const Bindings &bindings, std::ostream &stream) const
{
stream << this->value;
}
};
class VariableChunk : public Chunk
{
protected:
std::string variable;
int offset;
unsigned int width;
bool auto_width;
bool pad_0;
public:
explicit VariableChunk(const std::string &);
virtual ~VariableChunk()
{ }
virtual void format(const Bindings &, std::ostream &) const;
};
class ValueError : public std::domain_error
{
public:
ValueError(const std::string &what)
: std::domain_error(what)
{ }
};
class ValueTypeError : public ValueError
{
public:
ValueTypeError(const std::string &what)
: ValueError(what)
{ }
};
class FormatError : public std::runtime_error
{
public:
FormatError(const std::string &var, const std::string &what)
: std::runtime_error(string_printf(_("Unable to format field {%s}: %s"), var.c_str(), what.c_str()))
{ }
};
}
string_format::Value string_format::Bindings::get(const std::string &value) const
{
const_iterator it = this->find(value);
if (it == this->end())
throw ValueError(_("no such variable"));
return it->second;
}
string_format::VariableChunk::VariableChunk(const std::string &description)
: offset(0), width(0), auto_width(false), pad_0(false)
{
enum
{
NAME,
OFFSET_1,
OFFSET_2,
WIDTH_1,
WIDTH_2,
END
} mode = NAME;
std::string::const_iterator it = description.begin();
int offset_sign = 1;
while (it != description.end())
{
switch (mode)
{
case NAME:
if (*it == '+' || *it == '-' || *it == ':')
{
this->variable = description.substr(0, it - description.begin());
if (*it == ':')
mode = WIDTH_1;
else
{
offset_sign = (*it == '+') ? +1 : -1;
mode = OFFSET_1;
}
}
break;
case OFFSET_1:
if (*it >= '0' && *it <= '9')
{
this->offset = *it - '0';
mode = OFFSET_2;
}
else
throw string_format::ParseError();
break;
case OFFSET_2:
if (*it >= '0' && *it <= '9')
{
if (this->offset > (INT_MAX - 9) / 10)
throw string_format::FormatError(this->variable, _("integer overflow"));
this->offset = this->offset * 10 + (*it - '0');
}
else if (*it == ':')
mode = WIDTH_1;
else
throw string_format::ParseError();
break;
case WIDTH_1:
if (*it == '0')
this->pad_0 = true;
else if (*it >= '0' && *it <= '9')
this->width = *it - '0';
else
throw string_format::ParseError();
mode = WIDTH_2;
break;
case WIDTH_2:
if (*it >= '0' && *it <= '9')
{
if (this->width > (UINT_MAX - 9) / 10)
throw string_format::FormatError(this->variable, _("integer overflow"));
this->width = this->width * 10 + (*it - '0');
}
else if (*it == '*')
{
this->auto_width = true;
mode = END;
}
else
throw string_format::ParseError();
break;
case END:
throw string_format::ParseError();
}
it++;
}
if (mode == NAME)
this->variable = description;
this->offset *= offset_sign;
}
unsigned int string_format::Value::as_int(int offset)
{
if (!this->is_int)
throw string_format::ValueTypeError(_("type error: expected number, not string"));
if (offset < 0)
{
unsigned int uoffset = -offset;
if (uoffset > this->v_uint)
return 0;
else
return this->v_uint - uoffset;
}
else
{
unsigned int uoffset = offset;
if (this->v_uint + uoffset >= this->v_uint)
return this->v_uint + uoffset;
else
throw string_format::ValueError(_("integer overflow"));
}
}
const std::string & string_format::Value::as_string()
{
if (this->is_int)
throw string_format::ValueTypeError(_("type error: expected string, not number"));
return this->v_string;
}
void string_format::VariableChunk::format(const Bindings &bindings, std::ostream &stream) const
try
{
Value value = bindings.get(this->variable);
unsigned int width = this->width;
if (this->auto_width)
{
unsigned int max_value;
try
{
max_value = bindings.get("max_" + this->variable).as_int(this->offset);
}
catch (const string_format::ValueError &)
{
throw FormatError(this->variable, _("unknown maximum width"));
}
unsigned int max_digits = 0;
while (max_value > 0)
{
max_digits++;
max_value /= 10;
}
if (max_digits > width)
width = max_digits;
}
stream
<< std::setfill(this->pad_0 ? '0' : ' ')
<< std::setw(width);
try
{
stream << value.as_int(this->offset);
}
catch (const string_format::ValueTypeError &)
{
if (this->offset != 0)
throw;
stream << value.as_string();
}
}
catch (const string_format::ValueError &exc)
{
throw FormatError(this->variable, exc.what());
}
string_format::Template::Template(const std::string &source)
{
enum
{
TEXT,
KET,
FORMAT_1,
FORMAT_2
} mode = TEXT;
std::string::const_iterator left = source.begin();
std::string::const_iterator right = left;
while (right != source.end())
{
switch (mode)
{
case TEXT:
if (*right == '{' || *right == '}')
{
if (left != right)
{
std::string text = source.substr(left - source.begin(), right - left);
this->chunks.push_back(new StaticChunk(text));
}
left = right + 1;
mode = *right == '}' ? KET : FORMAT_1;
}
break;
case KET:
if (*right == '}')
{
left = right;
mode = TEXT;
}
else
throw string_format::ParseError();
break;
case FORMAT_1:
if (*right == '{')
{
left = right;
mode = TEXT;
}
else
mode = FORMAT_2;
break;
case FORMAT_2:
if (*right == '}')
{
std::string text = source.substr(left - source.begin(), right - left);
this->chunks.push_back(new VariableChunk(text));
left = right + 1;
mode = TEXT;
}
break;
}
right++;
}
if (mode != TEXT)
throw string_format::ParseError();
if (left != source.end())
{
std::string text = source.substr(left - source.begin(), std::string::npos);
this->chunks.push_back(new StaticChunk(text));
}
}
string_format::Template::~Template()
{
for (auto &chunk : this->chunks)
delete chunk;
}
void string_format::Template::format(const Bindings &bindings, std::ostream &stream) const
{
for (const Chunk* chunk : this->chunks)
(*chunk).format(bindings, stream);
}
std::string string_format::Template::format(const Bindings &bindings) const
{
std::ostringstream stream;
this->format(bindings, stream);
return stream.str();
}
// vim:ts=2 sts=2 sw=2 et