version 1.31, 2001/06/08 00:40:42
|
version 1.39.2.6, 2001/08/22 08:11:08
|
|
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM |
// Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM |
// | // |
// Permission is hereby granted, free of charge, to any person obtaining a |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
// copy of this software and associated documentation files (the "Software"), |
// of this software and associated documentation files (the "Software"), to |
// to deal in the Software without restriction, including without limitation |
// deal in the Software without restriction, including without limitation the |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
// and/or sell copies of the Software, and to permit persons to whom the |
// sell copies of the Software, and to permit persons to whom the Software is |
// Software is furnished to do so, subject to the following conditions: |
// furnished to do so, subject to the following conditions: |
// | // |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
// DEALINGS IN THE SOFTWARE. |
// 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. |
// | // |
//============================================================================== | //============================================================================== |
// | // |
|
|
#define Pegasus_String_h | #define Pegasus_String_h |
| |
#include <iostream> | #include <iostream> |
|
#include <fstream> |
#include <cstring> | #include <cstring> |
#include <Pegasus/Common/Config.h> | #include <Pegasus/Common/Config.h> |
#include <Pegasus/Common/Char16.h> | #include <Pegasus/Common/Char16.h> |
|
|
*/ | */ |
Uint32 size() const { return _rep.size() - 1; } | Uint32 size() const { return _rep.size() - 1; } |
| |
/** Returns a pointer to the first character in the null-terminated string |
/** getData Returns a pointer to the first character in the |
string. |
null-terminated string string of the String object. |
@return Pointer to the first character of the String object | @return Pointer to the first character of the String object |
<pre> | <pre> |
String t1 = "abc"; | String t1 = "abc"; |
|
|
*/ | */ |
const Char16* getData() const { return _rep.getData(); } | const Char16* getData() const { return _rep.getData(); } |
| |
/** AallocateCString - llocates an 8 bit representation of this string. The |
/** AallocateCString - llocates an 8 bit representation of this String |
user is |
object. The user is responsible for freeing the result. If any |
responsible for freeing the result. If any characters are truncated, |
characters are truncated, a TruncatedCharacter exception is thrown. |
a TruncatedCharacter exception is thrown. This exception may |
This exception may be suppressed by passing true as the noThrow |
be suppressed by passing true as the noThrow argument. Extra |
argument. Extra characters may be allocated at the end of the |
characters may be allocated at the end of the new string by |
new string by passing a non-zero value to the extraBytes argument. |
passing a non-zero value to the extraBytes argument. |
|
@param extraBytes Defines the number of extra characters to be | @param extraBytes Defines the number of extra characters to be |
allocated at the end of the new string. Default is zero. | allocated at the end of the new string. Default is zero. |
|
|
@param noThrow If true, no exception will be thrown if characters | @param noThrow If true, no exception will be thrown if characters |
are truncated | are truncated |
|
|
@return pointer to the new representation of the string | @return pointer to the new representation of the string |
|
|
@exception Throws TruncatedCharacter exception if any characters are | @exception Throws TruncatedCharacter exception if any characters are |
truncated | truncated |
<pre> | <pre> |
String test = "abc"; | String test = "abc"; |
char* p = test.allocateCString(); | char* p = test.allocateCString(); |
|
... |
|
delete [] p; |
</pre> | </pre> |
*/ | */ |
char* allocateCString(Uint32 extraBytes = 0, Boolean noThrow = false) const; | char* allocateCString(Uint32 extraBytes = 0, Boolean noThrow = false) const; |
| |
/** appendToCString - Append the given string to a C-string. If the length |
/** appendToCString - Append the given String object to a C-string. If the |
is not PEG_NOT_FOUND, then the lesser of the the length argument and the |
length is not PEG_NOT_FOUND, then the lesser of the the length argument |
length of this string is truncated. Otherwise, the entire string is |
and he length of this string is truncated. Otherwise, the entire string |
trunctated. The TruncatedCharacter exception is thrown if any characters |
is trunctated. The TruncatedCharacter exception is thrown if any |
are truncated. |
characters are truncated. |
@param str Char pointer to the string to append | @param str Char pointer to the string to append |
@param length Length to append or PEG_NOT_FOUND (Uint32(-1) | @param length Length to append or PEG_NOT_FOUND (Uint32(-1) |
@param noThrow - If false, throw the "TruncatedCharacter" exception of | @param noThrow - If false, throw the "TruncatedCharacter" exception of |
|
|
*/ | */ |
Uint32 find(Char16 c) const; | Uint32 find(Char16 c) const; |
| |
|
/** Same as above but starts searching from the given position. */ |
|
Uint32 find(Uint32 pos, Char16 c) const; |
| |
/** Find the position of the first occurence of the string object. | /** Find the position of the first occurence of the string object. |
This function finds one string inside another | This function finds one string inside another |
|
|
*/ | */ |
void translate(Char16 fromChar, Char16 toChar); | void translate(Char16 fromChar, Char16 toChar); |
| |
|
/** Method for printing a string. |
|
*/ |
|
void print() const; |
|
|
/** Compare the first n characters of the two strings.. | /** Compare the first n characters of the two strings.. |
@param s1 First null-terminated string for the comparison. | @param s1 First null-terminated string for the comparison. |
@param s2 Second null-terminated string for the comparison. | @param s2 Second null-terminated string for the comparison. |
|
|
*/ | */ |
static int compareNoCase(const char* s1, const char* s2, Uint32 n); | static int compareNoCase(const char* s1, const char* s2, Uint32 n); |
| |
|
static int compareNoCase(const char* s1, const char* s2); |
|
|
/** Compare two null-terminated strings. | /** Compare two null-terminated strings. |
@param s1 First null-terminated string for the comparison. | @param s1 First null-terminated string for the comparison. |
@param s2 Second null-terminated string for the comparison. | @param s2 Second null-terminated string for the comparison. |
|
|
/// Convert the plain old C-string to lower case: | /// Convert the plain old C-string to lower case: |
static void toLower(char* str); | static void toLower(char* str); |
| |
/** Split the given string into fields. Fields are separated by |
|
whitespace. Fields containing whitespace must be surrounded |
|
by quotes. This method modifies the original string (while |
|
unescaping embedded quote characters). |
|
*/ |
|
static void split(const String& line, Array<String>& fields); |
|
|
|
/** Joins fields into a single string (the reverse of the split() method. |
|
*/ |
|
static void join(Array<String>& fields, String& line); |
|
|
|
/** Escape special characters in a string. This method is used by |
|
join(). It escapes newlines, returns, tabs, form-feeds, and full quotes. |
|
If the string has spaces, then full quotes will be placed around it. |
|
@param in input string |
|
@param in output string (which is appended to). |
|
@returns true if string has spaces. |
|
*/ |
|
static Boolean escape(const String& in, String& out); |
|
|
|
/** EMPTY - Represent an empty string. | /** EMPTY - Represent an empty string. |
This member is used to represent empty strings. Using this member | This member is used to represent empty strings. Using this member |
avoids an expensive construction of an empty string (e.g., String()). | avoids an expensive construction of an empty string (e.g., String()). |
*/ | */ |
static const String EMPTY; | static const String EMPTY; |
| |
|
/** Return true if the str parameter matches the pattern. C-Shell style |
|
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 |
|
glob matching is used. Ignore case in all comparisons. |
|
*/ |
|
static Boolean matchNoCase(const String& str, const String& pattern); |
|
|
private: | private: |
| |
static Uint32 _min(Uint32 x, Uint32 y) { return x < y ? x : y; } | static Uint32 _min(Uint32 x, Uint32 y) { return x < y ? x : y; } |
|
|
*/ | */ |
PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2); | PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2); |
| |
|
inline int EqualNoCase(const char* s1, const char* s2) |
|
{ |
|
return CompareNoCase(s1, s2) == 0; |
|
} |
|
|
/** Get the next line from the input file. | /** Get the next line from the input file. |
*/ | */ |
PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line); | PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line); |
|
|
| |
PEGASUS_COMMON_LINKAGE Boolean Equal(const String& x, const String& y); | PEGASUS_COMMON_LINKAGE Boolean Equal(const String& x, const String& y); |
| |
|
inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path) |
|
{ |
|
char* tmpPath = path.allocateCString(); |
|
is.open(tmpPath); |
|
delete [] tmpPath; |
|
return !!is; |
|
} |
|
|
|
inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path) |
|
{ |
|
char* tmpPath = path.allocateCString(); |
|
os.open(tmpPath); |
|
delete [] tmpPath; |
|
return !!os; |
|
} |
|
|
|
inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path) |
|
{ |
|
char* tmpPath = path.allocateCString(); |
|
os.open(tmpPath, PEGASUS_STD(ios::app)); |
|
delete [] tmpPath; |
|
return !!os; |
|
} |
|
|
#define PEGASUS_ARRAY_T String | #define PEGASUS_ARRAY_T String |
#include <Pegasus/Common/ArrayInter.h> | #include <Pegasus/Common/ArrayInter.h> |
#undef PEGASUS_ARRAY_T | #undef PEGASUS_ARRAY_T |
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |
| |
#endif /* Pegasus_String_h */ | #endif /* Pegasus_String_h */ |
|
|
|
|