string_format.h Source File - ClanLib SDK
Description: string_format.h Source File

string_format.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
33namespace clan
34{
37
70 class StringFormat
71 {
72 public:
76 StringFormat(const std::string &format_string);
77 ~StringFormat();
78
80 const std::string &get_result() const;
81
86 void set_arg(int index, const std::string &text);
87
93 void set_arg(int index, int value, int min_length = 0);
94
100 void set_arg(int index, unsigned int value, int min_length = 0);
101
107 void set_arg(int index, long unsigned int value, int min_length = 0);
108
114 void set_arg(int index, long long value, int min_length = 0);
115
121 void set_arg(int index, unsigned long long value, int min_length = 0);
122
127 void set_arg(int index, float value);
128
133 void set_arg(int index, double value);
134
135 private:
141 void create_arg(int index, int start, int length);
142
143 std::string string;
144
145 struct ArgPosition
146 {
147 ArgPosition() : start(0), length(-1) { }
148 ArgPosition(int s, int l) : start(s), length(l) {}
149 int start;
150 int length;
151 };
152
153 std::vector<ArgPosition> args;
154 };
155
157 inline std::string string_format(const std::string &format)
158 {
159 return format;
160 }
161
163 template <class Arg1>
164 std::string string_format(const std::string &format, Arg1 arg1)
165 {
166 StringFormat f(format); f.set_arg(1, arg1); return f.get_result();
167 }
168
170 template <class Arg1, class Arg2>
171 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2)
172 {
173 StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); return f.get_result();
174 }
175
177 template <class Arg1, class Arg2, class Arg3>
178 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
179 {
180 StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); return f.get_result();
181 }
182
184 template <class Arg1, class Arg2, class Arg3, class Arg4>
185 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
186 {
187 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();
188 }
189
191 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
192 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
193 {
194 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();
195 }
196
198 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
199 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
200 {
201 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();
202 }
203
205 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
206 std::string string_format(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
207 {
208 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();
209 }
210
212}
clan::StringFormat
String formatting class.
Definition string_format.h:71
clan::StringFormat::set_arg
void set_arg(int index, unsigned int value, int min_length=0)
Sets an argument (unsigned int version)
clan::StringFormat::set_arg
void set_arg(int index, float value)
Sets an argument (float version)
clan::StringFormat::~StringFormat
~StringFormat()
clan::StringFormat::set_arg
void set_arg(int index, double value)
Sets an argument (double version)
clan::StringFormat::set_arg
void set_arg(int index, long unsigned int value, int min_length=0)
Sets an argument (long unsigned int version)
clan::StringFormat::set_arg
void set_arg(int index, int value, int min_length=0)
Sets an argument (int version)
clan::StringFormat::StringFormat
StringFormat(const std::string &format_string)
Constructs a formatted string object.
clan::StringFormat::set_arg
void set_arg(int index, unsigned long long value, int min_length=0)
Sets an argument (unsigned long long version)
clan::StringFormat::get_result
const std::string & get_result() const
Retrieves the formatted string with all argument replacements.
clan::StringFormat::set_arg
void set_arg(int index, const std::string &text)
Sets an argument (string version)
clan::StringFormat::set_arg
void set_arg(int index, long long value, int min_length=0)
Sets an argument (long long version)
clan::string_format
std::string string_format(const std::string &format)
See clan::StringFormat for details.
Definition string_format.h:157
clan
Definition clanapp.h:36
clan::StyleValueType::length
@ length
value is a keyword
Definition style_value_type.h:38
clan::Key::s
@ s
Definition keys.h:99
clan::Key::l
@ l
Definition keys.h:92
clan::Key::f
@ f
Definition keys.h:86