Panda3D
string_utils.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file string_utils.h
10  * @author drose
11  * @date 1999-01-18
12  */
13 
14 #ifndef STRING_UTILS_H
15 #define STRING_UTILS_H
16 
17 #include "dtoolbase.h"
18 
19 #include <string>
20 #include "vector_string.h"
21 #include "pdtoa.h"
22 
23 // Case-insensitive string comparison, from Stroustrup's C++ third edition.
24 // Works like strcmp().
25 EXPCL_DTOOL_DTOOLUTIL int cmp_nocase(const std::string &s, const std::string &s2);
26 
27 // Similar, except it also accepts hyphen and underscore as equivalent.
28 EXPCL_DTOOL_DTOOLUTIL int cmp_nocase_uh(const std::string &s, const std::string &s2);
29 
30 // Returns the string converted to lowercase.
31 EXPCL_DTOOL_DTOOLUTIL std::string downcase(const std::string &s);
32 
33 // Returns the string converted to uppercase.
34 EXPCL_DTOOL_DTOOLUTIL std::string upcase(const std::string &s);
35 
36 // Separates the string into words according to whitespace.
37 EXPCL_DTOOL_DTOOLUTIL int extract_words(const std::string &str, vector_string &words);
38 EXPCL_DTOOL_DTOOLUTIL int extract_words(const std::wstring &str, pvector<std::wstring> &words);
39 
40 // Separates the string into words according to the indicated delimiters.
41 EXPCL_DTOOL_DTOOLUTIL void tokenize(const std::string &str, vector_string &words,
42  const std::string &delimiters,
43  bool discard_repeated_delimiters = false);
44 EXPCL_DTOOL_DTOOLUTIL void tokenize(const std::wstring &str, pvector<std::wstring> &words,
45  const std::wstring &delimiters,
46  bool discard_repeated_delimiters = false);
47 
48 // Trims leading andor trailing whitespace from the string.
49 EXPCL_DTOOL_DTOOLUTIL std::string trim_left(const std::string &str);
50 EXPCL_DTOOL_DTOOLUTIL std::wstring trim_left(const std::wstring &str);
51 EXPCL_DTOOL_DTOOLUTIL std::string trim_right(const std::string &str);
52 EXPCL_DTOOL_DTOOLUTIL std::wstring trim_right(const std::wstring &str);
53 EXPCL_DTOOL_DTOOLUTIL std::string trim(const std::string &str);
54 EXPCL_DTOOL_DTOOLUTIL std::wstring trim(const std::wstring &str);
55 
56 // Functions to parse numeric values out of a string.
57 EXPCL_DTOOL_DTOOLUTIL int string_to_int(const std::string &str, std::string &tail);
58 EXPCL_DTOOL_DTOOLUTIL bool string_to_int(const std::string &str, int &result);
59 EXPCL_DTOOL_DTOOLUTIL double string_to_double(const std::string &str, std::string &tail);
60 EXPCL_DTOOL_DTOOLUTIL bool string_to_double(const std::string &str, double &result);
61 EXPCL_DTOOL_DTOOLUTIL bool string_to_float(const std::string &str, float &result);
62 EXPCL_DTOOL_DTOOLUTIL bool string_to_stdfloat(const std::string &str, PN_stdfloat &result);
63 
64 // Convenience function to make a string from anything that has an ostream
65 // operator.
66 template<class Thing>
67 INLINE std::string format_string(const Thing &thing);
68 
69 // Fast specializations for some primitive types.
70 INLINE std::string format_string(const std::string &value);
71 INLINE std::string format_string(bool value);
72 INLINE std::string format_string(float value);
73 INLINE std::string format_string(double value);
74 INLINE std::string format_string(unsigned int value);
75 INLINE std::string format_string(int value);
76 INLINE std::string format_string(int64_t value);
77 
78 #include "string_utils.I"
79 
80 #endif
EXPCL_DTOOL_DTOOLUTIL int extract_words(const std::string &str, vector_string &words)
Divides the string into a number of words according to whitespace.
EXPCL_DTOOL_DTOOLUTIL std::string trim(const std::string &str)
Returns a new string representing the contents of the given string with both leading and trailing whi...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
EXPCL_DTOOL_DTOOLUTIL void tokenize(const std::string &str, vector_string &words, const std::string &delimiters, bool discard_repeated_delimiters=false)
Chops the source string up into pieces delimited by any of the characters specified in delimiters.
EXPCL_DTOOL_DTOOLUTIL std::string trim_left(const std::string &str)
Returns a new string representing the contents of the given string with the leading whitespace remove...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
EXPCL_DTOOL_DTOOLUTIL double string_to_double(const std::string &str, std::string &tail)
A string-interface wrapper around the C library strtol().
EXPCL_DTOOL_DTOOLUTIL int string_to_int(const std::string &str, std::string &tail)
A string-interface wrapper around the C library strtol().
EXPCL_DTOOL_DTOOLUTIL std::string downcase(const std::string &s)
Returns the input string with all uppercase letters converted to lowercase.
EXPCL_DTOOL_DTOOLUTIL std::string trim_right(const std::string &str)
Returns a new string representing the contents of the given string with the trailing whitespace remov...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
EXPCL_DTOOL_DTOOLUTIL std::string upcase(const std::string &s)
Returns the input string with all lowercase letters converted to uppercase.