intl/icu/source/tools/genrb/ustr.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/genrb/ustr.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,217 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1998-2012, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +*******************************************************************************
    1.11 +*
    1.12 +* File ustr.c
    1.13 +*
    1.14 +* Modification History:
    1.15 +*
    1.16 +*   Date        Name        Description
    1.17 +*   05/28/99    stephen     Creation.
    1.18 +*******************************************************************************
    1.19 +*/
    1.20 +
    1.21 +#include "ustr.h"
    1.22 +#include "cmemory.h"
    1.23 +#include "cstring.h"
    1.24 +#include "unicode/ustring.h"
    1.25 +#include "unicode/putil.h"
    1.26 +#include "unicode/utf16.h"
    1.27 +
    1.28 +/* Protos */
    1.29 +static void ustr_resize(struct UString *s, int32_t len, UErrorCode *status);
    1.30 +
    1.31 +/* Macros */
    1.32 +#define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1))
    1.33 +
    1.34 +U_CFUNC void
    1.35 +ustr_init(struct UString *s)
    1.36 +{
    1.37 +    s->fChars = 0;
    1.38 +    s->fLength = s->fCapacity = 0;
    1.39 +}
    1.40 +
    1.41 +U_CFUNC void
    1.42 +ustr_initChars(struct UString *s, const char* source, int32_t length, UErrorCode *status)
    1.43 +{
    1.44 +    int i = 0;
    1.45 +    if (U_FAILURE(*status)) return;
    1.46 +    s->fChars = 0;
    1.47 +    s->fLength = s->fCapacity = 0;
    1.48 +    if (length == -1) {
    1.49 +        length = (int32_t)uprv_strlen(source);
    1.50 +    }
    1.51 +    if(s->fCapacity < length) {
    1.52 +      ustr_resize(s, ALLOCATION(length), status);
    1.53 +      if(U_FAILURE(*status)) return;
    1.54 +    }
    1.55 +    for (; i < length; i++)
    1.56 +    {
    1.57 +      UChar charToAppend;
    1.58 +      u_charsToUChars(source+i, &charToAppend, 1);
    1.59 +      ustr_ucat(s, charToAppend, status);
    1.60 +      /*
    1.61 +#if U_CHARSET_FAMILY==U_ASCII_FAMILY
    1.62 +        ustr_ucat(s, (UChar)(uint8_t)(source[i]), status);
    1.63 +#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
    1.64 +        ustr_ucat(s, (UChar)asciiFromEbcdic[(uint8_t)(*cs++)], status);
    1.65 +#else
    1.66 +#   error U_CHARSET_FAMILY is not valid
    1.67 +#endif
    1.68 +      */
    1.69 +    }
    1.70 +}
    1.71 +
    1.72 +U_CFUNC void
    1.73 +ustr_deinit(struct UString *s)
    1.74 +{
    1.75 +    if (s) {
    1.76 +        uprv_free(s->fChars);
    1.77 +        s->fChars = 0;
    1.78 +        s->fLength = s->fCapacity = 0;
    1.79 +    }
    1.80 +}
    1.81 +
    1.82 +U_CFUNC void
    1.83 +ustr_cpy(struct UString *dst,
    1.84 +     const struct UString *src,
    1.85 +     UErrorCode *status)
    1.86 +{
    1.87 +    if(U_FAILURE(*status) || dst == src)
    1.88 +        return;
    1.89 +
    1.90 +    if(dst->fCapacity < src->fLength) {
    1.91 +        ustr_resize(dst, ALLOCATION(src->fLength), status);
    1.92 +        if(U_FAILURE(*status))
    1.93 +            return;
    1.94 +    }
    1.95 +    if(src->fChars == NULL || dst->fChars == NULL){
    1.96 +        return;
    1.97 +    }
    1.98 +    uprv_memcpy(dst->fChars, src->fChars, sizeof(UChar) * src->fLength);
    1.99 +    dst->fLength = src->fLength;
   1.100 +    dst->fChars[dst->fLength] = 0x0000;
   1.101 +}
   1.102 +
   1.103 +U_CFUNC void
   1.104 +ustr_setlen(struct UString *s,
   1.105 +        int32_t len,
   1.106 +        UErrorCode *status)
   1.107 +{
   1.108 +    if(U_FAILURE(*status))
   1.109 +        return;
   1.110 +
   1.111 +    if(s->fCapacity < (len + 1)) {
   1.112 +        ustr_resize(s, ALLOCATION(len), status);
   1.113 +        if(U_FAILURE(*status))
   1.114 +            return;
   1.115 +    }
   1.116 +
   1.117 +    s->fLength = len;
   1.118 +    s->fChars[len] = 0x0000;
   1.119 +}
   1.120 +
   1.121 +U_CFUNC void
   1.122 +ustr_cat(struct UString *dst,
   1.123 +     const struct UString *src,
   1.124 +     UErrorCode *status)
   1.125 +{
   1.126 +    ustr_ncat(dst, src, src->fLength, status);
   1.127 +}
   1.128 +
   1.129 +U_CFUNC void
   1.130 +ustr_ncat(struct UString *dst,
   1.131 +      const struct UString *src,
   1.132 +      int32_t n,
   1.133 +      UErrorCode *status)
   1.134 +{
   1.135 +    if(U_FAILURE(*status) || dst == src)
   1.136 +        return;
   1.137 +    
   1.138 +    if(dst->fCapacity < (dst->fLength + n)) {
   1.139 +        ustr_resize(dst, ALLOCATION(dst->fLength + n), status);
   1.140 +        if(U_FAILURE(*status))
   1.141 +            return;
   1.142 +    }
   1.143 +    
   1.144 +    uprv_memcpy(dst->fChars + dst->fLength, src->fChars,
   1.145 +                sizeof(UChar) * n);
   1.146 +    dst->fLength += src->fLength;
   1.147 +    dst->fChars[dst->fLength] = 0x0000;
   1.148 +}
   1.149 +
   1.150 +U_CFUNC void
   1.151 +ustr_ucat(struct UString *dst,
   1.152 +      UChar c,
   1.153 +      UErrorCode *status)
   1.154 +{
   1.155 +    if(U_FAILURE(*status))
   1.156 +        return;
   1.157 +
   1.158 +    if(dst->fCapacity < (dst->fLength + 1)) {
   1.159 +        ustr_resize(dst, ALLOCATION(dst->fLength + 1), status);
   1.160 +        if(U_FAILURE(*status))
   1.161 +            return;
   1.162 +    }
   1.163 +
   1.164 +    uprv_memcpy(dst->fChars + dst->fLength, &c,
   1.165 +        sizeof(UChar) * 1);
   1.166 +    dst->fLength += 1;
   1.167 +    dst->fChars[dst->fLength] = 0x0000;
   1.168 +}
   1.169 +U_CFUNC void 
   1.170 +ustr_u32cat(struct UString *dst, UChar32 c, UErrorCode *status){
   1.171 +    if(c > 0x10FFFF){
   1.172 +        *status = U_ILLEGAL_CHAR_FOUND;
   1.173 +        return;
   1.174 +    }
   1.175 +    if(c >0xFFFF){
   1.176 +        ustr_ucat(dst, U16_LEAD(c), status);
   1.177 +        ustr_ucat(dst, U16_TRAIL(c), status);
   1.178 +    }else{
   1.179 +        ustr_ucat(dst, (UChar) c, status);
   1.180 +    }
   1.181 +}
   1.182 +U_CFUNC void
   1.183 +ustr_uscat(struct UString *dst,
   1.184 +      const UChar* src,int len,
   1.185 +      UErrorCode *status)
   1.186 +{
   1.187 +    if(U_FAILURE(*status)) 
   1.188 +        return;
   1.189 +
   1.190 +    if(dst->fCapacity < (dst->fLength + len)) {
   1.191 +        ustr_resize(dst, ALLOCATION(dst->fLength + len), status);
   1.192 +        if(U_FAILURE(*status))
   1.193 +            return;
   1.194 +    }
   1.195 +
   1.196 +    uprv_memcpy(dst->fChars + dst->fLength, src,
   1.197 +        sizeof(UChar) * len);
   1.198 +    dst->fLength += len;
   1.199 +    dst->fChars[dst->fLength] = 0x0000;
   1.200 +}
   1.201 +
   1.202 +/* Destroys data in the string */
   1.203 +static void
   1.204 +ustr_resize(struct UString *s,
   1.205 +        int32_t len,
   1.206 +        UErrorCode *status)
   1.207 +{
   1.208 +    if(U_FAILURE(*status))
   1.209 +        return;
   1.210 +
   1.211 +    /* +1 for trailing 0x0000 */
   1.212 +    s->fChars = (UChar*) uprv_realloc(s->fChars, sizeof(UChar) * (len + 1));
   1.213 +    if(s->fChars == 0) {
   1.214 +        *status = U_MEMORY_ALLOCATION_ERROR;
   1.215 +        s->fLength = s->fCapacity = 0;
   1.216 +        return;
   1.217 +    }
   1.218 +
   1.219 +    s->fCapacity = len;
   1.220 +}

mercurial