string_format.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 
30 #pragma once
31 
32 #include "../api_core.h"
33 #include <vector>
34 
35 namespace clan
36 {
39 
41 class CL_API_CORE StringFormat
42 {
45 
46 public:
47 
51  StringFormat(const std::string &format_string);
52 
53  ~StringFormat();
54 
58 
59 public:
60  const std::string &get_result() const;
61 
65 
66 public:
67 
72  void set_arg(int index, const std::string &text);
73 
79  void set_arg(int index, int value, int min_length = 0);
80 
86  void set_arg(int index, unsigned int value, int min_length = 0);
87 
93  void set_arg(int index, long unsigned int value, int min_length = 0);
94 
100  void set_arg(int index, long long value, int min_length = 0);
101 
107  void set_arg(int index, unsigned long long value, int min_length = 0);
108 
113  void set_arg(int index, float value);
114 
119  void set_arg(int index, double value);
120 
124 
125 private:
126 
132  void create_arg(int index, int start, int length);
133 
134  std::string string;
135 
136  struct ArgPosition
137  {
138  ArgPosition() : start(0), length(-1) { }
139  ArgPosition(int s, int l) : start(s), length(l) {}
140  int start;
141  int length;
142  };
143 
144  std::vector<ArgPosition> args;
146 };
147 
148 inline std::string string_format(const std::string &format)
149 { return format; }
150 
151 template <class Arg1>
152 std::string string_format(const std::string &format, Arg1 arg1)
153 { StringFormat f(format); f.set_arg(1, arg1); return f.get_result(); }
154 
155 template <class Arg1, class Arg2>
156 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2)
157 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); return f.get_result(); }
158 
159 template <class Arg1, class Arg2, class Arg3>
160 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
161 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); return f.get_result(); }
162 
163 template <class Arg1, class Arg2, class Arg3, class Arg4>
164 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
165 { 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(); }
166 
167 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
168 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
169 { 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(); }
170 
171 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
172 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
173 { 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(); }
174 
175 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
176 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
177 { 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(); }
178 
179 }
180 
void set_arg(int index, const std::string &text)
Set arg.
std::string string_format(const std::string &format)
Definition: string_format.h:148
ArgPosition(int s, int l)
Definition: string_format.h:139
int start
Definition: string_format.h:140
int length
Definition: string_format.h:141
String formatting class.
Definition: string_format.h:41
const std::string & get_result() const
ArgPosition()
Definition: string_format.h:138