From b20a9793f9ea8b42349b8f61be09ceed13f1659c Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Fri, 13 Sep 2013 19:25:01 +0000 Subject: [PATCH] ICU-10282 deprecate bad API RuleBasedCollator::cloneRuleData(), call cloneBinary() X-SVN-Rev: 34314 --- icu4c/source/i18n/tblcoll.cpp | 20 +++++++++- icu4c/source/i18n/ucol.cpp | 62 ----------------------------- icu4c/source/i18n/ucol_imp.h | 14 +------ icu4c/source/i18n/unicode/tblcoll.h | 11 +++-- icu4c/source/tools/genrb/parse.cpp | 3 +- 5 files changed, 27 insertions(+), 83 deletions(-) diff --git a/icu4c/source/i18n/tblcoll.cpp b/icu4c/source/i18n/tblcoll.cpp index cbe0394930c..75458542ff8 100644 --- a/icu4c/source/i18n/tblcoll.cpp +++ b/icu4c/source/i18n/tblcoll.cpp @@ -1,6 +1,6 @@ /* ****************************************************************************** - * Copyright (C) 1996-2012, International Business Machines Corporation and + * Copyright (C) 1996-2013, International Business Machines Corporation and * others. All Rights Reserved. ****************************************************************************** */ @@ -456,7 +456,23 @@ int32_t RuleBasedCollator::getMaxExpansion(int32_t order) const uint8_t* RuleBasedCollator::cloneRuleData(int32_t &length, UErrorCode &status) { - return ucol_cloneRuleData(ucollator, &length, &status); + if (U_FAILURE(status)) { return NULL; } + LocalMemory buffer((uint8_t *)uprv_malloc(20000)); + if (buffer.isNull()) { + status = U_MEMORY_ALLOCATION_ERROR; + return NULL; + } + length = cloneBinary(buffer.getAlias(), 20000, status); + if (status == U_BUFFER_OVERFLOW_ERROR) { + if (buffer.allocateInsteadAndCopy(length, 0) == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + return NULL; + } + status = U_ZERO_ERROR; + length = cloneBinary(buffer.getAlias(), length, status); + } + if (U_FAILURE(status)) { return NULL; } + return buffer.orphan(); } diff --git a/icu4c/source/i18n/ucol.cpp b/icu4c/source/i18n/ucol.cpp index 6a5a820becc..54fe8a3d390 100644 --- a/icu4c/source/i18n/ucol.cpp +++ b/icu4c/source/i18n/ucol.cpp @@ -764,68 +764,6 @@ ucol_close(UCollator *coll) UTRACE_EXIT(); } -/* This one is currently used by genrb & tests. After constructing from rules (tailoring),*/ -/* you should be able to get the binary chunk to write out... Doesn't look very full now */ -U_CFUNC uint8_t* U_EXPORT2 -ucol_cloneRuleData(const UCollator *coll, int32_t *length, UErrorCode *status) -{ - uint8_t *result = NULL; - if(U_FAILURE(*status)) { - return NULL; - } - if(coll->hasRealData == TRUE) { - *length = coll->image->size; - result = (uint8_t *)uprv_malloc(*length); - /* test for NULL */ - if (result == NULL) { - *status = U_MEMORY_ALLOCATION_ERROR; - return NULL; - } - uprv_memcpy(result, coll->image, *length); - } else { - *length = (int32_t)(paddedsize(sizeof(UCATableHeader))+paddedsize(sizeof(UColOptionSet))); - result = (uint8_t *)uprv_malloc(*length); - /* test for NULL */ - if (result == NULL) { - *status = U_MEMORY_ALLOCATION_ERROR; - return NULL; - } - - /* build the UCATableHeader with minimal entries */ - /* do not copy the header from the UCA file because its values are wrong! */ - /* uprv_memcpy(result, UCA->image, sizeof(UCATableHeader)); */ - - /* reset everything */ - uprv_memset(result, 0, *length); - - /* set the tailoring-specific values */ - UCATableHeader *myData = (UCATableHeader *)result; - myData->size = *length; - - /* offset for the options, the only part of the data that is present after the header */ - myData->options = sizeof(UCATableHeader); - - /* need to always set the expansion value for an upper bound of the options */ - myData->expansion = myData->options + sizeof(UColOptionSet); - - myData->magic = UCOL_HEADER_MAGIC; - myData->isBigEndian = U_IS_BIG_ENDIAN; - myData->charSetFamily = U_CHARSET_FAMILY; - - /* copy UCA's version; genrb will override all but the builder version with tailoring data */ - uprv_memcpy(myData->version, coll->image->version, sizeof(UVersionInfo)); - - uprv_memcpy(myData->UCAVersion, coll->image->UCAVersion, sizeof(UVersionInfo)); - uprv_memcpy(myData->UCDVersion, coll->image->UCDVersion, sizeof(UVersionInfo)); - uprv_memcpy(myData->formatVersion, coll->image->formatVersion, sizeof(UVersionInfo)); - myData->jamoSpecial = coll->image->jamoSpecial; - - /* copy the collator options */ - uprv_memcpy(result+paddedsize(sizeof(UCATableHeader)), coll->options, sizeof(UColOptionSet)); - } - return result; -} - void ucol_setOptionsFromHeader(UCollator* result, UColOptionSet * opts, UErrorCode *status) { if(U_FAILURE(*status)) { return; diff --git a/icu4c/source/i18n/ucol_imp.h b/icu4c/source/i18n/ucol_imp.h index 301188accf9..deaa2dbdb0e 100644 --- a/icu4c/source/i18n/ucol_imp.h +++ b/icu4c/source/i18n/ucol_imp.h @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 1998-2012, International Business Machines +* Copyright (C) 1998-2013, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -578,18 +578,6 @@ SortKeyGenerator(const UCollator *coll, #endif -/** - * Makes a copy of the Collator's rule data. The format is - * that of .col files. - * - * @param length returns the length of the data, in bytes. - * @param status the error status - * @return memory, owned by the caller, of size 'length' bytes. - * @internal INTERNAL USE ONLY - */ -U_CFUNC uint8_t* U_EXPORT2 -ucol_cloneRuleData(const UCollator *coll, int32_t *length, UErrorCode *status); - /** * Used to set requested and valid locales on a collator returned by the collator * service. diff --git a/icu4c/source/i18n/unicode/tblcoll.h b/icu4c/source/i18n/unicode/tblcoll.h index 04fd248084a..947ac65d59d 100644 --- a/icu4c/source/i18n/unicode/tblcoll.h +++ b/icu4c/source/i18n/unicode/tblcoll.h @@ -439,16 +439,19 @@ public: */ static UClassID U_EXPORT2 getStaticClassID(void); +#ifndef U_HIDE_DEPRECATED_API /** - * Returns the binary format of the class's rules. The format is that of - * .col files. + * Do not use this method: The caller and the ICU library might use different heaps. + * Use cloneBinary() instead which writes to caller-provided memory. + * + * Returns a binary format of this collator. * @param length Returns the length of the data, in bytes * @param status the error code status. * @return memory, owned by the caller, of size 'length' bytes. - * @stable ICU 2.2 + * @deprecated ICU 52. Use cloneBinary() instead. */ uint8_t *cloneRuleData(int32_t &length, UErrorCode &status); - +#endif /* U_HIDE_DEPRECATED_API */ /** Creates a binary image of a collator. This binary image can be stored and * later used to instantiate a collator using ucol_openBinary. diff --git a/icu4c/source/tools/genrb/parse.cpp b/icu4c/source/tools/genrb/parse.cpp index 5546411581e..ab7bcca23d4 100644 --- a/icu4c/source/tools/genrb/parse.cpp +++ b/icu4c/source/tools/genrb/parse.cpp @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 1998-2012, International Business Machines +* Copyright (C) 1998-2013, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -937,7 +937,6 @@ addCollation(ParseState* state, struct SResource *result, uint32_t startline, U data = (uint8_t *)uprv_malloc(len); intStatus = U_ZERO_ERROR; len = ucol_cloneBinary(coll, data, len, &intStatus); - /*data = ucol_cloneRuleData(coll, &len, &intStatus);*/ /* tailoring rules version */ /* This is wrong! */