1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2004-2011 by The Allacrost Project
3 // Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 // All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See https://www.gnu.org/copyleft/gpl.html for details.
9 ///////////////////////////////////////////////////////////////////////////////
10
11 /** ***************************************************************************(
12 *** \file utils_strings.h
13 *** \author Tyler Olsen, roots@allacrost.org
14 *** \author Yohann Ferreira, yohann ferreira orange fr
15 *** \brief Header file for the utility code.
16 ***
17 *** This code includes various utility functions that are used for strings
18 *** manipulation.
19 *** ***************************************************************************/
20
21 #ifndef __UTILS_STRINGS_HEADER__
22 #define __UTILS_STRINGS_HEADER__
23
24 #include <string>
25 #include <sstream>
26
27 //! Contains utility code used across the entire source code
28 namespace vt_utils
29 {
30
31 //! A static empty string, used when needed to return a const reference to
32 //! that kind of data.
33 static const std::string _empty_string;
34
35 //! Determines whether the code in the vt_utils namespace should print debug statements or not.
36 extern bool UTILS_DEBUG;
37
38 //! \brief Returns the uppercased version of a string
39 std::string Upcase(std::string text);
40
41 //! \brief Returns the string with the first letter uppercased.
42 std::string UpcaseFirst(std::string text);
43
44 /** \brief A safe version of sprintf that returns a std::string of the result.
45 *** Copyright The Mana Developers (2012) - GPLv2
46 */
47 std::string strprintf(char const *, ...)
48 #ifdef __GNUC__
49 // This attribute is nice: it even works through gettext invokation. For
50 // example, gcc will complain that strprintf(_("%s"), 42) is ill-formed.
51 __attribute__((__format__(__printf__, 1, 2)))
52 #endif
53 ;
54
55 //! \name String Utility Functions
56 //@{
57 /** \brief Converts an integer type into a standard string
58 *** \param T The integer type to convert to a string
59 *** \return A std::string containing the parameter in string form
60 **/
61 template <typename T>
NumberToString(const T t)62 std::string NumberToString(const T t)
63 {
64 std::ostringstream text("");
65 text << static_cast<int32_t>(t);
66 return text.str();
67 }
68
69 //! Specialization for a float type
70 //! declared as inline to avoid linker errors.
71 template <>
72 inline std::string NumberToString<float>(const float t)
73 {
74 std::ostringstream text("");
75 text << t;
76 return text.str();
77 }
78
79 /** \brief Determines if a string is a valid numeric string
80 *** \param text The string to check
81 *** \return A std::string containing the parameter in string form
82 ***
83 *** This function will accept strings with + or - characters as the first
84 *** string element and strings including a single decimal point.
85 *** Examples of valid numeric strings are: "50", ".2350", "-252.5"
86 **/
87 bool IsStringNumeric(const std::string &text);
88 //@}
89
90 } // namespace vt_utils
91
92 #endif // __UTILS_STRINGS_HEADER__
93