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