2 *******************************************************************************
4 * Copyright (C) 1998-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 05/28/99 stephen Creation.
15 *******************************************************************************
21 #include "unicode/ustring.h"
22 #include "unicode/putil.h"
25 static void ustr_resize(struct UString
*s
, int32_t len
, UErrorCode
*status
);
28 #define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1))
31 ustr_init(struct UString
*s
)
34 s
->fLength
= s
->fCapacity
= 0;
38 ustr_initChars(struct UString
*s
, const char* source
, int32_t length
, UErrorCode
*status
)
41 if (U_FAILURE(*status
)) return;
43 s
->fLength
= s
->fCapacity
= 0;
45 length
= (int32_t)uprv_strlen(source
);
47 if(s
->fCapacity
< length
) {
48 ustr_resize(s
, ALLOCATION(length
), status
);
49 if(U_FAILURE(*status
)) return;
51 for (; i
< length
; i
++)
54 u_charsToUChars(source
+i
, &charToAppend
, 1);
55 ustr_ucat(s
, charToAppend
, status
);
57 #if U_CHARSET_FAMILY==U_ASCII_FAMILY
58 ustr_ucat(s, (UChar)(uint8_t)(source[i]), status);
59 #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
60 ustr_ucat(s, (UChar)asciiFromEbcdic[(uint8_t)(*cs++)], status);
62 # error U_CHARSET_FAMILY is not valid
69 ustr_deinit(struct UString
*s
)
73 s
->fLength
= s
->fCapacity
= 0;
77 ustr_cpy(struct UString
*dst
,
78 const struct UString
*src
,
81 if(U_FAILURE(*status
) || dst
== src
)
84 if(dst
->fCapacity
< src
->fLength
) {
85 ustr_resize(dst
, ALLOCATION(src
->fLength
), status
);
86 if(U_FAILURE(*status
))
89 if(src
->fChars
== NULL
|| dst
->fChars
== NULL
){
92 uprv_memcpy(dst
->fChars
, src
->fChars
, sizeof(UChar
) * src
->fLength
);
93 dst
->fLength
= src
->fLength
;
94 dst
->fChars
[dst
->fLength
] = 0x0000;
98 ustr_setlen(struct UString
*s
,
102 if(U_FAILURE(*status
))
105 if(s
->fCapacity
< (len
+ 1)) {
106 ustr_resize(s
, ALLOCATION(len
), status
);
107 if(U_FAILURE(*status
))
112 s
->fChars
[len
] = 0x0000;
116 ustr_cat(struct UString
*dst
,
117 const struct UString
*src
,
120 ustr_ncat(dst
, src
, src
->fLength
, status
);
124 ustr_ncat(struct UString
*dst
,
125 const struct UString
*src
,
129 if(U_FAILURE(*status
) || dst
== src
)
132 if(dst
->fCapacity
< (dst
->fLength
+ n
)) {
133 ustr_resize(dst
, ALLOCATION(dst
->fLength
+ n
), status
);
134 if(U_FAILURE(*status
))
138 uprv_memcpy(dst
->fChars
+ dst
->fLength
, src
->fChars
,
140 dst
->fLength
+= src
->fLength
;
141 dst
->fChars
[dst
->fLength
] = 0x0000;
145 ustr_ucat(struct UString
*dst
,
149 if(U_FAILURE(*status
))
152 if(dst
->fCapacity
< (dst
->fLength
+ 1)) {
153 ustr_resize(dst
, ALLOCATION(dst
->fLength
+ 1), status
);
154 if(U_FAILURE(*status
))
158 uprv_memcpy(dst
->fChars
+ dst
->fLength
, &c
,
161 dst
->fChars
[dst
->fLength
] = 0x0000;
164 ustr_u32cat(struct UString
*dst
, UChar32 c
, UErrorCode
*status
){
166 *status
= U_ILLEGAL_CHAR_FOUND
;
170 ustr_ucat(dst
, U16_LEAD(c
), status
);
171 ustr_ucat(dst
, U16_TRAIL(c
), status
);
173 ustr_ucat(dst
, (UChar
) c
, status
);
177 ustr_uscat(struct UString
*dst
,
178 const UChar
* src
,int len
,
181 if(U_FAILURE(*status
))
184 if(dst
->fCapacity
< (dst
->fLength
+ len
)) {
185 ustr_resize(dst
, ALLOCATION(dst
->fLength
+ len
), status
);
186 if(U_FAILURE(*status
))
190 uprv_memcpy(dst
->fChars
+ dst
->fLength
, src
,
191 sizeof(UChar
) * len
);
193 dst
->fChars
[dst
->fLength
] = 0x0000;
196 /* Destroys data in the string */
198 ustr_resize(struct UString
*s
,
202 if(U_FAILURE(*status
))
205 /* +1 for trailing 0x0000 */
206 s
->fChars
= (UChar
*) uprv_realloc(s
->fChars
, sizeof(UChar
) * (len
+ 1));
208 *status
= U_MEMORY_ALLOCATION_ERROR
;
209 s
->fLength
= s
->fCapacity
= 0;