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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 1998-2012, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 *******************************************************************************
michael@0 8 *
michael@0 9 * File ustr.c
michael@0 10 *
michael@0 11 * Modification History:
michael@0 12 *
michael@0 13 * Date Name Description
michael@0 14 * 05/28/99 stephen Creation.
michael@0 15 *******************************************************************************
michael@0 16 */
michael@0 17
michael@0 18 #include "ustr.h"
michael@0 19 #include "cmemory.h"
michael@0 20 #include "cstring.h"
michael@0 21 #include "unicode/ustring.h"
michael@0 22 #include "unicode/putil.h"
michael@0 23 #include "unicode/utf16.h"
michael@0 24
michael@0 25 /* Protos */
michael@0 26 static void ustr_resize(struct UString *s, int32_t len, UErrorCode *status);
michael@0 27
michael@0 28 /* Macros */
michael@0 29 #define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1))
michael@0 30
michael@0 31 U_CFUNC void
michael@0 32 ustr_init(struct UString *s)
michael@0 33 {
michael@0 34 s->fChars = 0;
michael@0 35 s->fLength = s->fCapacity = 0;
michael@0 36 }
michael@0 37
michael@0 38 U_CFUNC void
michael@0 39 ustr_initChars(struct UString *s, const char* source, int32_t length, UErrorCode *status)
michael@0 40 {
michael@0 41 int i = 0;
michael@0 42 if (U_FAILURE(*status)) return;
michael@0 43 s->fChars = 0;
michael@0 44 s->fLength = s->fCapacity = 0;
michael@0 45 if (length == -1) {
michael@0 46 length = (int32_t)uprv_strlen(source);
michael@0 47 }
michael@0 48 if(s->fCapacity < length) {
michael@0 49 ustr_resize(s, ALLOCATION(length), status);
michael@0 50 if(U_FAILURE(*status)) return;
michael@0 51 }
michael@0 52 for (; i < length; i++)
michael@0 53 {
michael@0 54 UChar charToAppend;
michael@0 55 u_charsToUChars(source+i, &charToAppend, 1);
michael@0 56 ustr_ucat(s, charToAppend, status);
michael@0 57 /*
michael@0 58 #if U_CHARSET_FAMILY==U_ASCII_FAMILY
michael@0 59 ustr_ucat(s, (UChar)(uint8_t)(source[i]), status);
michael@0 60 #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
michael@0 61 ustr_ucat(s, (UChar)asciiFromEbcdic[(uint8_t)(*cs++)], status);
michael@0 62 #else
michael@0 63 # error U_CHARSET_FAMILY is not valid
michael@0 64 #endif
michael@0 65 */
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 U_CFUNC void
michael@0 70 ustr_deinit(struct UString *s)
michael@0 71 {
michael@0 72 if (s) {
michael@0 73 uprv_free(s->fChars);
michael@0 74 s->fChars = 0;
michael@0 75 s->fLength = s->fCapacity = 0;
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 U_CFUNC void
michael@0 80 ustr_cpy(struct UString *dst,
michael@0 81 const struct UString *src,
michael@0 82 UErrorCode *status)
michael@0 83 {
michael@0 84 if(U_FAILURE(*status) || dst == src)
michael@0 85 return;
michael@0 86
michael@0 87 if(dst->fCapacity < src->fLength) {
michael@0 88 ustr_resize(dst, ALLOCATION(src->fLength), status);
michael@0 89 if(U_FAILURE(*status))
michael@0 90 return;
michael@0 91 }
michael@0 92 if(src->fChars == NULL || dst->fChars == NULL){
michael@0 93 return;
michael@0 94 }
michael@0 95 uprv_memcpy(dst->fChars, src->fChars, sizeof(UChar) * src->fLength);
michael@0 96 dst->fLength = src->fLength;
michael@0 97 dst->fChars[dst->fLength] = 0x0000;
michael@0 98 }
michael@0 99
michael@0 100 U_CFUNC void
michael@0 101 ustr_setlen(struct UString *s,
michael@0 102 int32_t len,
michael@0 103 UErrorCode *status)
michael@0 104 {
michael@0 105 if(U_FAILURE(*status))
michael@0 106 return;
michael@0 107
michael@0 108 if(s->fCapacity < (len + 1)) {
michael@0 109 ustr_resize(s, ALLOCATION(len), status);
michael@0 110 if(U_FAILURE(*status))
michael@0 111 return;
michael@0 112 }
michael@0 113
michael@0 114 s->fLength = len;
michael@0 115 s->fChars[len] = 0x0000;
michael@0 116 }
michael@0 117
michael@0 118 U_CFUNC void
michael@0 119 ustr_cat(struct UString *dst,
michael@0 120 const struct UString *src,
michael@0 121 UErrorCode *status)
michael@0 122 {
michael@0 123 ustr_ncat(dst, src, src->fLength, status);
michael@0 124 }
michael@0 125
michael@0 126 U_CFUNC void
michael@0 127 ustr_ncat(struct UString *dst,
michael@0 128 const struct UString *src,
michael@0 129 int32_t n,
michael@0 130 UErrorCode *status)
michael@0 131 {
michael@0 132 if(U_FAILURE(*status) || dst == src)
michael@0 133 return;
michael@0 134
michael@0 135 if(dst->fCapacity < (dst->fLength + n)) {
michael@0 136 ustr_resize(dst, ALLOCATION(dst->fLength + n), status);
michael@0 137 if(U_FAILURE(*status))
michael@0 138 return;
michael@0 139 }
michael@0 140
michael@0 141 uprv_memcpy(dst->fChars + dst->fLength, src->fChars,
michael@0 142 sizeof(UChar) * n);
michael@0 143 dst->fLength += src->fLength;
michael@0 144 dst->fChars[dst->fLength] = 0x0000;
michael@0 145 }
michael@0 146
michael@0 147 U_CFUNC void
michael@0 148 ustr_ucat(struct UString *dst,
michael@0 149 UChar c,
michael@0 150 UErrorCode *status)
michael@0 151 {
michael@0 152 if(U_FAILURE(*status))
michael@0 153 return;
michael@0 154
michael@0 155 if(dst->fCapacity < (dst->fLength + 1)) {
michael@0 156 ustr_resize(dst, ALLOCATION(dst->fLength + 1), status);
michael@0 157 if(U_FAILURE(*status))
michael@0 158 return;
michael@0 159 }
michael@0 160
michael@0 161 uprv_memcpy(dst->fChars + dst->fLength, &c,
michael@0 162 sizeof(UChar) * 1);
michael@0 163 dst->fLength += 1;
michael@0 164 dst->fChars[dst->fLength] = 0x0000;
michael@0 165 }
michael@0 166 U_CFUNC void
michael@0 167 ustr_u32cat(struct UString *dst, UChar32 c, UErrorCode *status){
michael@0 168 if(c > 0x10FFFF){
michael@0 169 *status = U_ILLEGAL_CHAR_FOUND;
michael@0 170 return;
michael@0 171 }
michael@0 172 if(c >0xFFFF){
michael@0 173 ustr_ucat(dst, U16_LEAD(c), status);
michael@0 174 ustr_ucat(dst, U16_TRAIL(c), status);
michael@0 175 }else{
michael@0 176 ustr_ucat(dst, (UChar) c, status);
michael@0 177 }
michael@0 178 }
michael@0 179 U_CFUNC void
michael@0 180 ustr_uscat(struct UString *dst,
michael@0 181 const UChar* src,int len,
michael@0 182 UErrorCode *status)
michael@0 183 {
michael@0 184 if(U_FAILURE(*status))
michael@0 185 return;
michael@0 186
michael@0 187 if(dst->fCapacity < (dst->fLength + len)) {
michael@0 188 ustr_resize(dst, ALLOCATION(dst->fLength + len), status);
michael@0 189 if(U_FAILURE(*status))
michael@0 190 return;
michael@0 191 }
michael@0 192
michael@0 193 uprv_memcpy(dst->fChars + dst->fLength, src,
michael@0 194 sizeof(UChar) * len);
michael@0 195 dst->fLength += len;
michael@0 196 dst->fChars[dst->fLength] = 0x0000;
michael@0 197 }
michael@0 198
michael@0 199 /* Destroys data in the string */
michael@0 200 static void
michael@0 201 ustr_resize(struct UString *s,
michael@0 202 int32_t len,
michael@0 203 UErrorCode *status)
michael@0 204 {
michael@0 205 if(U_FAILURE(*status))
michael@0 206 return;
michael@0 207
michael@0 208 /* +1 for trailing 0x0000 */
michael@0 209 s->fChars = (UChar*) uprv_realloc(s->fChars, sizeof(UChar) * (len + 1));
michael@0 210 if(s->fChars == 0) {
michael@0 211 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 212 s->fLength = s->fCapacity = 0;
michael@0 213 return;
michael@0 214 }
michael@0 215
michael@0 216 s->fCapacity = len;
michael@0 217 }

mercurial