| 1 | /** |
|---|
| 2 | @file stringutils.h |
|---|
| 3 | |
|---|
| 4 | @maintainer Morgan McGuire, matrix@graphics3d.com |
|---|
| 5 | |
|---|
| 6 | @author 2000-09-09 |
|---|
| 7 | @edited 2002-11-30 |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | #ifndef G3D_STRINGUTILS_H |
|---|
| 11 | #define G3D_STRINGUTILS_H |
|---|
| 12 | |
|---|
| 13 | #include "G3D/platform.h" |
|---|
| 14 | #include "G3D/Array.h" |
|---|
| 15 | #include <string> |
|---|
| 16 | |
|---|
| 17 | namespace G3D { |
|---|
| 18 | |
|---|
| 19 | extern const char* NEWLINE; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | Returns true if the test string begins with the pattern string. |
|---|
| 23 | */ |
|---|
| 24 | bool beginsWith( |
|---|
| 25 | const std::string& test, |
|---|
| 26 | const std::string& pattern); |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | Returns true if the test string ends with the pattern string. |
|---|
| 30 | */ |
|---|
| 31 | bool endsWith( |
|---|
| 32 | const std::string& test, |
|---|
| 33 | const std::string& pattern); |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | Produces a new string that is the input string |
|---|
| 37 | wrapped at a certain number of columns (where |
|---|
| 38 | the line is broken at the latest space before the |
|---|
| 39 | column limit.) Platform specific NEWLINEs |
|---|
| 40 | are inserted to wrap. |
|---|
| 41 | */ |
|---|
| 42 | std::string wordWrap( |
|---|
| 43 | const std::string& input, |
|---|
| 44 | int numCols); |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | A comparison function for passing to Array::sort. |
|---|
| 48 | */ |
|---|
| 49 | int stringCompare( |
|---|
| 50 | const std::string& s1, |
|---|
| 51 | const std::string& s2); |
|---|
| 52 | |
|---|
| 53 | int stringPtrCompare( |
|---|
| 54 | const std::string* s1, |
|---|
| 55 | const std::string* s2); |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | Returns a new string that is an uppercase version of x. |
|---|
| 59 | */ |
|---|
| 60 | std::string toUpper( |
|---|
| 61 | const std::string& x); |
|---|
| 62 | |
|---|
| 63 | std::string toLower( |
|---|
| 64 | const std::string& x); |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | Splits x at each occurance of splitChar. |
|---|
| 68 | */ |
|---|
| 69 | G3D::Array<std::string> stringSplit( |
|---|
| 70 | const std::string& x, |
|---|
| 71 | char splitChar); |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | joinChar is not inserted at the beginning or end, just in between |
|---|
| 75 | elements. |
|---|
| 76 | */ |
|---|
| 77 | std::string stringJoin( |
|---|
| 78 | const G3D::Array<std::string>& a, |
|---|
| 79 | char joinChar); |
|---|
| 80 | |
|---|
| 81 | std::string stringJoin( |
|---|
| 82 | const G3D::Array<std::string>& a, |
|---|
| 83 | const std::string& joinStr); |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | Strips whitespace from both ends of the string. |
|---|
| 87 | */ |
|---|
| 88 | std::string trimWhitespace( |
|---|
| 89 | const std::string& s); |
|---|
| 90 | |
|---|
| 91 | /** These standard C functions are renamed for clarity/naming |
|---|
| 92 | conventions and to return bool, not int. |
|---|
| 93 | */ |
|---|
| 94 | inline bool isWhiteSpace(const char c) { |
|---|
| 95 | return isspace(c) != 0; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** These standard C functions are renamed for clarity/naming |
|---|
| 99 | conventions and to return bool, not int. |
|---|
| 100 | */ |
|---|
| 101 | inline bool isNewline(const char c) { |
|---|
| 102 | return (c == '\n') || (c == '\r'); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** These standard C functions are renamed for clarity/naming |
|---|
| 106 | conventions and to return bool, not int. |
|---|
| 107 | */ |
|---|
| 108 | inline bool isDigit(const char c) { |
|---|
| 109 | return isdigit(c) != 0; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** These standard C functions are renamed for clarity/naming |
|---|
| 113 | conventions and to return bool, not int. |
|---|
| 114 | */ |
|---|
| 115 | inline bool isLetter(const char c) { |
|---|
| 116 | return isalpha(c) != 0; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | inline bool isSlash(const char c) { |
|---|
| 120 | return (c == '\\') || (c == '/'); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | inline bool isQuote(const char c) { |
|---|
| 124 | return (c == '\'') || (c == '\"'); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | }; // namespace |
|---|
| 128 | |
|---|
| 129 | #endif |
|---|
| 130 | |
|---|