version 1.17, 2001/05/14 00:39:53
|
version 1.34, 2002/04/18 23:33:17
|
|
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
// | // |
// 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. |
// | // |
//============================================================================== | //============================================================================== |
// | // |
|
|
#include "String.h" | #include "String.h" |
#include "Exception.h" | #include "Exception.h" |
#include "String.h" | #include "String.h" |
|
|
// For debugging |
|
#include <iostream> | #include <iostream> |
using namespace std; |
|
| |
|
PEGASUS_USING_STD; |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
#define PEGASUS_ARRAY_T String |
|
#include <Pegasus/Common/ArrayImpl.h> |
|
#undef PEGASUS_ARRAY_T |
|
|
const String String::EMPTY; | const String String::EMPTY; |
| |
|
#if 0 // Apparently dead code |
|
static inline void _SkipWhitespace(const Char16*& p) |
|
{ |
|
while (*p && isspace(*p)) |
|
p++; |
|
} |
|
#endif |
|
|
inline Uint32 StrLen(const char* str) | inline Uint32 StrLen(const char* str) |
{ | { |
if (!str) | if (!str) |
|
|
| |
String::String(const char* str, Uint32 n_) | String::String(const char* str, Uint32 n_) |
{ | { |
Uint32 n = _min(strlen(str), n_); |
Uint32 n = _pegasusMin(strlen(str), n_); |
reserve(n + 1); | reserve(n + 1); |
| |
while (n--) | while (n--) |
|
|
String& String::assign(const Char16* str, Uint32 n) | String& String::assign(const Char16* str, Uint32 n) |
{ | { |
_rep.clear(); | _rep.clear(); |
Uint32 m = _min(StrLen(str), n); |
Uint32 m = _pegasusMin(StrLen(str), n); |
_rep.append(str, m); | _rep.append(str, m); |
_rep.append('\0'); | _rep.append('\0'); |
return *this; | return *this; |
|
|
{ | { |
_rep.clear(); | _rep.clear(); |
| |
Uint32 n = _min(strlen(x), n_); |
Uint32 n = _pegasusMin(strlen(x), n_); |
_rep.reserve(n + 1); | _rep.reserve(n + 1); |
| |
while (n--) | while (n--) |
|
|
if (!str) | if (!str) |
throw NullPointer(); | throw NullPointer(); |
| |
Uint32 n = _min(size(), length); |
Uint32 n = _pegasusMin(size(), length); |
| |
char* p = str + strlen(str); | char* p = str + strlen(str); |
const Char16* q = getData(); | const Char16* q = getData(); |
|
|
| |
String& String::append(const Char16* str, Uint32 n) | String& String::append(const Char16* str, Uint32 n) |
{ | { |
Uint32 m = _min(StrLen(str), n); |
Uint32 m = _pegasusMin(StrLen(str), n); |
_rep.reserve(_rep.size() + m); | _rep.reserve(_rep.size() + m); |
_rep.remove(_rep.size() - 1); | _rep.remove(_rep.size() - 1); |
_rep.append(str, m); | _rep.append(str, m); |
|
|
| |
void String::remove(Uint32 pos, Uint32 size) | void String::remove(Uint32 pos, Uint32 size) |
{ | { |
if (size == Uint32(-1)) |
if (size == PEG_NOT_FOUND) |
size = this->size() - pos; | size = this->size() - pos; |
| |
if (pos + size > this->size()) | if (pos + size > this->size()) |
|
|
| |
while (n--) | while (n--) |
{ | { |
|
#ifdef PEGASUS_HAS_EBCDIC |
|
if (*p <= 255 && *q <= 255) |
|
#else |
if (*p <= 127 && *q <= 127) | if (*p <= 127 && *q <= 127) |
|
#endif |
{ | { |
if (tolower(*p++) != tolower(*q++)) | if (tolower(*p++) != tolower(*q++)) |
return false; | return false; |
|
|
{ | { |
if (pos < size()) | if (pos < size()) |
{ | { |
if (length == Uint32(-1)) |
if (length == PEG_NOT_FOUND) |
length = size() - pos; | length = size() - pos; |
| |
return String(getData() + pos, length); | return String(getData() + pos, length); |
|
|
return p - first; | return p - first; |
} | } |
| |
return Uint32(-1); |
return PEG_NOT_FOUND; |
|
} |
|
|
|
Uint32 String::find(Uint32 pos, Char16 c) const |
|
{ |
|
const Char16* data = getData(); |
|
|
|
for (Uint32 i = pos, n = size(); i < n; i++) |
|
{ |
|
if (data[i] == c) |
|
return i; |
|
} |
|
|
|
return PEG_NOT_FOUND; |
} | } |
| |
Uint32 String::find(const String& s) const | Uint32 String::find(const String& s) const |
|
|
Uint32 subStrLen = s.size(); | Uint32 subStrLen = s.size(); |
Uint32 strLen = size(); | Uint32 strLen = size(); |
| |
|
if (subStrLen > strLen) |
|
{ |
|
return PEG_NOT_FOUND; |
|
} |
|
|
// loop to find first char match | // loop to find first char match |
Uint32 loc = 0; | Uint32 loc = 0; |
for( ; loc <= (strLen-subStrLen); loc++) | for( ; loc <= (strLen-subStrLen); loc++) |
|
|
return loc; | return loc; |
} | } |
} | } |
return Uint32(-1); |
return PEG_NOT_FOUND; |
} | } |
| |
// ATTN:KS 5 apr 2000 Need to add the Char16* version. |
|
Uint32 String::find(const char* s) const | Uint32 String::find(const char* s) const |
{ | { |
return find(String(s)); | return find(String(s)); |
} | } |
| |
|
Uint32 String::find(const Char16* s) const |
|
{ |
|
return find(String(s)); |
|
} |
|
|
Uint32 String::reverseFind(Char16 c) const | Uint32 String::reverseFind(Char16 c) const |
{ | { |
const Char16* first = getData(); | const Char16* first = getData(); |
|
|
return last - first; | return last - first; |
} | } |
| |
return Uint32(-1); |
return PEG_NOT_FOUND; |
} | } |
| |
void String::toLower() | void String::toLower() |
{ | { |
for (Char16* p = &_rep[0]; *p; p++) | for (Char16* p = &_rep[0]; *p; p++) |
{ | { |
|
#ifdef PEGASUS_HAS_EBCDIC |
|
if (*p <= 255) |
|
#else |
if (*p <= 127) | if (*p <= 127) |
|
#endif |
*p = tolower(*p); | *p = tolower(*p); |
} | } |
} | } |
|
|
return 0; | return 0; |
} | } |
| |
std::ostream& operator<<(std::ostream& os, const String& x) |
int String::compareNoCase(const char* s1, const char* s2) |
|
{ |
|
while (*s1 && *s2) |
|
{ |
|
int r = tolower(*s1++) - tolower(*s2++); |
|
|
|
if (r) |
|
return r; |
|
} |
|
|
|
if (*s2) |
|
return -1; |
|
else if (*s1) |
|
return 1; |
|
|
|
return 0; |
|
} |
|
|
|
PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const String& x) |
{ | { |
for (Uint32 i = 0, n = x.size(); i < n; i++) | for (Uint32 i = 0, n = x.size(); i < n; i++) |
os << x[i]; | os << x[i]; |
|
|
{ | { |
Char16 c = tmp[i]; | Char16 c = tmp[i]; |
| |
|
#ifdef PEGASUS_HAS_EBCDIC |
|
if (c <= 255) |
|
#else |
if (c <= 127) | if (c <= 127) |
|
#endif |
tmp[i] = tolower(c); | tmp[i] = tolower(c); |
} | } |
| |
|
|
return 0; | return 0; |
} | } |
| |
Boolean GetLine(istream& is, String& line) |
Boolean GetLine(PEGASUS_STD(istream)& is, String& line) |
{ | { |
line.clear(); | line.clear(); |
| |
|
|
_rep.append('\0'); | _rep.append('\0'); |
} | } |
| |
|
void String::print() const |
|
{ |
|
cout << *this << endl; |
|
} |
|
|
void String::reserve(Uint32 capacity) | void String::reserve(Uint32 capacity) |
{ | { |
_rep.reserve(capacity + 1); | _rep.reserve(capacity + 1); |