version 1.30, 2001/12/13 14:32:18
|
version 1.37, 2002/05/03 05:26:17
|
|
|
#include <Pegasus/Common/ArrayImpl.h> | #include <Pegasus/Common/ArrayImpl.h> |
#undef PEGASUS_ARRAY_T | #undef PEGASUS_ARRAY_T |
| |
const String String::EMPTY; |
const String String::EMPTY = String(); |
| |
|
#if 0 // Apparently dead code |
static inline void _SkipWhitespace(const Char16*& p) | static inline void _SkipWhitespace(const Char16*& p) |
{ | { |
while (*p && isspace(*p)) | while (*p && isspace(*p)) |
p++; | p++; |
} | } |
|
#endif |
| |
inline Uint32 StrLen(const char* str) | inline Uint32 StrLen(const char* str) |
{ | { |
|
|
| |
String::String(const char* str, Uint32 n_) | String::String(const char* str, Uint32 n_) |
{ | { |
Uint32 n = PEG_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 = PEG_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 = PEG_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 = PEG_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 = PEG_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); |
|
|
return PEG_NOT_FOUND; | 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(); |
|
|
static Array<String> tmp; | static Array<String> tmp; |
return tmp; | return tmp; |
} | } |
|
//#define NEWMATCHFUNCTION |
|
#if defined NEWMATCHFUNCTION |
|
// Wildcard String matching function that may be useful in the future |
|
// The following code was provided by Bob Blair. |
|
|
|
/* _StringMatch Match input MatchString against a GLOB style pattern |
|
Note that MatchChar is the char type so that this source |
|
in portable to different string types. This is an internal function |
|
|
|
Results: The return value is 1 if string matches pattern, and |
|
0 otherwise. The matching operation permits the following |
|
special characters in the pattern: *?\[] (see the manual |
|
entry for details on what these mean). |
|
|
|
Side effects: None. |
|
*/ |
|
|
|
/* MatchChar defined as a separate entity because this function source used |
|
elsewhere was an unsigned char *. Here we use Uint16 to maintain 16 bit |
|
size. |
|
*/ |
|
typedef Uint16 MatchChar; |
|
|
|
inline Uint16 _ToLower(Uint16 ch) |
|
{ |
|
#ifdef PEGASUS_HAS_EBCDIC |
|
return ch <= 255 ? tolower(char(ch)) : ch; |
|
#else |
|
return ch <= 127 ? tolower(char(ch)) : ch; |
|
#endif |
|
} |
|
|
|
inline Boolean _Equal(MatchChar ch1, MatchChar ch2, int nocase) |
|
{ |
|
if (nocase) |
|
return _ToLower(ch1) == _ToLower(ch2); |
|
else |
|
return ch1 == ch2; |
|
} |
|
|
|
|
|
static const MatchChar * |
|
_matchrange(const MatchChar *range, MatchChar c, int nocase) |
|
{ |
|
const MatchChar *p = range; |
|
const MatchChar *rstart = range + 1; |
|
const MatchChar *rend = 0; |
|
MatchChar compchar; |
|
|
|
for (rend = rstart; *rend && *rend != ']'; rend++); |
|
if (*rend == ']') { // if there is an end to this pattern |
|
for (compchar = *rstart; rstart != rend; rstart++) { |
|
if (_Equal(*rstart, c, nocase)) |
|
return ++rend; |
|
if (*rstart == '-') { |
|
rstart++; |
|
if (c >= compchar && c <= *rstart) |
|
return ++rend; |
|
} |
|
} |
|
} |
|
return (const MatchChar *)0; |
|
} |
|
|
|
static int |
|
_StringMatch( |
|
const MatchChar *testString, |
|
const MatchChar *pattern, |
|
int nocase ) /* Ignore case if this is true */ |
|
{ |
|
const MatchChar *pat = pattern; |
|
const MatchChar *str = testString; |
|
unsigned int done = 0; |
|
unsigned int res = 0; // the result: 1 == match |
|
|
|
while (!done) { // main loop walks through pattern and test string |
|
//cerr << "Comparing <" << *pat << "> and <" << *str << ">" << endl; |
|
if (!*pat) { //end of pattern |
|
done = 1; // we're done |
|
if (!*str) //end of test, too? |
|
res = 1; // then we matched |
|
} else { //Not end of pattern |
|
if (!*str) { // but end of test |
|
done = 1; // We're done |
|
if (*pat == '*') // If pattern openends |
|
res = 1; // then we matched |
|
} else { //Not end of test |
|
if (*pat == '*') { //Ambiguuity found |
|
if (!*++pat) { //and it ends pattern |
|
done = 1; // then we're done |
|
res = 1; // and match |
|
} else { //if it doesn't end |
|
while (!done) { // until we're done |
|
if (_StringMatch(str, pat, nocase)) { // we recurse |
|
done = 1; //if it recurses true |
|
res = 1; // we done and match |
|
} else { //it recurses false |
|
if (!*str) // see if test is done |
|
done = 1; // yes: we done |
|
else // not done: |
|
str++; // keep testing |
|
} // end test on recursive call |
|
} // end looping on recursive calls |
|
} // end logic when pattern is ambiguous |
|
} else { //pattern not ambiguus |
|
if (*pat == '?') { //pattern is 'any' |
|
pat++, str++; // so move along |
|
} else if (*pat == '[') { //see if it's a range |
|
pat = _matchrange(pat, *str, nocase); // and is a match |
|
if (!pat) { //It is not a match |
|
done = 1; // we're done |
|
res = 1; // no match |
|
} else { //Range matches |
|
str++, pat++; // keep going |
|
} |
|
} else { // only case left is individual characters |
|
if (!_Equal(*pat++, *str++, nocase)) // if they don't match |
|
done = 1; // bail. |
|
} |
|
} // end ("pattern is not ambiguous (*)" logic |
|
} // end logic when pattern and string still have data |
|
} // end logic when pattern still has data |
|
} // end main loop |
|
return res; |
|
} |
| |
|
#else |
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
// | // |
// String matching routines borrowed from Tcl 8.0: | // String matching routines borrowed from Tcl 8.0: |
|
|
*---------------------------------------------------------------------- | *---------------------------------------------------------------------- |
*/ | */ |
| |
typedef Uint16 Tcl_Char; |
typedef Uint16 MatchChar; |
| |
inline Uint16 _ToLower(Uint16 ch) | inline Uint16 _ToLower(Uint16 ch) |
{ | { |
|
|
return ch1 == ch2; | return ch1 == ch2; |
} | } |
| |
int Tcl_StringMatch( |
int _StringMatch( |
Tcl_Char *string, /* String. */ |
MatchChar *string, /* String. */ |
Tcl_Char *pattern, /* Pattern, which may contain special |
MatchChar *pattern, /* Pattern, which may contain special |
* characters. */ | * characters. */ |
int nocase) /* Ignore case if this is true */ | int nocase) /* Ignore case if this is true */ |
{ | { |
Tcl_Char c2; |
MatchChar c2; |
| |
while (1) { | while (1) { |
/* See if we're at the end of both the pattern and the string. | /* See if we're at the end of both the pattern and the string. |
|
|
return 1; | return 1; |
} | } |
while (1) { | while (1) { |
if (Tcl_StringMatch(string, pattern, nocase)) { |
if (_StringMatch(string, pattern, nocase)) { |
return 1; | return 1; |
} | } |
if (*string == 0) { | if (*string == 0) { |
|
|
string += 1; | string += 1; |
} | } |
} | } |
|
#endif |
Boolean String::match(const String& str, const String& pattern) | Boolean String::match(const String& str, const String& pattern) |
{ | { |
return Tcl_StringMatch( |
return _StringMatch( |
(Uint16*)str.getData(), (Uint16*)pattern.getData(), 0) != 0; | (Uint16*)str.getData(), (Uint16*)pattern.getData(), 0) != 0; |
} | } |
| |
Boolean String::matchNoCase(const String& str, const String& pattern) | Boolean String::matchNoCase(const String& str, const String& pattern) |
{ | { |
return Tcl_StringMatch( |
return _StringMatch( |
(Uint16*)str.getData(), (Uint16*)pattern.getData(), 1) != 0; | (Uint16*)str.getData(), (Uint16*)pattern.getData(), 1) != 0; |
} | } |
| |