intl/icu/source/common/ucnv_cnv.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ucnv_cnv.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     1.4 +/*
     1.5 +******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1999-2004, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +******************************************************************************
    1.11 +*
    1.12 +*   uconv_cnv.c:
    1.13 +*   Implements all the low level conversion functions
    1.14 +*   T_UnicodeConverter_{to,from}Unicode_$ConversionType
    1.15 +*
    1.16 +*   Change history:
    1.17 +*
    1.18 +*   06/29/2000  helena      Major rewrite of the callback APIs.
    1.19 +*/
    1.20 +
    1.21 +#include "unicode/utypes.h"
    1.22 +
    1.23 +#if !UCONFIG_NO_CONVERSION
    1.24 +
    1.25 +#include "unicode/ucnv_err.h"
    1.26 +#include "unicode/ucnv.h"
    1.27 +#include "unicode/uset.h"
    1.28 +#include "ucnv_cnv.h"
    1.29 +#include "ucnv_bld.h"
    1.30 +#include "cmemory.h"
    1.31 +
    1.32 +U_CFUNC void
    1.33 +ucnv_getCompleteUnicodeSet(const UConverter *cnv,
    1.34 +                   const USetAdder *sa,
    1.35 +                   UConverterUnicodeSet which,
    1.36 +                   UErrorCode *pErrorCode) {
    1.37 +    sa->addRange(sa->set, 0, 0x10ffff);
    1.38 +}
    1.39 +
    1.40 +U_CFUNC void
    1.41 +ucnv_getNonSurrogateUnicodeSet(const UConverter *cnv,
    1.42 +                               const USetAdder *sa,
    1.43 +                               UConverterUnicodeSet which,
    1.44 +                               UErrorCode *pErrorCode) {
    1.45 +    sa->addRange(sa->set, 0, 0xd7ff);
    1.46 +    sa->addRange(sa->set, 0xe000, 0x10ffff);
    1.47 +}
    1.48 +
    1.49 +U_CFUNC void
    1.50 +ucnv_fromUWriteBytes(UConverter *cnv,
    1.51 +                     const char *bytes, int32_t length,
    1.52 +                     char **target, const char *targetLimit,
    1.53 +                     int32_t **offsets,
    1.54 +                     int32_t sourceIndex,
    1.55 +                     UErrorCode *pErrorCode) {
    1.56 +    char *t=*target;
    1.57 +    int32_t *o;
    1.58 +
    1.59 +    /* write bytes */
    1.60 +    if(offsets==NULL || (o=*offsets)==NULL) {
    1.61 +        while(length>0 && t<targetLimit) {
    1.62 +            *t++=*bytes++;
    1.63 +            --length;
    1.64 +        }
    1.65 +    } else {
    1.66 +        /* output with offsets */
    1.67 +        while(length>0 && t<targetLimit) {
    1.68 +            *t++=*bytes++;
    1.69 +            *o++=sourceIndex;
    1.70 +            --length;
    1.71 +        }
    1.72 +        *offsets=o;
    1.73 +    }
    1.74 +    *target=t;
    1.75 +
    1.76 +    /* write overflow */
    1.77 +    if(length>0) {
    1.78 +        if(cnv!=NULL) {
    1.79 +            t=(char *)cnv->charErrorBuffer;
    1.80 +            cnv->charErrorBufferLength=(int8_t)length;
    1.81 +            do {
    1.82 +                *t++=(uint8_t)*bytes++;
    1.83 +            } while(--length>0);
    1.84 +        }
    1.85 +        *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
    1.86 +    }
    1.87 +}
    1.88 +
    1.89 +U_CFUNC void
    1.90 +ucnv_toUWriteUChars(UConverter *cnv,
    1.91 +                    const UChar *uchars, int32_t length,
    1.92 +                    UChar **target, const UChar *targetLimit,
    1.93 +                    int32_t **offsets,
    1.94 +                    int32_t sourceIndex,
    1.95 +                    UErrorCode *pErrorCode) {
    1.96 +    UChar *t=*target;
    1.97 +    int32_t *o;
    1.98 +
    1.99 +    /* write UChars */
   1.100 +    if(offsets==NULL || (o=*offsets)==NULL) {
   1.101 +        while(length>0 && t<targetLimit) {
   1.102 +            *t++=*uchars++;
   1.103 +            --length;
   1.104 +        }
   1.105 +    } else {
   1.106 +        /* output with offsets */
   1.107 +        while(length>0 && t<targetLimit) {
   1.108 +            *t++=*uchars++;
   1.109 +            *o++=sourceIndex;
   1.110 +            --length;
   1.111 +        }
   1.112 +        *offsets=o;
   1.113 +    }
   1.114 +    *target=t;
   1.115 +
   1.116 +    /* write overflow */
   1.117 +    if(length>0) {
   1.118 +        if(cnv!=NULL) {
   1.119 +            t=cnv->UCharErrorBuffer;
   1.120 +            cnv->UCharErrorBufferLength=(int8_t)length;
   1.121 +            do {
   1.122 +                *t++=*uchars++;
   1.123 +            } while(--length>0);
   1.124 +        }
   1.125 +        *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
   1.126 +    }
   1.127 +}
   1.128 +
   1.129 +U_CFUNC void
   1.130 +ucnv_toUWriteCodePoint(UConverter *cnv,
   1.131 +                       UChar32 c,
   1.132 +                       UChar **target, const UChar *targetLimit,
   1.133 +                       int32_t **offsets,
   1.134 +                       int32_t sourceIndex,
   1.135 +                       UErrorCode *pErrorCode) {
   1.136 +    UChar *t;
   1.137 +    int32_t *o;
   1.138 +
   1.139 +    t=*target;
   1.140 +
   1.141 +    if(t<targetLimit) {
   1.142 +        if(c<=0xffff) {
   1.143 +            *t++=(UChar)c;
   1.144 +            c=U_SENTINEL;
   1.145 +        } else /* c is a supplementary code point */ {
   1.146 +            *t++=U16_LEAD(c);
   1.147 +            c=U16_TRAIL(c);
   1.148 +            if(t<targetLimit) {
   1.149 +                *t++=(UChar)c;
   1.150 +                c=U_SENTINEL;
   1.151 +            }
   1.152 +        }
   1.153 +
   1.154 +        /* write offsets */
   1.155 +        if(offsets!=NULL && (o=*offsets)!=NULL) {
   1.156 +            *o++=sourceIndex;
   1.157 +            if((*target+1)<t) {
   1.158 +                *o++=sourceIndex;
   1.159 +            }
   1.160 +            *offsets=o;
   1.161 +        }
   1.162 +    }
   1.163 +
   1.164 +    *target=t;
   1.165 +
   1.166 +    /* write overflow from c */
   1.167 +    if(c>=0) {
   1.168 +        if(cnv!=NULL) {
   1.169 +            int8_t i=0;
   1.170 +            U16_APPEND_UNSAFE(cnv->UCharErrorBuffer, i, c);
   1.171 +            cnv->UCharErrorBufferLength=i;
   1.172 +        }
   1.173 +        *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
   1.174 +    }
   1.175 +}
   1.176 +
   1.177 +#endif

mercurial