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