From 47f1c672ec8fa76ccb2537fe2ca65f439c5e4685 Mon Sep 17 00:00:00 2001 From: "darin%meer.net" Date: Sat, 4 Feb 2006 00:42:18 +0000 Subject: [PATCH] fixes bug 311456 "Calling Truncate() on an nsAutoString makes next append work hard and doesn't clear F_VOIDED flag" r=bzbarsky git-svn-id: svn://10.0.0.236/trunk@188910 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/string/src/nsTSubstring.cpp | 10 +++- mozilla/xpcom/tests/TestStrings.cpp | 64 +++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/mozilla/xpcom/string/src/nsTSubstring.cpp b/mozilla/xpcom/string/src/nsTSubstring.cpp index f2872c2ff67..2f3cbe06cf7 100644 --- a/mozilla/xpcom/string/src/nsTSubstring.cpp +++ b/mozilla/xpcom/string/src/nsTSubstring.cpp @@ -79,8 +79,10 @@ nsTSubstring_CharT::MutatePrep( size_type capacity, char_type** oldData, PRUint3 if (curCapacity != size_type(-1)) { - if (capacity <= curCapacity) + if (capacity <= curCapacity) { + mFlags &= ~F_VOIDED; // mutation clears voided flag return PR_TRUE; + } if (curCapacity > 0) { @@ -119,6 +121,7 @@ nsTSubstring_CharT::MutatePrep( size_type capacity, char_type** oldData, PRUint3 hdr = newHdr; mData = (char_type*) hdr->Data(); + mFlags &= ~F_VOIDED; // mutation clears voided flag return PR_TRUE; } } @@ -562,6 +565,11 @@ nsTSubstring_CharT::SetCapacity( size_type capacity ) void nsTSubstring_CharT::SetLength( size_type length ) { + if (mLength == length) { + mFlags &= ~F_VOIDED; // mutation clears voided flag + return; + } + SetCapacity(length); // XXX(darin): SetCapacity may fail, but it doesn't give us a way to find diff --git a/mozilla/xpcom/tests/TestStrings.cpp b/mozilla/xpcom/tests/TestStrings.cpp index 86e8caaabf7..fedf12f33eb 100644 --- a/mozilla/xpcom/tests/TestStrings.cpp +++ b/mozilla/xpcom/tests/TestStrings.cpp @@ -613,6 +613,68 @@ PRBool test_stringbuffer() return rv; } +PRBool test_voided() + { + const char kData[] = "hello world"; + + nsXPIDLCString str; + if (str) + return PR_FALSE; + if (!str.IsVoid()) + return PR_FALSE; + if (!str.IsEmpty()) + return PR_FALSE; + + str.Assign(kData); + if (strcmp(str, kData) != 0) + return PR_FALSE; + + str.SetIsVoid(PR_TRUE); + if (str) + return PR_FALSE; + if (!str.IsVoid()) + return PR_FALSE; + if (!str.IsEmpty()) + return PR_FALSE; + + str.SetIsVoid(PR_FALSE); + if (strcmp(str, "") != 0) + return PR_FALSE; + + return PR_TRUE; + } + +PRBool test_voided_autostr() + { + const char kData[] = "hello world"; + + nsCAutoString str; + if (str.IsVoid()) + return PR_FALSE; + if (!str.IsEmpty()) + return PR_FALSE; + + str.Assign(kData); + if (strcmp(str.get(), kData) != 0) + return PR_FALSE; + + str.SetIsVoid(PR_TRUE); + if (!str.IsVoid()) + return PR_FALSE; + if (!str.IsEmpty()) + return PR_FALSE; + + str.Assign(kData); + if (str.IsVoid()) + return PR_FALSE; + if (str.IsEmpty()) + return PR_FALSE; + if (strcmp(str.get(), kData) != 0) + return PR_FALSE; + + return PR_TRUE; + } + //---- typedef PRBool (*TestFunc)(); @@ -651,6 +713,8 @@ tests[] = { "test_findcharinset", test_findcharinset }, { "test_rfindcharinset", test_rfindcharinset }, { "test_stringbuffer", test_stringbuffer }, + { "test_voided", test_voided }, + { "test_voided_autostr", test_voided_autostr }, { nsnull, nsnull } };