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