The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
string_utils.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <[email protected]>
3  Copyright (C) 2005 - 2016 by Guillaume Melquiond <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #ifndef FORMULA_STRING_UTILS_HPP_INCLUDED
17 #define FORMULA_STRING_UTILS_HPP_INCLUDED
18 
20 #include <boost/assign.hpp>
21 
22 class variable_set;
23 
24 namespace utils {
25 
26 /**
27  * Determines if a string might contain variables to interpolate.
28  * This can allow one to skip future interpolations (plural -- if there is only
29  * one interpolation, the savings are not worth this check). In this spirit,
30  * precision is sacrificed in the name of efficiency; the check is quick and
31  * allows false positives, but there are no false negatives. (A false negative
32  * would lead to incorrect behavior, whereas a false positive leads to merely
33  * inefficient behavior.) In practice, false positives should be uncommon enough
34  * to not worry about.
35  */
36 inline bool might_contain_variables(const std::string &str)
37 { return str.find('$') != std::string::npos; }
38 
39 /**
40  * Function which will interpolate variables, starting with '$' in the string
41  * 'str' with the equivalent symbols in the given symbol table. If 'symbols'
42  * is nullptr, then game event variables will be used instead.
43  */
44 std::string interpolate_variables_into_string(const std::string &str, const string_map * const symbols);
46 
47 /**
48  * Function that does the same as the above, for t_stringS.
49  * If a change was made, then the t_string's translation is done in this
50  * function, rather than at use. This limitation is due to the use of
51  * create-time interpolation, rather than use-time.
52  */
54 
55 }
56 /// An alias for boost::assign::map_list_of<std::string, std::string>
57 inline boost::assign_detail::generic_list< std::pair
58  <
61  > >
63 {
64  return boost::assign::map_list_of<std::string, std::string>(k, v);
65 }
66 
67 /** Handy wrappers around interpolate_variables_into_string and gettext. */
68 std::string vgettext(const char* msgid, const utils::string_map& symbols);
69 std::string vgettext(const char* domain
70  , const char* msgid
71  , const utils::string_map& symbols);
72 
73 std::string vngettext(const char*, const char*, int, const utils::string_map&);
74 
75 /**
76  * @todo Convert all functions.
77  *
78  * All function in this file should have an overloaded version with a domain
79  * and probably convert all callers to use the macro instead of directly calling
80  * the function.
81  */
82 
83 #ifdef GETTEXT_DOMAIN
84 #define VGETTEXT(msgid, symbols) vgettext(GETTEXT_DOMAIN, msgid, symbols)
85 #else
86 #define VGETTEXT(msgid, symbols) vgettext(msgid, symbols)
87 #endif
88 
89 #endif
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
std::string vgettext(const char *msgid, const utils::string_map &symbols)
Handy wrappers around interpolate_variables_into_string and gettext.
boost::assign_detail::generic_list< std::pair< boost::assign_detail::assign_decay< std::string >::type, boost::assign_detail::assign_decay< std::string >::type > > string_map_of(const std::string &k, const std::string &v)
An alias for boost::assign::map_list_of
std::map< std::string, t_string > string_map
const GLdouble * v
Definition: glew.h:1359
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
bool might_contain_variables(const std::string &str)
Determines if a string might contain variables to interpolate.
t_string interpolate_variables_into_tstring(const t_string &tstr, const variable_set &variables)
Function that does the same as the above, for t_stringS.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::string vngettext(const char *, const char *, int, const utils::string_map &)