X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/73c04bcfe1096173b00431f0cdc742894b15eef0..b801cf366c7671a99bdcef84d1e9c0ec64b36723:/icuSources/common/ustrenum.cpp?ds=inline diff --git a/icuSources/common/ustrenum.cpp b/icuSources/common/ustrenum.cpp index 2a7acf68..762bf99d 100644 --- a/icuSources/common/ustrenum.cpp +++ b/icuSources/common/ustrenum.cpp @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (c) 2002-2005, International Business Machines +* Copyright (c) 2002-2014, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -8,6 +8,8 @@ * Since: ICU 2.4 ********************************************************************** */ +#include "utypeinfo.h" // for 'typeid' to work + #include "unicode/ustring.h" #include "unicode/strenum.h" #include "unicode/putil.h" @@ -39,7 +41,7 @@ StringEnumeration::clone() const { const char * StringEnumeration::next(int32_t *resultLength, UErrorCode &status) { const UnicodeString *s=snext(status); - if(s!=NULL) { + if(U_SUCCESS(status) && s!=NULL) { unistr=*s; ensureCharsCapacity(unistr.length()+1, status); if(U_SUCCESS(status)) { @@ -57,19 +59,24 @@ StringEnumeration::next(int32_t *resultLength, UErrorCode &status) { const UChar * StringEnumeration::unext(int32_t *resultLength, UErrorCode &status) { const UnicodeString *s=snext(status); - if(s!=NULL) { + if(U_SUCCESS(status) && s!=NULL) { unistr=*s; - if(U_SUCCESS(status)) { - if(resultLength!=NULL) { - *resultLength=unistr.length(); - } - return unistr.getTerminatedBuffer(); + if(resultLength!=NULL) { + *resultLength=unistr.length(); } + return unistr.getTerminatedBuffer(); } return NULL; } +const UnicodeString * +StringEnumeration::snext(UErrorCode &status) { + int32_t length; + const char *s=next(&length, status); + return setChars(s, length, status); +} + void StringEnumeration::ensureCharsCapacity(int32_t capacity, UErrorCode &status) { if(U_SUCCESS(status) && capacity>charsCapacity) { @@ -113,7 +120,7 @@ StringEnumeration::setChars(const char *s, int32_t length, UErrorCode &status) { } UBool StringEnumeration::operator==(const StringEnumeration& that)const { - return getDynamicClassID() == that.getDynamicClassID(); + return typeid(*this) == typeid(that); } UBool @@ -123,6 +130,22 @@ StringEnumeration::operator!=(const StringEnumeration& that)const { // UStringEnumeration implementation --------------------------------------- *** +UStringEnumeration * U_EXPORT2 +UStringEnumeration::fromUEnumeration( + UEnumeration *uenumToAdopt, UErrorCode &status) { + if (U_FAILURE(status)) { + uenum_close(uenumToAdopt); + return NULL; + } + UStringEnumeration *result = new UStringEnumeration(uenumToAdopt); + if (result == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + uenum_close(uenumToAdopt); + return NULL; + } + return result; +} + UStringEnumeration::UStringEnumeration(UEnumeration* _uenum) : uenum(_uenum) { U_ASSERT(_uenum != 0); @@ -136,6 +159,10 @@ int32_t UStringEnumeration::count(UErrorCode& status) const { return uenum_count(uenum, &status); } +const char *UStringEnumeration::next(int32_t *resultLength, UErrorCode &status) { + return uenum_next(uenum, resultLength, &status); +} + const UnicodeString* UStringEnumeration::snext(UErrorCode& status) { int32_t length; const UChar* str = uenum_unext(uenum, &length, &status); @@ -149,12 +176,12 @@ void UStringEnumeration::reset(UErrorCode& status) { uenum_reset(uenum, &status); } -UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration/*, StringEnumeration*/) +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration) U_NAMESPACE_END // C wrapper --------------------------------------------------------------- *** -#define THIS(en) ((StringEnumeration*)(en->context)) +#define THIS(en) ((icu::StringEnumeration*)(en->context)) U_CDECL_BEGIN @@ -231,7 +258,7 @@ U_CDECL_END * delete it (regardless of error status). */ U_CAPI UEnumeration* U_EXPORT2 -uenum_openStringEnumeration(StringEnumeration* adopted, UErrorCode* ec) { +uenum_openFromStringEnumeration(icu::StringEnumeration* adopted, UErrorCode* ec) { UEnumeration* result = NULL; if (U_SUCCESS(*ec) && adopted != NULL) { result = (UEnumeration*) uprv_malloc(sizeof(UEnumeration)); @@ -268,6 +295,22 @@ ucharstrenum_count(UEnumeration* en, return ((UCharStringEnumeration*)en)->count; } +static const UChar* U_CALLCONV +ucharstrenum_unext(UEnumeration* en, + int32_t* resultLength, + UErrorCode* /*ec*/) { + UCharStringEnumeration *e = (UCharStringEnumeration*) en; + if (e->index >= e->count) { + return NULL; + } + const UChar* result = ((const UChar**)e->uenum.context)[e->index++]; + if (resultLength) { + *resultLength = (int32_t)u_strlen(result); + } + return result; +} + + static const char* U_CALLCONV ucharstrenum_next(UEnumeration* en, int32_t* resultLength, @@ -299,10 +342,20 @@ static const UEnumeration UCHARSTRENUM_VT = { ucharstrenum_reset }; +static const UEnumeration UCHARSTRENUM_U_VT = { + NULL, + NULL, // store StringEnumeration pointer here + ucharstrenum_close, + ucharstrenum_count, + ucharstrenum_unext, + uenum_nextDefault, + ucharstrenum_reset +}; + U_CDECL_END U_CAPI UEnumeration* U_EXPORT2 -uenum_openCharStringsEnumeration(const char** strings, int32_t count, +uenum_openCharStringsEnumeration(const char* const strings[], int32_t count, UErrorCode* ec) { UCharStringEnumeration* result = NULL; if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != 0)) { @@ -312,7 +365,26 @@ uenum_openCharStringsEnumeration(const char** strings, int32_t count, } else { U_ASSERT((char*)result==(char*)(&result->uenum)); uprv_memcpy(result, &UCHARSTRENUM_VT, sizeof(UCHARSTRENUM_VT)); - result->uenum.context = strings; + result->uenum.context = (void*)strings; + result->index = 0; + result->count = count; + } + } + return (UEnumeration*) result; +} + +U_CAPI UEnumeration* U_EXPORT2 +uenum_openUCharStringsEnumeration(const UChar* const strings[], int32_t count, + UErrorCode* ec) { + UCharStringEnumeration* result = NULL; + if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != 0)) { + result = (UCharStringEnumeration*) uprv_malloc(sizeof(UCharStringEnumeration)); + if (result == NULL) { + *ec = U_MEMORY_ALLOCATION_ERROR; + } else { + U_ASSERT((char*)result==(char*)(&result->uenum)); + uprv_memcpy(result, &UCHARSTRENUM_U_VT, sizeof(UCHARSTRENUM_U_VT)); + result->uenum.context = (void*)strings; result->index = 0; result->count = count; } @@ -321,3 +393,4 @@ uenum_openCharStringsEnumeration(const char** strings, int32_t count, } +// end C Wrapper