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

  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.10 // Revision 1.9  2001/02/26 04:33:28  mike
 27            // Fixed many places where cim names were be compared with operator==(String,String).
 28            // Changed all of these to use CIMName::equal()
 29            //
 30 mike  1.9  // Revision 1.8  2001/02/20 14:05:24  karl
 31            // Comments for Document
 32            //
 33 karl  1.8  // Revision 1.7  2001/02/11 17:19:30  mike
 34            // added reverseFind() method
 35            //
 36 mike  1.7  // Revision 1.6  2001/02/11 05:42:33  mike
 37            // new
 38            //
 39 mike  1.6  // Revision 1.5  2001/01/30 08:00:43  karl
 40            // DOC++ Documentation update for header files
 41            //
 42 karl  1.5  // Revision 1.4  2001/01/28 07:05:18  mike
 43            // added instance name/reference converters
 44            //
 45 mike  1.4  // Revision 1.3  2001/01/28 04:11:03  mike
 46            // fixed qualifier resolution
 47            //
 48 mike  1.3  // Revision 1.2  2001/01/24 16:16:38  karl
 49            // Incorporate Doc++ Comments as documentation into .h files
 50            //
 51 karl  1.2  // Revision 1.1.1.1  2001/01/14 19:53:15  mike
 52            // Pegasus import
 53            //
 54 mike  1.1  //
 55            //END_HISTORY
 56            
 57            #ifndef Pegasus_String_h
 58            #define Pegasus_String_h
 59            
 60            #include <iostream>
 61            #include <cstring>
 62            #include <Pegasus/Common/Config.h>
 63            #include <Pegasus/Common/Char16.h>
 64            #include <Pegasus/Common/Array.h>
 65            
 66            PEGASUS_NAMESPACE_BEGIN
 67            
 68 karl  1.2  /**
 69 mike  1.6      The Pegasus String C++ Class implements the CIM string type
 70 karl  1.2  */
 71 mike  1.1  class PEGASUS_COMMON_LINKAGE String
 72            {
 73            public:
 74            
 75 mike  1.6      /// Default constructor.
 76 mike  1.1      String();
 77 mike  1.6  
 78                /// Copy constructor.
 79 mike  1.1      String(const String& x);
 80 mike  1.6  
 81                /// Initialize with first n characters from x.
 82 mike  1.1      String(const String& x, Uint32 n);
 83 mike  1.6  
 84                /// Initialize with x.
 85 mike  1.1      String(const Char16* x);
 86 mike  1.6  
 87                /// Initialize with first n characters of x.
 88 mike  1.1      String(const Char16* x, Uint32 n);
 89 mike  1.6  
 90                /// Initialize from a plain old C-String:
 91 mike  1.1      String(const char* x);
 92 mike  1.6  
 93                /// Initialize from the first n characters of a plain old C-String:
 94 mike  1.1      String(const char* x, Uint32 n);
 95            
 96 mike  1.6      /// Release all resources.
 97                ~String() 
 98                {
 99                }
100            
101                /// Assign this string with x.
102 mike  1.1      String& operator=(const String& x) { _rep = x._rep; return *this; }
103 mike  1.6  
104                /// Assign this string with x.
105 mike  1.1      String& operator=(const Char16* x) { assign(x); return *this; }
106 mike  1.6  
107 karl  1.8      /// Assign this string with x.
108 mike  1.1      String& assign(const String& x) { _rep = x._rep; return *this; }
109 mike  1.6  
110                /// Assign this string with x.
111 mike  1.1      String& assign(const Char16* x);
112 mike  1.6  
113                /// Assign this string with first n characters of x.
114 mike  1.1      String& assign(const Char16* x, Uint32 n);
115 mike  1.6  
116                /// Assign this string with the plain old C-String x.
117 mike  1.1      String& assign(const char* x);
118 mike  1.6  
119                /// Assign this string with first n characters of the plain old C-String x.
120 mike  1.1      String& assign(const char* x, Uint32 n);
121 mike  1.6  
122                /// Clear this string. After calling clear(), getLength() will return 0.
123 mike  1.1      void clear() { _rep.clear(); _rep.append('\0'); }
124 mike  1.4  
125 mike  1.6      /** Reserves memory for capacity characters. Notice that this does not
126            	change the size of the string (getSize() returns what it did before).
127            	If the capacity of the string is already greater or equal to the
128            	capacity argument, this method has no effect. After calling reserve(),
129            	getCapicty() returns a value which is greater or equal to the 
130            	capacity argument.
131 karl  1.2      */
132 mike  1.1      void reserve(Uint32 capacity) { _rep.reserve(capacity + 1); }
133 mike  1.4  
134 mike  1.6      /// Returns the length of the string.
135 mike  1.1      Uint32 getLength() const { return _rep.getSize() - 1; }
136 mike  1.4  
137 mike  1.6      /// Returns a pointer to the first character in the string string.
138 mike  1.1      const Char16* getData() const { return _rep.getData(); }
139            
140 mike  1.6      /** Allocates an 8 bit representation of this string. The user is 
141            	responsible for freeing the result. If any characters are truncated, 
142            	a TruncatedCharacter exception is thrown. This exception may
143            	be suppressed by passing true as the noThrow argument. Extra
144            	characters may be allocated at the end of the new string by
145            	passing a non-zero value to the extraBytes argument.
146                */
147 mike  1.1      char* allocateCString(Uint32 extraBytes = 0, Boolean noThrow = false) const;
148            
149 mike  1.6      /** Append the given string to a C-string. If the length is not Uint32(-1),
150            	then the lesser of the the length argument and the length of this 
151            	string is truncated. Otherwise, the entire string is trunctated. The 
152            	TruncatedCharacter exception is thrown if any characters are truncated.
153 karl  1.2      */
154 mike  1.1      void appendToCString(
155 karl  1.2  	char* str,
156 mike  1.1  	Uint32 length = Uint32(-1),
157            	Boolean noThrow = false) const;
158            
159 mike  1.6      /// Returns the Ith character of the string.
160 mike  1.1      Char16& operator[](Uint32 i);
161            
162 mike  1.6      /// Returns the Ith character of the string (const version).
163 mike  1.1      const Char16 operator[](Uint32 i) const;
164 mike  1.6  
165                /// Append the given character to the string.
166 karl  1.2      String& append(const Char16& c)
167                {
168            	_rep.insert(_rep.getSize() - 1, c);
169 mike  1.1  	return *this;
170                }
171 mike  1.6  
172                /// Append n characters from str to this string.
173 mike  1.1      String& append(const Char16* str, Uint32 n);
174 mike  1.6  
175                /// Append the characters of str to this string.
176 karl  1.2      String& append(const String& str)
177 mike  1.1      {
178            	return append(str.getData(), str.getLength());
179                }
180 mike  1.6  
181                /// Append the characters of str to this string.
182 mike  1.1      String& operator+=(const String& x)
183                {
184            	return append(x);
185                }
186 mike  1.6  
187                /// Append the character given by c to this string.
188 mike  1.1      String& operator+=(Char16 c)
189                {
190            	return append(c);
191                }
192 mike  1.6  
193                /// Append the character given by c to this string.
194 mike  1.1      String& operator+=(char c)
195                {
196            	return append(Char16(c));
197                }
198 mike  1.3  
199 mike  1.6      /** Remove size characters from the string starting at the given
200            	position. If size is -1, then all characters after pos are removed.
201                */
202 mike  1.1      void remove(Uint32 pos, Uint32 size = Uint32(-1));
203 mike  1.3  
204 karl  1.8      /** Return a new string which is initialzed with <TT>length</TT> 
205            	characters from this string starting at <TT>pos</TT>. 
206            	@param <TT>pos</TT> is the positon in string to start getting the 
207            	substring. 
208            	@param <TT>length</TT> is the number of characters to get. If length
209            	is -1, then all characters after pos are added to the new string.
210            	@return String with the defined substring.
211 mike  1.6      */
212 mike  1.1      String subString(Uint32 pos, Uint32 length = Uint32(-1)) const;
213 mike  1.3  
214 mike  1.7      /** Find the position of the first occurence of the character c.
215            	If the character is not found, -1 is returned.
216 mike  1.6      */
217 mike  1.1      Uint32 find(Char16 c) const;
218 mike  1.7  
219                /** Same as find() but start looking in reverse (last character first).
220                */
221                Uint32 reverseFind(Char16 c) const;
222 mike  1.3  
223 mike  1.6      /** Compare the first n characters of the two strings. Return -1 if s1
224            	is lexographically less than s2. If they are equavalent return 0.
225            	Otherwise return 1.
226                */
227 mike  1.1      static int compare(const Char16* s1, const Char16* s2, Uint32 n);
228 mike  1.3  
229 mike  1.6      /** Compare the two null-terminated strings. If s1 is less than s2,
230            	return -1; if equal return 0; otherwise, return 1.
231                */
232 mike  1.1      static int compare(const Char16* s1, const Char16* s2);
233 mike  1.3  
234 mike  1.9      /// Return true if the two strins are equal.
235                static Boolean equal(const String& x, const String& y);
236            
237                /// Return true if the two strins are equal.
238                static Boolean equal(const String& x, const Char16* y);
239            
240                /// Return true if the two strins are equal.
241                static Boolean equal(const Char16* x, const String& y);
242            
243                /// Return true if the two strins are equal.
244                static Boolean equal(const String& x, const char* y);
245            
246                /// Return true if the two strins are equal.
247                static Boolean equal(const char* x, const String& y);
248            
249 mike  1.6      /// Convert the plain old C-string to lower case:
250                static void toLower(char* str);
251            
252 karl  1.10     /**	EMPTY - Represent an empty string.
253 mike  1.6  	This member is used to represent empty strings. Using this member
254 karl  1.10 	avoids an expensive construction of an empty string (e.g., String()).
255 mike  1.6      */
256 mike  1.1      static const String EMPTY;
257            
258            private:
259            
260                static Uint32 _min(Uint32 x, Uint32 y) { return x < y ? x : y; }
261            
262 karl  1.2      Array<Char16> _rep;
263 mike  1.1  };
264            
265 mike  1.9  inline Boolean operator==(const String& x, const String& y)
266            {
267                return String::equal(x, y);
268            }
269 mike  1.1  
270 mike  1.9  inline Boolean operator==(const String& x, const char* y)
271            {
272                return String::equal(x, y);
273            }
274 mike  1.1  
275 mike  1.9  inline Boolean operator==(const char* x, const String& y)
276            {
277                return String::equal(x, y);
278            }
279 mike  1.1  
280            inline Boolean operator!=(const String& x, const String& y)
281            {
282 mike  1.9      return !String::equal(x, y);
283 mike  1.1  }
284            
285 mike  1.6  PEGASUS_COMMON_LINKAGE std::ostream& operator<<(
286                std::ostream& os, 
287                const String& x);
288 mike  1.1  
289            inline String operator+(const String& x, const String& y)
290            {
291                return String(x).append(y);
292            }
293            
294            inline Boolean operator<(const String& x, const String& y)
295            {
296                return String::compare(x.getData(), y.getData()) < 0;
297            }
298            
299            inline Boolean operator<=(const String& x, const String& y)
300            {
301                return String::compare(x.getData(), y.getData()) <= 0;
302            }
303            
304            inline Boolean operator>(const String& x, const String& y)
305            {
306                return String::compare(x.getData(), y.getData()) > 0;
307            }
308            
309 mike  1.1  inline Boolean operator>=(const String& x, const String& y)
310            {
311                return String::compare(x.getData(), y.getData()) >= 0;
312            }
313 mike  1.6  
314            /** Return a version of this string whose characters have been shifted
315                to lower case.
316            */
317            PEGASUS_COMMON_LINKAGE String ToLower(const String& str);
318            
319 mike  1.1  
320            PEGASUS_NAMESPACE_END
321            
322            #endif /* Pegasus_String_h */
323 karl  1.2  

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2