The Battle for Wesnoth  1.17.0-dev
string_utils.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 - 2021
3  by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #pragma once
18 
19 // Need this to get the default GETTEXT_DOMAIN for VGETTEXT/VNGETTEXT
20 #include "gettext.hpp"
21 
23 
24 #include <ctime>
25 
26 class variable_set;
27 
28 namespace utils {
29 
30  namespace detail {
31  extern std::string(* evaluate_formula)(const std::string& formula);
32  }
33 
34 /**
35  * Determines if a string might contain variables to interpolate.
36  * This can allow one to skip future interpolations (plural -- if there is only
37  * one interpolation, the savings are not worth this check). In this spirit,
38  * precision is sacrificed in the name of efficiency; the check is quick and
39  * allows false positives, but there are no false negatives. (A false negative
40  * would lead to incorrect behavior, whereas a false positive leads to merely
41  * inefficient behavior.) In practice, false positives should be uncommon enough
42  * to not worry about.
43  */
44 inline bool might_contain_variables(const std::string &str)
45 { return str.find('$') != std::string::npos; }
46 
47 /**
48  * Function which will interpolate variables, starting with '$' in the string
49  * 'str' with the equivalent symbols in the given symbol table. If 'symbols'
50  * is nullptr, then game event variables will be used instead.
51  */
52 std::string interpolate_variables_into_string(const std::string &str, const string_map * const symbols);
53 std::string interpolate_variables_into_string(const std::string &str, const std::map<std::string,std::string> * const symbols);
54 std::string interpolate_variables_into_string(const std::string &str, const variable_set& variables);
55 
56 /**
57  * Function that does the same as the above, for t_stringS.
58  * If a change was made, then the t_string's translation is done in this
59  * function, rather than at use. This limitation is due to the use of
60  * create-time interpolation, rather than use-time.
61  */
63 
64 /**
65  * Format a conjunctive list.
66  * @param empty The string to return for an empty list
67  * @param elems The list of entries in the list
68  * @return The elements of the list joined by "and".
69  */
70 std::string format_conjunct_list(const t_string& empty, const std::vector<t_string>& elems);
71 
72 /**
73  * Format a disjunctive list.
74  * @param empty The string to return for an empty list
75  * @param elems The list of entries in the list
76  * @return The elements of the list joined or "and".
77  */
78 std::string format_disjunct_list(const t_string& empty, const std::vector<t_string>& elems);
79 
80 /**
81  * Formats a timespan into human-readable text.
82  * @param time The timespan in seconds.
83  * @return A string such as "6 days, 12 hours, 4 minutes, 13 seconds". Years,
84  * months and weeks are also considered.
85  */
86 std::string format_timespan(std::time_t time);
87 }
88 
89 /**
90  * Implementation functions for the VGETTEXT and VNGETTEXT macros.
91  *
92  * DO NOT USE DIRECTLY unless you really know what you're doing.
93  * See https://github.com/wesnoth/wesnoth/issues/2716 for more info.
94  */
95 
96 std::string vgettext_impl(const char* domain, const char* msgid, const utils::string_map& symbols);
97 
98 std::string vngettext_impl(const char* domain,
99  const char* singular,
100  const char* plural,
101  int count,
102  const utils::string_map& symbols);
103 
104 /**
105  * Handy wrappers around interpolate_variables_into_string and gettext.
106  *
107  * These should cover most usecases. If you're not sure whether you want
108  * these macros or their implementation functions, use these. The only time
109  * you should need to use the implementation functions directly is to pass a
110  * different textdomain than the current value of GETTEXT_DOMAIN.
111  */
112 
113 #define VGETTEXT(msgid, ...) \
114  vgettext_impl(GETTEXT_DOMAIN, msgid, __VA_ARGS__)
115 
116 #define VNGETTEXT(msgid, msgid_plural, count, ...) \
117  vngettext_impl(GETTEXT_DOMAIN, msgid, msgid_plural, count, __VA_ARGS__)
std::string format_timespan(std::time_t time)
Formats a timespan into human-readable text.
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 format_disjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a disjunctive list.
std::string vgettext_impl(const char *domain, const char *msgid, const utils::string_map &symbols)
Implementation functions for the VGETTEXT and VNGETTEXT macros.
std::string(* evaluate_formula)(const std::string &formula)
std::string vngettext_impl(const char *domain, const char *singular, const char *plural, int count, const utils::string_map &symbols)
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.