2 *******************************************************************************
4 * Copyright (C) 1998-2012, 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"
23 #include "unicode/utf16.h"
26 static void ustr_resize(struct UString *s, int32_t len, UErrorCode *status);
29 #define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1))
32 ustr_init(struct UString *s)
35 s->fLength = s->fCapacity = 0;
39 ustr_initChars(struct UString *s, const char* source, int32_t length, UErrorCode *status)
42 if (U_FAILURE(*status)) return;
44 s->fLength = s->fCapacity = 0;
46 length = (int32_t)uprv_strlen(source);
48 if(s->fCapacity < length) {
49 ustr_resize(s, ALLOCATION(length), status);
50 if(U_FAILURE(*status)) return;
52 for (; i < length; i++)
55 u_charsToUChars(source+i, &charToAppend, 1);
56 ustr_ucat(s, charToAppend, status);
58 #if U_CHARSET_FAMILY==U_ASCII_FAMILY
59 ustr_ucat(s, (UChar)(uint8_t)(source[i]), status);
60 #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
61 ustr_ucat(s, (UChar)asciiFromEbcdic[(uint8_t)(*cs++)], status);
63 # error U_CHARSET_FAMILY is not valid
70 ustr_deinit(struct UString *s)
75 s->fLength = s->fCapacity = 0;
80 ustr_cpy(struct UString *dst,
81 const struct UString *src,
84 if(U_FAILURE(*status) || dst == src)
87 if(dst->fCapacity < src->fLength) {
88 ustr_resize(dst, ALLOCATION(src->fLength), status);
89 if(U_FAILURE(*status))
92 if(src->fChars == NULL || dst->fChars == NULL){
95 uprv_memcpy(dst->fChars, src->fChars, sizeof(UChar) * src->fLength);
96 dst->fLength = src->fLength;
97 dst->fChars[dst->fLength] = 0x0000;
101 ustr_setlen(struct UString *s,
105 if(U_FAILURE(*status))
108 if(s->fCapacity < (len + 1)) {
109 ustr_resize(s, ALLOCATION(len), status);
110 if(U_FAILURE(*status))
115 s->fChars[len] = 0x0000;
119 ustr_cat(struct UString *dst,
120 const struct UString *src,
123 ustr_ncat(dst, src, src->fLength, status);
127 ustr_ncat(struct UString *dst,
128 const struct UString *src,
132 if(U_FAILURE(*status) || dst == src)
135 if(dst->fCapacity < (dst->fLength + n)) {
136 ustr_resize(dst, ALLOCATION(dst->fLength + n), status);
137 if(U_FAILURE(*status))
141 uprv_memcpy(dst->fChars + dst->fLength, src->fChars,
143 dst->fLength += src->fLength;
144 dst->fChars[dst->fLength] = 0x0000;
148 ustr_ucat(struct UString *dst,
152 if(U_FAILURE(*status))
155 if(dst->fCapacity < (dst->fLength + 1)) {
156 ustr_resize(dst, ALLOCATION(dst->fLength + 1), status);
157 if(U_FAILURE(*status))
161 uprv_memcpy(dst->fChars + dst->fLength, &c,
164 dst->fChars[dst->fLength] = 0x0000;
167 ustr_u32cat(struct UString *dst, UChar32 c, UErrorCode *status){
169 *status = U_ILLEGAL_CHAR_FOUND;
173 ustr_ucat(dst, U16_LEAD(c), status);
174 ustr_ucat(dst, U16_TRAIL(c), status);
176 ustr_ucat(dst, (UChar) c, status);
180 ustr_uscat(struct UString *dst,
181 const UChar* src,int len,
184 if(U_FAILURE(*status))
187 if(dst->fCapacity < (dst->fLength + len)) {
188 ustr_resize(dst, ALLOCATION(dst->fLength + len), status);
189 if(U_FAILURE(*status))
193 uprv_memcpy(dst->fChars + dst->fLength, src,
194 sizeof(UChar) * len);
196 dst->fChars[dst->fLength] = 0x0000;
199 /* Destroys data in the string */
201 ustr_resize(struct UString *s,
205 if(U_FAILURE(*status))
208 /* +1 for trailing 0x0000 */
209 s->fChars = (UChar*) uprv_realloc(s->fChars, sizeof(UChar) * (len + 1));
211 *status = U_MEMORY_ALLOCATION_ERROR;
212 s->fLength = s->fCapacity = 0;