]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ucnv_cnv.cpp
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / common / ucnv_cnv.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4******************************************************************************
5*
374ca955 6* Copyright (C) 1999-2004, International Business Machines
b75a7d8f
A
7* Corporation and others. All Rights Reserved.
8*
9******************************************************************************
10*
11* uconv_cnv.c:
12* Implements all the low level conversion functions
13* T_UnicodeConverter_{to,from}Unicode_$ConversionType
14*
15* Change history:
16*
17* 06/29/2000 helena Major rewrite of the callback APIs.
18*/
19
20#include "unicode/utypes.h"
374ca955
A
21
22#if !UCONFIG_NO_CONVERSION
23
b75a7d8f
A
24#include "unicode/ucnv_err.h"
25#include "unicode/ucnv.h"
26#include "unicode/uset.h"
27#include "ucnv_cnv.h"
374ca955 28#include "ucnv_bld.h"
b75a7d8f
A
29#include "cmemory.h"
30
374ca955
A
31U_CFUNC void
32ucnv_getCompleteUnicodeSet(const UConverter *cnv,
73c04bcf 33 const USetAdder *sa,
374ca955
A
34 UConverterUnicodeSet which,
35 UErrorCode *pErrorCode) {
f3c0d7a5
A
36 (void)cnv;
37 (void)which;
38 (void)pErrorCode;
374ca955
A
39 sa->addRange(sa->set, 0, 0x10ffff);
40}
41
42U_CFUNC void
43ucnv_getNonSurrogateUnicodeSet(const UConverter *cnv,
73c04bcf 44 const USetAdder *sa,
374ca955
A
45 UConverterUnicodeSet which,
46 UErrorCode *pErrorCode) {
f3c0d7a5
A
47 (void)cnv;
48 (void)which;
49 (void)pErrorCode;
374ca955
A
50 sa->addRange(sa->set, 0, 0xd7ff);
51 sa->addRange(sa->set, 0xe000, 0x10ffff);
b75a7d8f
A
52}
53
374ca955
A
54U_CFUNC void
55ucnv_fromUWriteBytes(UConverter *cnv,
56 const char *bytes, int32_t length,
57 char **target, const char *targetLimit,
58 int32_t **offsets,
59 int32_t sourceIndex,
60 UErrorCode *pErrorCode) {
61 char *t=*target;
62 int32_t *o;
63
64 /* write bytes */
65 if(offsets==NULL || (o=*offsets)==NULL) {
66 while(length>0 && t<targetLimit) {
67 *t++=*bytes++;
68 --length;
b75a7d8f 69 }
374ca955
A
70 } else {
71 /* output with offsets */
72 while(length>0 && t<targetLimit) {
73 *t++=*bytes++;
74 *o++=sourceIndex;
75 --length;
76 }
77 *offsets=o;
b75a7d8f 78 }
374ca955
A
79 *target=t;
80
81 /* write overflow */
82 if(length>0) {
83 if(cnv!=NULL) {
84 t=(char *)cnv->charErrorBuffer;
85 cnv->charErrorBufferLength=(int8_t)length;
86 do {
87 *t++=(uint8_t)*bytes++;
88 } while(--length>0);
b75a7d8f 89 }
374ca955 90 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
b75a7d8f
A
91 }
92}
93
374ca955
A
94U_CFUNC void
95ucnv_toUWriteUChars(UConverter *cnv,
96 const UChar *uchars, int32_t length,
97 UChar **target, const UChar *targetLimit,
98 int32_t **offsets,
99 int32_t sourceIndex,
100 UErrorCode *pErrorCode) {
101 UChar *t=*target;
102 int32_t *o;
103
104 /* write UChars */
105 if(offsets==NULL || (o=*offsets)==NULL) {
106 while(length>0 && t<targetLimit) {
107 *t++=*uchars++;
108 --length;
109 }
110 } else {
111 /* output with offsets */
112 while(length>0 && t<targetLimit) {
113 *t++=*uchars++;
114 *o++=sourceIndex;
115 --length;
116 }
117 *offsets=o;
b75a7d8f 118 }
374ca955 119 *target=t;
b75a7d8f 120
374ca955
A
121 /* write overflow */
122 if(length>0) {
123 if(cnv!=NULL) {
124 t=cnv->UCharErrorBuffer;
125 cnv->UCharErrorBufferLength=(int8_t)length;
b75a7d8f 126 do {
374ca955
A
127 *t++=*uchars++;
128 } while(--length>0);
b75a7d8f 129 }
374ca955 130 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
b75a7d8f 131 }
b75a7d8f
A
132}
133
374ca955
A
134U_CFUNC void
135ucnv_toUWriteCodePoint(UConverter *cnv,
136 UChar32 c,
137 UChar **target, const UChar *targetLimit,
138 int32_t **offsets,
139 int32_t sourceIndex,
140 UErrorCode *pErrorCode) {
141 UChar *t;
142 int32_t *o;
143
144 t=*target;
145
146 if(t<targetLimit) {
147 if(c<=0xffff) {
148 *t++=(UChar)c;
149 c=U_SENTINEL;
150 } else /* c is a supplementary code point */ {
151 *t++=U16_LEAD(c);
152 c=U16_TRAIL(c);
153 if(t<targetLimit) {
154 *t++=(UChar)c;
155 c=U_SENTINEL;
b75a7d8f
A
156 }
157 }
b75a7d8f 158
374ca955
A
159 /* write offsets */
160 if(offsets!=NULL && (o=*offsets)!=NULL) {
161 *o++=sourceIndex;
162 if((*target+1)<t) {
163 *o++=sourceIndex;
b75a7d8f 164 }
374ca955 165 *offsets=o;
b75a7d8f
A
166 }
167 }
168
374ca955 169 *target=t;
b75a7d8f 170
374ca955
A
171 /* write overflow from c */
172 if(c>=0) {
173 if(cnv!=NULL) {
174 int8_t i=0;
175 U16_APPEND_UNSAFE(cnv->UCharErrorBuffer, i, c);
176 cnv->UCharErrorBufferLength=i;
177 }
178 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
179 }
b75a7d8f
A
180}
181
374ca955 182#endif