(file) Return to String.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  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            #include <iostream>
 35            #include <Pegasus/Common/Config.h>
 36            #include <Pegasus/Common/Char16.h>
 37 kumpf 1.54 #include <Pegasus/Common/Linkage.h>
 38 mike  1.38 
 39            PEGASUS_NAMESPACE_BEGIN
 40            
 41 kumpf 1.59 class String;
 42 kumpf 1.51 class StringRep;
 43            
 44 kumpf 1.59 /** The CString class provides access to an 8-bit String representation.
 45            */
 46 kumpf 1.60 class PEGASUS_COMMON_LINKAGE CString
 47 kumpf 1.59 {
 48            public:
 49            
 50                CString();
 51            
 52                CString(const CString& cstr);
 53            
 54                ~CString();
 55            
 56 kumpf 1.62     CString& operator=(const CString& cstr);
 57            
 58 kumpf 1.59     operator const char*() const;
 59            
 60            private:
 61            
 62                CString(char* cstr);
 63            
 64                friend class String;
 65            
 66 kumpf 1.64     void* _rep;
 67 kumpf 1.59 };
 68            
 69 mike  1.38 /**
 70                The Pegasus String C++ Class implements the CIM string type.
 71                This class is based on the general handle/representation pattern
 72                defined for all the Pegasus objects.  However, it differes from
 73                most in that it implements "copy on assign" all of the others implement
 74                "no copy on assign" semantics. The string class uses the Array class and
 75                implements an array of characters.
 76            */
 77            class PEGASUS_COMMON_LINKAGE String
 78            {
 79            public:
 80            
 81 kumpf 1.48     /**	EMPTY - Represent an empty string.
 82            	This member is used to represent empty strings. Using this member
 83            	avoids an expensive construction of an empty string (e.g., String()).
 84                */
 85                static const String EMPTY;
 86            
 87 mike  1.38     /** Default constructor without parameters. This constructor creates a
 88            	null string
 89            	<pre>
 90            	    String test;
 91            	</pre>
 92                */
 93                String();
 94            
 95                /// Copy constructor.
 96 kumpf 1.48     String(const String& str);
 97 mike  1.38 
 98 kumpf 1.48     /// Initialize with first n characters from str.
 99                String(const String& str, Uint32 n);
100 mike  1.38 
101 kumpf 1.48     /// Initialize with str.
102                String(const Char16* str);
103 mike  1.38 
104 kumpf 1.48     /// Initialize with first n characters of str.
105                String(const Char16* str, Uint32 n);
106 mike  1.38 
107                /// Initialize from a plain old C-String:
108 kumpf 1.48     String(const char* str);
109 mike  1.38 
110                /// Initialize from the first n characters of a plain old C-String:
111 kumpf 1.48     String(const char* str, Uint32 n);
112 mike  1.38 
113                /// String destructor. Used by the representation of the String object
114                ~String();
115            
116 kumpf 1.48     /** Assign this string with str.
117 mike  1.38 	<pre>
118            	    String t1 = "abc";
119            	    String t2 = t1;
120            	</pre>
121                */
122 kumpf 1.48     String& operator=(const String& str);
123 mike  1.38 
124 kumpf 1.48     /** Assign this string with String str
125                @param str String to assign
126 mike  1.38     @return Returns the String
127                */
128 kumpf 1.48     String& assign(const String& str);
129 mike  1.38 
130 kumpf 1.48     /// Assign this string with str.
131                String& assign(const Char16* str);
132 mike  1.38 
133 kumpf 1.48     /// Assign this string with first n characters of str.
134                String& assign(const Char16* str, Uint32 n);
135 mike  1.38 
136 kumpf 1.48     /// Assign this string with the plain old C-String str.
137                String& assign(const char* str);
138 mike  1.38 
139 kumpf 1.48     /// Assign this string with first n characters of the plain old C-String str.
140                String& assign(const char* str, Uint32 n);
141 mike  1.38 
142                /** clear - Clear this string. After calling clear(), size() will return 0.
143            	<pre>
144            	    String test = "abc";
145            	    test.clear();	// String test is now NULL (length == 0)
146            	</pre>
147                */
148                void clear();
149            
150            
151 kumpf 1.53     /** reserveCapacity - Reserves memory for capacity characters. Notice
152                    that this does not change the size of the string (size() returns
153                    what it did before).  If the capacity of the string is already
154                    greater or equal to the capacity argument, this method has no
155                    effect.  The capacity of a String object has no bearing on its
156                    external behavior.  The capacity of a String is set only for
157                    performance reasons.
158 mike  1.38 	@param capacity defines the capacity in characters to reserve.
159                */
160 kumpf 1.51     void reserveCapacity(Uint32 capacity);
161 mike  1.38 
162                /** Returns the length of the String object.
163            	@return Length of the string in characters.
164            	<pre>
165            	    String s = "abcd";
166            	    assert(s.size() == 4);
167            	</pre>
168                */
169 kumpf 1.48     Uint32 size() const;
170 mike  1.38 
171 kumpf 1.61     /** getChar16Data Returns a pointer to the first character in the 
172            	null-terminated Char16 buffer of the String object.
173 mike  1.38 	@return	Pointer to the first character of the String object
174                	<pre>
175            	    String t1 = "abc";
176 kumpf 1.61 	    const Char16* q = t1.getChar16Data();
177 mike  1.38 	</pre>
178                */
179 kumpf 1.61     const Char16* getChar16Data() const;
180 mike  1.38 
181 kumpf 1.59     /** getCString - Create an 8-bit representation of this String object.
182            
183 kumpf 1.55 	@param truncatedCharacters Output parameter specifying whether any
184                    characters were truncated in the conversion.
185 mike  1.41 	
186 kumpf 1.59         @return CString object that provides access to the 8-bit String
187                    representation
188            
189 mike  1.38 	<pre>
190            	    String test = "abc";
191 kumpf 1.59             printf("test = %s\n", (const char*)test.getCString());
192 mike  1.38 	</pre>
193                */
194 kumpf 1.59     CString getCString() const;
195 mike  1.38 
196 kumpf 1.58     /** Returns the specified character of the String object.
197            	@param index Index of the character to access
198 kumpf 1.57 	@exception IndexOutOfBoundsException if the index
199 kumpf 1.58 	is outside the bounds of the String.
200 mike  1.38 	<pre>
201            	    String t1 = "abc;
202            	    Char16 c = t1[1];	// character b
203            	</pre>
204                */
205 kumpf 1.58     Char16& operator[](Uint32 index);
206 mike  1.38 
207 kumpf 1.58     /** Returns the specified character of the String object (const version).
208            	@param index Index of the character to access
209            	@exception IndexOutOfBoundsException if the index
210            	is outside the bounds of the String.
211 mike  1.38     */
212 kumpf 1.58     const Char16 operator[](Uint32 index) const;
213 mike  1.38 
214 kumpf 1.57     /** Append the given character to this String.
215            	@param c Character to append.
216            	@return This String
217            	<pre>
218            	    String t1 = "abc";
219            	    t1 += Char16('d')
220            	    assert(t1 == "abcd");
221 mike  1.38 	</pre>
222                */
223                String& append(const Char16& c);
224            
225 kumpf 1.57     /// Append n characters from str to this String.
226 mike  1.38     String& append(const Char16* str, Uint32 n);
227            
228 kumpf 1.57     /** Append the given String to this String.
229            	@param str String to append.
230 mike  1.38 	@return This String
231            	<pre>
232            	String test = "abc";
233 kumpf 1.57 	test.append("def");
234 mike  1.38 	assert(test == "abcdef");
235            	</pre>
236                */
237 kumpf 1.57     String& append(const String& str);
238 mike  1.38 
239                /** Remove size characters from the string starting at the given
240 kumpf 1.58 	index. If size is PEG_NOT_FOUND, then all characters after index are
241 mike  1.38 	removed.
242 kumpf 1.58 	@param index Position in string to start remove
243 mike  1.38 	@param size Number of characters to remove. Default is PEG_NOT_FOUND
244 kumpf 1.58 	which causes all characters after index to be removed
245 mike  1.38 	<pre>
246            	    String s;
247            	    s = "abc";
248            	    s.remove(0, 1);
249            	    assert(String::equal(s, "bc"));
250            	    assert(s.size() == 2);
251            	    s.remove(0);
252            	    assert(String::equal(s, ""));
253            	    assert(s.size() == 0);
254            	</pre>
255 kumpf 1.55 	@exception IndexOutOfBoundsException if size is greater than
256 kumpf 1.58 	length of String plus starting index for remove.
257 mike  1.38     */
258 kumpf 1.58     void remove(Uint32 index, Uint32 size = PEG_NOT_FOUND);
259 mike  1.38 
260                /** Return a new String which is initialzed with <TT>length</TT>
261 kumpf 1.58 	characters from this string starting at <TT>index</TT>.
262            	@param <TT>index</TT> is the index in string to start getting the
263 mike  1.38 	substring.
264            	@param <TT>length</TT> is the number of characters to get. If length
265 kumpf 1.58 	is PEG_NOT_FOUND, then all characters after index are added to the new
266 mike  1.38 	string.
267            	@return String with the defined substring.
268            	<pre>
269            	    s = "abcdefg";
270            	    s.remove(3);
271            	    assert(String::equal(s, "abc"));
272            	</pre>
273                */
274 kumpf 1.58     String subString(Uint32 index, Uint32 length = PEG_NOT_FOUND) const;
275 mike  1.38 
276 kumpf 1.58     /** Find the index of the first occurence of the character c.
277 mike  1.38 	If the character is not found, PEG_NOT_FOUND is returned.
278            	@param c Char to be found in the String
279            	@return Position of the character in the string or PEG_NOT_FOUND if not
280            	found.
281                */
282                Uint32 find(Char16 c) const;
283            
284 kumpf 1.58     /** Same as above but starts searching from the given index. */
285                Uint32 find(Uint32 index, Char16 c) const;
286 mike  1.38 
287 kumpf 1.58     /** Find the index of the first occurence of the string object.
288 mike  1.38 	This function finds one string inside another
289            	If the matching substring is not found, PEG_NOT_FOUND is returned.
290            	@param s String object to be found in the String
291            	@return Position of the substring in the String or PEG_NOT_FOUND if not
292            	found.
293                */
294                Uint32 find(const String& s) const;
295            
296                /** reverseFind - Same as find() but start looking in reverse (last
297                character first).
298                	@param c Char16 character to find in String.
299            	@Seealso find
300            	@return Position of the character in the string or PEG_NOT_FOUND if not
301            	found.
302            
303            	NOTE: This function is defined only for char* input, not for
304            	String.
305                */
306                Uint32 reverseFind(Char16 c) const;
307            
308                /** Converts all characters in this string to lower case.
309 mike  1.38     */
310                void toLower();
311 kumpf 1.48 
312 mike  1.38     /** Compare the first n characters of the two strings..
313                	@param s1 First null-terminated string for the comparison.
314            	@param s2 Second null-terminated string for the comparison.
315            	@param n Number of characters to compare.
316            	@return Return -1 if s1 is lexographically less than s2. If they are
317            	equavalent return 0. Otherwise return 1.
318                */
319 kumpf 1.51     static int compare(const String& s1, const String& s2, Uint32 n);
320 mike  1.38 
321                /** Compare two null-terminated strings.
322                	@param s1 First null-terminated string for the comparison.
323            	@param s2 Second null-terminated string for the comparison.
324            	@return If s1 is less than s2, return -1; if equal return 0;
325            	otherwise, return 1.
326            
327            	NOTE: Use the comparison operators <,<= > >= to compare
328            	String objects.
329                */
330 kumpf 1.51     static int compare(const String& s1, const String& s2);
331 mike  1.38 
332 kumpf 1.48     /** Just like one above except ignores case differences.
333                */
334 kumpf 1.49     static int compareNoCase(const String& s1, const String& s2);
335 kumpf 1.48 
336 mike  1.38     /** Compare two String objects for equality.
337            	@param s1 First <TT>String</TT> for comparison.
338            	@param s2 Second <TT>String</TT> for comparison.
339            
340            	@return Boolean true if the two strings are equal.
341            	<pre>
342            	    String s1 = "Hello World";
343            	    String s2 = s1;
344            	    String s3(s2);
345            	    assert(String::equal(s1, s3));
346            	</pre>
347                */
348 kumpf 1.48     static Boolean equal(const String& str1, const String& str2);
349 mike  1.38 
350 karl  1.47     /** equalNoCase - Compares two strings and returuns true if they
351            	are equal indpedent of case of the characters.
352 kumpf 1.48 	@param str1 First String parameter
353            	@param str2 Second String parameter
354 karl  1.47 	@return true if strings are equal independent of case.
355                */
356 kumpf 1.48     static Boolean equalNoCase(const String& str1, const String& str2);
357 mike  1.38 
358 karl  1.46     /** match matches a string against a GLOB style pattern.
359                    Return trues if the String parameter matches the pattern. C-Shell style
360            	glob matching is used.
361                    @param str String to be matched against the pattern
362                    @param pattern Pattern to use in the match
363                    @return Boolean true if str matches pattern
364                    The pattern definition is as follows:
365                    <pre>
366                    *             Matches any number of any characters
367                    ?             Match exactly one character
368                    [chars]       Match any character in chars
369                    [chara-charb] Match any character in the range between chara and charb
370                    </pre>
371                    The literal characters *, ?, [, ] can be included in a string by
372                    escaping them with backslash "\".  Ranges of characters can be concatenated.
373                    <pre>
374                    examples:
375                    Boolean result = String::match("This is a test", "*is*");
376                    Boolean works =  String::match("abcdef123", "*[0-9]");
377                    </pre>
378                */
379 karl  1.46     static Boolean match(const String& str, const String& pattern);
380            
381 kumpf 1.48     /** matchNoCase Matches a String against a GLOB style pattern independent
382 karl  1.46         of case. 
383                    Returns true if the str parameter matches the pattern. C-Shell style
384            	glob matching is used. Ignore case in all comparisons. Case is
385                    ignored in the match.
386                    @parm str String containing the string to be matched\
387                    @parm pattern GLOB style patterh to use in the match.
388                    @return Boolean true if str matches patterh
389                    @SeeAlso match
390                */
391                static Boolean matchNoCase(const String& str, const String& pattern);
392            
393 mike  1.38 private:
394            
395 kumpf 1.51     StringRep* _rep;
396 mike  1.38 };
397 mike  1.40 
398 mike  1.38 /** String operator ==. Test for equality between two strings of any of the
399                types String or char*.
400                @return Boolean - True if the strings are equal
401            */
402 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(
403                const String& str1,
404                const String& str2);
405 mike  1.38 
406            /** String operator ==. Test for equality between two strings
407            
408            */
409 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(const String& str1, const char* str2);
410 mike  1.38 
411            /** String operator ==. Test for equality between two strings
412            
413            */
414 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(const char* str1, const String& str2);
415 mike  1.38 
416            /** String operator ==. Test for equality between two strings
417            
418            */
419 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator!=(
420                const String& str1,
421                const String& str2);
422 mike  1.38 
423            PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
424                PEGASUS_STD(ostream)& os,
425 kumpf 1.48     const String& str);
426 mike  1.38 
427            /** overload operator +	 - Concatenates String objects.
428            
429                <pre>
430            	String t1 = "abc";
431            	String t2;
432            	t2 = t1 + "def"
433            	assert(t2 == "abcdef");
434                </pre>
435            */
436 kumpf 1.48 PEGASUS_COMMON_LINKAGE String operator+(const String& str1, const String& str2);
437 mike  1.38 
438            /** overload operator < - Compares String obects.
439                <pre>
440            	String t1 = "def";
441            	String t2 = "a";
442            	assert (t2 < t1);
443                </pre>
444            */
445 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator<(
446                const String& str1,
447                const String& str2);
448 mike  1.38 
449            /** overload operator <= compares String objects.
450            
451            */
452 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator<=(
453                const String& str1,
454                const String& str2);
455 mike  1.38 
456            /** Overload operator > compares String objects
457            */
458 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator>(
459                const String& str1,
460                const String& str2);
461 mike  1.38 
462            /** overload operator >= - Compares String objects
463            */
464 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator>=(
465                const String& str1,
466                const String& str2);
467 mike  1.38 
468 kumpf 1.66 #ifndef PEGASUS_REMOVE_DEPRECATED
469 mike  1.38 /** Compare two strings but ignore any case differences.
470            */
471            PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2);
472 kumpf 1.65 #endif
473 mike  1.41 
474 mike  1.38 PEGASUS_NAMESPACE_END
475            
476            #endif /* Pegasus_String_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2