Spinning Topp Logo BlackTopp Studios
inc
stringtool.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _stringtool_h
41 #define _stringtool_h
42 
43 #ifndef SWIG
44  #include "Input/metacode.h"
45 #endif
46 #include "vector2.h"
47 #include "vector3.h"
48 #include "quaternion.h"
49 #include "colourvalue.h"
50 #include "countedptr.h"
51 
52 namespace Mezzanine
53 {
54  ///////////////////////////////////////////////////////////////////////////////
55  /// @brief This is a utility to help perform all kinds of string related tasks.
56  /// @details
57  ///////////////////////////////////////
58  namespace StringTools
59  {
60  ///////////////////////////////////////////////////////////////////////////////
61  // Globals, mostly used for comparison and such
62 
63  /// @brief Convenience String for comparing to return blank Strings where necessary.
64  const String Blank = "";
65 
66  ///////////////////////////////////////////////////////////////////////////////
67  // Character Manipulation and checks
68 
69  /// @brief Checks if a character is a decimal digit.
70  /// @param ToCheck The character to be checked.
71  /// @return Returns true if the character is a decimal digit, false otherwise.
72  Boole MEZZ_LIB IsDigit(const Char8 ToCheck);
73  /// @brief Checks if a character is a lower-case alphabetic letter.
74  /// @param ToCheck The character to be checked.
75  /// @return Returns true if the character is a lower-case letter, false otherwise.
76  Boole MEZZ_LIB IsLowerAlphaLetter(const Char8 ToCheck);
77  /// @brief Checks if a character is a upper-case alphabetic letter.
78  /// @param ToCheck The character to be checked.
79  /// @return Returns true if the character is a upper-case letter, false otherwise.
80  Boole MEZZ_LIB IsUpperAlphaLetter(const Char8 ToCheck);
81  /// @brief Checks if a character is a lower or upper case letter.
82  /// @param ToCheck The character to be checked.
83  /// @return Returns true if the character is a lower or upper case letter, false otherwise.
84  Boole MEZZ_LIB IsAlphaLetter(const Char8 ToCheck);
85  /// @brief Checks if a character is a lower-case hexadecimal letter.
86  /// @param ToCheck The character to be checked.
87  /// @return Returns true if the character is a lower-case letter, false otherwise.
88  Boole MEZZ_LIB IsLowerHexLetter(const Char8 ToCheck);
89  /// @brief Checks if a character is a upper-case hexadecimal letter.
90  /// @param ToCheck The character to be checked.
91  /// @return Returns true if the character is a upper-case letter, false otherwise.
92  Boole MEZZ_LIB IsUpperHexLetter(const Char8 ToCheck);
93  /// @brief Checks if a character is a hexadecimal letter.
94  /// @param ToCheck The character to be checked.
95  /// @return Returns true if the character is a hexadecimal letter, false otherwise.
96  Boole MEZZ_LIB IsHexLetter(const Char8 ToCheck);
97  /// @brief Checks if a character is a hexadecimal digit.
98  /// @param ToCheck The character to be checked.
99  /// @return Returns true if the character is a hexadecimal digit, false otherwise.
100  Boole MEZZ_LIB IsHexDigit(const Char8 ToCheck);
101  /// @brief Checks if a character is a letter or digit.
102  /// @param ToCheck The character to be checked.
103  /// @return Returns true if the character is a letter or digit, false otherwise.
104  Boole MEZZ_LIB IsAlphanumeric(const Char8 ToCheck);
105 
106  ///////////////////////////////////////////////////////////////////////////////
107  // String Manipulation and Checks
108 
109  /// @brief Trims all whitespaces and tabs from a one or both sides of a String.
110  /// @param Source The original string to be trimmed.
111  /// @param Left Whether or not to trim the left side of the String.
112  /// @param Right Whether or not to trim the right side of the String.
113  void MEZZ_LIB Trim(String& Source, Boole Left = true, Boole Right = true);
114  /// @brief Splits a string into multiple substrings based on the specified delimiters.
115  /// @param Source The String to be split.
116  /// @param Delims The characters to look for and use as split points in the source String.
117  /// @param MaxSplits The maximum number of splits to perform on this String. Value of zero means unlimited splits.
118  /// @return Returns a vector containing all the substrings generated from the source String.
119  StringVector MEZZ_LIB Split(const String& Source, const String& Delims = " \t\n", const Whole MaxSplits = 0);
120  /// @brief Converts all lower case characters in a string to their respective upper case.
121  /// @param Source The String to be converted.
122  void MEZZ_LIB ToUpperCase(String& Source);
123  /// @brief Create a copy of the String that is upper case.
124  /// @param Source The original String to copy.
125  /// @return Returns a copy of the String that has been modified to be in all upper case.
127  /// @brief Converts all upper case characters in a string to their respective lower case.
128  /// @param Source The String to be converted.
129  void MEZZ_LIB ToLowerCase(String& Source);
130  /// @brief Create a copy of the String that is lower case.
131  /// @param Source The original String to copy.
132  /// @return Returns a copy of the String that has been modified to be in all lower case.
134  /// @brief Converts the first letter of each word to upper case and all other letters to lower case.
135  /// @param Source The String to be converted.
136  void MEZZ_LIB ToCamelCase(String& Source);
137  /// @brief Create a copy of the String that has the first letter of each word upper case and other letters lower case.
138  /// @param Source The original String to copy.
139  /// @return Returns a copy of the String that has been modified to be in Camel Case.
141  /// @brief Checks a String to see if it starts with a specific pattern.
142  /// @param Str The String to check.
143  /// @param Pattern The sequence to check for at the start of the String.
144  /// @param CaseSensitive If false this function will check lower-case copies for the pattern, otherwise the strings will be checked as is.
145  /// @return Returns true if the String starts with the provided pattern, false otherwise.
146  Boole MEZZ_LIB StartsWith(const String& Str, const String& Pattern, const Boole CaseSensitive);
147  /// @brief Checks a String to see if it ends with a specific pattern.
148  /// @param Str The String to check.
149  /// @param Pattern The sequence to check for at the end of the String.
150  /// @param CaseSensitive If false this function will check lower-case copies for the pattern, otherwise the strings will be checked as is.
151  /// @return Returns true if the string ends with the provided pattern, false otherwise.
152  Boole MEZZ_LIB EndsWith(const String& Str, const String& Pattern, const Boole CaseSensitive);
153  /// @brief Replaces all instances of multiple consecutive whitespaces with only a single whitespace.
154  /// @param Source The String to be altered.
156 
157  ///////////////////////////////////////////////////////////////////////////////
158  // Data Class Utilities
159 
160  /// @brief Convert two numbers in a string into a Vector2.
161  /// @param ToConvert The string to be converted.
162  /// @remarks The string is expected to have a certain format. The format should be "X Y". If there are not 2 numbers an exception will be thrown.
163  /// @return Returns a Vector2 populated with the values from the string passed in.
164  Vector2 MEZZ_LIB ConvertToVector2(const String& ToConvert);
165  /// @brief Converts a Vector2 into a string.
166  /// @param ToConvert The Vector2 to be converted.
167  /// @return Returns a string containing the values from the Vector2 in "X Y" format.
168  String MEZZ_LIB ConvertToString(const Vector2& ToConvert);
169  /// @brief Convert three numbers in a string into a Vector3.
170  /// @param ToConvert The string to be converted.
171  /// @remarks The string is expected to have a certain format. The format should be "X Y Z". If there are not 3 numbers an exception will be thrown.
172  /// @return Returns a Vector3 populated with the values from the string passed in.
173  Vector3 MEZZ_LIB ConvertToVector3(const String& ToConvert);
174  /// @brief Converts a Vector3 into a string.
175  /// @param ToConvert The Vector3 to be converted.
176  /// @return Returns a string containing the values from the Vector3 in "X Y Z" format.
177  String MEZZ_LIB ConvertToString(const Vector3& ToConvert);
178  /// @brief Convert four numbers in a string into a Quaternion.
179  /// @param ToConvert The string to be converted.
180  /// @remarks The string is expected to have a certain format. The format should be "X Y Z W". If there are not 4 numbers an exception will be thrown.
181  /// @return Returns a Quaternion populated with the values from the string passed in.
182  Quaternion MEZZ_LIB ConvertToQuaternion(const String& ToConvert);
183  /// @brief Converts a Quaternion into a string.
184  /// @param ToConvert The Quaternion to be converted.
185  /// @return Returns a string containing the values from the Quaternion in "X Y Z W" format.
186  String MEZZ_LIB ConvertToString(const Quaternion& ToConvert);
187  /// @brief Convert four numbers in a string into a ColourValue.
188  /// @param ToConvert The string to be converted.
189  /// @remarks The string is expected to have a certain format. The format should be "R G B A". If there are not 4 numbers an exception will be thrown.
190  /// @return Returns a ColourValue populated with the values from the string passed in.
192  /// @brief Converts a ColourValue into a string.
193  /// @param ToConvert The ColourValue to be converted.
194  /// @return Returns a string containing the values from the ColourValue in "R G B A" format.
195  String MEZZ_LIB ConvertToString(const ColourValue& ToConvert);
196 
197  /// @brief Converts a Hex code in a string into a ColourValue.
198  /// @param ToConvert The string to be converted.
199  /// @return Returns a ColourValue populated with the values from the string passed in.
201  /// @brief Converts a ColourValue into a string as a Hex code.
202  /// @param ToConvert The ColourValue to be converted.
203  /// @return Returns a string containing the values from the string passed in.
204  String MEZZ_LIB ConvertToHexString(const ColourValue& ToConvert);
205 
206  ///////////////////////////////////////////////////////////////////////////////
207  // Convert-To-Data functions
208 
209  /// @brief Converts a string into a Boole.
210  /// @param ToConvert The string to be converted to a Boole.
211  /// @return Returns a Boole with the converted value.
212  Boole MEZZ_LIB ConvertToBool(const String& ToConvert, const Boole Default = false);
213  /// @brief Converts a string into a Real.
214  /// @param ToConvert The string to be converted to a Real.
215  /// @return Returns a Real with the converted value.
216  Real MEZZ_LIB ConvertToReal(const String& ToConvert);
217  /// @brief Converts a string into an Integer.
218  /// @param ToConvert The string to be converted to an Integer.
219  /// @return Returns an Integer with the converted value.
220  Integer MEZZ_LIB ConvertToInteger(const String& ToConvert);
221  /// @brief Converts a string into an Whole.
222  /// @param ToConvert The string to be converted to an Whole.
223  /// @return Returns an Whole with the converted value.
224  Whole MEZZ_LIB ConvertToWhole(const String& ToConvert);
225  /// @brief Converts a string into an Int8.
226  /// @param ToConvert The string to be converted to an Int8.
227  /// @return Returns an Int8 with the converted value.
228  Int8 MEZZ_LIB ConvertToInt8(const String& ToConvert);
229  /// @brief Converts a string into a UInt8.
230  /// @param ToConvert The string to be converted to a UInt8.
231  /// @return Returns a UInt8 with the converted value.
232  UInt8 MEZZ_LIB ConvertToUInt8(const String& ToConvert);
233  /// @brief Converts a string into an Int16.
234  /// @param ToConvert The string to be converted to an Int16.
235  /// @return Returns an Int16 with the converted value.
236  Int16 MEZZ_LIB ConvertToInt16(const String& ToConvert);
237  /// @brief Converts a string into a UInt16.
238  /// @param ToConvert The string to be converted to a UInt16.
239  /// @return Returns a UInt16 with the converted value.
240  UInt16 MEZZ_LIB ConvertToUInt16(const String& ToConvert);
241  /// @brief Converts an string into an Int32.
242  /// @param ToConvert The string to be converted to an Int32.
243  /// @return Returns an Int32 with the converted value.
244  Int32 MEZZ_LIB ConvertToInt32(const String& ToConvert);
245  /// @brief Converts a string into a UInt32.
246  /// @param ToConvert The string to be converted to a UInt32.
247  /// @return Returns a UInt32 with the converted value.
248  UInt32 MEZZ_LIB ConvertToUInt32(const String& ToConvert);
249 
250  ///////////////////////////////////////////////////////////////////////////////
251  // Convert-To-String functions
252 
253  /// @brief Converts any into a string.
254  /// @param ToConvert Stream class instance to be converted.
255  /// @return Returns a string containing the lexicagraphically converted data.
256  template<typename T>
257  String ConvertToString(const T& ToConvert)
258  {
259  StringStream converter;
260  converter << ToConvert;
261  return converter.str();
262  }
263 
264  // @brief Converts a Boole into a string.
265  // @param ToConvert The Boole to be converted.
266  // @return Returns "true" if true, or "false" if false.
267  //template<>
268  //String MEZZ_LIB ConvertToString<Boole>(const Boole& ToConvert)
269  //{
270  // if(ToConvert) return "true";
271  // else return "false";
272  //}
273 
274  /// @brief Converts a Input::InputCode into a string.
275  /// @param Code The input code to be converted.
276  /// @param ShiftPressed Whether or not the shift modifier key has been pressed.
277  /// @return Returns a string(usually with only one character) containing the converted input code.
278  String ConvertToString(const Input::InputCode& Code, Boole ShiftPressed);
279 
280  /// @brief Converts a Input::InputCode into a string, assuming shift was not pressed.
281  /// @details this is good for figuring out what key was pressed
282  /// @param Code The input code to be converted.
283  /// @return Returns a string(usually with only one character) containing the converted input code.
284  //template<>
285  //String MEZZ_LIB ConvertToString<Input::InputCode>(const Input::InputCode& Code)
286  // { return ConvertToString(Code, false); }
287 
288  } //stringtool
289 }//Mezzanine
290 
291 #endif
int32_t Int32
An 32-bit integer.
Definition: datatypes.h:124
InputCode
The InputCode enum defines all the posible types of inputs.
Boole StartsWith(const String &Str, const String &Pattern, const Boole CaseSensitive)
Checks a String to see if it starts with a specific pattern.
Definition: stringtool.cpp:227
Boole IsAlphaLetter(const Mezzanine::Char8 ToCheck)
Checks if a character is a lower or upper case letter.
Definition: stringtool.cpp:107
Boole IsUpperHexLetter(const Char8 ToCheck)
Checks if a character is a upper-case hexadecimal letter.
Definition: stringtool.cpp:113
Int16 ConvertToInt16(const String &ToConvert)
Converts a string into an Int16.
Definition: stringtool.cpp:432
std::vector< String > StringVector
This is a simple datatype for a vector container of strings.
Definition: datatypes.h:212
UInt32 ConvertToUInt32(const String &ToConvert)
Converts a string into a UInt32.
Definition: stringtool.cpp:456
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
ColourValue ConvertToColourValue(const String &ToConvert)
Convert four numbers in a string into a ColourValue.
Definition: stringtool.cpp:332
String ConvertToString(const Vector2 &ToConvert)
Converts a Vector2 into a string.
Definition: stringtool.cpp:291
Int32 ConvertToInt32(const String &ToConvert)
Converts an string into an Int32.
Definition: stringtool.cpp:448
String LowerCaseCopy(String Source)
Create a copy of the String that is lower case.
Definition: stringtool.cpp:198
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
Boole IsUpperAlphaLetter(const Mezzanine::Char8 ToCheck)
Checks if a character is a upper-case alphabetic letter.
Definition: stringtool.cpp:104
Vector3 ConvertToVector3(const String &ToConvert)
Convert three numbers in a string into a Vector3.
Definition: stringtool.cpp:298
Boole EndsWith(const String &Str, const String &Pattern, const Boole CaseSensitive)
Checks a String to see if it ends with a specific pattern.
Definition: stringtool.cpp:247
uint8_t UInt8
An 8-bit unsigned integer.
Definition: datatypes.h:118
void Trim(String &Source, Boole Left, Boole Right)
Trims all whitespaces and tabs from a one or both sides of a String.
Definition: stringtool.cpp:128
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
const String Blank
Convenience String for comparing to return blank Strings where necessary.
Definition: stringtool.h:64
Boole IsLowerHexLetter(const Char8 ToCheck)
Checks if a character is a lower-case hexadecimal letter.
Definition: stringtool.cpp:110
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Definition: datatypes.h:176
void ToLowerCase(String &Source)
Converts all upper case characters in a string to their respective lower case.
Definition: stringtool.cpp:193
UInt16 ConvertToUInt16(const String &ToConvert)
Converts a string into a UInt16.
Definition: stringtool.cpp:440
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
Integer ConvertToInteger(const String &ToConvert)
Converts a string into an Integer.
Definition: stringtool.cpp:400
Boole ConvertToBool(const String &ToConvert, const Boole Default)
Converts a string into a Boole.
Definition: stringtool.cpp:379
String CamelCaseCopy(String Source)
Create a copy of the String that has the first letter of each word upper case and other letters lower...
Definition: stringtool.cpp:221
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
StringVector Split(const String &Source, const String &Delims, const Whole MaxSplits)
Splits a string into multiple substrings based on the specified delimiters.
Definition: stringtool.cpp:155
This file describes and implements a reference counted pointer that is NOT threadsafe.
Boole IsLowerAlphaLetter(const Mezzanine::Char8 ToCheck)
Checks if a character is a lower-case alphabetic letter.
Definition: stringtool.cpp:101
String UpperCaseCopy(String Source)
Create a copy of the String that is upper case.
Definition: stringtool.cpp:187
String ConvertToHexString(const ColourValue &ToConvert)
Converts a ColourValue into a string as a Hex code.
Definition: stringtool.cpp:366
int16_t Int16
An 16-bit integer.
Definition: datatypes.h:120
ColourValue ConvertHexToColourValue(const String &ToConvert)
Converts a Hex code in a string into a ColourValue.
Definition: stringtool.cpp:349
Whole ConvertToWhole(const String &ToConvert)
Converts a string into an Whole.
Definition: stringtool.cpp:408
void ToCamelCase(String &Source)
Converts the first letter of each word to upper case and all other letters to lower case...
Definition: stringtool.cpp:204
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
UInt8 ConvertToUInt8(const String &ToConvert)
Converts a string into a UInt8.
Definition: stringtool.cpp:424
Boole IsDigit(const Mezzanine::Char8 ToCheck)
Checks if a character is a decimal digit.
Definition: stringtool.cpp:98
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Int8 ConvertToInt8(const String &ToConvert)
Converts a string into an Int8.
Definition: stringtool.cpp:416
Boole IsHexLetter(const Char8 ToCheck)
Checks if a character is a hexadecimal letter.
Definition: stringtool.cpp:116
void ToUpperCase(String &Source)
Converts all lower case characters in a string to their respective upper case.
Definition: stringtool.cpp:182
Quaternion ConvertToQuaternion(const String &ToConvert)
Convert four numbers in a string into a Quaternion.
Definition: stringtool.cpp:315
Boole IsAlphanumeric(const Char8 ToCheck)
Checks if a character is a letter or digit.
Definition: stringtool.cpp:122
This is used to store information about rotation in 3d space.
Definition: quaternion.h:68
void RemoveDuplicateWhitespaces(String &Source)
Replaces all instances of multiple consecutive whitespaces with only a single whitespace.
Definition: stringtool.cpp:267
int8_t Int8
An 8-bit integer.
Definition: datatypes.h:116
Vector2 ConvertToVector2(const String &ToConvert)
Convert two numbers in a string into a Vector2.
Definition: stringtool.cpp:281
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Real ConvertToReal(const String &ToConvert)
Converts a string into a Real.
Definition: stringtool.cpp:392
Boole IsHexDigit(const Mezzanine::Char8 ToCheck)
Checks if a character is a hexadecimal digit.
Definition: stringtool.cpp:119