ICU-12446 Resource sink size: move into new enter(size) method

X-SVN-Rev: 38606
This commit is contained in:
Markus Scherer 2016-04-07 00:18:33 +00:00
parent bc785127a3
commit f292628ed7
8 changed files with 67 additions and 96 deletions

View File

@ -1,6 +1,6 @@
/* /*
******************************************************************************* *******************************************************************************
* Copyright (C) 2015, International Business Machines * Copyright (C) 2015-2016, International Business Machines
* Corporation and others. All Rights Reserved. * Corporation and others. All Rights Reserved.
******************************************************************************* *******************************************************************************
* resource.cpp * resource.cpp
@ -22,16 +22,18 @@ ResourceValue::~ResourceValue() {}
ResourceArraySink::~ResourceArraySink() {} ResourceArraySink::~ResourceArraySink() {}
void ResourceArraySink::enter(int32_t /*size*/, UErrorCode & /*errorCode*/) {}
void ResourceArraySink::put( void ResourceArraySink::put(
int32_t /*index*/, const ResourceValue & /*value*/, UErrorCode & /*errorCode*/) {} int32_t /*index*/, const ResourceValue & /*value*/, UErrorCode & /*errorCode*/) {}
ResourceArraySink *ResourceArraySink::getOrCreateArraySink( ResourceArraySink *ResourceArraySink::getOrCreateArraySink(
int32_t /*index*/, int32_t /*size*/, UErrorCode & /*errorCode*/) { int32_t /*index*/, UErrorCode & /*errorCode*/) {
return NULL; return NULL;
} }
ResourceTableSink *ResourceArraySink::getOrCreateTableSink( ResourceTableSink *ResourceArraySink::getOrCreateTableSink(
int32_t /*index*/, int32_t /*initialSize*/, UErrorCode & /*errorCode*/) { int32_t /*index*/, UErrorCode & /*errorCode*/) {
return NULL; return NULL;
} }
@ -40,18 +42,20 @@ void ResourceArraySink::leave(UErrorCode & /*errorCode*/) {}
ResourceTableSink::~ResourceTableSink() {} ResourceTableSink::~ResourceTableSink() {}
void ResourceTableSink::enter(int32_t /*size*/, UErrorCode & /*errorCode*/) {}
void ResourceTableSink::put( void ResourceTableSink::put(
const char * /*key*/, const ResourceValue & /*value*/, UErrorCode & /*errorCode*/) {} const char * /*key*/, const ResourceValue & /*value*/, UErrorCode & /*errorCode*/) {}
void ResourceTableSink::putNoFallback(const char * /*key*/, UErrorCode & /*errorCode*/) {} void ResourceTableSink::putNoFallback(const char * /*key*/, UErrorCode & /*errorCode*/) {}
ResourceArraySink *ResourceTableSink::getOrCreateArraySink( ResourceArraySink *ResourceTableSink::getOrCreateArraySink(
const char * /*key*/, int32_t /*size*/, UErrorCode & /*errorCode*/) { const char * /*key*/, UErrorCode & /*errorCode*/) {
return NULL; return NULL;
} }
ResourceTableSink *ResourceTableSink::getOrCreateTableSink( ResourceTableSink *ResourceTableSink::getOrCreateTableSink(
const char * /*key*/, int32_t /*initialSize*/, UErrorCode & /*errorCode*/) { const char * /*key*/, UErrorCode & /*errorCode*/) {
return NULL; return NULL;
} }

View File

@ -1,6 +1,6 @@
/* /*
******************************************************************************* *******************************************************************************
* Copyright (C) 2015, International Business Machines * Copyright (C) 2015-2016, International Business Machines
* Corporation and others. All Rights Reserved. * Corporation and others. All Rights Reserved.
******************************************************************************* *******************************************************************************
* resource.h * resource.h
@ -120,6 +120,16 @@ public:
ResourceArraySink() {} ResourceArraySink() {}
virtual ~ResourceArraySink(); virtual ~ResourceArraySink();
/**
* "Enters" the array.
* Called just before enumerating the array's resource items.
* The size can be used to allocate storage for the items.
* It may differ between child and parent bundles.
*
* @param size number of array items
*/
virtual void enter(int32_t size, UErrorCode &errorCode);
/** /**
* Adds a value from a resource array. * Adds a value from a resource array.
* *
@ -137,11 +147,9 @@ public:
* This sink (not the caller) owns the nested sink. * This sink (not the caller) owns the nested sink.
* *
* @param index of the resource array item * @param index of the resource array item
* @param size number of array items
* @return nested-array sink, or NULL * @return nested-array sink, or NULL
*/ */
virtual ResourceArraySink *getOrCreateArraySink( virtual ResourceArraySink *getOrCreateArraySink(int32_t index, UErrorCode &errorCode);
int32_t index, int32_t size, UErrorCode &errorCode);
/** /**
* Returns a nested resource table at the array index as another sink. * Returns a nested resource table at the array index as another sink.
@ -152,11 +160,9 @@ public:
* This sink (not the caller) owns the nested sink. * This sink (not the caller) owns the nested sink.
* *
* @param index of the resource array item * @param index of the resource array item
* @param initialSize size hint for creating the sink if necessary
* @return nested-table sink, or NULL * @return nested-table sink, or NULL
*/ */
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(int32_t index, UErrorCode &errorCode);
int32_t index, int32_t initialSize, UErrorCode &errorCode);
/** /**
* "Leaves" the array. * "Leaves" the array.
@ -182,6 +188,16 @@ public:
ResourceTableSink() {} ResourceTableSink() {}
virtual ~ResourceTableSink(); virtual ~ResourceTableSink();
/**
* "Enters" the table.
* Called just before enumerating the table's resource items.
* The size can be used to allocate storage for the items.
* It usually differs between child and parent bundles.
*
* @param size number of table items
*/
virtual void enter(int32_t size, UErrorCode &errorCode);
/** /**
* Adds a key-value pair from a resource table. * Adds a key-value pair from a resource table.
* *
@ -210,11 +226,9 @@ public:
* This sink (not the caller) owns the nested sink. * This sink (not the caller) owns the nested sink.
* *
* @param key resource key string * @param key resource key string
* @param size number of array items
* @return nested-array sink, or NULL * @return nested-array sink, or NULL
*/ */
virtual ResourceArraySink *getOrCreateArraySink( virtual ResourceArraySink *getOrCreateArraySink(const char *key, UErrorCode &errorCode);
const char *key, int32_t size, UErrorCode &errorCode);
/** /**
* Returns a nested resource table for the key as another sink. * Returns a nested resource table for the key as another sink.
@ -225,11 +239,9 @@ public:
* This sink (not the caller) owns the nested sink. * This sink (not the caller) owns the nested sink.
* *
* @param key resource key string * @param key resource key string
* @param initialSize size hint for creating the sink if necessary
* @return nested-table sink, or NULL * @return nested-table sink, or NULL
*/ */
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode);
const char *key, int32_t initialSize, UErrorCode &errorCode);
/** /**
* "Leaves" the table. * "Leaves" the table.

View File

@ -463,42 +463,6 @@ res_countArrayItems(const ResourceData *pResData, Resource res) {
} }
} }
namespace {
int32_t getArrayLength(const ResourceData *pResData, Resource res) {
uint32_t offset=RES_GET_OFFSET(res);
if(offset == 0) {
return 0;
}
int32_t type = RES_GET_TYPE(res);
if(type == URES_ARRAY) {
return *(pResData->pRoot+offset);
} else if(type == URES_ARRAY16) {
return pResData->p16BitUnits[offset];
} else {
return 0;
}
}
int32_t getTableLength(const ResourceData *pResData, Resource res) {
uint32_t offset=RES_GET_OFFSET(res);
if(offset == 0) {
return 0;
}
int32_t type = RES_GET_TYPE(res);
if(type == URES_TABLE) {
return *((const uint16_t *)(pResData->pRoot+offset));
} else if(type == URES_TABLE16) {
return pResData->p16BitUnits[offset];
} else if(type == URES_TABLE32) {
return *(pResData->pRoot+offset);
} else {
return 0;
}
}
} // namespace
U_NAMESPACE_BEGIN U_NAMESPACE_BEGIN
ResourceDataValue::~ResourceDataValue() {} ResourceDataValue::~ResourceDataValue() {}
@ -735,6 +699,8 @@ ures_getAllTableItems(const ResourceData *pResData, Resource table,
errorCode = U_RESOURCE_TYPE_MISMATCH; errorCode = U_RESOURCE_TYPE_MISMATCH;
return; return;
} }
sink.enter(length, errorCode);
if(U_FAILURE(errorCode)) { return; }
for (int32_t i = 0; i < length; ++i) { for (int32_t i = 0; i < length; ++i) {
const char *key; const char *key;
@ -751,14 +717,12 @@ ures_getAllTableItems(const ResourceData *pResData, Resource table,
} }
int32_t type = RES_GET_TYPE(res); int32_t type = RES_GET_TYPE(res);
if (URES_IS_ARRAY(type)) { if (URES_IS_ARRAY(type)) {
int32_t numItems = getArrayLength(pResData, res); icu::ResourceArraySink *subSink = sink.getOrCreateArraySink(key, errorCode);
icu::ResourceArraySink *subSink = sink.getOrCreateArraySink(key, numItems, errorCode);
if (subSink != NULL) { if (subSink != NULL) {
ures_getAllArrayItems(pResData, res, value, *subSink, errorCode); ures_getAllArrayItems(pResData, res, value, *subSink, errorCode);
} }
} else if (URES_IS_TABLE(type)) { } else if (URES_IS_TABLE(type)) {
int32_t numItems = getTableLength(pResData, res); icu::ResourceTableSink *subSink = sink.getOrCreateTableSink(key, errorCode);
icu::ResourceTableSink *subSink = sink.getOrCreateTableSink(key, numItems, errorCode);
if (subSink != NULL) { if (subSink != NULL) {
ures_getAllTableItems(pResData, res, value, *subSink, errorCode); ures_getAllTableItems(pResData, res, value, *subSink, errorCode);
} }
@ -831,6 +795,8 @@ ures_getAllArrayItems(const ResourceData *pResData, Resource array,
errorCode = U_RESOURCE_TYPE_MISMATCH; errorCode = U_RESOURCE_TYPE_MISMATCH;
return; return;
} }
sink.enter(length, errorCode);
if(U_FAILURE(errorCode)) { return; }
for (int32_t i = 0; i < length; ++i) { for (int32_t i = 0; i < length; ++i) {
Resource res; Resource res;
@ -841,14 +807,12 @@ ures_getAllArrayItems(const ResourceData *pResData, Resource array,
} }
int32_t type = RES_GET_TYPE(res); int32_t type = RES_GET_TYPE(res);
if (URES_IS_ARRAY(type)) { if (URES_IS_ARRAY(type)) {
int32_t numItems = getArrayLength(pResData, res); icu::ResourceArraySink *subSink = sink.getOrCreateArraySink(i, errorCode);
icu::ResourceArraySink *subSink = sink.getOrCreateArraySink(i, numItems, errorCode);
if (subSink != NULL) { if (subSink != NULL) {
ures_getAllArrayItems(pResData, res, value, *subSink, errorCode); ures_getAllArrayItems(pResData, res, value, *subSink, errorCode);
} }
} else if (URES_IS_TABLE(type)) { } else if (URES_IS_TABLE(type)) {
int32_t numItems = getTableLength(pResData, res); icu::ResourceTableSink *subSink = sink.getOrCreateTableSink(i, errorCode);
icu::ResourceTableSink *subSink = sink.getOrCreateTableSink(i, numItems, errorCode);
if (subSink != NULL) { if (subSink != NULL) {
ures_getAllTableItems(pResData, res, value, *subSink, errorCode); ures_getAllTableItems(pResData, res, value, *subSink, errorCode);
} }

View File

@ -51,7 +51,7 @@ struct DayPeriodRulesDataSink : public ResourceTableSink {
virtual ~DayPeriodRulesDataSink(); virtual ~DayPeriodRulesDataSink();
// Entry point. // Entry point.
virtual ResourceTableSink *getOrCreateTableSink(const char *key, int32_t, UErrorCode &errorCode) { virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
if (uprv_strcmp(key, "locales") == 0) { if (uprv_strcmp(key, "locales") == 0) {
@ -88,7 +88,7 @@ struct DayPeriodRulesDataSink : public ResourceTableSink {
RulesSink(DayPeriodRulesDataSink &outer) : outer(outer) {} RulesSink(DayPeriodRulesDataSink &outer) : outer(outer) {}
virtual ~RulesSink(); virtual ~RulesSink();
virtual ResourceTableSink *getOrCreateTableSink(const char *key, int32_t, UErrorCode &errorCode) { virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
outer.ruleSetNum = parseSetNum(key, errorCode); outer.ruleSetNum = parseSetNum(key, errorCode);
@ -102,7 +102,7 @@ struct DayPeriodRulesDataSink : public ResourceTableSink {
RuleSetSink(DayPeriodRulesDataSink &outer) : outer(outer) {} RuleSetSink(DayPeriodRulesDataSink &outer) : outer(outer) {}
virtual ~RuleSetSink(); virtual ~RuleSetSink();
virtual ResourceTableSink *getOrCreateTableSink(const char *key, int32_t, UErrorCode &errorCode) { virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
outer.period = DayPeriodRules::getDayPeriodFromString(key); outer.period = DayPeriodRules::getDayPeriodFromString(key);
@ -138,7 +138,7 @@ struct DayPeriodRulesDataSink : public ResourceTableSink {
outer.addCutoff(type, value.getUnicodeString(errorCode), errorCode); outer.addCutoff(type, value.getUnicodeString(errorCode), errorCode);
} }
virtual ResourceArraySink *getOrCreateArraySink(const char *key, int32_t, UErrorCode &errorCode) { virtual ResourceArraySink *getOrCreateArraySink(const char *key, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
outer.cutoffType = getCutoffTypeFromString(key); outer.cutoffType = getCutoffTypeFromString(key);
return &outer.cutoffSink; return &outer.cutoffSink;
@ -318,7 +318,7 @@ struct DayPeriodRulesDataSink : public ResourceTableSink {
struct DayPeriodRulesCountSink : public ResourceTableSink { struct DayPeriodRulesCountSink : public ResourceTableSink {
virtual ~DayPeriodRulesCountSink(); virtual ~DayPeriodRulesCountSink();
virtual ResourceTableSink *getOrCreateTableSink(const char *key, int32_t, UErrorCode &errorCode) { virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
int32_t setNum = DayPeriodRulesDataSink::parseSetNum(key, errorCode); int32_t setNum = DayPeriodRulesDataSink::parseSetNum(key, errorCode);

View File

@ -234,8 +234,7 @@ struct DateIntervalSink : public ResourceTableSink {
SkeletonSink(DateIntervalSink &sink) : outer(sink) {} SkeletonSink(DateIntervalSink &sink) : outer(sink) {}
virtual ~SkeletonSink(); virtual ~SkeletonSink();
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
const char *key, int32_t, UErrorCode &errorCode) {
if (U_SUCCESS(errorCode)) { if (U_SUCCESS(errorCode)) {
outer.currentSkeleton = key; outer.currentSkeleton = key;
return &outer.patternSink; return &outer.patternSink;
@ -340,8 +339,7 @@ struct DateIntervalSink : public ResourceTableSink {
} }
} }
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
const char *key, int32_t, UErrorCode &errorCode) {
// Check if it's the intervalFormat table // Check if it's the intervalFormat table
if (U_SUCCESS(errorCode) && uprv_strcmp(key, gIntervalDateTimePatternTag) == 0) { if (U_SUCCESS(errorCode) && uprv_strcmp(key, gIntervalDateTimePatternTag) == 0) {
return &skeletonSink; return &skeletonSink;

View File

@ -440,7 +440,7 @@ struct AllowedHourFormatsSink : public ResourceTableSink {
virtual ~AllowedHourFormatsSink(); virtual ~AllowedHourFormatsSink();
// Entry point. // Entry point.
virtual ResourceTableSink *getOrCreateTableSink(const char *key, int32_t, UErrorCode &status) { virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &status) {
if (U_FAILURE(status)) { return NULL; } if (U_FAILURE(status)) { return NULL; }
locale = key; locale = key;
@ -467,19 +467,10 @@ struct AllowedHourFormatsSink : public ResourceTableSink {
} }
} }
virtual ResourceArraySink *getOrCreateArraySink(const char *key, int32_t size, UErrorCode &status) { virtual ResourceArraySink *getOrCreateArraySink(const char *key, UErrorCode &status) {
if (U_FAILURE(status)) { return NULL; } if (U_SUCCESS(status) && uprv_strcmp(key, "allowed") == 0) {
if (uprv_strcmp(key, "allowed") == 0) {
outer.allowedFormats = static_cast<int32_t *>(uprv_malloc((size + 1) * sizeof(int32_t)));
outer.allowedFormatsLength = size;
if (outer.allowedFormats == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return NULL;
} else {
return &outer.allowedListSink; return &outer.allowedListSink;
} }
}
return NULL; return NULL;
} }
@ -497,6 +488,14 @@ struct AllowedHourFormatsSink : public ResourceTableSink {
AllowedListSink(AllowedHourFormatsSink &outer) : outer(outer) {} AllowedListSink(AllowedHourFormatsSink &outer) : outer(outer) {}
virtual ~AllowedListSink(); virtual ~AllowedListSink();
virtual void enter(int32_t size, UErrorCode &status) {
if (U_FAILURE(status)) { return; }
outer.allowedFormats = static_cast<int32_t *>(uprv_malloc((size + 1) * sizeof(int32_t)));
outer.allowedFormatsLength = size;
if (outer.allowedFormats == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
}
}
virtual void put(int32_t index, const ResourceValue &value, UErrorCode &status) { virtual void put(int32_t index, const ResourceValue &value, UErrorCode &status) {
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }

View File

@ -260,8 +260,7 @@ struct UnitDataSink : public ResourceTableSink {
struct UnitSubtypeSink : public ResourceTableSink { struct UnitSubtypeSink : public ResourceTableSink {
UnitSubtypeSink(UnitDataSink &sink) : outer(sink) {} UnitSubtypeSink(UnitDataSink &sink) : outer(sink) {}
~UnitSubtypeSink(); ~UnitSubtypeSink();
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
const char *key, int32_t /* initialSize */, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
outer.unitIndex = MeasureUnit::internalGetIndexForTypeAndSubtype(outer.type, key); outer.unitIndex = MeasureUnit::internalGetIndexForTypeAndSubtype(outer.type, key);
if (outer.unitIndex >= 0) { if (outer.unitIndex >= 0) {
@ -296,8 +295,7 @@ struct UnitDataSink : public ResourceTableSink {
struct UnitTypeSink : public ResourceTableSink { struct UnitTypeSink : public ResourceTableSink {
UnitTypeSink(UnitDataSink &sink) : outer(sink) {} UnitTypeSink(UnitDataSink &sink) : outer(sink) {}
~UnitTypeSink(); ~UnitTypeSink();
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
const char *key, int32_t /* initialSize */, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
if (uprv_strcmp(key, "currency") == 0) { if (uprv_strcmp(key, "currency") == 0) {
// Skip. // Skip.
@ -342,8 +340,7 @@ struct UnitDataSink : public ResourceTableSink {
} }
cacheData.widthFallback[sourceWidth] = targetWidth; cacheData.widthFallback[sourceWidth] = targetWidth;
} }
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
const char *key, int32_t /* initialSize */, UErrorCode &errorCode) {
if (U_SUCCESS(errorCode) && (width = widthFromKey(key)) != UMEASFMT_WIDTH_COUNT) { if (U_SUCCESS(errorCode) && (width = widthFromKey(key)) != UMEASFMT_WIDTH_COUNT) {
return &typeSink; return &typeSink;
} }

View File

@ -344,8 +344,7 @@ struct RelDateTimeFmtDataSink : public ResourceTableSink {
RelativeTimeSink(RelDateTimeFmtDataSink &sink) : outer(sink) {} RelativeTimeSink(RelDateTimeFmtDataSink &sink) : outer(sink) {}
~RelativeTimeSink(); ~RelativeTimeSink();
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode& errorCode) {
const char *key, int32_t /* initialSize */, UErrorCode& errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
outer.relUnitIndex = relUnitFromGeneric(outer.genericUnit); outer.relUnitIndex = relUnitFromGeneric(outer.genericUnit);
if (outer.relUnitIndex < 0) { if (outer.relUnitIndex < 0) {
@ -442,8 +441,7 @@ struct RelDateTimeFmtDataSink : public ResourceTableSink {
} }
} }
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode &errorCode) {
const char *key, int32_t /* initialSize */, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return NULL; } if (U_FAILURE(errorCode)) { return NULL; }
if (uprv_strcmp(key, "relative") == 0) { if (uprv_strcmp(key, "relative") == 0) {
return &outer.relativeSink; return &outer.relativeSink;
@ -601,8 +599,7 @@ struct RelDateTimeFmtDataSink : public ResourceTableSink {
} }
// Top level sink // Top level sink
virtual ResourceTableSink *getOrCreateTableSink( virtual ResourceTableSink *getOrCreateTableSink(const char *key, UErrorCode& /* errorCode */) {
const char *key, int32_t /* initialSize */, UErrorCode& /* errorCode */) {
style= styleFromString(key); style= styleFromString(key);
int32_t unitSize = uprv_strlen(key) - styleSuffixLength(style); int32_t unitSize = uprv_strlen(key) - styleSuffixLength(style);
genericUnit = unitOrNegativeFromString(key, unitSize); genericUnit = unitOrNegativeFromString(key, unitSize);