ICU-1256 make US::getCapacity() public and use it

X-SVN-Rev: 6089
This commit is contained in:
Markus Scherer 2001-10-05 20:44:56 +00:00
parent c51eed2f61
commit 761c91b967
3 changed files with 41 additions and 19 deletions

View File

@ -554,7 +554,7 @@ Locale::getDisplayLanguage(const Locale &displayLocale,
}
length=uloc_getDisplayLanguage(fullName, displayLocale.fullName,
buffer, ULOC_FULLNAME_CAPACITY,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
@ -566,7 +566,7 @@ Locale::getDisplayLanguage(const Locale &displayLocale,
}
errorCode=U_ZERO_ERROR;
length=uloc_getDisplayLanguage(fullName, displayLocale.fullName,
buffer, length,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
}
@ -598,7 +598,7 @@ Locale::getDisplayCountry(const Locale &displayLocale,
}
length=uloc_getDisplayCountry(fullName, displayLocale.fullName,
buffer, ULOC_FULLNAME_CAPACITY,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
@ -610,7 +610,7 @@ Locale::getDisplayCountry(const Locale &displayLocale,
}
errorCode=U_ZERO_ERROR;
length=uloc_getDisplayCountry(fullName, displayLocale.fullName,
buffer, length,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
}
@ -642,7 +642,7 @@ Locale::getDisplayVariant(const Locale &displayLocale,
}
length=uloc_getDisplayVariant(fullName, displayLocale.fullName,
buffer, ULOC_FULLNAME_CAPACITY,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
@ -654,7 +654,7 @@ Locale::getDisplayVariant(const Locale &displayLocale,
}
errorCode=U_ZERO_ERROR;
length=uloc_getDisplayVariant(fullName, displayLocale.fullName,
buffer, length,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
}
@ -686,7 +686,7 @@ Locale::getDisplayName(const Locale &displayLocale,
}
length=uloc_getDisplayName(fullName, displayLocale.fullName,
buffer, ULOC_FULLNAME_CAPACITY,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
@ -698,7 +698,7 @@ Locale::getDisplayName(const Locale &displayLocale,
}
errorCode=U_ZERO_ERROR;
length=uloc_getDisplayName(fullName, displayLocale.fullName,
buffer, length,
buffer, result.getCapacity(),
&errorCode);
result.releaseBuffer(length);
}

View File

@ -1401,6 +1401,16 @@ public:
*/
inline UBool empty(void) const;
/**
* Return the capacity of the internal buffer of the UnicodeString object.
* This is useful together with the getBuffer functions.
* See there for details.
*
* @return the number of UChars available in the internal buffer
* @see getBuffer
* @draft ICU 2.0 was private before
*/
inline int32_t getCapacity(void) const;
/* Other operations */
@ -2144,6 +2154,10 @@ public:
* An attempted nested call will return 0, and will not further modify the
* state of the UnicodeString object.
*
* The actual capacity of the string buffer may be larger than minCapacity.
* getCapacity() returns the actual capacity.
* For many operations, the full capacity should be used to avoid reallocations.
*
* While the buffer is "open" between getBuffer(minCapacity)
* and releaseBuffer(newLength), the following applies:
* - The string length is set to 0.
@ -2202,7 +2216,16 @@ public:
* The pointer that it returns will remain valid until the UnicodeString object is modified,
* at which time the pointer is semantically invalidated and must not be used any more.
*
* The buffer contents is not NUL-terminated.
* The capacity of the buffer can be determined with getCapacity().
* The part after length() may or may not be initialized and valid,
* depending on the history of the UnicodeString object.
*
* The buffer contents is (probably) not NUL-terminated.
* You can check if it is with
* <code>(s.length()<s.getCapacity() && buffer[s.length()]==0)</code>.
*
* The buffer may reside in read-only memory. Its contents must not
* be modified.
*
* @return a read-only pointer to the internal string buffer,
* or 0 if the string is empty or bogus
@ -2571,9 +2594,6 @@ private:
inline UChar* getArrayStart(void);
inline const UChar* getArrayStart(void) const;
// get the "real" capacity of the array, adjusted for ref count
inline int32_t getCapacity(void) const;
// allocate the array; result may be fStackBuffer
// sets refCount to 1 if appropriate
// sets fArray, fCapacity, and fFlags

View File

@ -952,11 +952,15 @@ UnicodeStringTest::TestMiscellaneous()
errln("hashCode() failed");
// test getBuffer(minCapacity) and releaseBuffer()
test1=UnicodeString(); // make sure that it starts with its stackBuffer
UChar *p=test1.getBuffer(20);
if(test1.getCapacity()<20) {
errln("UnicodeString::getBuffer(20).getCapacity()<20");
}
test1.append((UChar)7); // must not be able to modify the string here
test1.setCharAt(3, 7);
test1.reverse();
if( test1.length()!=0 ||
test1.charAt(0)!=0xffff || test1.charAt(3)!=0xffff ||
test1.getBuffer(10)!=0 || test1.getBuffer()!=0
@ -996,15 +1000,13 @@ UnicodeStringTest::TestMiscellaneous()
errln("UnicodeString::releaseBuffer(-1) does not properly set the length of the UnicodeString");
}
// test releaseBuffer() with a NUL-terminated buffer
enum { capacity=254 }; // assume that this will be the actual UnicodeString capacity - getCapacity() is private
p=test1.getBuffer(capacity);
for(int32_t i=0; i<capacity; ++i) {
// test releaseBuffer() with a non-NUL-terminated buffer
p=test1.getBuffer(256);
for(int32_t i=0; i<test1.getCapacity(); ++i) {
p[i]=(UChar)1; // fill the buffer with all non-NUL code units
}
test1.releaseBuffer(); // implicit -1
if(test1.length()!=capacity || test1.charAt(1)!=1 || test1.charAt(100)!=1 || test1.charAt(capacity-1)!=1) {
if(test1.length()!=test1.getCapacity() || test1.charAt(1)!=1 || test1.charAt(100)!=1 || test1.charAt(test1.getCapacity()-1)!=1) {
errln("UnicodeString::releaseBuffer(-1 but no NUL) does not properly set the length of the UnicodeString");
}