string_format.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore 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 UICore 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 #include <string>
33 
34 namespace uicore
35 {
69  {
70  public:
74  StringFormat(const std::string &format_string);
75 
77  const std::string &result() const;
78 
83  void set_arg(int index, const std::string &text);
84 
90  void set_arg(int index, int value, int min_length = 0);
91 
97  void set_arg(int index, unsigned int value, int min_length = 0);
98 
104  void set_arg(int index, long long value, int min_length = 0);
105 
111  void set_arg(int index, unsigned long long value, int min_length = 0);
112 
117  void set_arg(int index, float value);
118 
123  void set_arg(int index, double value);
124 
125  private:
131  void create_arg(int index, int start, int length);
132 
133  std::string string;
134 
135  struct ArgPosition
136  {
137  ArgPosition() : start(0), length(-1) { }
138  ArgPosition(int s, int l) : start(s), length(l) {}
139  int start;
140  int length;
141  };
142 
143  std::vector<ArgPosition> args;
144  };
145 
147  inline std::string string_format(const std::string &format)
148  {
149  return format;
150  }
151 
153  template <class Arg1>
154  std::string string_format(const std::string &format, Arg1 arg1)
155  {
156  StringFormat f(format); f.set_arg(1, arg1); return f.result();
157  }
158 
160  template <class Arg1, class Arg2>
161  std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2)
162  {
163  StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); return f.result();
164  }
165 
167  template <class Arg1, class Arg2, class Arg3>
168  std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
169  {
170  StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); return f.result();
171  }
172 
174  template <class Arg1, class Arg2, class Arg3, class Arg4>
175  std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
176  {
177  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.result();
178  }
179 
181  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
182  std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
183  {
184  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.result();
185  }
186 
188  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
189  std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
190  {
191  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.result();
192  }
193 
195  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
196  std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
197  {
198  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.result();
199  }
200 }
std::string string_format(const std::string &format)
See uicore::StringFormat for details.
Definition: string_format.h:147
StringFormat(const std::string &format_string)
Constructs a formatted string object.
void set_arg(int index, const std::string &text)
Sets an argument (string version)
String formatting class.
Definition: string_format.h:68
const std::string & result() const
Retrieves the formatted string with all argument replacements.
Definition: Application/application.h:35