The Battle for Wesnoth  1.13.11+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 <dave@whitevine.net>
3  Copyright (C) 2005 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
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 #pragma once
17 
19 
20 class variable_set;
21 
22 namespace utils {
23 
24  namespace detail {
25  extern std::string(* evaluate_formula)(const std::string& formula);
26  }
27 
28 /**
29  * Determines if a string might contain variables to interpolate.
30  * This can allow one to skip future interpolations (plural -- if there is only
31  * one interpolation, the savings are not worth this check). In this spirit,
32  * precision is sacrificed in the name of efficiency; the check is quick and
33  * allows false positives, but there are no false negatives. (A false negative
34  * would lead to incorrect behavior, whereas a false positive leads to merely
35  * inefficient behavior.) In practice, false positives should be uncommon enough
36  * to not worry about.
37  */
38 inline bool might_contain_variables(const std::string &str)
39 { return str.find('$') != std::string::npos; }
40 
41 /**
42  * Function which will interpolate variables, starting with '$' in the string
43  * 'str' with the equivalent symbols in the given symbol table. If 'symbols'
44  * is nullptr, then game event variables will be used instead.
45  */
46 std::string interpolate_variables_into_string(const std::string &str, const string_map * const symbols);
47 std::string interpolate_variables_into_string(const std::string &str, const std::map<std::string,std::string> * const symbols);
49 
50 /**
51  * Function that does the same as the above, for t_stringS.
52  * If a change was made, then the t_string's translation is done in this
53  * function, rather than at use. This limitation is due to the use of
54  * create-time interpolation, rather than use-time.
55  */
57 
58 /**
59  * Format a conjunctive list.
60  * @param empty The string to return for an empty list
61  * @param elems The list of entries in the list
62  * @return The elements of the list joined by "and".
63  */
64 std::string format_conjunct_list(const t_string& empty, const std::vector<t_string>& elems);
65 
66 /**
67  * Format a disjunctive list.
68  * @param empty The string to return for an empty list
69  * @param elems The list of entries in the list
70  * @return The elements of the list joined or "and".
71  */
72 std::string format_disjunct_list(const t_string& empty, const std::vector<t_string>& elems);
73 
74 }
75 
76 /** Handy wrappers around interpolate_variables_into_string and gettext. */
77 std::string vgettext(const char* msgid, const utils::string_map& symbols);
78 std::string vgettext(const char* domain
79  , const char* msgid
80  , const utils::string_map& symbols);
81 
82 std::string vngettext(const char*, const char*, int, const utils::string_map&);
83 
84 std::string vngettext(const char*, const char*, const char*, int, const utils::string_map&);
85 
86 /**
87  * @todo Convert all functions.
88  *
89  * All function in this file should have an overloaded version with a domain
90  * and probably convert all callers to use the macro instead of directly calling
91  * the function.
92  */
93 
94 #ifdef GETTEXT_DOMAIN
95 #define VGETTEXT(msgid, ...) vgettext(GETTEXT_DOMAIN, msgid, __VA_ARGS__)
96 #define VNGETTEXT(msgid, msgid_plural, count, ...) vngettext(GETTEXT_DOMAIN, msgid, msgid_plural, count, __VA_ARGS__)
97 #else
98 #define VGETTEXT(msgid, ...) vgettext(msgid, __VA_ARGS__)
99 #define VNGETTEXT(msgid, msgid_plural, count, ...) vngettext(msgid, msgid_plural, count, __VA_ARGS__)
100 #endif
std::string format_conjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a conjunctive list.
std::vector< char_t > string
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::map< std::string, t_string > string_map
std::string vgettext(const char *msgid, const utils::string_map &symbols)
Handy wrappers around interpolate_variables_into_string and gettext.
std::string format_disjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a disjunctive list.
std::string(* evaluate_formula)(const std::string &formula)
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
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.
std::string vngettext(const char *, const char *, int, const utils::string_map &)