/*
**********************************************************************
-* Copyright (c) 2002-2005, International Business Machines
+* Copyright (c) 2002-2014, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
* Since: ICU 2.4
**********************************************************************
*/
+#include "utypeinfo.h" // for 'typeid' to work
+
#include "unicode/ustring.h"
#include "unicode/strenum.h"
#include "unicode/putil.h"
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)) {
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) {
}
UBool
StringEnumeration::operator==(const StringEnumeration& that)const {
- return getDynamicClassID() == that.getDynamicClassID();
+ return typeid(*this) == typeid(that);
}
UBool
// 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);
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);
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
* 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));
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,
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)) {
} 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;
}
}
+// end C Wrapper