2 **********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: November 11 2002
9 **********************************************************************
11 #include "unicode/ustring.h"
12 #include "unicode/strenum.h"
13 #include "unicode/putil.h"
21 // StringEnumeration implementation ---------------------------------------- ***
23 StringEnumeration::StringEnumeration()
24 : chars(charsBuffer
), charsCapacity(sizeof(charsBuffer
)) {
27 StringEnumeration::~StringEnumeration() {
28 if (chars
!= NULL
&& chars
!= charsBuffer
) {
33 // StringEnumeration base class clone() default implementation, does not clone
35 StringEnumeration::clone() const {
40 StringEnumeration::next(int32_t *resultLength
, UErrorCode
&status
) {
41 const UnicodeString
*s
=snext(status
);
44 ensureCharsCapacity(unistr
.length()+1, status
);
45 if(U_SUCCESS(status
)) {
46 if(resultLength
!=NULL
) {
47 *resultLength
=unistr
.length();
49 unistr
.extract(0, INT32_MAX
, chars
, charsCapacity
, US_INV
);
58 StringEnumeration::unext(int32_t *resultLength
, UErrorCode
&status
) {
59 const UnicodeString
*s
=snext(status
);
62 if(U_SUCCESS(status
)) {
63 if(resultLength
!=NULL
) {
64 *resultLength
=unistr
.length();
66 return unistr
.getTerminatedBuffer();
74 StringEnumeration::ensureCharsCapacity(int32_t capacity
, UErrorCode
&status
) {
75 if(U_SUCCESS(status
) && capacity
>charsCapacity
) {
76 if(capacity
<(charsCapacity
+charsCapacity
/2)) {
77 // avoid allocation thrashing
78 capacity
=charsCapacity
+charsCapacity
/2;
80 if(chars
!=charsBuffer
) {
83 chars
=(char *)uprv_malloc(capacity
);
86 charsCapacity
=sizeof(charsBuffer
);
87 status
=U_MEMORY_ALLOCATION_ERROR
;
89 charsCapacity
=capacity
;
95 StringEnumeration::setChars(const char *s
, int32_t length
, UErrorCode
&status
) {
96 if(U_SUCCESS(status
) && s
!=NULL
) {
98 length
=(int32_t)uprv_strlen(s
);
101 UChar
*buffer
=unistr
.getBuffer(length
+1);
103 u_charsToUChars(s
, buffer
, length
);
105 unistr
.releaseBuffer(length
);
108 status
=U_MEMORY_ALLOCATION_ERROR
;
115 // UStringEnumeration implementation --------------------------------------- ***
117 UStringEnumeration::UStringEnumeration(UEnumeration
* _uenum
) :
119 U_ASSERT(_uenum
!= 0);
122 UStringEnumeration::~UStringEnumeration() {
126 int32_t UStringEnumeration::count(UErrorCode
& status
) const {
127 return uenum_count(uenum
, &status
);
130 const UnicodeString
* UStringEnumeration::snext(UErrorCode
& status
) {
132 const UChar
* str
= uenum_unext(uenum
, &length
, &status
);
133 if (str
== 0 || U_FAILURE(status
)) {
136 return &unistr
.setTo(str
, length
);
139 void UStringEnumeration::reset(UErrorCode
& status
) {
140 uenum_reset(uenum
, &status
);
143 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration
/*, StringEnumeration*/)
146 // C wrapper --------------------------------------------------------------- ***
148 #define THIS(en) ((StringEnumeration*)(en->context))
153 * Wrapper API to make StringEnumeration look like UEnumeration.
155 static void U_CALLCONV
156 ustrenum_close(UEnumeration
* en
) {
162 * Wrapper API to make StringEnumeration look like UEnumeration.
164 static int32_t U_CALLCONV
165 ustrenum_count(UEnumeration
* en
,
168 return THIS(en
)->count(*ec
);
172 * Wrapper API to make StringEnumeration look like UEnumeration.
174 static const UChar
* U_CALLCONV
175 ustrenum_unext(UEnumeration
* en
,
176 int32_t* resultLength
,
179 return THIS(en
)->unext(resultLength
, *ec
);
183 * Wrapper API to make StringEnumeration look like UEnumeration.
185 static const char* U_CALLCONV
186 ustrenum_next(UEnumeration
* en
,
187 int32_t* resultLength
,
190 return THIS(en
)->next(resultLength
, *ec
);
194 * Wrapper API to make StringEnumeration look like UEnumeration.
196 static void U_CALLCONV
197 ustrenum_reset(UEnumeration
* en
,
200 THIS(en
)->reset(*ec
);
204 * Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
205 * The StringEnumeration pointer will be stored in 'context'.
207 static const UEnumeration USTRENUM_VT
= {
209 NULL
, // store StringEnumeration pointer here
220 * Given a StringEnumeration, wrap it in a UEnumeration. The
221 * StringEnumeration is adopted; after this call, the caller must not
222 * delete it (regardless of error status).
224 U_CAPI UEnumeration
* U_EXPORT2
225 uenum_openStringEnumeration(StringEnumeration
* adopted
, UErrorCode
* ec
) {
226 UEnumeration
* result
= NULL
;
227 if (U_SUCCESS(*ec
) && adopted
!= NULL
) {
228 result
= (UEnumeration
*) uprv_malloc(sizeof(UEnumeration
));
229 if (result
== NULL
) {
230 *ec
= U_MEMORY_ALLOCATION_ERROR
;
232 uprv_memcpy(result
, &USTRENUM_VT
, sizeof(USTRENUM_VT
));
233 result
->context
= adopted
;
236 if (result
== NULL
) {
242 // C wrapper --------------------------------------------------------------- ***
246 typedef struct UCharStringEnumeration
{
248 int32_t index
, count
;
249 } UCharStringEnumeration
;
251 static void U_CALLCONV
252 ucharstrenum_close(UEnumeration
* en
) {
256 static int32_t U_CALLCONV
257 ucharstrenum_count(UEnumeration
* en
,
258 UErrorCode
* /*ec*/) {
259 return ((UCharStringEnumeration
*)en
)->count
;
262 static const char* U_CALLCONV
263 ucharstrenum_next(UEnumeration
* en
,
264 int32_t* resultLength
,
265 UErrorCode
* /*ec*/) {
266 UCharStringEnumeration
*e
= (UCharStringEnumeration
*) en
;
267 if (e
->index
>= e
->count
) {
270 const char* result
= ((const char**)e
->uenum
.context
)[e
->index
++];
272 *resultLength
= (int32_t)uprv_strlen(result
);
277 static void U_CALLCONV
278 ucharstrenum_reset(UEnumeration
* en
,
279 UErrorCode
* /*ec*/) {
280 ((UCharStringEnumeration
*)en
)->index
= 0;
283 static const UEnumeration UCHARSTRENUM_VT
= {
285 NULL
, // store StringEnumeration pointer here
295 U_CAPI UEnumeration
* U_EXPORT2
296 uenum_openCharStringsEnumeration(const char** strings
, int32_t count
,
298 UCharStringEnumeration
* result
= NULL
;
299 if (U_SUCCESS(*ec
) && count
>= 0 && (count
== 0 || strings
!= 0)) {
300 result
= (UCharStringEnumeration
*) uprv_malloc(sizeof(UCharStringEnumeration
));
301 if (result
== NULL
) {
302 *ec
= U_MEMORY_ALLOCATION_ERROR
;
304 U_ASSERT((char*)result
==(char*)(&result
->uenum
));
305 uprv_memcpy(result
, &UCHARSTRENUM_VT
, sizeof(UCHARSTRENUM_VT
));
306 result
->uenum
.context
= strings
;
308 result
->count
= count
;
311 return (UEnumeration
*) result
;