1 mike 1.38 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.50 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
|
5 mike 1.38 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.50 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10 mike 1.38 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
|
13 kumpf 1.50 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 mike 1.38 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
16 kumpf 1.50 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19 mike 1.38 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Mike Brasher (mbrasher@bmc.com)
25 //
|
26 karl 1.43 // Modified By: Karl Schopmeyer(k.schopmeyer@opengroup.org)
|
27 kumpf 1.48 // Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
|
28 mike 1.38 //
29 //%/////////////////////////////////////////////////////////////////////////////
30
31 #ifndef Pegasus_String_h
32 #define Pegasus_String_h
33
|
34 kumpf 1.71 #ifdef PEGASUS_OS_HPUX
|
35 kumpf 1.72 # ifdef HPUX_IA64_NATIVE_COMPILER
36 # include <iostream>
37 # else
38 # include <iostream.h>
39 # endif
|
40 kumpf 1.71 #else
41 # include <iostream>
42 #endif
|
43 mike 1.38 #include <Pegasus/Common/Config.h>
44 #include <Pegasus/Common/Char16.h>
|
45 kumpf 1.54 #include <Pegasus/Common/Linkage.h>
|
46 mike 1.38
|
47 david 1.76 const char STRING_FLAG_ASCII[] = "ASCII";
48 const char STRING_FLAG_UTF8[] = "UTF8";
49
|
50 mike 1.38 PEGASUS_NAMESPACE_BEGIN
51
|
52 kumpf 1.59 class String;
|
53 kumpf 1.51 class StringRep;
54
|
55 kumpf 1.59 /** The CString class provides access to an 8-bit String representation.
56 */
|
57 kumpf 1.60 class PEGASUS_COMMON_LINKAGE CString
|
58 kumpf 1.59 {
59 public:
60
|
61 kumpf 1.74 ///
|
62 kumpf 1.59 CString();
63
|
64 kumpf 1.74 ///
|
65 kumpf 1.59 CString(const CString& cstr);
66
|
67 kumpf 1.74 ///
|
68 kumpf 1.59 ~CString();
69
|
70 kumpf 1.74 ///
|
71 kumpf 1.62 CString& operator=(const CString& cstr);
72
|
73 kumpf 1.59 operator const char*() const;
74
75 private:
76
77 CString(char* cstr);
78
79 friend class String;
80
|
81 kumpf 1.64 void* _rep;
|
82 kumpf 1.59 };
83
|
84 mike 1.38 /**
85 The Pegasus String C++ Class implements the CIM string type.
86 */
87 class PEGASUS_COMMON_LINKAGE String
88 {
89 public:
90
|
91 kumpf 1.74 /** This member is used to represent an empty string. Using this
92 member avoids construction of an empty string (e.g., String()).
|
93 kumpf 1.48 */
94 static const String EMPTY;
95
|
96 mike 1.38 /** Default constructor without parameters. This constructor creates a
|
97 kumpf 1.74 null string.
|
98 mike 1.38 <pre>
99 String test;
100 </pre>
101 */
102 String();
103
104 /// Copy constructor.
|
105 kumpf 1.48 String(const String& str);
|
106 mike 1.38
|
107 kumpf 1.48 /// Initialize with first n characters from str.
108 String(const String& str, Uint32 n);
|
109 mike 1.38
|
110 kumpf 1.48 /// Initialize with str.
111 String(const Char16* str);
|
112 mike 1.38
|
113 kumpf 1.48 /// Initialize with first n characters of str.
114 String(const Char16* str, Uint32 n);
|
115 mike 1.38
116 /// Initialize from a plain old C-String:
|
117 david 1.76 String(const char* str, const char*);
|
118 kumpf 1.48 String(const char* str);
|
119 mike 1.38
120 /// Initialize from the first n characters of a plain old C-String:
|
121 kumpf 1.48 String(const char* str, Uint32 n);
|
122 mike 1.38
|
123 kumpf 1.74 /// Destructor.
|
124 mike 1.38 ~String();
125
|
126 kumpf 1.48 /** Assign this string with str.
|
127 mike 1.38 <pre>
128 String t1 = "abc";
129 String t2 = t1;
130 </pre>
131 */
|
132 kumpf 1.48 String& operator=(const String& str);
|
133 mike 1.38
|
134 kumpf 1.74 /** Assign this string with String str.
135 @param str String to assign.
136 @return Returns the String.
|
137 mike 1.38 */
|
138 kumpf 1.48 String& assign(const String& str);
|
139 mike 1.38
|
140 kumpf 1.48 /// Assign this string with str.
141 String& assign(const Char16* str);
|
142 mike 1.38
|
143 kumpf 1.48 /// Assign this string with first n characters of str.
144 String& assign(const Char16* str, Uint32 n);
|
145 mike 1.38
|
146 kumpf 1.48 /// Assign this string with the plain old C-String str.
147 String& assign(const char* str);
|
148 mike 1.38
|
149 kumpf 1.48 /// Assign this string with first n characters of the plain old C-String str.
150 String& assign(const char* str, Uint32 n);
|
151 mike 1.38
|
152 kumpf 1.74 /** Clear this string. After calling clear(), size() will return 0.
|
153 mike 1.38 <pre>
154 String test = "abc";
|
155 kumpf 1.74 test.clear();
|
156 mike 1.38 </pre>
157 */
158 void clear();
159
160
|
161 kumpf 1.74 /** Reserves memory for capacity characters. Notice
|
162 kumpf 1.53 that this does not change the size of the string (size() returns
163 what it did before). If the capacity of the string is already
164 greater or equal to the capacity argument, this method has no
165 effect. The capacity of a String object has no bearing on its
166 external behavior. The capacity of a String is set only for
167 performance reasons.
|
168 mike 1.38 @param capacity defines the capacity in characters to reserve.
169 */
|
170 kumpf 1.51 void reserveCapacity(Uint32 capacity);
|
171 mike 1.38
172 /** Returns the length of the String object.
173 @return Length of the string in characters.
174 <pre>
175 String s = "abcd";
176 assert(s.size() == 4);
177 </pre>
178 */
|
179 kumpf 1.48 Uint32 size() const;
|
180 mike 1.38
|
181 kumpf 1.74 /** Returns a pointer to the first character in the
|
182 kumpf 1.61 null-terminated Char16 buffer of the String object.
|
183 kumpf 1.74 @return Pointer to the first character of the String object.
|
184 mike 1.38 <pre>
|
185 kumpf 1.74 String test = "abc";
186 const Char16* q = test.getChar16Data();
|
187 mike 1.38 </pre>
188 */
|
189 kumpf 1.61 const Char16* getChar16Data() const;
|
190 mike 1.38
|
191 kumpf 1.74 /** Create an 8-bit representation of this String object.
|
192 kumpf 1.59
193 @return CString object that provides access to the 8-bit String
|
194 kumpf 1.74 representation.
|
195 kumpf 1.59
|
196 mike 1.38 <pre>
197 String test = "abc";
|
198 kumpf 1.59 printf("test = %s\n", (const char*)test.getCString());
|
199 chuck 1.75
200 NOTE: Do not do the following:
201 const char * p = (const char *)test.getCString();
202 The pointer p will be invalid. This is because
203 the CString object is destructed, which deletes
204 the heap space for p.
|
205 mike 1.38 </pre>
206 */
|
207 kumpf 1.59 CString getCString() const;
|
208 mike 1.38
|
209 kumpf 1.58 /** Returns the specified character of the String object.
|
210 kumpf 1.74 @param index Index of the character to access.
211 @return specified character of the String object.
|
212 kumpf 1.57 @exception IndexOutOfBoundsException if the index
|
213 kumpf 1.58 is outside the bounds of the String.
|
214 mike 1.38 <pre>
|
215 kumpf 1.74 String test = "abc;
216 Char16 c = test[1];
|
217 mike 1.38 </pre>
218 */
|
219 kumpf 1.58 Char16& operator[](Uint32 index);
|
220 mike 1.38
|
221 kumpf 1.58 /** Returns the specified character of the String object (const version).
|
222 kumpf 1.74 @param index Index of the character to access.
223 @return specified character of the String object.
|
224 kumpf 1.58 @exception IndexOutOfBoundsException if the index
225 is outside the bounds of the String.
|
226 mike 1.38 */
|
227 kumpf 1.58 const Char16 operator[](Uint32 index) const;
|
228 mike 1.38
|
229 kumpf 1.57 /** Append the given character to this String.
230 @param c Character to append.
|
231 kumpf 1.74 @return This String.
|
232 kumpf 1.57 <pre>
|
233 kumpf 1.74 String test = "abc";
234 test.append(Char16('d'));
235 assert(test == "abcd");
|
236 mike 1.38 </pre>
237 */
238 String& append(const Char16& c);
239
|
240 kumpf 1.57 /// Append n characters from str to this String.
|
241 mike 1.38 String& append(const Char16* str, Uint32 n);
242
|
243 kumpf 1.57 /** Append the given String to this String.
244 @param str String to append.
|
245 kumpf 1.74 @return This String.
|
246 mike 1.38 <pre>
247 String test = "abc";
|
248 kumpf 1.57 test.append("def");
|
249 mike 1.38 assert(test == "abcdef");
250 </pre>
251 */
|
252 kumpf 1.57 String& append(const String& str);
|
253 mike 1.38
254 /** Remove size characters from the string starting at the given
|
255 kumpf 1.58 index. If size is PEG_NOT_FOUND, then all characters after index are
|
256 mike 1.38 removed.
|
257 kumpf 1.74 @param index Position in string to start remove.
|
258 mike 1.38 @param size Number of characters to remove. Default is PEG_NOT_FOUND
|
259 kumpf 1.74 which causes all characters after index to be removed.
|
260 mike 1.38 <pre>
261 String s;
262 s = "abc";
263 s.remove(0, 1);
264 assert(String::equal(s, "bc"));
265 assert(s.size() == 2);
266 s.remove(0);
267 assert(String::equal(s, ""));
268 assert(s.size() == 0);
269 </pre>
|
270 kumpf 1.55 @exception IndexOutOfBoundsException if size is greater than
|
271 kumpf 1.58 length of String plus starting index for remove.
|
272 mike 1.38 */
|
273 kumpf 1.58 void remove(Uint32 index, Uint32 size = PEG_NOT_FOUND);
|
274 mike 1.38
275 /** Return a new String which is initialzed with <TT>length</TT>
|
276 kumpf 1.58 characters from this string starting at <TT>index</TT>.
277 @param <TT>index</TT> is the index in string to start getting the
|
278 mike 1.38 substring.
279 @param <TT>length</TT> is the number of characters to get. If length
|
280 kumpf 1.58 is PEG_NOT_FOUND, then all characters after index are added to the new
|
281 mike 1.38 string.
282 @return String with the defined substring.
283 */
|
284 kumpf 1.58 String subString(Uint32 index, Uint32 length = PEG_NOT_FOUND) const;
|
285 mike 1.38
|
286 kumpf 1.58 /** Find the index of the first occurence of the character c.
|
287 mike 1.38 If the character is not found, PEG_NOT_FOUND is returned.
|
288 kumpf 1.74 @param c Char to be found in the String.
|
289 mike 1.38 @return Position of the character in the string or PEG_NOT_FOUND if not
290 found.
291 */
292 Uint32 find(Char16 c) const;
293
|
294 kumpf 1.74 /** Same as above but starts searching from the given index.
295 */
|
296 kumpf 1.58 Uint32 find(Uint32 index, Char16 c) const;
|
297 mike 1.38
|
298 kumpf 1.58 /** Find the index of the first occurence of the string object.
|
299 kumpf 1.74 This function finds one string inside another.
|
300 mike 1.38 If the matching substring is not found, PEG_NOT_FOUND is returned.
|
301 kumpf 1.74 @param s String object to be found in the String.
|
302 mike 1.38 @return Position of the substring in the String or PEG_NOT_FOUND if not
303 found.
304 */
305 Uint32 find(const String& s) const;
306
|
307 kumpf 1.74 /** Same as find() but start looking in reverse (last character first).
|
308 mike 1.38 @param c Char16 character to find in String.
309 @return Position of the character in the string or PEG_NOT_FOUND if not
310 found.
311 */
312 Uint32 reverseFind(Char16 c) const;
313
314 /** Converts all characters in this string to lower case.
315 */
316 void toLower();
|
317 kumpf 1.48
|
318 mike 1.38 /** Compare the first n characters of the two strings..
319 @param s1 First null-terminated string for the comparison.
320 @param s2 Second null-terminated string for the comparison.
321 @param n Number of characters to compare.
|
322 kumpf 1.74 @return Return -1 if s1 is lexographically less than s2; if they are
323 equavalent return 0; otherwise return 1.
|
324 mike 1.38 */
|
325 kumpf 1.51 static int compare(const String& s1, const String& s2, Uint32 n);
|
326 mike 1.38
327 /** Compare two null-terminated strings.
328 @param s1 First null-terminated string for the comparison.
329 @param s2 Second null-terminated string for the comparison.
|
330 kumpf 1.74 @return Return -1 if s1 is less than s2; if equal return 0;
331 otherwise return 1.
|
332 mike 1.38
333 NOTE: Use the comparison operators <,<= > >= to compare
334 String objects.
335 */
|
336 kumpf 1.51 static int compare(const String& s1, const String& s2);
|
337 mike 1.38
|
338 kumpf 1.74 /** Just like the compare method defined above except that
339 the compareNoCase ignores case differences.
|
340 kumpf 1.48 */
|
341 kumpf 1.49 static int compareNoCase(const String& s1, const String& s2);
|
342 kumpf 1.48
|
343 mike 1.38 /** Compare two String objects for equality.
344 @param s1 First <TT>String</TT> for comparison.
345 @param s2 Second <TT>String</TT> for comparison.
346
|
347 kumpf 1.74 @return true if the two strings are equal, false otherwise.
|
348 mike 1.38 <pre>
349 String s1 = "Hello World";
350 String s2 = s1;
351 String s3(s2);
352 assert(String::equal(s1, s3));
353 </pre>
354 */
|
355 kumpf 1.48 static Boolean equal(const String& str1, const String& str2);
|
356 mike 1.38
|
357 kumpf 1.74 /** Compares two strings and returns true if they
358 are equal indepedent of case of the characters.
359 @param str1 First String parameter.
360 @param str2 Second String parameter.
361 @return true if strings are equal independent of case, flase
362 otherwise.
|
363 karl 1.47 */
|
364 kumpf 1.48 static Boolean equalNoCase(const String& str1, const String& str2);
|
365 david 1.76
366 // UTF8 specific code:
367 String& assignUTF8(const char* str);
368 CString getCStringUTF8() const;
369 static Boolean isUTF8(const char*);
370
|
371 mike 1.38
372 private:
373
|
374 kumpf 1.51 StringRep* _rep;
|
375 mike 1.38 };
|
376 mike 1.40
|
377 mike 1.38 /** String operator ==. Test for equality between two strings of any of the
378 types String or char*.
|
379 kumpf 1.74 @return true if the strings are equal, false otherwise.
|
380 mike 1.38 */
|
381 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(
382 const String& str1,
383 const String& str2);
|
384 mike 1.38
|
385 kumpf 1.74 /** String operator ==. Test for equality between two strings.
|
386 mike 1.38 */
|
387 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(const String& str1, const char* str2);
|
388 mike 1.38
|
389 kumpf 1.74 /** String operator ==. Test for equality between two strings.
|
390 mike 1.38 */
|
391 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(const char* str1, const String& str2);
|
392 mike 1.38
|
393 kumpf 1.74 /** String operator ==. Test for equality between two strings.
|
394 mike 1.38 */
|
395 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator!=(
396 const String& str1,
397 const String& str2);
|
398 mike 1.38
|
399 kumpf 1.74 ///
|
400 mike 1.38 PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
401 PEGASUS_STD(ostream)& os,
|
402 kumpf 1.48 const String& str);
|
403 mike 1.38
404 /** overload operator + - Concatenates String objects.
405 <pre>
406 String t1 = "abc";
407 String t2;
408 t2 = t1 + "def"
409 assert(t2 == "abcdef");
410 </pre>
411 */
|
412 kumpf 1.48 PEGASUS_COMMON_LINKAGE String operator+(const String& str1, const String& str2);
|
413 mike 1.38
414 /** overload operator < - Compares String obects.
415 <pre>
416 String t1 = "def";
417 String t2 = "a";
418 assert (t2 < t1);
419 </pre>
420 */
|
421 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator<(
422 const String& str1,
423 const String& str2);
|
424 mike 1.38
425 /** overload operator <= compares String objects.
426 */
|
427 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator<=(
428 const String& str1,
429 const String& str2);
|
430 mike 1.38
431 /** Overload operator > compares String objects
432 */
|
433 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator>(
434 const String& str1,
435 const String& str2);
|
436 mike 1.38
437 /** overload operator >= - Compares String objects
438 */
|
439 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator>=(
440 const String& str1,
441 const String& str2);
|
442 mike 1.38
|
443 kumpf 1.66 #ifndef PEGASUS_REMOVE_DEPRECATED
|
444 mike 1.38 /** Compare two strings but ignore any case differences.
445 */
446 PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2);
|
447 kumpf 1.65 #endif
|
448 mike 1.41
|
449 mike 1.38 PEGASUS_NAMESPACE_END
450
451 #endif /* Pegasus_String_h */
|