michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1998-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * michael@0: * File ustr.c michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 05/28/99 stephen Creation. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "ustr.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/putil.h" michael@0: #include "unicode/utf16.h" michael@0: michael@0: /* Protos */ michael@0: static void ustr_resize(struct UString *s, int32_t len, UErrorCode *status); michael@0: michael@0: /* Macros */ michael@0: #define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1)) michael@0: michael@0: U_CFUNC void michael@0: ustr_init(struct UString *s) michael@0: { michael@0: s->fChars = 0; michael@0: s->fLength = s->fCapacity = 0; michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ustr_initChars(struct UString *s, const char* source, int32_t length, UErrorCode *status) michael@0: { michael@0: int i = 0; michael@0: if (U_FAILURE(*status)) return; michael@0: s->fChars = 0; michael@0: s->fLength = s->fCapacity = 0; michael@0: if (length == -1) { michael@0: length = (int32_t)uprv_strlen(source); michael@0: } michael@0: if(s->fCapacity < length) { michael@0: ustr_resize(s, ALLOCATION(length), status); michael@0: if(U_FAILURE(*status)) return; michael@0: } michael@0: for (; i < length; i++) michael@0: { michael@0: UChar charToAppend; michael@0: u_charsToUChars(source+i, &charToAppend, 1); michael@0: ustr_ucat(s, charToAppend, status); michael@0: /* michael@0: #if U_CHARSET_FAMILY==U_ASCII_FAMILY michael@0: ustr_ucat(s, (UChar)(uint8_t)(source[i]), status); michael@0: #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY michael@0: ustr_ucat(s, (UChar)asciiFromEbcdic[(uint8_t)(*cs++)], status); michael@0: #else michael@0: # error U_CHARSET_FAMILY is not valid michael@0: #endif michael@0: */ michael@0: } michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ustr_deinit(struct UString *s) michael@0: { michael@0: if (s) { michael@0: uprv_free(s->fChars); michael@0: s->fChars = 0; michael@0: s->fLength = s->fCapacity = 0; michael@0: } michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ustr_cpy(struct UString *dst, michael@0: const struct UString *src, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status) || dst == src) michael@0: return; michael@0: michael@0: if(dst->fCapacity < src->fLength) { michael@0: ustr_resize(dst, ALLOCATION(src->fLength), status); michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: } michael@0: if(src->fChars == NULL || dst->fChars == NULL){ michael@0: return; michael@0: } michael@0: uprv_memcpy(dst->fChars, src->fChars, sizeof(UChar) * src->fLength); michael@0: dst->fLength = src->fLength; michael@0: dst->fChars[dst->fLength] = 0x0000; michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ustr_setlen(struct UString *s, michael@0: int32_t len, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: michael@0: if(s->fCapacity < (len + 1)) { michael@0: ustr_resize(s, ALLOCATION(len), status); michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: } michael@0: michael@0: s->fLength = len; michael@0: s->fChars[len] = 0x0000; michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ustr_cat(struct UString *dst, michael@0: const struct UString *src, michael@0: UErrorCode *status) michael@0: { michael@0: ustr_ncat(dst, src, src->fLength, status); michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ustr_ncat(struct UString *dst, michael@0: const struct UString *src, michael@0: int32_t n, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status) || dst == src) michael@0: return; michael@0: michael@0: if(dst->fCapacity < (dst->fLength + n)) { michael@0: ustr_resize(dst, ALLOCATION(dst->fLength + n), status); michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: } michael@0: michael@0: uprv_memcpy(dst->fChars + dst->fLength, src->fChars, michael@0: sizeof(UChar) * n); michael@0: dst->fLength += src->fLength; michael@0: dst->fChars[dst->fLength] = 0x0000; michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ustr_ucat(struct UString *dst, michael@0: UChar c, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: michael@0: if(dst->fCapacity < (dst->fLength + 1)) { michael@0: ustr_resize(dst, ALLOCATION(dst->fLength + 1), status); michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: } michael@0: michael@0: uprv_memcpy(dst->fChars + dst->fLength, &c, michael@0: sizeof(UChar) * 1); michael@0: dst->fLength += 1; michael@0: dst->fChars[dst->fLength] = 0x0000; michael@0: } michael@0: U_CFUNC void michael@0: ustr_u32cat(struct UString *dst, UChar32 c, UErrorCode *status){ michael@0: if(c > 0x10FFFF){ michael@0: *status = U_ILLEGAL_CHAR_FOUND; michael@0: return; michael@0: } michael@0: if(c >0xFFFF){ michael@0: ustr_ucat(dst, U16_LEAD(c), status); michael@0: ustr_ucat(dst, U16_TRAIL(c), status); michael@0: }else{ michael@0: ustr_ucat(dst, (UChar) c, status); michael@0: } michael@0: } michael@0: U_CFUNC void michael@0: ustr_uscat(struct UString *dst, michael@0: const UChar* src,int len, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: michael@0: if(dst->fCapacity < (dst->fLength + len)) { michael@0: ustr_resize(dst, ALLOCATION(dst->fLength + len), status); michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: } michael@0: michael@0: uprv_memcpy(dst->fChars + dst->fLength, src, michael@0: sizeof(UChar) * len); michael@0: dst->fLength += len; michael@0: dst->fChars[dst->fLength] = 0x0000; michael@0: } michael@0: michael@0: /* Destroys data in the string */ michael@0: static void michael@0: ustr_resize(struct UString *s, michael@0: int32_t len, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) michael@0: return; michael@0: michael@0: /* +1 for trailing 0x0000 */ michael@0: s->fChars = (UChar*) uprv_realloc(s->fChars, sizeof(UChar) * (len + 1)); michael@0: if(s->fChars == 0) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: s->fLength = s->fCapacity = 0; michael@0: return; michael@0: } michael@0: michael@0: s->fCapacity = len; michael@0: }