-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat_string.cc
More file actions
104 lines (90 loc) · 1.86 KB
/
format_string.cc
File metadata and controls
104 lines (90 loc) · 1.86 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
#include <vector>
#include <stdexcept>
#include "format_string.hh"
using namespace std;
static vector<string> do_splitting(const string& s)
{
string build;
vector<string> items;
// keep adding characters and building strings
const string::const_iterator e = s.end();
for( string::const_iterator i = s.begin(); i != e; ++i )
{
const char c = *i;
// end of current item
if( c == 0 )
{
items.push_back(build);
build.clear();
}
else
{
build += c;
}
}
// add remainder if reqd
if( ! build.empty() )
items.push_back(build);
return items;
}
static string do_formatting( const string& format,
const vector<string>& items )
{
string out;
// iterate over chars in format string
string::const_iterator i = format.begin();
const string::const_iterator e = format.end();
while( i != e )
{
const char c = *i;
if( c == '%' )
{
// turn %% into %
if( i+1 != e && *(i+1) == '%' )
{
out.push_back('%');
i += 2;
continue;
}
// get a number
string no;
i++;
while( i != e && *i >= '0' && *i <= '9' )
{
no.push_back(*i);
i++;
}
// didn't find one
if( no.empty() )
throw invalid_argument("Util::FormatString:"
" invalid character after '%'");
// convert number to int
istringstream s(no);
int index = -1;
s >> index;
index -= 1;
// add on the string to the build
if( index >= 0 && index < int(items.size()) )
{
out += items[index];
}
else
{
throw out_of_range("Util::FormatString: index out of range");
}
}
else
{
out.push_back(c);
i++;
}
}
return out;
}
void Util::FormatString::format()
{
// split up items
const vector<string> items = do_splitting( this->str() );
_output = do_formatting( _format, items );
_done_format = true;
}