2 *******************************************************************************
4 * Copyright (C) 1998-2008, 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
)
74 s
->fLength
= s
->fCapacity
= 0;
79 ustr_cpy(struct UString
*dst
,
80 const struct UString
*src
,
83 if(U_FAILURE(*status
) || dst
== src
)
86 if(dst
->fCapacity
< src
->fLength
) {
87 ustr_resize(dst
, ALLOCATION(src
->fLength
), status
);
88 if(U_FAILURE(*status
))
91 if(src
->fChars
== NULL
|| dst
->fChars
== NULL
){
94 uprv_memcpy(dst
->fChars
, src
->fChars
, sizeof(UChar
) * src
->fLength
);
95 dst
->fLength
= src
->fLength
;
96 dst
->fChars
[dst
->fLength
] = 0x0000;
100 ustr_setlen(struct UString
*s
,
104 if(U_FAILURE(*status
))
107 if(s
->fCapacity
< (len
+ 1)) {
108 ustr_resize(s
, ALLOCATION(len
), status
);
109 if(U_FAILURE(*status
))
114 s
->fChars
[len
] = 0x0000;
118 ustr_cat(struct UString
*dst
,
119 const struct UString
*src
,
122 ustr_ncat(dst
, src
, src
->fLength
, status
);
126 ustr_ncat(struct UString
*dst
,
127 const struct UString
*src
,
131 if(U_FAILURE(*status
) || dst
== src
)
134 if(dst
->fCapacity
< (dst
->fLength
+ n
)) {
135 ustr_resize(dst
, ALLOCATION(dst
->fLength
+ n
), status
);
136 if(U_FAILURE(*status
))
140 uprv_memcpy(dst
->fChars
+ dst
->fLength
, src
->fChars
,
142 dst
->fLength
+= src
->fLength
;
143 dst
->fChars
[dst
->fLength
] = 0x0000;
147 ustr_ucat(struct UString
*dst
,
151 if(U_FAILURE(*status
))
154 if(dst
->fCapacity
< (dst
->fLength
+ 1)) {
155 ustr_resize(dst
, ALLOCATION(dst
->fLength
+ 1), status
);
156 if(U_FAILURE(*status
))
160 uprv_memcpy(dst
->fChars
+ dst
->fLength
, &c
,
163 dst
->fChars
[dst
->fLength
] = 0x0000;
166 ustr_u32cat(struct UString
*dst
, UChar32 c
, UErrorCode
*status
){
168 *status
= U_ILLEGAL_CHAR_FOUND
;
172 ustr_ucat(dst
, U16_LEAD(c
), status
);
173 ustr_ucat(dst
, U16_TRAIL(c
), status
);
175 ustr_ucat(dst
, (UChar
) c
, status
);
179 ustr_uscat(struct UString
*dst
,
180 const UChar
* src
,int len
,
183 if(U_FAILURE(*status
))
186 if(dst
->fCapacity
< (dst
->fLength
+ len
)) {
187 ustr_resize(dst
, ALLOCATION(dst
->fLength
+ len
), status
);
188 if(U_FAILURE(*status
))
192 uprv_memcpy(dst
->fChars
+ dst
->fLength
, src
,
193 sizeof(UChar
) * len
);
195 dst
->fChars
[dst
->fLength
] = 0x0000;
198 /* Destroys data in the string */
200 ustr_resize(struct UString
*s
,
204 if(U_FAILURE(*status
))
207 /* +1 for trailing 0x0000 */
208 s
->fChars
= (UChar
*) uprv_realloc(s
->fChars
, sizeof(UChar
) * (len
+ 1));
210 *status
= U_MEMORY_ALLOCATION_ERROR
;
211 s
->fLength
= s
->fCapacity
= 0;