1 mike 1.1 //BEGIN_LICENSE
2 //
3 // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 // DEALINGS IN THE SOFTWARE.
19 //
20 //END_LICENSE
21 //BEGIN_HISTORY
22 mike 1.1 //
23 // Author:
24 //
|
25 karl 1.2 // $Log: String.h,v $
|
26 karl 1.11 // Revision 1.10 2001/03/29 16:40:31 karl
27 // add doc
28 //
|
29 karl 1.10 // Revision 1.9 2001/02/26 04:33:28 mike
30 // Fixed many places where cim names were be compared with operator==(String,String).
31 // Changed all of these to use CIMName::equal()
32 //
|
33 mike 1.9 // Revision 1.8 2001/02/20 14:05:24 karl
34 // Comments for Document
35 //
|
36 karl 1.8 // Revision 1.7 2001/02/11 17:19:30 mike
37 // added reverseFind() method
38 //
|
39 mike 1.7 // Revision 1.6 2001/02/11 05:42:33 mike
40 // new
41 //
|
42 mike 1.6 // Revision 1.5 2001/01/30 08:00:43 karl
43 // DOC++ Documentation update for header files
44 //
|
45 karl 1.5 // Revision 1.4 2001/01/28 07:05:18 mike
46 // added instance name/reference converters
47 //
|
48 mike 1.4 // Revision 1.3 2001/01/28 04:11:03 mike
49 // fixed qualifier resolution
50 //
|
51 mike 1.3 // Revision 1.2 2001/01/24 16:16:38 karl
52 // Incorporate Doc++ Comments as documentation into .h files
53 //
|
54 karl 1.2 // Revision 1.1.1.1 2001/01/14 19:53:15 mike
55 // Pegasus import
56 //
|
57 mike 1.1 //
58 //END_HISTORY
59
60 #ifndef Pegasus_String_h
61 #define Pegasus_String_h
62
63 #include <iostream>
64 #include <cstring>
65 #include <Pegasus/Common/Config.h>
66 #include <Pegasus/Common/Char16.h>
67 #include <Pegasus/Common/Array.h>
68
69 PEGASUS_NAMESPACE_BEGIN
70
|
71 karl 1.2 /**
|
72 mike 1.6 The Pegasus String C++ Class implements the CIM string type
|
73 karl 1.2 */
|
74 mike 1.1 class PEGASUS_COMMON_LINKAGE String
75 {
76 public:
77
|
78 mike 1.6 /// Default constructor.
|
79 mike 1.1 String();
|
80 mike 1.6
81 /// Copy constructor.
|
82 mike 1.1 String(const String& x);
|
83 mike 1.6
84 /// Initialize with first n characters from x.
|
85 mike 1.1 String(const String& x, Uint32 n);
|
86 mike 1.6
87 /// Initialize with x.
|
88 mike 1.1 String(const Char16* x);
|
89 mike 1.6
90 /// Initialize with first n characters of x.
|
91 mike 1.1 String(const Char16* x, Uint32 n);
|
92 mike 1.6
93 /// Initialize from a plain old C-String:
|
94 mike 1.1 String(const char* x);
|
95 mike 1.6
96 /// Initialize from the first n characters of a plain old C-String:
|
97 mike 1.1 String(const char* x, Uint32 n);
98
|
99 karl 1.11 /// String destructor.
|
100 mike 1.6 ~String()
101 {
102 }
103
104 /// Assign this string with x.
|
105 mike 1.1 String& operator=(const String& x) { _rep = x._rep; return *this; }
|
106 mike 1.6
107 /// Assign this string with x.
|
108 mike 1.1 String& operator=(const Char16* x) { assign(x); return *this; }
|
109 mike 1.6
|
110 karl 1.8 /// Assign this string with x.
|
111 mike 1.1 String& assign(const String& x) { _rep = x._rep; return *this; }
|
112 mike 1.6
113 /// Assign this string with x.
|
114 mike 1.1 String& assign(const Char16* x);
|
115 mike 1.6
116 /// Assign this string with first n characters of x.
|
117 mike 1.1 String& assign(const Char16* x, Uint32 n);
|
118 mike 1.6
119 /// Assign this string with the plain old C-String x.
|
120 mike 1.1 String& assign(const char* x);
|
121 mike 1.6
122 /// Assign this string with first n characters of the plain old C-String x.
|
123 mike 1.1 String& assign(const char* x, Uint32 n);
|
124 mike 1.6
125 /// Clear this string. After calling clear(), getLength() will return 0.
|
126 mike 1.1 void clear() { _rep.clear(); _rep.append('\0'); }
|
127 mike 1.4
|
128 mike 1.6 /** Reserves memory for capacity characters. Notice that this does not
129 change the size of the string (getSize() returns what it did before).
130 If the capacity of the string is already greater or equal to the
131 capacity argument, this method has no effect. After calling reserve(),
132 getCapicty() returns a value which is greater or equal to the
133 capacity argument.
|
134 karl 1.2 */
|
135 mike 1.1 void reserve(Uint32 capacity) { _rep.reserve(capacity + 1); }
|
136 mike 1.4
|
137 mike 1.6 /// Returns the length of the string.
|
138 mike 1.1 Uint32 getLength() const { return _rep.getSize() - 1; }
|
139 mike 1.4
|
140 mike 1.6 /// Returns a pointer to the first character in the string string.
|
141 mike 1.1 const Char16* getData() const { return _rep.getData(); }
142
|
143 mike 1.6 /** Allocates an 8 bit representation of this string. The user is
144 responsible for freeing the result. If any characters are truncated,
145 a TruncatedCharacter exception is thrown. This exception may
146 be suppressed by passing true as the noThrow argument. Extra
147 characters may be allocated at the end of the new string by
148 passing a non-zero value to the extraBytes argument.
149 */
|
150 mike 1.1 char* allocateCString(Uint32 extraBytes = 0, Boolean noThrow = false) const;
151
|
152 mike 1.6 /** Append the given string to a C-string. If the length is not Uint32(-1),
153 then the lesser of the the length argument and the length of this
154 string is truncated. Otherwise, the entire string is trunctated. The
155 TruncatedCharacter exception is thrown if any characters are truncated.
|
156 karl 1.11 <pre>
157 const char STR0[] = "one two three four";
158 String s = STR0;
159 const char STR1[] = "zero ";
160 char* tmp = new char[strlen(STR1) + s.getLength() + 1];
161 strcpy(tmp, STR1);
162 s.appendToCString(tmp, 7);
163 assert(strcmp(tmp, "zero one two") == 0);
164 </pre>
|
165 karl 1.2 */
|
166 mike 1.1 void appendToCString(
|
167 karl 1.2 char* str,
|
168 mike 1.1 Uint32 length = Uint32(-1),
169 Boolean noThrow = false) const;
170
|
171 mike 1.6 /// Returns the Ith character of the string.
|
172 mike 1.1 Char16& operator[](Uint32 i);
173
|
174 mike 1.6 /// Returns the Ith character of the string (const version).
|
175 mike 1.1 const Char16 operator[](Uint32 i) const;
|
176 mike 1.6
|
177 karl 1.11 /** Append the given character to the string.
178 <pre>
179 String s4 = "Hello";
180 s4.append(Char16(0x0000))
181 </pre>
182 */
|
183 karl 1.2 String& append(const Char16& c)
184 {
185 _rep.insert(_rep.getSize() - 1, c);
|
186 mike 1.1 return *this;
187 }
|
188 mike 1.6
189 /// Append n characters from str to this string.
|
190 mike 1.1 String& append(const Char16* str, Uint32 n);
|
191 mike 1.6
192 /// Append the characters of str to this string.
|
193 karl 1.2 String& append(const String& str)
|
194 mike 1.1 {
195 return append(str.getData(), str.getLength());
196 }
|
197 mike 1.6
198 /// Append the characters of str to this string.
|
199 mike 1.1 String& operator+=(const String& x)
200 {
201 return append(x);
202 }
|
203 mike 1.6
204 /// Append the character given by c to this string.
|
205 mike 1.1 String& operator+=(Char16 c)
206 {
207 return append(c);
208 }
|
209 mike 1.6
210 /// Append the character given by c to this string.
|
211 mike 1.1 String& operator+=(char c)
212 {
213 return append(Char16(c));
214 }
|
215 mike 1.3
|
216 mike 1.6 /** Remove size characters from the string starting at the given
217 position. If size is -1, then all characters after pos are removed.
218 */
|
219 mike 1.1 void remove(Uint32 pos, Uint32 size = Uint32(-1));
|
220 mike 1.3
|
221 karl 1.11 /** Return a new String which is initialzed with <TT>length</TT>
|
222 karl 1.8 characters from this string starting at <TT>pos</TT>.
223 @param <TT>pos</TT> is the positon in string to start getting the
224 substring.
225 @param <TT>length</TT> is the number of characters to get. If length
226 is -1, then all characters after pos are added to the new string.
227 @return String with the defined substring.
|
228 karl 1.11 <pre>
229 s = "abcdefg";
230 s.remove(3);
231 assert(String::equal(s, "abc"));
232 </pre>
|
233 mike 1.6 */
|
234 mike 1.1 String subString(Uint32 pos, Uint32 length = Uint32(-1)) const;
|
235 mike 1.3
|
236 mike 1.7 /** Find the position of the first occurence of the character c.
237 If the character is not found, -1 is returned.
|
238 mike 1.6 */
|
239 mike 1.1 Uint32 find(Char16 c) const;
|
240 mike 1.7
241 /** Same as find() but start looking in reverse (last character first).
242 */
243 Uint32 reverseFind(Char16 c) const;
|
244 mike 1.3
|
245 mike 1.6 /** Compare the first n characters of the two strings. Return -1 if s1
246 is lexographically less than s2. If they are equavalent return 0.
247 Otherwise return 1.
248 */
|
249 mike 1.1 static int compare(const Char16* s1, const Char16* s2, Uint32 n);
|
250 mike 1.3
|
251 mike 1.6 /** Compare the two null-terminated strings. If s1 is less than s2,
252 return -1; if equal return 0; otherwise, return 1.
253 */
|
254 mike 1.1 static int compare(const Char16* s1, const Char16* s2);
|
255 mike 1.3
|
256 karl 1.11 /** Return true if the two strins are equal.
257 <pre>
258 String s1 = "Hello World";
259 String s2 = s1;
260 String s3(s2);
261 assert(String::equal(s1, s3));
262 </pre>
263 */
|
264 mike 1.9 static Boolean equal(const String& x, const String& y);
265
266 /// Return true if the two strins are equal.
267 static Boolean equal(const String& x, const Char16* y);
268
269 /// Return true if the two strins are equal.
270 static Boolean equal(const Char16* x, const String& y);
271
272 /// Return true if the two strins are equal.
273 static Boolean equal(const String& x, const char* y);
274
|
275 karl 1.11 /// Return true if the two strings are equal.
|
276 mike 1.9 static Boolean equal(const char* x, const String& y);
277
|
278 mike 1.6 /// Convert the plain old C-string to lower case:
279 static void toLower(char* str);
280
|
281 karl 1.10 /** EMPTY - Represent an empty string.
|
282 mike 1.6 This member is used to represent empty strings. Using this member
|
283 karl 1.10 avoids an expensive construction of an empty string (e.g., String()).
|
284 mike 1.6 */
|
285 mike 1.1 static const String EMPTY;
286
287 private:
288
289 static Uint32 _min(Uint32 x, Uint32 y) { return x < y ? x : y; }
290
|
291 karl 1.2 Array<Char16> _rep;
|
292 mike 1.1 };
293
|
294 mike 1.9 inline Boolean operator==(const String& x, const String& y)
295 {
296 return String::equal(x, y);
297 }
|
298 mike 1.1
|
299 mike 1.9 inline Boolean operator==(const String& x, const char* y)
300 {
301 return String::equal(x, y);
302 }
|
303 mike 1.1
|
304 mike 1.9 inline Boolean operator==(const char* x, const String& y)
305 {
306 return String::equal(x, y);
307 }
|
308 mike 1.1
309 inline Boolean operator!=(const String& x, const String& y)
310 {
|
311 mike 1.9 return !String::equal(x, y);
|
312 mike 1.1 }
313
|
314 mike 1.6 PEGASUS_COMMON_LINKAGE std::ostream& operator<<(
315 std::ostream& os,
316 const String& x);
|
317 mike 1.1
318 inline String operator+(const String& x, const String& y)
319 {
320 return String(x).append(y);
321 }
322
323 inline Boolean operator<(const String& x, const String& y)
324 {
325 return String::compare(x.getData(), y.getData()) < 0;
326 }
327
328 inline Boolean operator<=(const String& x, const String& y)
329 {
330 return String::compare(x.getData(), y.getData()) <= 0;
331 }
332
333 inline Boolean operator>(const String& x, const String& y)
334 {
335 return String::compare(x.getData(), y.getData()) > 0;
336 }
337
338 mike 1.1 inline Boolean operator>=(const String& x, const String& y)
339 {
340 return String::compare(x.getData(), y.getData()) >= 0;
341 }
|
342 mike 1.6
343 /** Return a version of this string whose characters have been shifted
344 to lower case.
345 */
346 PEGASUS_COMMON_LINKAGE String ToLower(const String& str);
347
|
348 mike 1.1
349 PEGASUS_NAMESPACE_END
350
351 #endif /* Pegasus_String_h */
|
352 karl 1.2
|