string_format.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28
29 #pragma once
30
31 #include <vector>
32
33 namespace clan
34 {
37
70 class StringFormat
71 {
74
75 public:
76
80 StringFormat(const std::string &format_string);
81
82 ~StringFormat();
83
87
88 public:
90 const std::string &get_result() const;
91
95
96 public:
97
102 void set_arg(int index, const std::string &text);
103
109 void set_arg(int index, int value, int min_length = 0);
110
116 void set_arg(int index, unsigned int value, int min_length = 0);
117
123 void set_arg(int index, long unsigned int value, int min_length = 0);
124
130 void set_arg(int index, long long value, int min_length = 0);
131
137 void set_arg(int index, unsigned long long value, int min_length = 0);
138
143 void set_arg(int index, float value);
144
149 void set_arg(int index, double value);
150
154
155 private:
156
162 void create_arg(int index, int start, int length);
163
164 std::string string;
165
166 struct ArgPosition
167 {
168 ArgPosition() : start(0), length(-1) { }
169 ArgPosition(int s, int l) : start(s), length(l) {}
170 int start;
171 int length;
172 };
173
174 std::vector<ArgPosition> args;
176 };
177
179 inline std::string string_format(const std::string &format)
180 { return format; }
181
183 template <class Arg1>
184 std::string string_format(const std::string &format, Arg1 arg1)
185 { StringFormat f(format); f.set_arg(1, arg1); return f.get_result(); }
186
188 template <class Arg1, class Arg2>
189 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2)
190 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); return f.get_result(); }
191
193 template <class Arg1, class Arg2, class Arg3>
194 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
195 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); return f.get_result(); }
196
198 template <class Arg1, class Arg2, class Arg3, class Arg4>
199 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
200 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); return f.get_result(); }
201
203 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
204 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
205 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); return f.get_result(); }
206
208 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
209 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
210 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); f.set_arg(6, arg6); return f.get_result(); }
211
213 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
214 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
215 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); f.set_arg(6, arg6); f.set_arg(7, arg7); return f.get_result(); }
216
217 }
218
clan
Definition: clanapp.h:35
clan::Key::s
clan::Key::l
clan::StringFormat::set_arg
void set_arg(int index, const std::string &text)
Sets an argument (string version)
clan::string_format
std::string string_format(const std::string &format)
See clan::StringFormat for details.
Definition: string_format.h:179
clan::StringFormat::ArgPosition::ArgPosition
ArgPosition(int s, int l)
Definition: string_format.h:169
clan::StyleTokenType::string
@ keyword
clan::Key::f
clan::StyleValueType::length
value is a keyword
clan::StringFormat::ArgPosition::start
int start
Definition: string_format.h:170
clan::StringFormat::ArgPosition::length
int length
Definition: string_format.h:171
clan::StringFormat
String formatting class.
Definition: string_format.h:70
clan::StringFormat::~StringFormat
~StringFormat()
clan::StringFormat::get_result
const std::string & get_result() const
Retrieves the formatted string with all argument replacements.
clan::StringFormat::ArgPosition::ArgPosition
ArgPosition()
Definition: string_format.h:168
clan::StringFormat::StringFormat
StringFormat(const std::string &format_string)
Constructs a formatted string object.