ICU-2351 allow remove(guaranteed everything) and truncate(0) to also un-bogus a string

X-SVN-Rev: 10289
This commit is contained in:
Markus Scherer 2002-11-19 02:12:39 +00:00
parent 6fe60cd9a4
commit dc2a132696
3 changed files with 61 additions and 19 deletions

View File

@ -148,11 +148,7 @@ UnicodeString CanonicalIterator::next() {
}
// delete old contents
if(buffer.isBogus()) {
buffer.setTo((UChar32)-1); // un-bogus the buffer
} else {
buffer.truncate(0); // keep the internal array allocated
}
buffer.remove();
// construct return value

View File

@ -1516,7 +1516,7 @@ public:
* @draft ICU 2.0
*/
int32_t
countChar32(int32_t start=0, int32_t length=0x7fffffff) const;
countChar32(int32_t start=0, int32_t length=INT32_MAX) const;
/**
* Check if the length UChar code units of the string
@ -1531,7 +1531,7 @@ public:
*
* @param start the index of the first code unit to check (0 for the entire string)
* @param length the number of UChar code units to check
* (use 0x7fffffff for the entire string; remember that start/length
* (use INT32_MAX for the entire string; remember that start/length
* values are pinned)
* @param number The number of code points in the (sub)string is compared against
* the 'number' parameter.
@ -1770,19 +1770,24 @@ public:
* take a UErrorCode for simplicity.
*
* More generally, a bogus string has a non-value that is different from an empty string.
* It behaves much like a null reference in Java, a NULL pointer in C,
* It behaves much (but of course not entirely) like a null reference in Java, a NULL pointer in C,
* or a NULL value in SQL.
* A "bogus" string does not contain a string value, and getBuffer()
* and similar will return NULL.
*
* The string object can be "revived" by assigning (operator=() or fastCopyFrom())
* another string, or by using one of the other setToXYZ functions.
* another string, or by using one of the other setToXYZ functions,
* or by using truncate() and remove() in ways that are always equivalent to assigning
* an empty string. See examples below.
*
* The simplest ways to turn a bogus string into an empty one
* is to assign an empty string, or to setTo an empty one.
* For example:
* is to use the remove() function.
* Examples for other functions that are equivalent to "set to empty string":
* \code
* if(s.isBogus()) {
* s.remove(); // set to an empty string (remove all), or
* s.remove(0, INT32_MAX); // set to an empty string (remove all), or
* s.truncate(0); // set to an empty string (complete truncation), or
* s=UnicodeString(); // assign an empty string, or
* s.setTo((UChar32)-1); // set to a pseudo code point that is out of range, or
* static const UChar nul=0;
@ -3954,11 +3959,26 @@ UnicodeString::insert(int32_t start,
inline UnicodeString&
UnicodeString::remove(int32_t start,
int32_t length)
{ return doReplace(start, length, NULL, 0, 0); }
{
if(start <= 0 && length == INT32_MAX) {
// remove(guaranteed everything) of a bogus string makes the string empty and non-bogus
return remove();
} else {
return doReplace(start, length, NULL, 0, 0);
}
}
inline UnicodeString&
UnicodeString::remove()
{ return doReplace(0, fLength, 0, 0, 0); }
{
// remove() of a bogus string makes the string empty and non-bogus
if(isBogus()) {
unBogus();
} else {
fLength = 0;
}
return *this;
}
inline UnicodeString&
UnicodeString::removeBetween(int32_t start,
@ -3968,7 +3988,11 @@ UnicodeString::removeBetween(int32_t start,
inline UBool
UnicodeString::truncate(int32_t targetLength)
{
if((uint32_t)targetLength < (uint32_t)fLength) {
if(isBogus() && targetLength == 0) {
// truncate(0) of a bogus string makes the string empty and non-bogus
unBogus();
return FALSE;
} else if((uint32_t)targetLength < (uint32_t)fLength) {
fLength = targetLength;
return TRUE;
} else {

View File

@ -1257,32 +1257,38 @@ UnicodeStringTest::TestBogus() {
}
// verify that non-assignment modifications fail and do not revive a bogus string
test3.setToBogus();
test3.append((UChar)0x61);
if(!test3.isBogus() || test3.getBuffer()!=0) {
errln("bogus.append('a') worked but must not");
}
test3.setToBogus();
test3.findAndReplace(UnicodeString((UChar)0x61), test2);
if(!test3.isBogus() || test3.getBuffer()!=0) {
errln("bogus.findAndReplace() worked but must not");
}
test3.setToBogus();
test3.trim();
if(!test3.isBogus() || test3.getBuffer()!=0) {
errln("bogus.trim() revived bogus but must not");
}
test3.remove();
test3.setToBogus();
test3.remove(1);
if(!test3.isBogus() || test3.getBuffer()!=0) {
errln("bogus.remove() revived bogus but must not");
errln("bogus.remove(1) revived bogus but must not");
}
test3.setToBogus();
if(!test3.setCharAt(0, 0x62).isBogus() || !test3.isEmpty()) {
errln("bogus.setCharAt(0, 'b') worked but must not");
}
if(test3.truncate(0) || !test3.isBogus() || !test3.isEmpty()) {
errln("bogus.truncate(0) revived bogus but must not");
test3.setToBogus();
if(test3.truncate(1) || !test3.isBogus() || !test3.isEmpty()) {
errln("bogus.truncate(1) revived bogus but must not");
}
// verify that assignments revive a bogus string
@ -1337,7 +1343,7 @@ UnicodeStringTest::TestBogus() {
errln("bogus.setTo(writable alias) failed");
}
// same with simple, documented ways to turn a bogus string into an empty one
// verify simple, documented ways to turn a bogus string into an empty one
test3.setToBogus();
if(!test3.isBogus() || (test3=UnicodeString()).isBogus() || !test3.isEmpty()) {
errln("bogus.operator=(UnicodeString()) failed");
@ -1348,6 +1354,21 @@ UnicodeStringTest::TestBogus() {
errln("bogus.setTo(UnicodeString()) failed");
}
test3.setToBogus();
if(test3.remove().isBogus() || test3.getBuffer()==0 || !test3.isEmpty()) {
errln("bogus.remove() failed");
}
test3.setToBogus();
if(test3.remove(0, INT32_MAX).isBogus() || test3.getBuffer()==0 || !test3.isEmpty()) {
errln("bogus.remove(0, INT32_MAX) failed");
}
test3.setToBogus();
if(test3.truncate(0) || test3.isBogus() || !test3.isEmpty()) {
errln("bogus.truncate(0) failed");
}
test3.setToBogus();
if(!test3.isBogus() || test3.setTo((UChar32)-1).isBogus() || !test3.isEmpty()) {
errln("bogus.setTo((UChar32)-1) failed");
@ -1365,6 +1386,7 @@ UnicodeStringTest::TestBogus() {
errln("setToBogus() failed to make a string bogus");
}
test3.setToBogus();
if(test1.isBogus() || !(test1=test3).isBogus()) {
errln("normal=bogus failed to make the left string bogus");
}