From 8c9b3faf99ebea77265ef3a38ad0fac1d94498e7 Mon Sep 17 00:00:00 2001 From: Neil Rashbrook Date: Fri, 25 Mar 2011 11:39:38 +0000 Subject: [PATCH] Bug 540322 Add some missing methods to the external string API r=bsmedberg --- xpcom/glue/nsStringAPI.h | 53 ++++++++++++++++++++--- xpcom/tests/external/TestMinStringAPI.cpp | 48 ++++++++++++++++++++ 2 files changed, 94 insertions(+), 7 deletions(-) diff --git a/xpcom/glue/nsStringAPI.h b/xpcom/glue/nsStringAPI.h index 19ca5338d07..35183df31f0 100644 --- a/xpcom/glue/nsStringAPI.h +++ b/xpcom/glue/nsStringAPI.h @@ -55,6 +55,13 @@ #include "prlog.h" #include "nsTArray.h" +/** + * Comparison function for use with nsACString::Equals + */ +NS_HIDDEN_(PRInt32) +CaseInsensitiveCompare(const char *a, const char *b, + PRUint32 length); + class nsAString { public: @@ -85,6 +92,12 @@ public: { return CharAt(0); } + NS_HIDDEN_(char_type) Last() const + { + const char_type* data; + PRUint32 dataLen = NS_StringGetData(*this, &data); + return data[dataLen - 1]; + } /** * Get the length, begin writing, and optionally set the length of a @@ -137,6 +150,7 @@ public: } NS_HIDDEN_(void) AssignLiteral(const char *aStr); + NS_HIDDEN_(void) AssignASCII(const char *aStr) { AssignLiteral(aStr); } NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; } NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; } @@ -156,11 +170,14 @@ public: PRUint32 dataLen = NS_StringGetData(readable, &data); NS_StringSetDataRange(*this, cutStart, cutLength, data, dataLen); } + NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos ) + { Replace(pos, 1, &c, 1); } NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); } NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); } NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); } NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr ); + NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { AppendLiteral(aASCIIStr); } NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; } NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; } @@ -265,6 +282,10 @@ public: } NS_HIDDEN_(PRBool) EqualsLiteral(const char *aASCIIString) const; + NS_HIDDEN_(PRBool) EqualsASCII(const char *aASCIIString) const + { + return EqualsLiteral(aASCIIString); + } /** * Case-insensitive match this string to a lowercase ASCII string. @@ -409,6 +430,12 @@ public: { return CharAt(0); } + NS_HIDDEN_(char_type) Last() const + { + const char_type* data; + PRUint32 dataLen = NS_CStringGetData(*this, &data); + return data[dataLen - 1]; + } /** * Get the length, begin writing, and optionally set the length of a @@ -463,6 +490,10 @@ public: { Assign(aData); } + NS_HIDDEN_(void) AssignASCII(const char_type *aData) + { + Assign(aData); + } NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString); return *this; } NS_HIDDEN_(self_type&) operator=(const char_type* aPtr) { Assign(aPtr); return *this; } @@ -482,11 +513,14 @@ public: PRUint32 dataLen = NS_CStringGetData(readable, &data); NS_CStringSetDataRange(*this, cutStart, cutLength, data, dataLen); } + NS_HIDDEN_(void) SetCharAt( char_type c, index_type pos ) + { Replace(pos, 1, &c, 1); } NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); } NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); } NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); } NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr ) { Append(aASCIIStr); } + NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { Append(aASCIIStr); } NS_HIDDEN_(self_type&) operator+=( char_type c ) { Append(c); return *this; } NS_HIDDEN_(self_type&) operator+=( const char_type* data ) { Append(data); return *this; } @@ -594,6 +628,18 @@ public: { return Equals(other); } + NS_HIDDEN_(PRBool) EqualsASCII( const char_type *other ) const + { + return Equals(other); + } + + /** + * Case-insensitive match this string to a lowercase ASCII string. + */ + NS_HIDDEN_(PRBool) LowerCaseEqualsLiteral(const char *aASCIIString) const + { + return Equals(aASCIIString, CaseInsensitiveCompare); + } /** * Find the first occurrence of aStr in this string. @@ -1393,13 +1439,6 @@ 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 diff --git a/xpcom/tests/external/TestMinStringAPI.cpp b/xpcom/tests/external/TestMinStringAPI.cpp index 070ed5fc0e2..91d5f1d2869 100644 --- a/xpcom/tests/external/TestMinStringAPI.cpp +++ b/xpcom/tests/external/TestMinStringAPI.cpp @@ -464,6 +464,52 @@ static PRBool test_mutation() return PR_TRUE; } +static PRBool test_ascii() +{ + nsCString testCString; + testCString.AppendASCII(kAsciiData); + if (!testCString.EqualsLiteral(kAsciiData)) + return PR_FALSE; + + testCString.AssignASCII(kAsciiData); + if (!testCString.LowerCaseEqualsLiteral("hello world")) + return PR_FALSE; + + nsString testString; + testString.AppendASCII(kAsciiData); + if (!testString.EqualsLiteral(kAsciiData)) + return PR_FALSE; + + testString.AssignASCII(kAsciiData); + if (!testString.LowerCaseEqualsLiteral("hello world")) + return PR_FALSE; + + return PR_TRUE; +} + +static PRBool test_chars() +{ + nsCString testCString(kAsciiData); + if (testCString.First() != 'H') + return PR_FALSE; + if (testCString.Last() != 'd') + return PR_FALSE; + testCString.SetCharAt('u', 8); + if (!testCString.EqualsASCII("Hello Would")) + return PR_FALSE; + + nsString testString(kUnicodeData); + if (testString.First() != 'H') + return PR_FALSE; + if (testString.Last() != 'd') + return PR_FALSE; + testString.SetCharAt('u', 8); + if (!testString.EqualsASCII("Hello Would")) + return PR_FALSE; + + return PR_TRUE; +} + static PRBool test_stripchars() { nsCString test(kAsciiData); @@ -965,6 +1011,8 @@ tests[] = { "test_adopt", test_adopt }, { "test_adopt_sub", test_adopt_sub }, { "test_mutation", test_mutation }, + { "test_ascii", test_ascii }, + { "test_chars", test_chars }, { "test_stripchars", test_stripchars }, { "test_trim", test_trim }, { "test_find", test_find },