version 1.39.2.6, 2001/08/22 08:11:08
|
version 1.106, 2014/06/14 16:14:04
|
|
|
//%///////////////////////////////////////////////////////////////////////////// |
//%LICENSE//////////////////////////////////////////////////////////////// |
// |
|
// Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM |
|
// | // |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
// Licensed to The Open Group (TOG) under one or more contributor license |
// of this software and associated documentation files (the "Software"), to |
// agreements. Refer to the OpenPegasusNOTICE.txt file distributed with |
// deal in the Software without restriction, including without limitation the |
// this work for additional information regarding copyright ownership. |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
// Each contributor licenses this file to you under the OpenPegasus Open |
// sell copies of the Software, and to permit persons to whom the Software is |
// Source License; you may not use this file except in compliance with the |
// furnished to do so, subject to the following conditions: |
// License. |
// | // |
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN |
// Permission is hereby granted, free of charge, to any person obtaining a |
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED |
// copy of this software and associated documentation files (the "Software"), |
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
// to deal in the Software without restriction, including without limitation |
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
// and/or sell copies of the Software, and to permit persons to whom the |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
// Software is furnished to do so, subject to the following conditions: |
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
// | // |
//============================================================================== |
// The above copyright notice and this permission notice shall be included |
|
// in all copies or substantial portions of the Software. |
// | // |
// Author: Mike Brasher (mbrasher@bmc.com) |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
// | // |
// Modified By: |
////////////////////////////////////////////////////////////////////////// |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#ifndef Pegasus_String_h | #ifndef Pegasus_String_h |
#define Pegasus_String_h | #define Pegasus_String_h |
| |
|
#ifdef PEGASUS_OS_HPUX |
|
# ifdef HPUX_IA64_NATIVE_COMPILER |
#include <iostream> | #include <iostream> |
#include <fstream> |
# else |
#include <cstring> |
# include <iostream.h> |
|
# endif |
|
#else |
|
# include <iostream> |
|
#endif |
#include <Pegasus/Common/Config.h> | #include <Pegasus/Common/Config.h> |
#include <Pegasus/Common/Char16.h> | #include <Pegasus/Common/Char16.h> |
#include <Pegasus/Common/Array.h> |
#include <Pegasus/Common/Linkage.h> |
|
#include <cstdarg> |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
class String; |
|
struct StringRep; |
|
|
/** | /** |
The Pegasus String C++ Class implements the CIM string type. |
The CString class provides access to an 8-bit String representation. |
This class is based on the general handle/representation pattern |
|
defined for all the Pegasus objects. However, it differes from |
|
most in that it implements "copy on assign" all of the others implement |
|
"no copy on assign" semantics. The string class uses the Array class and |
|
implements an array of characters. |
|
*/ | */ |
class PEGASUS_COMMON_LINKAGE String |
class PEGASUS_COMMON_LINKAGE CString |
{ | { |
public: | public: |
| |
/** Default constructor without parameters. This constructor creates a |
/** |
null string |
Constructs a CString object with a null string value. |
<pre> |
|
String test; |
|
</pre> |
|
*/ | */ |
String(); |
CString(); |
| |
/// Copy constructor. |
/** |
String(const String& x); |
Constructs an independent copy of a CString object. |
|
@param cstr The CString instance to copy. |
|
*/ |
|
CString(const CString& cstr); |
| |
/// Initialize with first n characters from x. |
/** |
String(const String& x, Uint32 n); |
Destructs a CString object. |
|
*/ |
|
~CString(); |
| |
/// Initialize with x. |
/** |
String(const Char16* x); |
Copies the value of another CString object. |
|
@param cstr The CString object from which to copy the value. |
|
@return A reference to the target CString object with its newly |
|
assigned value. |
|
*/ |
|
CString& operator=(const CString& cstr); |
| |
/// Initialize with first n characters of x. |
/** |
String(const Char16* x, Uint32 n); |
Gets the CString's data as a C string pointer. IMPORTANT: The |
|
returned pointer refers to memory owned by the CString object. The |
|
caller must not free this memory. The returned pointer is valid only |
|
until the CString object is destructed or reassigned. Use of this |
|
operator on a temporary CString object may result in a memory error. |
|
For example, this usage is invalid: |
| |
/// Initialize from a plain old C-String: |
const char* cstr = String("Hello").getCString(); |
String(const char* x); |
printf(cstr); |
| |
/// Initialize from the first n characters of a plain old C-String: |
@return Returns a const char pointer to the CString's data. |
String(const char* x, Uint32 n); |
*/ |
|
operator const char*() const; |
| |
/// String destructor. Used by the representation of the String object |
private: |
~String(); |
|
| |
/** Assign this string with x. |
CString(char* cstr); |
<pre> |
|
String t1 = "abc"; |
|
String t2 = t1; |
|
</pre> |
|
*/ |
|
String& operator=(const String& x) { return assign(x); } |
|
| |
/// Assign this string with Char16 x. |
friend class String; |
String& operator=(const Char16* x) { assign(x); return *this; } |
|
| |
/** Assign this string with String x |
char* _rep; |
@param x String to assign |
}; |
@return Returns the String |
|
*/ |
|
String& assign(const String& x); |
|
| |
/// Assign this string with x. |
/** |
String& assign(const Char16* x); |
This class implements the CIM string type. The intrinsic string format |
|
is UTF-16, which is a superset of the UCS-2 characters allowed in CIM |
|
strings. Facilities are provided for converting to and from UTF-8 |
|
character strings. |
|
|
|
Many of the method interfaces refer to a number of characters. In all |
|
cases, these characters are counted as 8- or 16-bit memory chunks rather |
|
than logical UTF-8 or UTF-16 character chains. |
|
*/ |
|
class PEGASUS_COMMON_LINKAGE String |
|
{ |
|
public: |
| |
/// Assign this string with first n characters of x. |
/** |
String& assign(const Char16* x, Uint32 n); |
Represents an empty string. This value may be used as a convenience |
|
to avoid construction of an empty String object. |
|
*/ |
|
static const String EMPTY; |
| |
/// Assign this string with the plain old C-String x. |
/** |
String& assign(const char* x); |
Constructs an empty String. |
|
*/ |
|
String(); |
| |
/// Assign this string with first n characters of the plain old C-String x. |
/** |
String& assign(const char* x, Uint32 n); |
Constructs a String with the value of another String. |
|
@param str The String from which to copy the value. |
|
*/ |
|
String(const String& str); |
| |
/** clear - Clear this string. After calling clear(), size() will return 0. |
/** |
<pre> |
Constructs a String with a specified number of characters of the |
String test = "abc"; |
value of another String. |
test.clear(); // String test is now NULL (length == 0) |
@param str The String from which to copy the value. |
</pre> |
@param n A Uint32 specifying the number of characters to copy. |
|
@exception IndexOutOfBoundsException If the specified String does not |
|
contain the specified number of characters. |
|
@exception bad_alloc If the construction fails because of a memory |
|
allocation failure. |
*/ | */ |
void clear(); |
String(const String& str, Uint32 n); |
| |
|
/** |
|
Constructs a String with the value from a Char16 buffer. |
|
@param str The Char16 buffer from which to copy the value. |
|
@exception NullPointer If the buffer pointer is NULL. |
|
@exception bad_alloc If the construction fails because of a memory |
|
allocation failure. |
|
*/ |
|
String(const Char16* str); |
| |
/** reserve - Reserves memory for capacity characters. Notice that this does |
/** |
not |
Constructs a String with a specified number of characters of the |
change the size of the string (size() returns what it did before). |
value from a Char16 buffer. |
If the capacity of the string is already greater or equal to the |
@param str The Char16 buffer from which to copy the value. |
capacity argument, this method has no effect. After calling reserve(), |
@param n A Uint32 specifying the number of characters to copy. |
getCapicty() returns a value which is greater or equal to the |
@exception NullPointer If the buffer pointer is NULL. |
capacity argument. |
@exception bad_alloc If the construction fails because of a memory |
@param capacity defines the capacity in characters to reserve. |
allocation failure. |
*/ | */ |
void reserve(Uint32 capacity); |
String(const Char16* str, Uint32 n); |
| |
/** Returns the length of the String object. |
/** |
@return Length of the string in characters. |
Constructs a String with the value from a C string in UTF-8 format. |
<pre> |
@param str The C string from which to copy the value. |
String s = "abcd"; |
@exception NullPointer If the C string pointer is NULL. |
assert(s.size() == 4); |
@exception bad_alloc If the construction fails because of a memory |
</pre> |
allocation failure. |
|
@exception Exception If the C string contains invalid UTF-8. |
*/ | */ |
Uint32 size() const { return _rep.size() - 1; } |
String(const char* str); |
| |
/** getData Returns a pointer to the first character in the |
/** |
null-terminated string string of the String object. |
Constructs a String with a specified number of characters of the |
@return Pointer to the first character of the String object |
value from a C string in UTF-8 format. |
<pre> |
@param str The C string from which to copy the value. |
String t1 = "abc"; |
@param n A Uint32 specifying the number of characters to copy. |
const Char16* q = t1.getData(); |
@exception NullPointer If the C string pointer is NULL. |
</pre> |
@exception bad_alloc If the construction fails because of a memory |
|
allocation failure. |
|
@exception Exception If the C string contains invalid UTF-8. |
*/ | */ |
const Char16* getData() const { return _rep.getData(); } |
String(const char* str, Uint32 n); |
| |
/** AallocateCString - llocates an 8 bit representation of this String |
/** |
object. The user is responsible for freeing the result. If any |
Destructs a String object. |
characters are truncated, a TruncatedCharacter exception is thrown. |
*/ |
This exception may be suppressed by passing true as the noThrow |
~String(); |
argument. Extra characters may be allocated at the end of the |
|
new string by passing a non-zero value to the extraBytes argument. |
|
| |
@param extraBytes Defines the number of extra characters to be |
/** |
allocated at the end of the new string. Default is zero. |
Assigns the value of a String to the value of another String. |
|
@param str The String from which to copy the value. |
|
@return A reference to the target String object with its newly |
|
assigned value. |
|
@exception bad_alloc If the assignment fails because of a memory |
|
allocation failure. |
|
*/ |
|
String& operator=(const String& str); |
| |
@param noThrow If true, no exception will be thrown if characters |
/** |
are truncated |
Assigns the value of a String to the value of another String. |
|
@param str The String from which to copy the value. |
|
@return A reference to the target String object with its newly |
|
assigned value. |
|
@exception bad_alloc If the assignment fails because of a memory |
|
allocation failure. |
|
*/ |
|
String& assign(const String& str); |
| |
@return pointer to the new representation of the string |
/** |
|
Assigns the value of a String to the value in a Char16 buffer. |
|
@param str The Char16 buffer from which to copy the value. |
|
@return A reference to the target String object with its newly |
|
assigned value. |
|
@exception NullPointer If the buffer pointer is NULL. |
|
@exception bad_alloc If the assignment fails because of a memory |
|
allocation failure. |
|
*/ |
|
String& assign(const Char16* str); |
| |
@exception Throws TruncatedCharacter exception if any characters are |
/** |
truncated |
Assigns the value of a String with a specified number of characters |
<pre> |
of the value from a Char16 buffer. |
String test = "abc"; |
@param str The Char16 buffer from which to copy the value. |
char* p = test.allocateCString(); |
@param n A Uint32 specifying the number of characters to copy. |
... |
@return A reference to the target String object with its newly |
delete [] p; |
assigned value. |
</pre> |
@exception NullPointer If the buffer pointer is NULL. |
|
@exception bad_alloc If the assignment fails because of a memory |
|
allocation failure. |
*/ | */ |
char* allocateCString(Uint32 extraBytes = 0, Boolean noThrow = false) const; |
String& assign(const Char16* str, Uint32 n); |
| |
/** appendToCString - Append the given String object to a C-string. If the |
/** |
length is not PEG_NOT_FOUND, then the lesser of the the length argument |
Assigns the value of a String to the value from a C string in UTF-8 |
and he length of this string is truncated. Otherwise, the entire string |
format. |
is trunctated. The TruncatedCharacter exception is thrown if any |
@param str The C string from which to copy the value. |
characters are truncated. |
@return A reference to the target String object with its newly |
@param str Char pointer to the string to append |
assigned value. |
@param length Length to append or PEG_NOT_FOUND (Uint32(-1) |
@exception NullPointer If the C string pointer is NULL. |
@param noThrow - If false, throw the "TruncatedCharacter" exception of |
@exception bad_alloc If the assignment fails because of a memory |
any characters are truncated |
allocation failure. |
@return void |
@exception Exception If the C string contains invalid UTF-8. |
@exception Throws TruncatedCharacter exception of characters are |
|
truncated and noThrow parameter is false. |
|
<pre> |
|
const char STR0[] = "one two three four"; |
|
String s = STR0; |
|
const char STR1[] = "zero "; |
|
char* tmp = new char[strlen(STR1) + s.size() + 1]; |
|
strcpy(tmp, STR1); |
|
s.appendToCString(tmp, 7); |
|
assert(strcmp(tmp, "zero one two") == 0); |
|
</pre> |
|
*/ | */ |
void appendToCString( |
String& assign(const char* str); |
char* str, |
|
Uint32 length = PEG_NOT_FOUND, |
/** |
Boolean noThrow = false) const; |
Assigns the value of a String with a specified number of characters |
|
of the value from a C string in UTF-8 format. |
/** Returns the Ith character of the String object. |
@param str The C string from which to copy the value. |
@exception - Throws exception "OutofBounds" if the index |
@param n A Uint32 specifying the number of characters to copy. |
is outside the length of the string. |
@return A reference to the target String object with its newly |
<pre> |
assigned value. |
String t1 = "abc; |
@exception NullPointer If the C string pointer is NULL. |
Char16 c = t1[1]; // character b |
@exception bad_alloc If the assignment fails because of a memory |
</pre> |
allocation failure. |
|
@exception Exception If the C string contains invalid UTF-8. |
*/ | */ |
Char16& operator[](Uint32 i); |
String& assign(const char* str, Uint32 n); |
| |
/** Returns the Ith character of the String (const version). |
/** |
@exception - Throws exception "OutofBounds" if the index |
Sets a String value to the empty String. |
is outside the length of the string. |
*/ |
|
void clear(); |
| |
|
/** |
|
Reserves memory for a specified number of (16-bit) characters. |
|
This method does not change the size() of the string or any other |
|
external behavior. If the capacity of the string is already greater |
|
than or equal to the specified size, this method has no effect. The |
|
capacity of a String is set only for performance reasons. |
|
@param capacity A Uint32 specifying the number of characters the |
|
String should be prepared to hold. |
*/ | */ |
const Char16 operator[](Uint32 i) const; |
void reserveCapacity(Uint32 capacity); |
| |
/** Append the given character to the string. |
/** |
<pre> |
Returns the number of characters in a String value. No termination |
String s4 = "Hello"; |
character is included in the count. For example, String("abcd").size() |
s4.append(Char16(0x0000)) |
returns 4. |
</pre> |
|
*/ | */ |
String& append(const Char16& c); |
Uint32 size() const; |
| |
/// Append n characters from str to this String object. |
/** |
String& append(const Char16* str, Uint32 n); |
Gets a null-terminated Char16 buffer containing the String value. |
|
The buffer is valid until the original String object is modified or |
|
destructed. |
|
@return A pointer to a null-terminated Char16 buffer containing the |
|
String value. |
|
*/ |
|
const Char16* getChar16Data() const; |
| |
/// Append the characters of str to this String object. |
/** |
String& append(const String& str) |
Gets a CString object containing the String value in UTF-8 format. |
{ |
Important: A character pointer extracted from a CString object is |
return append(str.getData(), str.size()); |
only valid while the CString object exists and is unmodified. (See |
} |
the CString documentation.) Thus, in the following example, the |
|
variable p holds a dangling (invalid) pointer: |
|
<pre> |
|
const char * p = (const char *)test.getCString(); |
|
</pre> |
|
This situation can be corrected by declaring a CString variable in |
|
the same scope. |
|
|
|
@return A CString object containing the String value in UTF-8 format. |
|
@exception bad_alloc If the operation fails because of a memory |
|
allocation failure. |
|
*/ |
|
CString getCString() const; |
| |
/** Overload operator += appends the parameter String to this String. |
/** |
@param String to append. |
Gets a specified character from the String value. |
@return This String |
@param index Index of the character to access. |
<pre> |
@return The Char16 character at the specified index. |
String test = "abc"; |
@exception IndexOutOfBoundsException If the String does not contain a |
test += "def"; |
character at the specified index. |
assert(test == "abcdef"); |
|
</pre> |
|
*/ | */ |
String& operator+=(const String& x) |
Char16& operator[](Uint32 index); |
{ |
|
return append(x); |
|
} |
|
| |
/** Append the character given by c to this String object. |
/** |
@param c Single character to be appended |
Gets a specified character from the String value. |
@return String with appended character |
@param index Index of the character to access. |
|
@return The Char16 character at the specified index. |
|
@exception IndexOutOfBoundsException If the String does not contain a |
|
character at the specified index. |
*/ | */ |
String& operator+=(Char16 c) |
const Char16 operator[](Uint32 index) const; |
{ |
|
return append(c); |
|
} |
|
| |
/** Append the character given by c to this string. |
/** |
<pre> |
Appends a character to the String. |
String t1 = "abc"; |
@param c The Char16 character to append. |
t1 += 'd' |
@return A reference to the String object containing the newly appended |
assert(t1 == "abcd"); |
character. |
</pre> |
@exception bad_alloc If the append fails because of a memory |
|
allocation failure. |
*/ | */ |
String& operator+=(char c) |
String& append(const Char16& c); |
{ |
|
return append(Char16(c)); |
|
} |
|
| |
/** Remove size characters from the string starting at the given |
/** |
position. If size is PEG_NOT_FOUND, then all characters after pos are |
Appends a specified number of characters to the String from a Char16 |
removed. |
buffer. |
@param pos Position in string to start remove |
@param str The Char16 buffer from which to append the characters. |
@param size Number of characters to remove. Default is PEG_NOT_FOUND |
@param n A Uint32 specifying the number of characters to append from |
(Uint32(-1) which causes all characters after pos to be removed |
the buffer. |
<pre> |
@return A reference to the String object containing the newly appended |
String s; |
characters. |
s = "abc"; |
@exception NullPointer If the buffer pointer is NULL. |
s.remove(0, 1); |
@exception bad_alloc If the append fails because of a memory |
assert(String::equal(s, "bc")); |
allocation failure. |
assert(s.size() == 2); |
|
s.remove(0); |
|
assert(String::equal(s, "")); |
|
assert(s.size() == 0); |
|
</pre> |
|
@exception throws "OutOfBounds" exception if size is greater than |
|
length of String plus starting position for remove. |
|
*/ | */ |
void remove(Uint32 pos, Uint32 size = PEG_NOT_FOUND); |
String& append(const Char16* str, Uint32 n); |
| |
/** Return a new String which is initialzed with <TT>length</TT> |
/** |
characters from this string starting at <TT>pos</TT>. |
Appends a String value to the String. |
@param <TT>pos</TT> is the positon in string to start getting the |
@param str The String to append. |
substring. |
@return A reference to the String object containing the newly appended |
@param <TT>length</TT> is the number of characters to get. If length |
characters. |
is PEG_NOT_FOUND, then all characters after pos are added to the new |
@exception bad_alloc If the append fails because of a memory |
string. |
allocation failure. |
@return String with the defined substring. |
|
<pre> |
|
s = "abcdefg"; |
|
s.remove(3); |
|
assert(String::equal(s, "abc")); |
|
</pre> |
|
*/ | */ |
String subString(Uint32 pos, Uint32 length = PEG_NOT_FOUND) const; |
String& append(const String& str); |
| |
/** Find the position of the first occurence of the character c. |
/** |
If the character is not found, PEG_NOT_FOUND is returned. |
Removes a specified number of characters from the String starting at a |
@param c Char to be found in the String |
given index. If the number of characters to remove is specified as |
@return Position of the character in the string or PEG_NOT_FOUND if not |
PEG_NOT_FOUND, then all characters from the index to the end of the |
found. |
String are removed. |
|
@param index Uint32 position in String from which to remove characters. |
|
@param size A Uint32 specifying the number of characters to remove. |
|
The default value is PEG_NOT_FOUND, which means all characters |
|
from the index to the end of the String are to be removed. |
|
@exception IndexOutOfBoundsException If the index plus the size (if not |
|
PEG_NOT_FOUND) is greater than the number of characters in the |
|
String. |
*/ | */ |
Uint32 find(Char16 c) const; |
void remove(Uint32 index, Uint32 size = PEG_NOT_FOUND); |
| |
/** Same as above but starts searching from the given position. */ |
/** |
Uint32 find(Uint32 pos, Char16 c) const; |
Creates a new String containing up to the specified number of |
|
characters from the specified index in the String. |
|
@param index A Uint32 specifying the index at which to copy characters |
|
into the new String. |
|
@param n A Uint32 specifying the maximum number of characters to copy |
|
into the new String. If the value is PEG_NOT_FOUND or is greater |
|
than the number of characters from the index to the end of the |
|
String, the new String contains all characters from the index to |
|
the end of the String. |
|
@return A new String containing up to the specified number of |
|
characters from the specified index in the String. |
|
@exception bad_alloc If the operation fails because of a memory |
|
allocation failure. |
|
*/ |
|
String subString(Uint32 index, Uint32 n = PEG_NOT_FOUND) const; |
| |
/** Find the position of the first occurence of the string object. |
/** |
This function finds one string inside another |
Finds the index of the first occurrence of a specified character in |
If the matching substring is not found, PEG_NOT_FOUND is returned. |
the String. If the character is not found, PEG_NOT_FOUND is returned. |
@param s String object to be found in the String |
@param c The Char16 value to find in the String. |
@return Position of the substring in the String or PEG_NOT_FOUND if not |
@return The Uint32 index of the character in the String if found, |
found. |
PEG_NOT_FOUND otherwise. |
*/ | */ |
Uint32 find(const String& s) const; |
Uint32 find(Char16 c) const; |
| |
/** Find substring |
/** |
@ param 16 bit character pointer |
Finds the index of the first occurrence of a specified character in |
@seealso find |
the String beginning at a specified index. If the character is not |
@return Position of the substring in the String or PEG_NOT_FOUND if not |
found, PEG_NOT_FOUND is returned. |
found. |
@param c The Char16 value to find in the String. |
*/ |
@param index The Uint32 index at which to start the search. |
Uint32 find(const Char16* s) const; |
@return The Uint32 index of the character in the String if found, |
|
PEG_NOT_FOUND otherwise. |
/** find substring |
|
@param s char* to substring |
|
@return Position of the substring in the String or PEG_NOT_FOUND if not |
|
found. |
|
*/ | */ |
Uint32 find(const char* s) const; |
Uint32 find(Uint32 index, Char16 c) const; |
| |
/** reverseFind - Same as find() but start looking in reverse (last |
/** |
character first). |
Finds the index of the first occurrence of a specified String value in |
@param c Char16 character to find in String. |
the String. If the String value is not found, PEG_NOT_FOUND is |
@Seealso find |
returned. |
@return Position of the character in the string or PEG_NOT_FOUND if not |
@param s The String value to find in the String. |
found. |
@return The Uint32 index of the beginning of the String value if found, |
|
PEG_NOT_FOUND otherwise. |
|
*/ |
|
Uint32 find(const String& s) const; |
| |
NOTE: This function is defined only for char* input, not for |
/** |
String. |
Finds the index of the last occurrence of a specified character in |
|
the String. If the character is not found, PEG_NOT_FOUND is returned. |
|
@param c The Char16 value to find in the String. |
|
@return The Uint32 index of the character in the String if found, |
|
PEG_NOT_FOUND otherwise. |
*/ | */ |
Uint32 reverseFind(Char16 c) const; | Uint32 reverseFind(Char16 c) const; |
| |
/** Converts all characters in this string to lower case. |
/** |
|
Converts all characters in the String to lower case. |
*/ | */ |
void toLower(); | void toLower(); |
| |
/** Translate any occurences of fromChar to toChar. |
/** |
|
Constructs a String based on printf specifications. For some |
|
compilers the PEGASUS_FORMAT generates warning messages if |
|
the format string does not match the input arguments. |
|
@param format const char * The format specification as defined |
|
for printf. The format specification corresponds to the |
|
standard C++ printf format specification |
|
@param ... The list of arguments that will be formated. |
*/ | */ |
void translate(Char16 fromChar, Char16 toChar); |
PEGASUS_FORMAT(2,3) |
|
void appendPrintf(const char* format, ...); |
| |
/** Method for printing a string. |
#ifdef PEGASUS_USE_EXPERIMENTAL_INTERFACES |
|
/** |
|
<I><B>Experimental Interface</B></I><BR> |
|
Converts all characters in the String to upper case. |
*/ | */ |
void print() const; |
void toUpper(); |
|
#endif |
| |
/** Compare the first n characters of the two strings.. |
/** |
@param s1 First null-terminated string for the comparison. |
Compares the first n characters of two String objects. |
@param s2 Second null-terminated string for the comparison. |
@param s1 The first String to compare. |
@param n Number of characters to compare. |
@param s2 The second String to compare. |
@return Return -1 if s1 is lexographically less than s2. If they are |
@param n The maximum number of characters to compare. |
equavalent return 0. Otherwise return 1. |
@return A negative integer if the first n characters of s1 are |
|
lexographically less than s2, 0 if the first n characters of s1 |
|
and s2 are equal, and a positive integer otherwise. |
*/ | */ |
static int compare(const Char16* s1, const Char16* s2, Uint32 n); |
static int compare(const String& s1, const String& s2, Uint32 n); |
| |
/** Just like one above except ignores case differences. |
/** |
|
Compares two String objects. (Note: Use the comparison |
|
operators < <= > >= to compare String objects.) |
|
@param s1 The first String to compare. |
|
@param s2 The second String to compare. |
|
@return A negative integer if s1 is lexographically less than s2, |
|
0 if s1 and s2 are equal, and a positive integer otherwise. |
*/ | */ |
static int compareNoCase(const char* s1, const char* s2, Uint32 n); |
static int compare(const String& s1, const String& s2); |
| |
static int compareNoCase(const char* s1, const char* s2); |
/** |
|
Compares two String objects, ignoring case differences. |
/** Compare two null-terminated strings. |
@param s1 The first String to compare. |
@param s1 First null-terminated string for the comparison. |
@param s2 The second String to compare. |
@param s2 Second null-terminated string for the comparison. |
@return A negative integer if s1 is lexographically less than s2, |
@return If s1 is less than s2, return -1; if equal return 0; |
0 if s1 and s2 are equal, and a positive integer otherwise. |
otherwise, return 1. |
(Case differences are ignored in all cases.) |
|
|
NOTE: Use the comparison operators <,<= > >= to compare |
|
String objects. |
|
*/ | */ |
static int compare(const Char16* s1, const Char16* s2); |
static int compareNoCase(const String& s1, const String& s2); |
| |
/** Compare two String objects for equality. |
/** |
@param s1 First <TT>String</TT> for comparison. |
Compares two String objects for equality. For example, |
@param s2 Second <TT>String</TT> for comparison. |
|
|
|
@return Boolean true if the two strings are equal. |
|
<pre> | <pre> |
String s1 = "Hello World"; | String s1 = "Hello World"; |
String s2 = s1; | String s2 = s1; |
String s3(s2); |
assert(String::equal(s1, s2)); |
assert(String::equal(s1, s3)); |
|
</pre> | </pre> |
|
@param s1 The first String to compare. |
|
@param s2 The second String to compare. |
|
@return True if the two strings are equal, false otherwise. |
*/ | */ |
static Boolean equal(const String& x, const String& y); |
static Boolean equal(const String& s1, const String& s2); |
| |
/// Return true if the two strings are equal. |
/** |
static Boolean equal(const String& x, const Char16* y); |
Compares two strings and returns true if they are equal independent of |
|
the case of the characters. |
|
@param ... Variable arguments as defined for printf |
|
*/ |
|
static Boolean equalNoCase(const String& s1, const String& s2); |
| |
/// Return true if the two strings are equal. |
#ifdef PEGASUS_USE_EXPERIMENTAL_INTERFACES |
static Boolean equal(const Char16* x, const String& y); |
|
| |
/// Return true if the two strings are equal. |
String(const String& s1, const String& s2); |
static Boolean equal(const String& x, const char* y); |
|
| |
/// Return true if the two strings are equal. |
String(const String& s1, const char* s2); |
static Boolean equal(const char* x, const String& y); |
|
| |
static Boolean equalNoCase(const String& x, const String& y); |
String(const char* s1, const String& s2); |
| |
/// Convert the plain old C-string to lower case: |
String& operator=(const char* str); |
static void toLower(char* str); |
|
| |
/** EMPTY - Represent an empty string. |
Uint32 find(const char* s) const; |
This member is used to represent empty strings. Using this member |
|
avoids an expensive construction of an empty string (e.g., String()). |
|
*/ |
|
static const String EMPTY; |
|
| |
/** Return true if the str parameter matches the pattern. C-Shell style |
static Boolean equal(const String& s1, const char* s2); |
glob matching is used. |
|
*/ |
|
static Boolean match(const String& str, const String& pattern); |
|
| |
/** Return true if the str parameter matches the pattern. C-Shell style |
static int compare(const String& s1, const char* s2); |
glob matching is used. Ignore case in all comparisons. |
|
*/ |
|
static Boolean matchNoCase(const String& str, const String& pattern); |
|
| |
private: |
String& append(const char* str); |
| |
static Uint32 _min(Uint32 x, Uint32 y) { return x < y ? x : y; } |
String& append(const char* str, Uint32 size); |
| |
Array<Char16> _rep; |
static Boolean equalNoCase(const String& s1, const char* s2); |
}; |
|
| |
/** String operator ==. Test for equality between two strings of any of the |
#endif /* PEGASUS_USE_EXPERIMENTAL_INTERFACES */ |
types String or char*. |
|
@return Boolean - True if the strings are equal |
|
| |
*/ |
private: |
inline Boolean operator==(const String& x, const String& y) |
|
{ |
|
return String::equal(x, y); |
|
} |
|
| |
/** String operator ==. Test for equality between two strings |
StringRep* _rep; |
|
}; |
| |
*/ |
/** |
inline Boolean operator==(const String& x, const char* y) |
Compares two String objects for equality. |
{ |
@param str1 The first String to compare. |
return String::equal(x, y); |
@param str2 The second String to compare. |
} |
@return True if the strings are equal, false otherwise. |
|
*/ |
|
PEGASUS_COMMON_LINKAGE Boolean operator==( |
|
const String& str1, |
|
const String& str2); |
| |
/** String operator ==. Test for equality between two strings |
/** |
|
Compares a String and a C string for equality. |
|
@param str1 The String to compare. |
|
@param str2 The C string to compare. |
|
@return True if the strings are equal, false otherwise. |
|
*/ |
|
PEGASUS_COMMON_LINKAGE Boolean operator==(const String& str1, const char* str2); |
| |
|
/** |
|
Compares a String and a C string for equality. |
|
@param str1 The C string to compare. |
|
@param str2 The String to compare. |
|
@return True if the strings are equal. false otherwise. |
*/ | */ |
inline Boolean operator==(const char* x, const String& y) |
PEGASUS_COMMON_LINKAGE Boolean operator==(const char* str1, const String& str2); |
{ |
|
return String::equal(x, y); |
|
} |
|
| |
/** String operator ==. Test for equality between two strings |
/** |
|
Compares two String objects for inequality. |
|
@param str1 The first String to compare. |
|
@param str2 The second String to compare. |
|
@return False if the strings are equal, true otherwise. |
|
*/ |
|
PEGASUS_COMMON_LINKAGE Boolean operator!=( |
|
const String& str1, |
|
const String& str2); |
| |
|
/** |
|
Writes a String value to an output stream. Characters with a zero value or |
|
with a non-zero high-order byte are written in a hexadecimal encoding. |
|
@param os The output stream to which the String value is written. |
|
@param str The String to write to the output stream. |
|
@return A reference to the output stream. |
*/ | */ |
inline Boolean operator!=(const String& x, const String& y) |
|
{ |
|
return !String::equal(x, y); |
|
} |
|
|
|
PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<( | PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<( |
PEGASUS_STD(ostream)& os, | PEGASUS_STD(ostream)& os, |
const String& x); |
const String& str); |
|
|
/** overload operator + - Concatenates String objects. |
|
| |
|
/** |
|
Concatenates String objects. For example, |
<pre> | <pre> |
String t1 = "abc"; | String t1 = "abc"; |
String t2; | String t2; |
t2 = t1 + "def" | t2 = t1 + "def" |
assert(t2 == "abcdef"); | assert(t2 == "abcdef"); |
</pre> | </pre> |
|
@param str1 The first String to concatenate. |
|
@param str2 The second String to concatenate. |
|
@return The concatenated String. |
*/ | */ |
inline String operator+(const String& x, const String& y) |
PEGASUS_COMMON_LINKAGE String operator+(const String& str1, const String& str2); |
{ |
|
return String(x).append(y); |
|
} |
|
|
|
/** overload operator < - Compares String obects. |
|
<pre> |
|
String t1 = "def"; |
|
String t2 = "a"; |
|
assert (t2 < t1); |
|
</pre> |
|
*/ |
|
inline Boolean operator<(const String& x, const String& y) |
|
{ |
|
return String::compare(x.getData(), y.getData()) < 0; |
|
} |
|
|
|
/** overload operator <= compares String objects. |
|
|
|
*/ |
|
inline Boolean operator<=(const String& x, const String& y) |
|
{ |
|
return String::compare(x.getData(), y.getData()) <= 0; |
|
} |
|
| |
/** Overload operator > compares String objects |
/** |
*/ |
Compares two String objects. |
inline Boolean operator>(const String& x, const String& y) |
@param s1 The first String to compare. |
{ |
@param s2 The second String to compare. |
return String::compare(x.getData(), y.getData()) > 0; |
@return True if s1 is lexographically less than s2, false otherwise. |
} |
*/ |
|
PEGASUS_COMMON_LINKAGE Boolean operator<( |
/** overload operator >= - Compares String objects |
const String& str1, |
*/ |
const String& str2); |
inline Boolean operator>=(const String& x, const String& y) |
|
{ |
|
return String::compare(x.getData(), y.getData()) >= 0; |
|
} |
|
|
|
/** Return a version of this string whose characters have been shifted |
|
to lower case. |
|
*/ |
|
PEGASUS_COMMON_LINKAGE String ToLower(const String& str); |
|
|
|
/** Compare two strings but ignore any case differences. |
|
*/ |
|
PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2); |
|
| |
inline int EqualNoCase(const char* s1, const char* s2) |
/** |
{ |
Compares two String objects. |
return CompareNoCase(s1, s2) == 0; |
@param s1 The first String to compare. |
} |
@param s2 The second String to compare. |
|
@return True if s1 is lexographically less than or equal to s2, |
|
false otherwise. |
|
*/ |
|
PEGASUS_COMMON_LINKAGE Boolean operator<=( |
|
const String& str1, |
|
const String& str2); |
| |
/** Get the next line from the input file. |
/** |
*/ |
Compares two String objects. |
PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line); |
@param s1 The first String to compare. |
|
@param s2 The second String to compare. |
|
@return True if s1 is lexographically greater than s2, false otherwise. |
|
*/ |
|
PEGASUS_COMMON_LINKAGE Boolean operator>( |
|
const String& str1, |
|
const String& str2); |
| |
/* This is an internal class to be used by the internal Pegasus |
/** |
components only. It provides an easy way to create an 8-bit string |
Compares two String objects. |
representation on the fly without calling allocateCString() and |
@param s1 The first String to compare. |
then worrying about deleting the string. The underscore before the |
@param s2 The second String to compare. |
class name denotes that this class is internal, unsupported, undocumented, |
@return True if s1 is lexographically greater than or equal to s2, |
and may be removed in future releases. |
false otherwise. |
*/ | */ |
class _CString |
PEGASUS_COMMON_LINKAGE Boolean operator>=( |
{ |
const String& str1, |
public: |
const String& str2); |
| |
_CString(const String& x) |
#ifdef PEGASUS_USE_EXPERIMENTAL_INTERFACES |
{ |
|
_rep = x.allocateCString(); |
|
} |
|
| |
_CString(const _CString& x) |
PEGASUS_COMMON_LINKAGE Boolean operator==(const String& s1, const String& s2); |
{ |
|
_rep = strcpy(new char[strlen(x._rep) + 1], x._rep); |
|
} |
|
| |
~_CString() |
PEGASUS_COMMON_LINKAGE Boolean operator==(const String& s1, const char* s2); |
{ |
|
delete [] _rep; |
|
} |
|
| |
_CString& operator=(const _CString& x) |
PEGASUS_COMMON_LINKAGE Boolean operator==(const char* s1, const String& s2); |
{ |
|
if (this != &x) |
|
_rep = strcpy(new char[strlen(x._rep) + 1], x._rep); |
|
| |
return *this; |
PEGASUS_COMMON_LINKAGE Boolean operator!=(const String& s1, const String& s2); |
} |
|
| |
operator const char*() const |
PEGASUS_COMMON_LINKAGE Boolean operator!=(const String& s1, const char* s2); |
{ |
|
return _rep; |
|
} |
|
| |
const char* data() const |
PEGASUS_COMMON_LINKAGE Boolean operator!=(const char* s1, const String& s2); |
{ |
|
return _rep; |
|
} |
|
| |
private: |
PEGASUS_COMMON_LINKAGE Boolean operator<(const String& s1, const String& s2); |
| |
char* _rep; |
PEGASUS_COMMON_LINKAGE Boolean operator<(const String& s1, const char* s2); |
}; |
|
|
|
inline Uint32 _Length(const String& s1) { return s1.size(); } |
|
|
|
inline Uint32 _Length(const char* s1) { return strlen(s1); } |
|
| |
inline Uint32 _Length(const char) { return 1; } |
PEGASUS_COMMON_LINKAGE Boolean operator<(const char* s1, const String& s2); |
| |
template<class S1, class S2> |
PEGASUS_COMMON_LINKAGE Boolean operator>(const String& s1, const String& s2); |
inline String Cat(const S1& s1, const S2& s2) |
|
{ |
|
String tmp; |
|
tmp.reserve(_Length(s1) + _Length(s2)); |
|
tmp.append(s1); |
|
tmp.append(s2); |
|
return tmp; |
|
} |
|
| |
template<class S1, class S2, class S3> |
PEGASUS_COMMON_LINKAGE Boolean operator>(const String& s1, const char* s2); |
inline String Cat(const S1& s1, const S2& s2, const S3& s3) |
|
{ |
|
String tmp; |
|
tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3)); |
|
tmp.append(s1); |
|
tmp.append(s2); |
|
tmp.append(s3); |
|
return tmp; |
|
} |
|
| |
template<class S1, class S2, class S3, class S4> |
PEGASUS_COMMON_LINKAGE Boolean operator>(const char* s1, const String& s2); |
inline String Cat(const S1& s1, const S2& s2, const S3& s3, const S4& s4) |
|
{ |
|
String tmp; |
|
tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4)); |
|
tmp.append(s1); |
|
tmp.append(s2); |
|
tmp.append(s3); |
|
tmp.append(s4); |
|
return tmp; |
|
} |
|
|
|
template<class S1, class S2, class S3, class S4, class S5> |
|
inline String Cat( |
|
const S1& s1, |
|
const S2& s2, |
|
const S3& s3, |
|
const S4& s4, |
|
const S5& s5) |
|
{ |
|
String tmp; |
|
| |
tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) + |
PEGASUS_COMMON_LINKAGE Boolean operator<=(const String& s1, const String& s2); |
_Length(s5)); |
|
| |
tmp.append(s1); |
PEGASUS_COMMON_LINKAGE Boolean operator<=(const String& s1, const char* s2); |
tmp.append(s2); |
|
tmp.append(s3); |
|
tmp.append(s4); |
|
tmp.append(s5); |
|
|
|
return tmp; |
|
} |
|
|
|
template<class S1, class S2, class S3, class S4, class S5, class S6> |
|
inline String Cat( |
|
const S1& s1, |
|
const S2& s2, |
|
const S3& s3, |
|
const S4& s4, |
|
const S5& s5, |
|
const S6& s6) |
|
{ |
|
String tmp; |
|
| |
tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) + |
PEGASUS_COMMON_LINKAGE Boolean operator<=(const char* s1, const String& s2); |
_Length(s5) + _Length(s6)); |
|
| |
tmp.append(s1); |
PEGASUS_COMMON_LINKAGE Boolean operator>=(const String& s1, const String& s2); |
tmp.append(s2); |
|
tmp.append(s3); |
|
tmp.append(s4); |
|
tmp.append(s5); |
|
tmp.append(s6); |
|
| |
return tmp; |
PEGASUS_COMMON_LINKAGE Boolean operator>=(const String& s1, const char* s2); |
} |
|
| |
PEGASUS_COMMON_LINKAGE const Array<String>& EmptyStringArray(); |
PEGASUS_COMMON_LINKAGE Boolean operator>=(const char* s1, const String& s2); |
| |
PEGASUS_COMMON_LINKAGE Boolean Equal(const String& x, const String& y); |
PEGASUS_COMMON_LINKAGE String operator+(const String& s1, const String& s2); |
| |
inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path) |
PEGASUS_COMMON_LINKAGE String operator+(const String& s1, const char* s2); |
{ |
|
char* tmpPath = path.allocateCString(); |
|
is.open(tmpPath); |
|
delete [] tmpPath; |
|
return !!is; |
|
} |
|
| |
inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path) |
PEGASUS_COMMON_LINKAGE String operator+(const char* s1, const String& s2); |
{ |
|
char* tmpPath = path.allocateCString(); |
|
os.open(tmpPath); |
|
delete [] tmpPath; |
|
return !!os; |
|
} |
|
| |
inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path) |
#endif /* PEGASUS_USE_EXPERIMENTAL_INTERFACES */ |
{ |
|
char* tmpPath = path.allocateCString(); |
|
os.open(tmpPath, PEGASUS_STD(ios::app)); |
|
delete [] tmpPath; |
|
return !!os; |
|
} |
|
|
|
#define PEGASUS_ARRAY_T String |
|
#include <Pegasus/Common/ArrayInter.h> |
|
#undef PEGASUS_ARRAY_T |
|
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |
| |
|
#if defined(PEGASUS_INTERNALONLY) |
|
# include "StringInline.h" |
|
#endif |
|
|
#endif /* Pegasus_String_h */ | #endif /* Pegasus_String_h */ |