CARMA C++
String.h
Go to the documentation of this file.
1 #ifndef SZA_UTIL_STRING_H
2 #define SZA_UTIL_STRING_H
3 
11 #include <string>
12 #include <vector>
13 
14 namespace sza {
15  namespace util {
16 
17  class String {
18  public:
19 
23  String();
24  String(unsigned int, bool convertNonAscii = false);
25  String(const std::string& str, bool convertNonAscii = false);
26  String(const std::vector<unsigned char>& ucvec, bool convertNonAscii = false);
27  String(const std::vector<char>& ucvec, bool convertNonAscii = false);
28 
32  virtual ~String();
33 
34  static void strip(std::string& targetStr, const std::string& stripStr);
35  static void strip(std::string& targetStr, char stripChar);
36 
37  void strip(const std::string& stripStr);
38  void strip(char stripChar);
39 
40  // Wrap strings to the specified length. Strings longer than
41  // this will have newlines inserted into them as appropriate.
42  // However, existing newlines will be preserved
43 
44  void wrapTo(unsigned nChar, unsigned nIndent);
45 
46  bool contains(char c);
47  bool contains(std::string s);
48  bool remainderContains(std::string s);
49  String remainder();
50 
51  void replace(char stripChar, char replaceChar);
52  static void replace(std::string& targetStr, char stripChar, char replaceChar);
53 
54  void advanceToNextNonWhitespaceChar();
55 
56  void operator=(const std::string& str);
57  void operator=(const String str);
58 
59  bool operator<(String& str);
60  bool operator==(String str);
61  bool operator==(std::string str);
62  bool operator!=(String str);
63 
64  char& operator[](unsigned int index);
65 
66  inline std::string& str()
67  {
68  return str_;
69  }
70 
71  // Allows cout << String
72 
73  friend std::ostream& operator<<(std::ostream& os, String str);
74 
75  String findFirstInstanceOf(std::string start, bool useStart,
76  std::string stop, bool useStop);
77 
78  String findFirstInstanceOf(std::string start, std::string stop);
79 
80  String findFirstInstanceOf(std::string stop);
81 
82  String findNextInstanceOf(std::string start, bool useStart,
83  std::string stop, bool useStop, bool consumeStop=false);
84 
85  String findNextInstanceOf(std::string start, std::string stop);
86 
87  String findNextInstanceOf(std::string stop);
88 
89  String findNextString();
90 
91  String toLower();
92  String toUpper();
93  String firstToLower();
94  String firstToUpper();
95  String capitalized();
96 
97  static const std::string emptyString_;
98  static const std::string whiteSpace_;
99 
100  static std::string toLower(std::string str);
101  static std::string toUpper(std::string str);
102  static std::string firstToLower(std::string str);
103  static std::string firstToUpper(std::string str);
104  static std::string capitalized(std::string str);
105 
106  String findNextStringSeparatedByChars(std::string separators, bool matchEndOfString=true);
107  bool atEnd();
108  void resetToBeginning();
109 
110  void initialize();
111 
112  static int toInt(std::string s);
113 
114  int toInt();
115  float toFloat();
116  double toDouble();
117 
118  bool isEmpty();
119  bool matches(unsigned char c, std::string matchSet);
120 
121  unsigned size();
122 
123  // Convert from string representation of non-ascii bytes to
124  // bytes
125 
126  static std::vector<unsigned char>
127  stringToBytes(std::string s, bool convertNonAscii=false);
128 
129  static void parseNonAscii(std::string& s,
130  unsigned int& iChar, unsigned int nChar,
131  std::vector<unsigned char>& ucvec);
132 
133  static std::string bytesToString(std::vector<unsigned char> ucvec, bool convertNonAscii = false);
134  static std::string bytesToString(std::vector<char> cvec, bool convertNonAscii = false);
135 
136  std::vector<unsigned char> getData();
137 
138  private:
139 
140  std::string::size_type iStart_;
141  std::string str_;
142  std::vector<unsigned char> ucvec_;
143 
144  // If true, the 'string' may contain non-ascii bytes. In this
145  // case, we represent non-printable characters by their decimal
146  // integer equivalents, and any string to be compared to our
147  // string is similary converted before the comparison is
148  // performed
149 
150  bool convertNonAscii_;
151 
152  // Convert an input char or string to the appropriate
153  // representation for comparison with this String. For example,
154  // if we can contain non-Ascii bytes, then any non-ascii bytes
155  // in the input will have been converted to their decimal
156  // integer equivalents
157 
158  std::string convert(char& c);
159  std::string convert(std::string& s);
160 
161  }; // End class String
162 
163  } // End namespace util
164 } // End namespace sza
165 
166 
167 
168 
169 #endif // End #ifndef SZA_UTIL_STRING_H