зеркало из https://github.com/mozilla/pjs.git
Bug 337730 - add additional utilities to nsStringAPI, r=darin
TestMinStringAPI.cpp moved from xpcom/glue to xpcom/glue/external
This commit is contained in:
Родитель
b8a8dde802
Коммит
0bca496fd6
|
@ -196,6 +196,10 @@ public:
|
|||
static char* strtok(char* str, const char* delims, char* *newStr);
|
||||
|
||||
static PRUint32 strlen(const PRUnichar* s) {
|
||||
if (!s) {
|
||||
NS_ERROR("Passing null to nsCRT::strlen");
|
||||
return 0;
|
||||
}
|
||||
return NS_strlen(s);
|
||||
}
|
||||
|
||||
|
|
|
@ -134,7 +134,7 @@ NS_strndup(const PRUnichar *aString, PRUint32 aLen)
|
|||
|
||||
// This table maps uppercase characters to lower case characters;
|
||||
// characters that are neither upper nor lower case are unaffected.
|
||||
static const unsigned char kUpper2Lower[256] = {
|
||||
const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = {
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
|
||||
|
@ -158,7 +158,7 @@ static const unsigned char kUpper2Lower[256] = {
|
|||
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
|
||||
};
|
||||
|
||||
static const unsigned char kLower2Upper[256] = {
|
||||
const unsigned char nsLowerUpperUtils::kLower2Upper[256] = {
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
|
||||
|
@ -182,24 +182,14 @@ static const unsigned char kLower2Upper[256] = {
|
|||
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
|
||||
};
|
||||
|
||||
char NS_ToUpper(char aChar)
|
||||
{
|
||||
return (char)kLower2Upper[(unsigned char)aChar];
|
||||
}
|
||||
|
||||
char NS_ToLower(char aChar)
|
||||
{
|
||||
return (char)kUpper2Lower[(unsigned char)aChar];
|
||||
}
|
||||
|
||||
PRBool NS_IsUpper(char aChar)
|
||||
{
|
||||
return aChar != (char)kUpper2Lower[(unsigned char)aChar];
|
||||
return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
|
||||
}
|
||||
|
||||
PRBool NS_IsLower(char aChar)
|
||||
{
|
||||
return aChar != (char)kLower2Upper[(unsigned char)aChar];
|
||||
return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
|
||||
}
|
||||
|
||||
PRBool NS_IsAscii(PRUnichar aChar)
|
||||
|
|
|
@ -99,8 +99,27 @@ NS_strndup(const PRUnichar *aString, PRUint32 aLen);
|
|||
// The following case-conversion methods only deal in the ascii repertoire
|
||||
// A-Z and a-z
|
||||
|
||||
NS_COM_GLUE char NS_ToUpper(char aChar);
|
||||
NS_COM_GLUE char NS_ToLower(char aChar);
|
||||
// semi-private data declarations... don't use these directly.
|
||||
class NS_COM_GLUE nsLowerUpperUtils {
|
||||
private:
|
||||
static const unsigned char kLower2Upper[256];
|
||||
static const unsigned char kUpper2Lower[256];
|
||||
|
||||
friend char NS_ToUpper(char);
|
||||
friend char NS_ToLower(char);
|
||||
friend PRBool NS_IsUpper(char);
|
||||
friend PRBool NS_IsLower(char);
|
||||
};
|
||||
|
||||
inline char NS_ToUpper(char aChar)
|
||||
{
|
||||
return (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
|
||||
}
|
||||
|
||||
inline char NS_ToLower(char aChar)
|
||||
{
|
||||
return (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
|
||||
}
|
||||
|
||||
NS_COM_GLUE PRBool NS_IsUpper(char aChar);
|
||||
NS_COM_GLUE PRBool NS_IsLower(char aChar);
|
||||
|
|
|
@ -45,6 +45,7 @@
|
|||
#endif
|
||||
|
||||
#include "nsXPCOMStrings.h"
|
||||
#include "nsDebug.h"
|
||||
|
||||
#ifdef MOZILLA_INTERNAL_API
|
||||
#undef MOZILLA_INTERNAL_API
|
||||
|
@ -52,6 +53,12 @@
|
|||
|
||||
#include "nsStringAPI.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef XP_WIN
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
// nsAString
|
||||
|
||||
PRUint32
|
||||
|
@ -114,6 +121,47 @@ nsAString::SetLength(PRUint32 aLen)
|
|||
return data != nsnull;
|
||||
}
|
||||
|
||||
void
|
||||
nsAString::Trim(const char *aSet, PRBool aLeading, PRBool aTrailing)
|
||||
{
|
||||
NS_ASSERTION(aLeading || aTrailing, "Ineffective Trim");
|
||||
|
||||
const PRUnichar *start, *end;
|
||||
PRUint32 cutLen;
|
||||
|
||||
if (aLeading) {
|
||||
BeginReading(&start, &end);
|
||||
for (cutLen = 0; start < end; ++start, ++cutLen) {
|
||||
const char *test;
|
||||
for (test = aSet; *test; ++test) {
|
||||
if (*test == *start)
|
||||
break;
|
||||
}
|
||||
if (!*test)
|
||||
break;
|
||||
}
|
||||
if (cutLen) {
|
||||
NS_StringCutData(*this, 0, cutLen);
|
||||
}
|
||||
}
|
||||
if (aTrailing) {
|
||||
PRUint32 len = BeginReading(&start, &end);
|
||||
--end;
|
||||
for (cutLen = 0; end >= start; --end, ++cutLen) {
|
||||
const char *test;
|
||||
for (test = aSet; *test; ++test) {
|
||||
if (*test == *end)
|
||||
break;
|
||||
}
|
||||
if (!*test)
|
||||
break;
|
||||
}
|
||||
if (cutLen) {
|
||||
NS_StringCutData(*this, len - cutLen, cutLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsAString::DefaultComparator(const char_type *a, const char_type *b,
|
||||
PRUint32 len)
|
||||
|
@ -155,6 +203,100 @@ nsAString::Equals(const self_type &other, ComparatorFunc c) const
|
|||
return c(cself, cother, selflen) == 0;
|
||||
}
|
||||
|
||||
PRBool
|
||||
nsAString::LowerCaseEqualsLiteral(const char *aASCIIString) const
|
||||
{
|
||||
const PRUnichar *begin, *end;
|
||||
BeginReading(&begin, &end);
|
||||
|
||||
for (; begin < end; ++begin, ++aASCIIString) {
|
||||
if (!*aASCIIString || !NS_IsAscii(*begin) ||
|
||||
NS_ToLower((char) *begin) != *aASCIIString) {
|
||||
return PR_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if (*aASCIIString)
|
||||
return PR_FALSE;
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsAString::Find(const self_type& aStr, ComparatorFunc c) const
|
||||
{
|
||||
const char_type *begin, *end;
|
||||
PRUint32 selflen = BeginReading(&begin, &end);
|
||||
|
||||
const char_type *other;
|
||||
PRUint32 otherlen = aStr.BeginReading(&other);
|
||||
|
||||
if (otherlen > selflen)
|
||||
return -1;
|
||||
|
||||
// We want to stop searching otherlen characters before the end of the string
|
||||
end -= otherlen;
|
||||
|
||||
for (const char_type *cur = begin; cur <= end; ++cur) {
|
||||
if (!c(cur, other, otherlen))
|
||||
return cur - begin;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static PRBool ns_strnmatch(const PRUnichar *aStr, const char* aSubstring,
|
||||
PRUint32 aLen)
|
||||
{
|
||||
for (; aLen; ++aStr, ++aSubstring, --aLen) {
|
||||
if (!NS_IsAscii(*aStr))
|
||||
return PR_FALSE;
|
||||
|
||||
if ((char) *aStr != *aSubstring)
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool ns_strnimatch(const PRUnichar *aStr, const char* aSubstring,
|
||||
PRUint32 aLen)
|
||||
{
|
||||
for (; aLen; ++aStr, ++aSubstring, --aLen) {
|
||||
if (!NS_IsAscii(*aStr))
|
||||
return PR_FALSE;
|
||||
|
||||
if (NS_ToLower((char) *aStr) != *aSubstring)
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsAString::Find(const char *aStr, PRBool aIgnoreCase) const
|
||||
{
|
||||
PRBool (*match)(const PRUnichar*, const char*, PRUint32) =
|
||||
aIgnoreCase ? ns_strnimatch : ns_strnmatch;
|
||||
|
||||
const char_type *begin, *end;
|
||||
PRUint32 selflen = BeginReading(&begin, &end);
|
||||
|
||||
PRUint32 otherlen = strlen(aStr);
|
||||
|
||||
if (otherlen > selflen)
|
||||
return -1;
|
||||
|
||||
// We want to stop searching otherlen characters before the end of the string
|
||||
end -= otherlen;
|
||||
|
||||
for (const char_type *cur = begin; cur <= end; ++cur) {
|
||||
if (match(cur, aStr, otherlen)) {
|
||||
return cur - begin;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsAString::RFindChar(char_type aChar) const
|
||||
{
|
||||
|
@ -172,6 +314,35 @@ nsAString::RFindChar(char_type aChar) const
|
|||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
nsAString::AppendInt(int aInt, PRInt32 aRadix)
|
||||
{
|
||||
const char *fmt;
|
||||
switch (aRadix) {
|
||||
case 8:
|
||||
fmt = "%o";
|
||||
break;
|
||||
|
||||
case 10:
|
||||
fmt = "%d";
|
||||
break;
|
||||
|
||||
case 16:
|
||||
fmt = "%x";
|
||||
break;
|
||||
|
||||
default:
|
||||
NS_ERROR("Unrecognized radix");
|
||||
fmt = "";
|
||||
};
|
||||
|
||||
char buf[20];
|
||||
int len = snprintf(buf, sizeof(buf), fmt, aInt);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
|
||||
Append(NS_ConvertASCIItoUTF16(buf, len));
|
||||
}
|
||||
|
||||
// nsACString
|
||||
|
||||
PRUint32
|
||||
|
@ -234,6 +405,47 @@ nsACString::SetLength(PRUint32 aLen)
|
|||
return data != nsnull;
|
||||
}
|
||||
|
||||
void
|
||||
nsACString::Trim(const char *aSet, PRBool aLeading, PRBool aTrailing)
|
||||
{
|
||||
NS_ASSERTION(aLeading || aTrailing, "Ineffective Trim");
|
||||
|
||||
const char *start, *end;
|
||||
PRUint32 cutLen;
|
||||
|
||||
if (aLeading) {
|
||||
BeginReading(&start, &end);
|
||||
for (cutLen = 0; start < end; ++start, ++cutLen) {
|
||||
const char *test;
|
||||
for (test = aSet; *test; ++test) {
|
||||
if (*test == *start)
|
||||
break;
|
||||
}
|
||||
if (!*test)
|
||||
break;
|
||||
}
|
||||
if (cutLen) {
|
||||
NS_CStringCutData(*this, 0, cutLen);
|
||||
}
|
||||
}
|
||||
if (aTrailing) {
|
||||
PRUint32 len = BeginReading(&start, &end);
|
||||
--end;
|
||||
for (cutLen = 0; end >= start; --end, ++cutLen) {
|
||||
const char *test;
|
||||
for (test = aSet; *test; ++test) {
|
||||
if (*test == *end)
|
||||
break;
|
||||
}
|
||||
if (!*test)
|
||||
break;
|
||||
}
|
||||
if (cutLen) {
|
||||
NS_CStringCutData(*this, len - cutLen, cutLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::DefaultComparator(const char_type *a, const char_type *b,
|
||||
PRUint32 len)
|
||||
|
@ -268,6 +480,44 @@ nsACString::Equals(const self_type &other, ComparatorFunc c) const
|
|||
return c(cself, cother, selflen) == 0;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::Find(const self_type& aStr, ComparatorFunc c) const
|
||||
{
|
||||
const char_type *begin;
|
||||
PRUint32 len = aStr.BeginReading(&begin);
|
||||
return Find(begin, len, c);
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::Find(const char_type *aStr, ComparatorFunc c) const
|
||||
{
|
||||
return Find(aStr, strlen(aStr), c);
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::Find(const char_type *aStr, PRUint32 aLen, ComparatorFunc c) const
|
||||
{
|
||||
const char_type *begin, *end;
|
||||
PRUint32 selflen = BeginReading(&begin, &end);
|
||||
|
||||
if (aLen == 0) {
|
||||
NS_WARNING("Searching for zero-length string.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aLen > selflen)
|
||||
return -1;
|
||||
|
||||
// We want to stop searching otherlen characters before the end of the string
|
||||
end -= aLen;
|
||||
|
||||
for (const char_type *cur = begin; cur <= end; ++cur) {
|
||||
if (!c(begin, aStr, aLen))
|
||||
return cur - begin;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::RFindChar(char_type aChar) const
|
||||
{
|
||||
|
@ -282,6 +532,35 @@ nsACString::RFindChar(char_type aChar) const
|
|||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
nsACString::AppendInt(int aInt, PRInt32 aRadix)
|
||||
{
|
||||
const char *fmt;
|
||||
switch (aRadix) {
|
||||
case 8:
|
||||
fmt = "%o";
|
||||
break;
|
||||
|
||||
case 10:
|
||||
fmt = "%d";
|
||||
break;
|
||||
|
||||
case 16:
|
||||
fmt = "%x";
|
||||
break;
|
||||
|
||||
default:
|
||||
NS_ERROR("Unrecognized radix");
|
||||
fmt = "";
|
||||
};
|
||||
|
||||
char buf[20];
|
||||
int len = snprintf(buf, sizeof(buf), fmt, aInt);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
|
||||
Append(buf, len);
|
||||
}
|
||||
|
||||
// Substrings
|
||||
|
||||
nsDependentSubstring::nsDependentSubstring(const abstract_string_type& aStr,
|
||||
|
@ -357,3 +636,119 @@ ToNewUTF8String(const nsAString& aSource)
|
|||
CopyUTF16toUTF8(aSource, temp);
|
||||
return NS_CStringCloneData(temp);
|
||||
}
|
||||
|
||||
void
|
||||
CompressWhitespace(nsAString& aString)
|
||||
{
|
||||
aString.Trim(" \n\t\r");
|
||||
|
||||
PRUnichar *start;
|
||||
PRUint32 len = NS_StringGetMutableData(aString, PR_UINT32_MAX, &start);
|
||||
PRUnichar *end = start + len;
|
||||
|
||||
for (PRUnichar *cur = start; cur < end; ++cur) {
|
||||
if (!NS_IsAsciiWhitespace(*cur))
|
||||
continue;
|
||||
|
||||
*cur = ' ';
|
||||
|
||||
PRUnichar *wend;
|
||||
for (wend = cur + 1; wend < end && NS_IsAsciiWhitespace(*wend); ++wend) {
|
||||
// nothing to do but loop
|
||||
}
|
||||
|
||||
if (wend == cur + 1)
|
||||
continue;
|
||||
|
||||
PRUint32 wlen = wend - cur - 1;
|
||||
|
||||
// fix "end"
|
||||
end -= wlen;
|
||||
|
||||
// move everything forwards a bit
|
||||
for (PRUnichar *m = cur + 1; m < end; ++m) {
|
||||
*m = *(m + wlen);
|
||||
}
|
||||
}
|
||||
|
||||
// re-terminate
|
||||
*end = '\0';
|
||||
|
||||
// Set the new length.
|
||||
aString.SetLength(end - start);
|
||||
}
|
||||
|
||||
PRUint32
|
||||
ToLowerCase(nsACString& aStr)
|
||||
{
|
||||
char *begin, *end;
|
||||
PRUint32 len = aStr.BeginWriting(&begin, &end);
|
||||
|
||||
for (; begin < end; ++begin) {
|
||||
*begin = NS_ToLower(*begin);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
PRUint32
|
||||
ToUpperCase(nsACString& aStr)
|
||||
{
|
||||
char *begin, *end;
|
||||
PRUint32 len = aStr.BeginWriting(&begin, &end);
|
||||
|
||||
for (; begin < end; ++begin) {
|
||||
*begin = NS_ToUpper(*begin);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
PRUint32
|
||||
ToLowerCase(const nsACString& aSrc, nsACString& aDest)
|
||||
{
|
||||
const char *begin, *end;
|
||||
PRUint32 len = aSrc.BeginReading(&begin, &end);
|
||||
|
||||
char *dest;
|
||||
NS_CStringGetMutableData(aDest, len, &dest);
|
||||
|
||||
for (; begin < end; ++begin, ++dest) {
|
||||
*dest = NS_ToLower(*begin);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
PRUint32
|
||||
ToUpperCase(const nsACString& aSrc, nsACString& aDest)
|
||||
{
|
||||
const char *begin, *end;
|
||||
PRUint32 len = aSrc.BeginReading(&begin, &end);
|
||||
|
||||
char *dest;
|
||||
NS_CStringGetMutableData(aDest, len, &dest);
|
||||
|
||||
for (; begin < end; ++begin, ++dest) {
|
||||
*dest = NS_ToUpper(*begin);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
CaseInsensitiveCompare(const char *a, const char *b,
|
||||
PRUint32 len)
|
||||
{
|
||||
for (const char *aend = a + len; a < aend; ++a, ++b) {
|
||||
char la = NS_ToLower(*a);
|
||||
char lb = NS_ToLower(*b);
|
||||
|
||||
if (la == lb)
|
||||
continue;
|
||||
|
||||
return la < lb ? -1 : 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@
|
|||
|
||||
#include "nsXPCOMStrings.h"
|
||||
|
||||
class NS_COM_GLUE nsAString
|
||||
class nsAString
|
||||
{
|
||||
public:
|
||||
typedef PRUnichar char_type;
|
||||
|
@ -61,11 +61,11 @@ public:
|
|||
/**
|
||||
* Returns the length, beginning, and end of a string in one operation.
|
||||
*/
|
||||
PRUint32 BeginReading(const char_type **begin,
|
||||
const char_type **end = nsnull) const;
|
||||
NS_HIDDEN_(PRUint32) BeginReading(const char_type **begin,
|
||||
const char_type **end = nsnull) const;
|
||||
|
||||
const char_type* BeginReading() const;
|
||||
const char_type* EndReading() const;
|
||||
NS_HIDDEN_(const char_type*) BeginReading() const;
|
||||
NS_HIDDEN_(const char_type*) EndReading() const;
|
||||
|
||||
/**
|
||||
* Get the length, begin writing, and optionally set the length of a
|
||||
|
@ -75,14 +75,14 @@ public:
|
|||
* to leave the length unchanged.
|
||||
* @return The new length of the string, or 0 if resizing failed.
|
||||
*/
|
||||
PRUint32 BeginWriting(char_type **begin,
|
||||
char_type **end = nsnull,
|
||||
PRUint32 newSize = PR_UINT32_MAX);
|
||||
NS_HIDDEN_(PRUint32) BeginWriting(char_type **begin,
|
||||
char_type **end = nsnull,
|
||||
PRUint32 newSize = PR_UINT32_MAX);
|
||||
|
||||
char_type* BeginWriting();
|
||||
char_type* EndWriting();
|
||||
NS_HIDDEN_(char_type*) BeginWriting();
|
||||
NS_HIDDEN_(char_type*) EndWriting();
|
||||
|
||||
PRBool SetLength(PRUint32 aLen);
|
||||
NS_HIDDEN_(PRBool) SetLength(PRUint32 aLen);
|
||||
|
||||
NS_HIDDEN_(size_type) Length() const
|
||||
{
|
||||
|
@ -143,6 +143,9 @@ public:
|
|||
|
||||
NS_HIDDEN_(void) Truncate() { SetLength(0); }
|
||||
|
||||
NS_HIDDEN_(void) Trim(const char *aSet, PRBool aLeading = PR_TRUE,
|
||||
PRBool aTrailing = PR_TRUE);
|
||||
|
||||
/**
|
||||
* Compare strings of characters. Return 0 if the characters are equal,
|
||||
*/
|
||||
|
@ -150,15 +153,36 @@ public:
|
|||
const char_type *b,
|
||||
PRUint32 length);
|
||||
|
||||
static PRInt32 DefaultComparator(const char_type *a,
|
||||
const char_type *b,
|
||||
PRUint32 length);
|
||||
static NS_HIDDEN_(PRInt32) DefaultComparator(const char_type *a,
|
||||
const char_type *b,
|
||||
PRUint32 length);
|
||||
|
||||
PRBool Equals( const char_type *other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
NS_HIDDEN_(PRBool) Equals( const char_type *other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
|
||||
PRBool Equals( const self_type &other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
NS_HIDDEN_(PRBool) Equals( const self_type &other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
|
||||
/**
|
||||
* Case-insensitive match this string to a lowercase ASCII string.
|
||||
*/
|
||||
NS_HIDDEN_(PRBool) LowerCaseEqualsLiteral(const char *aASCIIString) const;
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) Find(const self_type& aStr,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
|
||||
/**
|
||||
* Find an ASCII string within this string.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) Find(const char *aStr, PRBool aIgnoreCase = PR_FALSE) const;
|
||||
|
||||
/**
|
||||
* Search for the offset of the last occurrence of a character in a
|
||||
|
@ -167,14 +191,19 @@ public:
|
|||
* @return The offset of the character from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
PRInt32 RFindChar(char_type aChar) const;
|
||||
NS_HIDDEN_(PRInt32) RFindChar(char_type aChar) const;
|
||||
|
||||
/**
|
||||
* Append a string representation of a number.
|
||||
*/
|
||||
NS_HIDDEN_(void) AppendInt(int aInt, PRInt32 aRadix = 10);
|
||||
|
||||
protected:
|
||||
// Prevent people from allocating a nsAString directly.
|
||||
~nsAString() {}
|
||||
};
|
||||
|
||||
class NS_COM_GLUE nsACString
|
||||
class nsACString
|
||||
{
|
||||
public:
|
||||
typedef char char_type;
|
||||
|
@ -185,11 +214,11 @@ public:
|
|||
/**
|
||||
* Returns the length, beginning, and end of a string in one operation.
|
||||
*/
|
||||
PRUint32 BeginReading(const char_type **begin,
|
||||
const char_type **end = nsnull) const;
|
||||
NS_HIDDEN_(PRUint32) BeginReading(const char_type **begin,
|
||||
const char_type **end = nsnull) const;
|
||||
|
||||
const char_type* BeginReading() const;
|
||||
const char_type* EndReading() const;
|
||||
NS_HIDDEN_(const char_type*) BeginReading() const;
|
||||
NS_HIDDEN_(const char_type*) EndReading() const;
|
||||
|
||||
/**
|
||||
* Get the length, begin writing, and optionally set the length of a
|
||||
|
@ -199,14 +228,14 @@ public:
|
|||
* to leave the length unchanged.
|
||||
* @return The new length of the string, or 0 if resizing failed.
|
||||
*/
|
||||
PRUint32 BeginWriting(char_type **begin,
|
||||
char_type **end = nsnull,
|
||||
PRUint32 newSize = PR_UINT32_MAX);
|
||||
NS_HIDDEN_(PRUint32) BeginWriting(char_type **begin,
|
||||
char_type **end = nsnull,
|
||||
PRUint32 newSize = PR_UINT32_MAX);
|
||||
|
||||
char_type* BeginWriting();
|
||||
char_type* EndWriting();
|
||||
NS_HIDDEN_(char_type*) BeginWriting();
|
||||
NS_HIDDEN_(char_type*) EndWriting();
|
||||
|
||||
PRBool SetLength(PRUint32 aLen);
|
||||
NS_HIDDEN_(PRBool) SetLength(PRUint32 aLen);
|
||||
|
||||
NS_HIDDEN_(size_type) Length() const
|
||||
{
|
||||
|
@ -267,6 +296,9 @@ public:
|
|||
|
||||
NS_HIDDEN_(void) Truncate() { SetLength(0); }
|
||||
|
||||
NS_HIDDEN_(void) Trim(const char *aSet, PRBool aLeading = PR_TRUE,
|
||||
PRBool aTrailing = PR_TRUE);
|
||||
|
||||
/**
|
||||
* Compare strings of characters. Return 0 if the characters are equal,
|
||||
*/
|
||||
|
@ -278,11 +310,30 @@ public:
|
|||
const char_type *b,
|
||||
PRUint32 length);
|
||||
|
||||
PRBool Equals( const char_type *other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
NS_HIDDEN_(PRBool) Equals( const char_type *other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
|
||||
PRBool Equals( const self_type &other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
NS_HIDDEN_(PRBool) Equals( const self_type &other,
|
||||
ComparatorFunc c = DefaultComparator ) const;
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) Find(const self_type& aStr,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) Find(const char_type *aStr,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
NS_HIDDEN_(PRInt32) Find(const char_type *aStr, PRUint32 aLen,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Search for the offset of the last occurrence of a character in a
|
||||
|
@ -291,7 +342,12 @@ public:
|
|||
* @return The offset of the character from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
PRInt32 RFindChar(char_type aChar) const;
|
||||
NS_HIDDEN_(PRInt32) RFindChar(char_type aChar) const;
|
||||
|
||||
/**
|
||||
* Append a string representation of a number.
|
||||
*/
|
||||
NS_HIDDEN_(void) AppendInt(int aInt, PRInt32 aRadix = 10);
|
||||
|
||||
protected:
|
||||
// Prevent people from allocating a nsAString directly.
|
||||
|
@ -951,9 +1007,39 @@ StringEndsWith(const nsACString& aSource, const nsACString& aSubstring,
|
|||
Equals(aSubstring, aComparator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim whitespace from the beginning and end of a string; then compress
|
||||
* remaining runs of whitespace characters to a single space.
|
||||
*/
|
||||
NS_HIDDEN_(void)
|
||||
CompressWhitespace(nsAString& aString);
|
||||
|
||||
#define EmptyCString() nsCString()
|
||||
#define EmptyString() nsString()
|
||||
|
||||
/**
|
||||
* Convert an ASCII string to all upper/lowercase (a-z,A-Z only). As a bonus,
|
||||
* returns the string length.
|
||||
*/
|
||||
NS_HIDDEN_(PRUint32)
|
||||
ToLowerCase(nsACString& aStr);
|
||||
|
||||
NS_HIDDEN_(PRUint32)
|
||||
ToUpperCase(nsACString& aStr);
|
||||
|
||||
NS_HIDDEN_(PRUint32)
|
||||
ToLowerCase(const nsACString& aSrc, nsACString& aDest);
|
||||
|
||||
NS_HIDDEN_(PRUint32)
|
||||
ToUpperCase(const nsACString& aSrc, nsACString& aDest);
|
||||
|
||||
/**
|
||||
* Comparison function for use with nsACString::Equals
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32)
|
||||
CaseInsensitiveCompare(const char *a, const char *b,
|
||||
PRUint32 length);
|
||||
|
||||
/**
|
||||
* The following declarations are *deprecated*, and are included here only
|
||||
* to make porting from existing code that doesn't use the frozen string API
|
||||
|
|
|
@ -48,7 +48,7 @@ ifndef MOZ_ENABLE_LIBXUL
|
|||
MOZILLA_INTERNAL_API = 1
|
||||
endif
|
||||
|
||||
DIRS = dynamic services
|
||||
DIRS = dynamic services external
|
||||
ifeq ($(OS_ARCH),WINNT)
|
||||
DIRS += windows
|
||||
endif
|
||||
|
@ -70,7 +70,6 @@ CPPSRCS = \
|
|||
TestObserverService.cpp \
|
||||
TestServMgr.cpp \
|
||||
TestAutoPtr.cpp \
|
||||
TestMinStringAPI.cpp \
|
||||
TestVersionComparator.cpp \
|
||||
$(NULL)
|
||||
|
||||
|
|
|
@ -0,0 +1,580 @@
|
|||
/* vim:set ts=2 sw=2 et cindent: */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla.
|
||||
*
|
||||
* The Initial Developer of the Original Code is IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@meer.net>
|
||||
* Benjamin Smedberg <benjamin@smedbergs.us>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "nsStringAPI.h"
|
||||
#include "nsXPCOM.h"
|
||||
#include "nsMemory.h"
|
||||
|
||||
static const char kAsciiData[] = "Hello World";
|
||||
|
||||
static const PRUnichar kUnicodeData[] =
|
||||
{'H','e','l','l','o',' ','W','o','r','l','d','\0'};
|
||||
|
||||
static PRBool test_basic_1()
|
||||
{
|
||||
nsCStringContainer s;
|
||||
NS_CStringContainerInit(s);
|
||||
|
||||
const char *ptr;
|
||||
PRUint32 len;
|
||||
char *clone;
|
||||
|
||||
NS_CStringGetData(s, &ptr);
|
||||
if (ptr == nsnull || *ptr != '\0')
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
NS_CStringSetData(s, kAsciiData, PR_UINT32_MAX);
|
||||
len = NS_CStringGetData(s, &ptr);
|
||||
if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
if (len != sizeof(kAsciiData)-1)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
clone = NS_CStringCloneData(s);
|
||||
if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
NS_Free(clone);
|
||||
|
||||
nsCStringContainer temp;
|
||||
NS_CStringContainerInit(temp);
|
||||
NS_CStringCopy(temp, s);
|
||||
|
||||
len = NS_CStringGetData(temp, &ptr);
|
||||
if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
if (len != sizeof(kAsciiData)-1)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
NS_CStringContainerFinish(temp);
|
||||
|
||||
NS_CStringContainerFinish(s);
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool test_basic_2()
|
||||
{
|
||||
nsStringContainer s;
|
||||
NS_StringContainerInit(s);
|
||||
|
||||
const PRUnichar *ptr;
|
||||
PRUint32 len;
|
||||
PRUnichar *clone;
|
||||
|
||||
NS_StringGetData(s, &ptr);
|
||||
if (ptr == nsnull || *ptr != '\0')
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
NS_StringSetData(s, kUnicodeData, PR_UINT32_MAX);
|
||||
len = NS_StringGetData(s, &ptr);
|
||||
if (len != sizeof(kUnicodeData)/2 - 1)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
if (ptr == nsnull || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
clone = NS_StringCloneData(s);
|
||||
if (ptr == nsnull || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
NS_Free(clone);
|
||||
|
||||
nsStringContainer temp;
|
||||
NS_StringContainerInit(temp);
|
||||
NS_StringCopy(temp, s);
|
||||
|
||||
len = NS_StringGetData(temp, &ptr);
|
||||
if (len != sizeof(kUnicodeData)/2 - 1)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
if (ptr == nsnull || memcmp(ptr, kUnicodeData, sizeof(kUnicodeData)) != 0)
|
||||
{
|
||||
NS_ERROR("unexpected result");
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
NS_StringContainerFinish(temp);
|
||||
|
||||
NS_StringContainerFinish(s);
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool test_convert()
|
||||
{
|
||||
nsStringContainer s;
|
||||
NS_StringContainerInit(s);
|
||||
NS_StringSetData(s, kUnicodeData, sizeof(kUnicodeData)/2 - 1);
|
||||
|
||||
nsCStringContainer temp;
|
||||
NS_CStringContainerInit(temp);
|
||||
|
||||
const char *data;
|
||||
|
||||
NS_UTF16ToCString(s, NS_CSTRING_ENCODING_ASCII, temp);
|
||||
NS_CStringGetData(temp, &data);
|
||||
if (strcmp(data, kAsciiData) != 0)
|
||||
return PR_FALSE;
|
||||
|
||||
NS_UTF16ToCString(s, NS_CSTRING_ENCODING_UTF8, temp);
|
||||
NS_CStringGetData(temp, &data);
|
||||
if (strcmp(data, kAsciiData) != 0)
|
||||
return PR_FALSE;
|
||||
|
||||
NS_CStringContainerFinish(temp);
|
||||
|
||||
NS_StringContainerFinish(s);
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool test_append()
|
||||
{
|
||||
nsCStringContainer s;
|
||||
NS_CStringContainerInit(s);
|
||||
|
||||
NS_CStringSetData(s, "foo");
|
||||
NS_CStringAppendData(s, "bar");
|
||||
|
||||
NS_CStringContainerFinish(s);
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
// Replace all occurrences of |matchVal| with |newVal|
|
||||
static void ReplaceSubstring( nsACString& str,
|
||||
const nsACString& matchVal,
|
||||
const nsACString& newVal )
|
||||
{
|
||||
const char* sp, *mp, *np;
|
||||
PRUint32 sl, ml, nl;
|
||||
|
||||
sl = NS_CStringGetData(str, &sp);
|
||||
ml = NS_CStringGetData(matchVal, &mp);
|
||||
nl = NS_CStringGetData(newVal, &np);
|
||||
|
||||
for (const char* iter = sp; iter <= sp + sl - ml; ++iter)
|
||||
{
|
||||
if (memcmp(iter, mp, ml) == 0)
|
||||
{
|
||||
PRUint32 offset = iter - sp;
|
||||
|
||||
NS_CStringSetDataRange(str, offset, ml, np, nl);
|
||||
|
||||
sl = NS_CStringGetData(str, &sp);
|
||||
|
||||
iter = sp + offset + nl - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static PRBool test_replace_driver(const char *strVal,
|
||||
const char *matchVal,
|
||||
const char *newVal,
|
||||
const char *finalVal)
|
||||
{
|
||||
nsCStringContainer a;
|
||||
NS_CStringContainerInit(a);
|
||||
NS_CStringSetData(a, strVal);
|
||||
|
||||
nsCStringContainer b;
|
||||
NS_CStringContainerInit(b);
|
||||
NS_CStringSetData(b, matchVal);
|
||||
|
||||
nsCStringContainer c;
|
||||
NS_CStringContainerInit(c);
|
||||
NS_CStringSetData(c, newVal);
|
||||
|
||||
ReplaceSubstring(a, b, c);
|
||||
|
||||
const char *data;
|
||||
NS_CStringGetData(a, &data);
|
||||
if (strcmp(data, finalVal) != 0)
|
||||
return PR_FALSE;
|
||||
|
||||
NS_CStringContainerFinish(c);
|
||||
NS_CStringContainerFinish(b);
|
||||
NS_CStringContainerFinish(a);
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool test_replace()
|
||||
{
|
||||
PRBool rv;
|
||||
|
||||
rv = test_replace_driver("hello world, hello again!",
|
||||
"hello",
|
||||
"goodbye",
|
||||
"goodbye world, goodbye again!");
|
||||
if (!rv)
|
||||
return rv;
|
||||
|
||||
rv = test_replace_driver("foofoofoofoo!",
|
||||
"foo",
|
||||
"bar",
|
||||
"barbarbarbar!");
|
||||
if (!rv)
|
||||
return rv;
|
||||
|
||||
rv = test_replace_driver("foo bar systems",
|
||||
"xyz",
|
||||
"crazy",
|
||||
"foo bar systems");
|
||||
if (!rv)
|
||||
return rv;
|
||||
|
||||
rv = test_replace_driver("oh",
|
||||
"xyz",
|
||||
"crazy",
|
||||
"oh");
|
||||
if (!rv)
|
||||
return rv;
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static const char* kWhitespace="\b\t\r\n ";
|
||||
|
||||
static void
|
||||
CompressWhitespace(nsACString &str)
|
||||
{
|
||||
const char *p;
|
||||
PRInt32 i, len = (PRInt32) NS_CStringGetData(str, &p);
|
||||
|
||||
// trim leading whitespace
|
||||
|
||||
for (i=0; i<len; ++i)
|
||||
{
|
||||
if (!strchr(kWhitespace, (char) p[i]))
|
||||
break;
|
||||
}
|
||||
|
||||
if (i>0)
|
||||
{
|
||||
NS_CStringCutData(str, 0, i);
|
||||
len = (PRInt32) NS_CStringGetData(str, &p);
|
||||
}
|
||||
|
||||
// trim trailing whitespace
|
||||
|
||||
for (i=len-1; i>=0; --i)
|
||||
{
|
||||
if (!strchr(kWhitespace, (char) p[i]))
|
||||
break;
|
||||
}
|
||||
|
||||
if (++i < len)
|
||||
NS_CStringCutData(str, i, len - i);
|
||||
}
|
||||
|
||||
static PRBool test_compress_ws()
|
||||
{
|
||||
nsCStringContainer s;
|
||||
NS_CStringContainerInit(s);
|
||||
NS_CStringSetData(s, " \thello world\r \n");
|
||||
CompressWhitespace(s);
|
||||
const char *d;
|
||||
NS_CStringGetData(s, &d);
|
||||
PRBool rv = !strcmp(d, "hello world");
|
||||
if (!rv)
|
||||
printf("=> \"%s\"\n", d);
|
||||
NS_CStringContainerFinish(s);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PRBool test_depend()
|
||||
{
|
||||
static const char kData[] = "hello world";
|
||||
|
||||
nsCStringContainer s;
|
||||
NS_ENSURE_SUCCESS(
|
||||
NS_CStringContainerInit2(s, kData, sizeof(kData)-1,
|
||||
NS_CSTRING_CONTAINER_INIT_DEPEND),
|
||||
PR_FALSE);
|
||||
|
||||
const char *sd;
|
||||
NS_CStringGetData(s, &sd);
|
||||
|
||||
PRBool rv = (sd == kData);
|
||||
NS_CStringContainerFinish(s);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PRBool test_depend_sub()
|
||||
{
|
||||
static const char kData[] = "hello world";
|
||||
|
||||
nsCStringContainer s;
|
||||
NS_ENSURE_SUCCESS(
|
||||
NS_CStringContainerInit2(s, kData, sizeof(kData)-1,
|
||||
NS_CSTRING_CONTAINER_INIT_DEPEND |
|
||||
NS_CSTRING_CONTAINER_INIT_SUBSTRING),
|
||||
PR_FALSE);
|
||||
|
||||
PRBool terminated;
|
||||
const char *sd;
|
||||
PRUint32 len = NS_CStringGetData(s, &sd, &terminated);
|
||||
|
||||
PRBool rv = (sd == kData && len == sizeof(kData)-1 && !terminated);
|
||||
NS_CStringContainerFinish(s);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PRBool test_adopt()
|
||||
{
|
||||
static const char kData[] = "hello world";
|
||||
|
||||
char *data = (char *) nsMemory::Clone(kData, sizeof(kData));
|
||||
if (!data)
|
||||
return PR_FALSE;
|
||||
|
||||
nsCStringContainer s;
|
||||
NS_ENSURE_SUCCESS(
|
||||
NS_CStringContainerInit2(s, data, PR_UINT32_MAX,
|
||||
NS_CSTRING_CONTAINER_INIT_ADOPT),
|
||||
PR_FALSE); // leaks data on failure *shrug*
|
||||
|
||||
const char *sd;
|
||||
NS_CStringGetData(s, &sd);
|
||||
|
||||
PRBool rv = (sd == data);
|
||||
NS_CStringContainerFinish(s);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PRBool test_adopt_sub()
|
||||
{
|
||||
static const char kData[] = "hello world";
|
||||
|
||||
char *data = (char *) nsMemory::Clone(kData, sizeof(kData)-1);
|
||||
if (!data)
|
||||
return PR_FALSE;
|
||||
|
||||
nsCStringContainer s;
|
||||
NS_ENSURE_SUCCESS(
|
||||
NS_CStringContainerInit2(s, data, sizeof(kData)-1,
|
||||
NS_CSTRING_CONTAINER_INIT_ADOPT |
|
||||
NS_CSTRING_CONTAINER_INIT_SUBSTRING),
|
||||
PR_FALSE); // leaks data on failure *shrug*
|
||||
|
||||
PRBool terminated;
|
||||
const char *sd;
|
||||
PRUint32 len = NS_CStringGetData(s, &sd, &terminated);
|
||||
|
||||
PRBool rv = (sd == data && len == sizeof(kData)-1 && !terminated);
|
||||
NS_CStringContainerFinish(s);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PRBool test_mutation()
|
||||
{
|
||||
nsCStringContainer s;
|
||||
NS_CStringContainerInit(s);
|
||||
|
||||
const char kText[] = "Every good boy does fine.";
|
||||
|
||||
char *buf;
|
||||
PRUint32 len = NS_CStringGetMutableData(s, sizeof(kText) - 1, &buf);
|
||||
if (!buf || len != sizeof(kText) - 1)
|
||||
return PR_FALSE;
|
||||
memcpy(buf, kText, sizeof(kText));
|
||||
|
||||
const char *data;
|
||||
NS_CStringGetData(s, &data);
|
||||
if (strcmp(data, kText) != 0)
|
||||
return PR_FALSE;
|
||||
|
||||
PRUint32 newLen = len + 1;
|
||||
len = NS_CStringGetMutableData(s, newLen, &buf);
|
||||
if (!buf || len != newLen)
|
||||
return PR_FALSE;
|
||||
|
||||
buf[len - 1] = '.';
|
||||
|
||||
NS_CStringGetData(s, &data);
|
||||
if (strncmp(data, kText, len - 1) != 0 || data[len - 1] != '.')
|
||||
return PR_FALSE;
|
||||
|
||||
NS_CStringContainerFinish(s);
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool test_trim()
|
||||
{
|
||||
static const char kWS[] = "\n\t\r ";
|
||||
static const char kTestString[] = " \n\tTesting...\n\r";
|
||||
|
||||
nsCString test1(kTestString);
|
||||
nsCString test2(kTestString);
|
||||
nsCString test3(kTestString);
|
||||
|
||||
test1.Trim(kWS);
|
||||
test2.Trim(kWS, PR_TRUE, PR_FALSE);
|
||||
test3.Trim(kWS, PR_FALSE, PR_TRUE);
|
||||
|
||||
if (!test1.Equals("Testing..."))
|
||||
return PR_FALSE;
|
||||
|
||||
if (!test2.Equals("Testing...\n\r"))
|
||||
return PR_FALSE;
|
||||
|
||||
if (!test3.Equals(" \n\tTesting..."))
|
||||
return PR_FALSE;
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool test_find()
|
||||
{
|
||||
nsString uni(kUnicodeData);
|
||||
|
||||
static const char kHello[] = "Hello";
|
||||
static const char khello[] = "hello";
|
||||
static const char kBye[] = "Bye!";
|
||||
|
||||
PRInt32 found;
|
||||
|
||||
found = uni.Find(kHello);
|
||||
if (found != 0)
|
||||
return PR_FALSE;
|
||||
|
||||
found = uni.Find(khello, PR_FALSE);
|
||||
if (found != -1)
|
||||
return PR_FALSE;
|
||||
|
||||
found = uni.Find(khello, PR_TRUE);
|
||||
if (found != 0)
|
||||
return PR_FALSE;
|
||||
|
||||
found = uni.Find(kBye);
|
||||
if (found != -1)
|
||||
return PR_FALSE;
|
||||
|
||||
found = uni.Find(NS_LITERAL_STRING("World"));
|
||||
if (found != 6)
|
||||
return PR_FALSE;
|
||||
|
||||
found = uni.Find(uni);
|
||||
if (found != 0)
|
||||
return PR_FALSE;
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool test_compressws()
|
||||
{
|
||||
nsString check(NS_LITERAL_STRING(" \tTesting \n\t1\n 2 3\n "));
|
||||
CompressWhitespace(check);
|
||||
return check.Equals(NS_LITERAL_STRING("Testing 1 2 3"));
|
||||
}
|
||||
|
||||
//----
|
||||
|
||||
typedef PRBool (*TestFunc)();
|
||||
|
||||
static const struct Test
|
||||
{
|
||||
const char* name;
|
||||
TestFunc func;
|
||||
}
|
||||
tests[] =
|
||||
{
|
||||
{ "test_basic_1", test_basic_1 },
|
||||
{ "test_basic_2", test_basic_2 },
|
||||
{ "test_convert", test_convert },
|
||||
{ "test_append", test_append },
|
||||
{ "test_replace", test_replace },
|
||||
{ "test_compress_ws", test_compress_ws },
|
||||
{ "test_depend", test_depend },
|
||||
{ "test_depend_sub", test_depend_sub },
|
||||
{ "test_adopt", test_adopt },
|
||||
{ "test_adopt_sub", test_adopt_sub },
|
||||
{ "test_mutation", test_mutation },
|
||||
{ "test_trim", test_trim },
|
||||
{ "test_find", test_find },
|
||||
{ "test_compressws", test_compressws },
|
||||
{ nsnull, nsnull }
|
||||
};
|
||||
|
||||
//----
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int count = 1;
|
||||
if (argc > 1)
|
||||
count = atoi(argv[1]);
|
||||
|
||||
while (count--)
|
||||
{
|
||||
for (const Test* t = tests; t->name != nsnull; ++t)
|
||||
{
|
||||
printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Загрузка…
Ссылка в новой задаче