diff options
author | Oliver-Rainer Wittmann <orw@apache.org> | 2012-11-19 09:10:46 +0000 |
---|---|---|
committer | Oliver-Rainer Wittmann <orw@apache.org> | 2012-11-19 09:10:46 +0000 |
commit | 3e1227e38c424776cd28f9aba625b2ac61221e92 (patch) | |
tree | 8d05321884efc4fe7fde97fd2709841aef0b017c | |
parent | 5fa621198a981ea994d9903298e8a1d884ee686b (diff) |
#121139# - string handling: clear reference counting and passing ownership
Found by: Regina Henschel, Ariel Constenla-Haile
Review by: Herbert Duerr
Notes
14 files changed, 42 insertions, 45 deletions
diff --git a/i18npool/source/characterclassification/cclass_unicode.cxx b/i18npool/source/characterclassification/cclass_unicode.cxx index b46a54960d4b..a2128a2e361c 100644 --- a/i18npool/source/characterclassification/cclass_unicode.cxx +++ b/i18npool/source/characterclassification/cclass_unicode.cxx @@ -94,7 +94,7 @@ cclass_Unicode::toTitle( const OUString& Text, sal_Int32 nPos, sal_Int32 nCount, nCount = len - nPos; trans->setMappingType(MappingTypeToTitle, rLocale); - rtl_uString* pStr = x_rtl_uString_new_WithLength( nCount, 1 ); + rtl_uString* pStr = x_rtl_uString_new_WithLength( nCount ); // defined in x_rtl_ustring.h sal_Unicode* out = pStr->buffer; BreakIteratorImpl brk(xMSF); Boundary bdy = brk.getWordBoundary(Text, nPos, rLocale, diff --git a/i18npool/source/nativenumber/nativenumbersupplier.cxx b/i18npool/source/nativenumber/nativenumbersupplier.cxx index d3e484cc691c..7eda6d220bb1 100644 --- a/i18npool/source/nativenumber/nativenumbersupplier.cxx +++ b/i18npool/source/nativenumber/nativenumbersupplier.cxx @@ -73,7 +73,7 @@ OUString SAL_CALL AsciiToNativeChar( const OUString& inStr, sal_Int32 startPos, Sequence< sal_Int32 >& offset, sal_Bool useOffset, sal_Int16 number ) throw(RuntimeException) { const sal_Unicode *src = inStr.getStr() + startPos; - rtl_uString *newStr = x_rtl_uString_new_WithLength(nCount); + rtl_uString *newStr = x_rtl_uString_new_WithLength( nCount ); // defined in x_rtl_ustring.h if (useOffset) offset.realloc(nCount); @@ -93,7 +93,7 @@ OUString SAL_CALL AsciiToNativeChar( const OUString& inStr, sal_Int32 startPos, if (useOffset) offset[i] = startPos + i; } - return OUString( newStr, SAL_NO_ACQUIRE); + return OUString( newStr, SAL_NO_ACQUIRE); // take over ownership of <newStr> } sal_Bool SAL_CALL AsciiToNative_numberMaker(const sal_Unicode *str, sal_Int32 begin, sal_Int32 len, @@ -246,7 +246,10 @@ OUString SAL_CALL AsciiToNative( const OUString& inStr, sal_Int32 startPos, sal_ if (useOffset) offset.realloc(count); - return OUString(newStr->buffer, count); + OUString resultStr( newStr->buffer, count ); + x_rtl_uString_release( newStr ); + x_rtl_uString_release( srcStr ); + return resultStr; } return OUString(); } @@ -309,7 +312,7 @@ static OUString SAL_CALL NativeToAscii(const OUString& inStr, if (nCount > 0) { const sal_Unicode *str = inStr.getStr() + startPos; - rtl_uString *newStr = x_rtl_uString_new_WithLength(nCount * MultiplierExponent_7_CJK[0] + 1); + rtl_uString *newStr = x_rtl_uString_new_WithLength( nCount * MultiplierExponent_7_CJK[0] + 1 ); if (useOffset) offset.realloc( nCount * MultiplierExponent_7_CJK[0] + 1 ); sal_Int32 count = 0, index; @@ -367,7 +370,9 @@ static OUString SAL_CALL NativeToAscii(const OUString& inStr, for (i = 0; i < count; i++) offset[i] += startPos; } - return OUString(newStr->buffer, count); + OUString resultStr( newStr->buffer, count ); + x_rtl_uString_release( newStr ); + return resultStr; } return OUString(); } diff --git a/i18npool/source/textconversion/textconversion_ko.cxx b/i18npool/source/textconversion/textconversion_ko.cxx index e17b7ea1ba8f..7ce4adbfa702 100644 --- a/i18npool/source/textconversion/textconversion_ko.cxx +++ b/i18npool/source/textconversion/textconversion_ko.cxx @@ -155,7 +155,8 @@ TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData) { rtl_uString * newStr = x_rtl_uString_new_WithLength( nLength ); // defined in x_rtl_ustring.h sal_Int32 count = 0; - while (count < nLength) { + while (count < nLength) + { ch = aText[nStartPos + count]; sal_Unicode address = getHanja2HangulIndex()[ch>>8]; if (address != 0xFFFF) @@ -166,10 +167,12 @@ TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos else break; } - if (count > 0) { + if (count > 0) + { output.realloc(1); - output[0] = OUString( newStr->buffer, count); + output[0] = OUString( newStr->buffer, count ); } + x_rtl_uString_release( newStr ); } return output; } diff --git a/i18npool/source/textconversion/textconversion_zh.cxx b/i18npool/source/textconversion/textconversion_zh.cxx index b88929886411..012dfc77456e 100644 --- a/i18npool/source/textconversion/textconversion_zh.cxx +++ b/i18npool/source/textconversion/textconversion_zh.cxx @@ -84,7 +84,7 @@ TextConversion_zh::getCharConversion(const OUString& aText, sal_Int32 nStartPos, for (sal_Int32 i = 0; i < nLength; i++) newStr->buffer[i] = getOneCharConversion(aText[nStartPos+i], Data, Index); - return OUString( newStr, SAL_NO_ACQUIRE); + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } OUString SAL_CALL @@ -214,7 +214,9 @@ TextConversion_zh::getWordConversion(const OUString& aText, sal_Int32 nStartPos, } if (offset.getLength() > 0) offset.realloc(one2one ? 0 : count); - return OUString( newStr->buffer, count); + OUString resultStr( newStr->buffer, count ); + x_rtl_uString_release( newStr ); + return resultStr; } TextConversionResult SAL_CALL diff --git a/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx b/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx index 5cbd3883398d..b997498ab8e2 100644 --- a/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx @@ -139,7 +139,7 @@ ignoreIandEfollowedByYa_ja_JP::folding( const OUString& inStr, sal_Int32 startPo newStr->length = sal_Int32(dst - newStr->buffer); if (useOffset) offset.realloc(newStr->length); - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } } } } } diff --git a/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx b/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx index 478bd0b0628f..462d0d178fa5 100644 --- a/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx @@ -146,7 +146,7 @@ ignoreIterationMark_ja_JP::folding( const OUString& inStr, sal_Int32 startPos, s newStr->length = sal_Int32(dst - newStr->buffer); if (useOffset) offset.realloc(newStr->length); - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } diff --git a/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx b/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx index 30963c2b7ade..701c7f354731 100644 --- a/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx @@ -96,7 +96,7 @@ ignoreKiKuFollowedBySa_ja_JP::folding( const OUString& inStr, sal_Int32 startPos newStr->length = sal_Int32(dst - newStr->buffer); if (useOffset) offset.realloc(newStr->length); - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } } } } } diff --git a/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx b/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx index e3efe058d4f4..53d9a9a4545e 100644 --- a/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx @@ -354,7 +354,7 @@ ignoreProlongedSoundMark_ja_JP::folding( const OUString& inStr, sal_Int32 startP newStr->length = sal_Int32(dst - newStr->buffer); if (useOffset) offset.realloc(newStr->length); - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } diff --git a/i18npool/source/transliteration/transliteration_Ignore.cxx b/i18npool/source/transliteration/transliteration_Ignore.cxx index e613be133d7b..5c3b41a23c39 100644 --- a/i18npool/source/transliteration/transliteration_Ignore.cxx +++ b/i18npool/source/transliteration/transliteration_Ignore.cxx @@ -204,7 +204,7 @@ transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos, offset.realloc(newStr->length); *dst = (sal_Unicode) 0; - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } sal_Unicode SAL_CALL diff --git a/i18npool/source/transliteration/transliteration_Numeric.cxx b/i18npool/source/transliteration/transliteration_Numeric.cxx index 639adef0e89d..341606d51eb3 100644 --- a/i18npool/source/transliteration/transliteration_Numeric.cxx +++ b/i18npool/source/transliteration/transliteration_Numeric.cxx @@ -72,7 +72,7 @@ transliteration_Numeric::transliterateBullet( const OUString& inStr, sal_Int32 s if (endPos > inStr.getLength()) endPos = inStr.getLength(); - rtl_uString* pStr = x_rtl_uString_new_WithLength( nCount, 1 ); // our x_rtl_ustring.h + rtl_uString* pStr = x_rtl_uString_new_WithLength( nCount ); // our x_rtl_ustring.h sal_Unicode* out = pStr->buffer; if (useOffset) @@ -114,7 +114,7 @@ transliteration_Numeric::transliterateBullet( const OUString& inStr, sal_Int32 s if (useOffset) offset.realloc(j); - return OUString( pStr, SAL_NO_ACQUIRE ); + return OUString( pStr, SAL_NO_ACQUIRE ); // take over ownership of <pStr> } OUString SAL_CALL diff --git a/i18npool/source/transliteration/transliteration_OneToOne.cxx b/i18npool/source/transliteration/transliteration_OneToOne.cxx index f05314995355..4be6efc05784 100644 --- a/i18npool/source/transliteration/transliteration_OneToOne.cxx +++ b/i18npool/source/transliteration/transliteration_OneToOne.cxx @@ -91,7 +91,7 @@ transliteration_OneToOne::transliterate( const OUString& inStr, sal_Int32 startP } *dst = (sal_Unicode) 0; - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } sal_Unicode SAL_CALL diff --git a/i18npool/source/transliteration/transliteration_body.cxx b/i18npool/source/transliteration/transliteration_body.cxx index 48358f7b0593..15902486ba36 100644 --- a/i18npool/source/transliteration/transliteration_body.cxx +++ b/i18npool/source/transliteration/transliteration_body.cxx @@ -183,7 +183,7 @@ Transliteration_body::transliterate( const Mapping &map = casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType ); nOffCount += map.nmap; } - rtl_uString* pStr = x_rtl_uString_new_WithLength( nOffCount, 1 ); // our x_rtl_ustring.h + rtl_uString* pStr = x_rtl_uString_new_WithLength( nOffCount ); // our x_rtl_ustring.h sal_Unicode* out = pStr->buffer; if ( nOffCount != offset.getLength() ) @@ -207,7 +207,7 @@ Transliteration_body::transliterate( } out[j] = 0; - return OUString( pStr, SAL_NO_ACQUIRE ); + return OUString( pStr, SAL_NO_ACQUIRE ); // take over ownership of <pStr> } else { @@ -254,7 +254,7 @@ OUString SAL_CALL Transliteration_body::transliterateChar2String( sal_Unicode inChar ) throw(RuntimeException) { const Mapping &map = casefolding::getValue(&inChar, 0, 1, aLocale, nMappingType); - rtl_uString* pStr = x_rtl_uString_new_WithLength( map.nmap, 1 ); // our x_rtl_ustring.h + rtl_uString* pStr = x_rtl_uString_new_WithLength( map.nmap ); // our x_rtl_ustring.h sal_Unicode* out = pStr->buffer; sal_Int32 i; @@ -262,7 +262,7 @@ Transliteration_body::transliterateChar2String( sal_Unicode inChar ) throw(Runti out[i] = map.map[i]; out[i] = 0; - return OUString( pStr, SAL_NO_ACQUIRE ); + return OUString( pStr, SAL_NO_ACQUIRE ); // take over ownership of <pStr> } sal_Unicode SAL_CALL diff --git a/i18nutil/inc/i18nutil/x_rtl_ustring.h b/i18nutil/inc/i18nutil/x_rtl_ustring.h index bdc24f4dc749..89942eb3f9f9 100644 --- a/i18nutil/inc/i18nutil/x_rtl_ustring.h +++ b/i18nutil/inc/i18nutil/x_rtl_ustring.h @@ -32,18 +32,15 @@ /** * Allocates a new <code>rtl_uString</code> which can hold nLen + 1 characters. - * The reference count is 0. The characters of room is not cleared. - * This method is similar to rtl_uString_new_WithLength in rtl/ustring.h, but - * can allocate a new string more efficiently. You need to "acquire" by such as - * OUString( rtl_uString * value ) if you intend to use it for a while. - * @param [output] newStr + * The reference count is 1. The memory allocated for the characters is not initialized. * @param [input] nLen */ -inline void SAL_CALL x_rtl_uString_new_WithLength( rtl_uString ** newStr, sal_Int32 nLen, sal_Int32 _refCount = 0 ) +inline rtl_uString * SAL_CALL x_rtl_uString_new_WithLength( sal_Int32 nLen ) { - *newStr = (rtl_uString*) rtl_allocateMemory ( sizeof(rtl_uString) + sizeof(sal_Unicode) * nLen); - (*newStr)->refCount = _refCount; - (*newStr)->length = nLen; + rtl_uString *newStr = (rtl_uString*) rtl_allocateMemory ( sizeof(rtl_uString) + sizeof(sal_Unicode) * nLen); + newStr->refCount = 1; + newStr->length = nLen; + return newStr; // rtl_uString is defined in rtl/ustring.h as below: //typedef struct _rtl_uString @@ -54,14 +51,6 @@ inline void SAL_CALL x_rtl_uString_new_WithLength( rtl_uString ** newStr, sal_In //} rtl_uString; } -inline rtl_uString * SAL_CALL x_rtl_uString_new_WithLength( sal_Int32 nLen, sal_Int32 _refCount = 0 ) -{ - rtl_uString *newStr = (rtl_uString*) rtl_allocateMemory ( sizeof(rtl_uString) + sizeof(sal_Unicode) * nLen); - newStr->refCount = _refCount; - newStr->length = nLen; - return newStr; -} - /** * Release <code>rtl_uString</code> regardless its reference count. */ diff --git a/i18nutil/source/utility/widthfolding.cxx b/i18nutil/source/utility/widthfolding.cxx index 2b6d365f3937..ee1554d9972e 100644 --- a/i18nutil/source/utility/widthfolding.cxx +++ b/i18nutil/source/utility/widthfolding.cxx @@ -49,8 +49,7 @@ OUString widthfolding::decompose_ja_voiced_sound_marks (const OUString& inStr, s { // Create a string buffer which can hold nCount * 2 + 1 characters. // Its size may become double of nCount. - rtl_uString * newStr; - x_rtl_uString_new_WithLength( &newStr, nCount * 2 ); // defined in x_rtl_ustring.h The reference count is 0 now. + rtl_uString * newStr = x_rtl_uString_new_WithLength( nCount * 2 ); // defined in x_rtl_ustring.h sal_Int32 *p = NULL; sal_Int32 position = 0; @@ -94,7 +93,7 @@ OUString widthfolding::decompose_ja_voiced_sound_marks (const OUString& inStr, s newStr->length = sal_Int32(dst - newStr->buffer); if (useOffset) offset.realloc(newStr->length); - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } oneToOneMapping& widthfolding::getfull2halfTable(void) @@ -111,7 +110,6 @@ OUString widthfolding::compose_ja_voiced_sound_marks (const OUString& inStr, sal { // Create a string buffer which can hold nCount + 1 characters. // Its size may become equal to nCount or smaller. - // The reference count is 0 now. rtl_uString * newStr = x_rtl_uString_new_WithLength( nCount ); // defined in x_rtl_ustring.h // Prepare pointers of unicode character arrays. @@ -199,7 +197,7 @@ OUString widthfolding::compose_ja_voiced_sound_marks (const OUString& inStr, sal } if (useOffset) offset.realloc(newStr->length); - return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. + return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr> } oneToOneMapping& widthfolding::gethalf2fullTable(void) |