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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2