Bug 337730 - add additional utilities to nsStringAPI, r=darin

TestMinStringAPI.cpp moved from xpcom/glue to xpcom/glue/external
This commit is contained in:
benjamin@smedbergs.us
2006-05-19 22:37:17 +00:00
parent 0d4d7865d8
commit 10cccea95b
7 changed files with 621 additions and 59 deletions

View File

@@ -196,6 +196,10 @@ public:
static char* strtok(char* str, const char* delims, char* *newStr); static char* strtok(char* str, const char* delims, char* *newStr);
static PRUint32 strlen(const PRUnichar* s) { static PRUint32 strlen(const PRUnichar* s) {
if (!s) {
NS_ERROR("Passing null to nsCRT::strlen");
return 0;
}
return NS_strlen(s); return NS_strlen(s);
} }

View File

@@ -134,7 +134,7 @@ NS_strndup(const PRUnichar *aString, PRUint32 aLen)
// This table maps uppercase characters to lower case characters; // This table maps uppercase characters to lower case characters;
// characters that are neither upper nor lower case are unaffected. // 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, 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, 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, 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 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, 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, 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, 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 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) 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) 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) PRBool NS_IsAscii(PRUnichar aChar)

View File

@@ -99,8 +99,27 @@ NS_strndup(const PRUnichar *aString, PRUint32 aLen);
// The following case-conversion methods only deal in the ascii repertoire // The following case-conversion methods only deal in the ascii repertoire
// A-Z and a-z // A-Z and a-z
NS_COM_GLUE char NS_ToUpper(char aChar); // semi-private data declarations... don't use these directly.
NS_COM_GLUE char NS_ToLower(char aChar); 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_IsUpper(char aChar);
NS_COM_GLUE PRBool NS_IsLower(char aChar); NS_COM_GLUE PRBool NS_IsLower(char aChar);

View File

@@ -45,6 +45,7 @@
#endif #endif
#include "nsXPCOMStrings.h" #include "nsXPCOMStrings.h"
#include "nsDebug.h"
#ifdef MOZILLA_INTERNAL_API #ifdef MOZILLA_INTERNAL_API
#undef MOZILLA_INTERNAL_API #undef MOZILLA_INTERNAL_API
@@ -52,6 +53,12 @@
#include "nsStringAPI.h" #include "nsStringAPI.h"
#include <stdio.h>
#ifdef XP_WIN
#define snprintf _snprintf
#endif
// nsAString // nsAString
PRUint32 PRUint32
@@ -114,6 +121,47 @@ nsAString::SetLength(PRUint32 aLen)
return data != nsnull; 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 PRInt32
nsAString::DefaultComparator(const char_type *a, const char_type *b, nsAString::DefaultComparator(const char_type *a, const char_type *b,
PRUint32 len) PRUint32 len)
@@ -155,6 +203,100 @@ nsAString::Equals(const self_type &other, ComparatorFunc c) const
return c(cself, cother, selflen) == 0; 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 PRInt32
nsAString::RFindChar(char_type aChar) const nsAString::RFindChar(char_type aChar) const
{ {
@@ -172,6 +314,35 @@ nsAString::RFindChar(char_type aChar) const
return -1; 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 // nsACString
PRUint32 PRUint32
@@ -234,6 +405,47 @@ nsACString::SetLength(PRUint32 aLen)
return data != nsnull; 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 PRInt32
nsACString::DefaultComparator(const char_type *a, const char_type *b, nsACString::DefaultComparator(const char_type *a, const char_type *b,
PRUint32 len) PRUint32 len)
@@ -268,6 +480,44 @@ nsACString::Equals(const self_type &other, ComparatorFunc c) const
return c(cself, cother, selflen) == 0; 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 PRInt32
nsACString::RFindChar(char_type aChar) const nsACString::RFindChar(char_type aChar) const
{ {
@@ -282,6 +532,35 @@ nsACString::RFindChar(char_type aChar) const
return -1; 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 // Substrings
nsDependentSubstring::nsDependentSubstring(const abstract_string_type& aStr, nsDependentSubstring::nsDependentSubstring(const abstract_string_type& aStr,
@@ -357,3 +636,119 @@ ToNewUTF8String(const nsAString& aSource)
CopyUTF16toUTF8(aSource, temp); CopyUTF16toUTF8(aSource, temp);
return NS_CStringCloneData(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;
}

View File

@@ -50,7 +50,7 @@
#include "nsXPCOMStrings.h" #include "nsXPCOMStrings.h"
class NS_COM_GLUE nsAString class nsAString
{ {
public: public:
typedef PRUnichar char_type; typedef PRUnichar char_type;
@@ -61,11 +61,11 @@ public:
/** /**
* Returns the length, beginning, and end of a string in one operation. * Returns the length, beginning, and end of a string in one operation.
*/ */
PRUint32 BeginReading(const char_type **begin, NS_HIDDEN_(PRUint32) BeginReading(const char_type **begin,
const char_type **end = nsnull) const; const char_type **end = nsnull) const;
const char_type* BeginReading() const; NS_HIDDEN_(const char_type*) BeginReading() const;
const char_type* EndReading() const; NS_HIDDEN_(const char_type*) EndReading() const;
/** /**
* Get the length, begin writing, and optionally set the length of a * Get the length, begin writing, and optionally set the length of a
@@ -75,14 +75,14 @@ public:
* to leave the length unchanged. * to leave the length unchanged.
* @return The new length of the string, or 0 if resizing failed. * @return The new length of the string, or 0 if resizing failed.
*/ */
PRUint32 BeginWriting(char_type **begin, NS_HIDDEN_(PRUint32) BeginWriting(char_type **begin,
char_type **end = nsnull, char_type **end = nsnull,
PRUint32 newSize = PR_UINT32_MAX); PRUint32 newSize = PR_UINT32_MAX);
char_type* BeginWriting(); NS_HIDDEN_(char_type*) BeginWriting();
char_type* EndWriting(); NS_HIDDEN_(char_type*) EndWriting();
PRBool SetLength(PRUint32 aLen); NS_HIDDEN_(PRBool) SetLength(PRUint32 aLen);
NS_HIDDEN_(size_type) Length() const NS_HIDDEN_(size_type) Length() const
{ {
@@ -143,6 +143,9 @@ public:
NS_HIDDEN_(void) Truncate() { SetLength(0); } 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, * Compare strings of characters. Return 0 if the characters are equal,
*/ */
@@ -150,15 +153,36 @@ public:
const char_type *b, const char_type *b,
PRUint32 length); PRUint32 length);
static PRInt32 DefaultComparator(const char_type *a, static NS_HIDDEN_(PRInt32) DefaultComparator(const char_type *a,
const char_type *b, const char_type *b,
PRUint32 length); PRUint32 length);
PRBool Equals( const char_type *other, NS_HIDDEN_(PRBool) Equals( const char_type *other,
ComparatorFunc c = DefaultComparator ) const; ComparatorFunc c = DefaultComparator ) const;
PRBool Equals( const self_type &other, NS_HIDDEN_(PRBool) Equals( const self_type &other,
ComparatorFunc c = DefaultComparator ) const; 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 * 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, * @return The offset of the character from the beginning of the string,
* or -1 if not found. * 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: protected:
// Prevent people from allocating a nsAString directly. // Prevent people from allocating a nsAString directly.
~nsAString() {} ~nsAString() {}
}; };
class NS_COM_GLUE nsACString class nsACString
{ {
public: public:
typedef char char_type; typedef char char_type;
@@ -185,11 +214,11 @@ public:
/** /**
* Returns the length, beginning, and end of a string in one operation. * Returns the length, beginning, and end of a string in one operation.
*/ */
PRUint32 BeginReading(const char_type **begin, NS_HIDDEN_(PRUint32) BeginReading(const char_type **begin,
const char_type **end = nsnull) const; const char_type **end = nsnull) const;
const char_type* BeginReading() const; NS_HIDDEN_(const char_type*) BeginReading() const;
const char_type* EndReading() const; NS_HIDDEN_(const char_type*) EndReading() const;
/** /**
* Get the length, begin writing, and optionally set the length of a * Get the length, begin writing, and optionally set the length of a
@@ -199,14 +228,14 @@ public:
* to leave the length unchanged. * to leave the length unchanged.
* @return The new length of the string, or 0 if resizing failed. * @return The new length of the string, or 0 if resizing failed.
*/ */
PRUint32 BeginWriting(char_type **begin, NS_HIDDEN_(PRUint32) BeginWriting(char_type **begin,
char_type **end = nsnull, char_type **end = nsnull,
PRUint32 newSize = PR_UINT32_MAX); PRUint32 newSize = PR_UINT32_MAX);
char_type* BeginWriting(); NS_HIDDEN_(char_type*) BeginWriting();
char_type* EndWriting(); NS_HIDDEN_(char_type*) EndWriting();
PRBool SetLength(PRUint32 aLen); NS_HIDDEN_(PRBool) SetLength(PRUint32 aLen);
NS_HIDDEN_(size_type) Length() const NS_HIDDEN_(size_type) Length() const
{ {
@@ -267,6 +296,9 @@ public:
NS_HIDDEN_(void) Truncate() { SetLength(0); } 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, * Compare strings of characters. Return 0 if the characters are equal,
*/ */
@@ -278,11 +310,30 @@ public:
const char_type *b, const char_type *b,
PRUint32 length); PRUint32 length);
PRBool Equals( const char_type *other, NS_HIDDEN_(PRBool) Equals( const char_type *other,
ComparatorFunc c = DefaultComparator ) const; ComparatorFunc c = DefaultComparator ) const;
PRBool Equals( const self_type &other, NS_HIDDEN_(PRBool) Equals( const self_type &other,
ComparatorFunc c = DefaultComparator ) const; 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 * 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, * @return The offset of the character from the beginning of the string,
* or -1 if not found. * 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: protected:
// Prevent people from allocating a nsAString directly. // Prevent people from allocating a nsAString directly.
@@ -951,9 +1007,39 @@ StringEndsWith(const nsACString& aSource, const nsACString& aSubstring,
Equals(aSubstring, aComparator); 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 EmptyCString() nsCString()
#define EmptyString() nsString() #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 * The following declarations are *deprecated*, and are included here only
* to make porting from existing code that doesn't use the frozen string API * to make porting from existing code that doesn't use the frozen string API

View File

@@ -48,7 +48,7 @@ ifndef MOZ_ENABLE_LIBXUL
MOZILLA_INTERNAL_API = 1 MOZILLA_INTERNAL_API = 1
endif endif
DIRS = dynamic services DIRS = dynamic services external
ifeq ($(OS_ARCH),WINNT) ifeq ($(OS_ARCH),WINNT)
DIRS += windows DIRS += windows
endif endif
@@ -70,7 +70,6 @@ CPPSRCS = \
TestObserverService.cpp \ TestObserverService.cpp \
TestServMgr.cpp \ TestServMgr.cpp \
TestAutoPtr.cpp \ TestAutoPtr.cpp \
TestMinStringAPI.cpp \
TestVersionComparator.cpp \ TestVersionComparator.cpp \
$(NULL) $(NULL)

View File

@@ -20,6 +20,7 @@
* *
* Contributor(s): * Contributor(s):
* Darin Fisher <darin@meer.net> * Darin Fisher <darin@meer.net>
* Benjamin Smedberg <benjamin@smedbergs.us>
* *
* Alternatively, the contents of this file may be used under the terms of * 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 * either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -35,20 +36,16 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#ifdef MOZILLA_INTERNAL_API
#undef MOZILLA_INTERNAL_API
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "nsStringAPI.h" #include "nsStringAPI.h"
#include "nsXPCOM.h" #include "nsXPCOM.h"
#include "nsMemory.h" #include "nsMemory.h"
static const char kAsciiData[] = "hello world"; static const char kAsciiData[] = "Hello World";
static const PRUnichar kUnicodeData[] = static const PRUnichar kUnicodeData[] =
{'h','e','l','l','o',' ','w','o','r','l','d','\0'}; {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
static PRBool test_basic_1() static PRBool test_basic_1()
{ {
@@ -466,6 +463,75 @@ static PRBool test_mutation()
return PR_TRUE; 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)(); typedef PRBool (*TestFunc)();
@@ -488,6 +554,9 @@ tests[] =
{ "test_adopt", test_adopt }, { "test_adopt", test_adopt },
{ "test_adopt_sub", test_adopt_sub }, { "test_adopt_sub", test_adopt_sub },
{ "test_mutation", test_mutation }, { "test_mutation", test_mutation },
{ "test_trim", test_trim },
{ "test_find", test_find },
{ "test_compressws", test_compressws },
{ nsnull, nsnull } { nsnull, nsnull }
}; };