2 **********************************************************************
3 * Copyright (c) 2002-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: November 11 2002
9 **********************************************************************
11 #include "utypeinfo.h" // for 'typeid' to work
13 #include "unicode/ustring.h"
14 #include "unicode/strenum.h"
15 #include "unicode/putil.h"
23 // StringEnumeration implementation ---------------------------------------- ***
25 StringEnumeration::StringEnumeration()
26 : chars(charsBuffer
), charsCapacity(sizeof(charsBuffer
)) {
29 StringEnumeration::~StringEnumeration() {
30 if (chars
!= NULL
&& chars
!= charsBuffer
) {
35 // StringEnumeration base class clone() default implementation, does not clone
37 StringEnumeration::clone() const {
42 StringEnumeration::next(int32_t *resultLength
, UErrorCode
&status
) {
43 const UnicodeString
*s
=snext(status
);
44 if(U_SUCCESS(status
) && s
!=NULL
) {
46 ensureCharsCapacity(unistr
.length()+1, status
);
47 if(U_SUCCESS(status
)) {
48 if(resultLength
!=NULL
) {
49 *resultLength
=unistr
.length();
51 unistr
.extract(0, INT32_MAX
, chars
, charsCapacity
, US_INV
);
60 StringEnumeration::unext(int32_t *resultLength
, UErrorCode
&status
) {
61 const UnicodeString
*s
=snext(status
);
62 if(U_SUCCESS(status
) && s
!=NULL
) {
64 if(resultLength
!=NULL
) {
65 *resultLength
=unistr
.length();
67 return unistr
.getTerminatedBuffer();
74 StringEnumeration::snext(UErrorCode
&status
) {
76 const char *s
=next(&length
, status
);
77 return setChars(s
, length
, status
);
81 StringEnumeration::ensureCharsCapacity(int32_t capacity
, UErrorCode
&status
) {
82 if(U_SUCCESS(status
) && capacity
>charsCapacity
) {
83 if(capacity
<(charsCapacity
+charsCapacity
/2)) {
84 // avoid allocation thrashing
85 capacity
=charsCapacity
+charsCapacity
/2;
87 if(chars
!=charsBuffer
) {
90 chars
=(char *)uprv_malloc(capacity
);
93 charsCapacity
=sizeof(charsBuffer
);
94 status
=U_MEMORY_ALLOCATION_ERROR
;
96 charsCapacity
=capacity
;
102 StringEnumeration::setChars(const char *s
, int32_t length
, UErrorCode
&status
) {
103 if(U_SUCCESS(status
) && s
!=NULL
) {
105 length
=(int32_t)uprv_strlen(s
);
108 UChar
*buffer
=unistr
.getBuffer(length
+1);
110 u_charsToUChars(s
, buffer
, length
);
112 unistr
.releaseBuffer(length
);
115 status
=U_MEMORY_ALLOCATION_ERROR
;
122 StringEnumeration::operator==(const StringEnumeration
& that
)const {
123 return typeid(*this) == typeid(that
);
127 StringEnumeration::operator!=(const StringEnumeration
& that
)const {
128 return !operator==(that
);
131 // UStringEnumeration implementation --------------------------------------- ***
133 UStringEnumeration::UStringEnumeration(UEnumeration
* _uenum
) :
135 U_ASSERT(_uenum
!= 0);
138 UStringEnumeration::~UStringEnumeration() {
142 int32_t UStringEnumeration::count(UErrorCode
& status
) const {
143 return uenum_count(uenum
, &status
);
146 const char *UStringEnumeration::next(int32_t *resultLength
, UErrorCode
&status
) {
147 return uenum_next(uenum
, resultLength
, &status
);
150 const UnicodeString
* UStringEnumeration::snext(UErrorCode
& status
) {
152 const UChar
* str
= uenum_unext(uenum
, &length
, &status
);
153 if (str
== 0 || U_FAILURE(status
)) {
156 return &unistr
.setTo(str
, length
);
159 void UStringEnumeration::reset(UErrorCode
& status
) {
160 uenum_reset(uenum
, &status
);
163 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration
)
166 // C wrapper --------------------------------------------------------------- ***
168 #define THIS(en) ((icu::StringEnumeration*)(en->context))
173 * Wrapper API to make StringEnumeration look like UEnumeration.
175 static void U_CALLCONV
176 ustrenum_close(UEnumeration
* en
) {
182 * Wrapper API to make StringEnumeration look like UEnumeration.
184 static int32_t U_CALLCONV
185 ustrenum_count(UEnumeration
* en
,
188 return THIS(en
)->count(*ec
);
192 * Wrapper API to make StringEnumeration look like UEnumeration.
194 static const UChar
* U_CALLCONV
195 ustrenum_unext(UEnumeration
* en
,
196 int32_t* resultLength
,
199 return THIS(en
)->unext(resultLength
, *ec
);
203 * Wrapper API to make StringEnumeration look like UEnumeration.
205 static const char* U_CALLCONV
206 ustrenum_next(UEnumeration
* en
,
207 int32_t* resultLength
,
210 return THIS(en
)->next(resultLength
, *ec
);
214 * Wrapper API to make StringEnumeration look like UEnumeration.
216 static void U_CALLCONV
217 ustrenum_reset(UEnumeration
* en
,
220 THIS(en
)->reset(*ec
);
224 * Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
225 * The StringEnumeration pointer will be stored in 'context'.
227 static const UEnumeration USTRENUM_VT
= {
229 NULL
, // store StringEnumeration pointer here
240 * Given a StringEnumeration, wrap it in a UEnumeration. The
241 * StringEnumeration is adopted; after this call, the caller must not
242 * delete it (regardless of error status).
244 U_CAPI UEnumeration
* U_EXPORT2
245 uenum_openFromStringEnumeration(icu::StringEnumeration
* adopted
, UErrorCode
* ec
) {
246 UEnumeration
* result
= NULL
;
247 if (U_SUCCESS(*ec
) && adopted
!= NULL
) {
248 result
= (UEnumeration
*) uprv_malloc(sizeof(UEnumeration
));
249 if (result
== NULL
) {
250 *ec
= U_MEMORY_ALLOCATION_ERROR
;
252 uprv_memcpy(result
, &USTRENUM_VT
, sizeof(USTRENUM_VT
));
253 result
->context
= adopted
;
256 if (result
== NULL
) {
262 // C wrapper --------------------------------------------------------------- ***
266 typedef struct UCharStringEnumeration
{
268 int32_t index
, count
;
269 } UCharStringEnumeration
;
271 static void U_CALLCONV
272 ucharstrenum_close(UEnumeration
* en
) {
276 static int32_t U_CALLCONV
277 ucharstrenum_count(UEnumeration
* en
,
278 UErrorCode
* /*ec*/) {
279 return ((UCharStringEnumeration
*)en
)->count
;
282 static const UChar
* U_CALLCONV
283 ucharstrenum_unext(UEnumeration
* en
,
284 int32_t* resultLength
,
285 UErrorCode
* /*ec*/) {
286 UCharStringEnumeration
*e
= (UCharStringEnumeration
*) en
;
287 if (e
->index
>= e
->count
) {
290 const UChar
* result
= ((const UChar
**)e
->uenum
.context
)[e
->index
++];
292 *resultLength
= (int32_t)u_strlen(result
);
298 static const char* U_CALLCONV
299 ucharstrenum_next(UEnumeration
* en
,
300 int32_t* resultLength
,
301 UErrorCode
* /*ec*/) {
302 UCharStringEnumeration
*e
= (UCharStringEnumeration
*) en
;
303 if (e
->index
>= e
->count
) {
306 const char* result
= ((const char**)e
->uenum
.context
)[e
->index
++];
308 *resultLength
= (int32_t)uprv_strlen(result
);
313 static void U_CALLCONV
314 ucharstrenum_reset(UEnumeration
* en
,
315 UErrorCode
* /*ec*/) {
316 ((UCharStringEnumeration
*)en
)->index
= 0;
319 static const UEnumeration UCHARSTRENUM_VT
= {
321 NULL
, // store StringEnumeration pointer here
329 static const UEnumeration UCHARSTRENUM_U_VT
= {
331 NULL
, // store StringEnumeration pointer here
341 U_CAPI UEnumeration
* U_EXPORT2
342 uenum_openCharStringsEnumeration(const char* const strings
[], int32_t count
,
344 UCharStringEnumeration
* result
= NULL
;
345 if (U_SUCCESS(*ec
) && count
>= 0 && (count
== 0 || strings
!= 0)) {
346 result
= (UCharStringEnumeration
*) uprv_malloc(sizeof(UCharStringEnumeration
));
347 if (result
== NULL
) {
348 *ec
= U_MEMORY_ALLOCATION_ERROR
;
350 U_ASSERT((char*)result
==(char*)(&result
->uenum
));
351 uprv_memcpy(result
, &UCHARSTRENUM_VT
, sizeof(UCHARSTRENUM_VT
));
352 result
->uenum
.context
= (void*)strings
;
354 result
->count
= count
;
357 return (UEnumeration
*) result
;
360 U_CAPI UEnumeration
* U_EXPORT2
361 uenum_openUCharStringsEnumeration(const UChar
* const strings
[], int32_t count
,
363 UCharStringEnumeration
* result
= NULL
;
364 if (U_SUCCESS(*ec
) && count
>= 0 && (count
== 0 || strings
!= 0)) {
365 result
= (UCharStringEnumeration
*) uprv_malloc(sizeof(UCharStringEnumeration
));
366 if (result
== NULL
) {
367 *ec
= U_MEMORY_ALLOCATION_ERROR
;
369 U_ASSERT((char*)result
==(char*)(&result
->uenum
));
370 uprv_memcpy(result
, &UCHARSTRENUM_U_VT
, sizeof(UCHARSTRENUM_U_VT
));
371 result
->uenum
.context
= (void*)strings
;
373 result
->count
= count
;
376 return (UEnumeration
*) result
;