]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/ustr_cnv.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1998-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: ustr_cnv.cpp
12 * tab size: 8 (not used)
15 * created on: 2004aug24
16 * created by: Markus W. Scherer
18 * Character conversion functions moved here from ustring.c
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_CONVERSION
25 #include "unicode/ustring.h"
26 #include "unicode/ucnv.h"
32 /* mutexed access to a shared default converter ----------------------------- */
34 static UConverter
*gDefaultConverter
= NULL
;
36 U_CAPI UConverter
* U_EXPORT2
37 u_getDefaultConverter(UErrorCode
*status
)
39 UConverter
*converter
= NULL
;
41 if (gDefaultConverter
!= NULL
) {
44 /* need to check to make sure it wasn't taken out from under us */
45 if (gDefaultConverter
!= NULL
) {
46 converter
= gDefaultConverter
;
47 gDefaultConverter
= NULL
;
52 /* if the cache was empty, create a converter */
53 if(converter
== NULL
) {
54 converter
= ucnv_open(NULL
, status
);
55 if(U_FAILURE(*status
)) {
56 ucnv_close(converter
);
65 u_releaseDefaultConverter(UConverter
*converter
)
67 if(gDefaultConverter
== NULL
) {
68 if (converter
!= NULL
) {
69 ucnv_reset(converter
);
73 if(gDefaultConverter
== NULL
) {
74 gDefaultConverter
= converter
;
80 if(converter
!= NULL
) {
81 ucnv_close(converter
);
86 u_flushDefaultConverter()
88 UConverter
*converter
= NULL
;
90 if (gDefaultConverter
!= NULL
) {
93 /* need to check to make sure it wasn't taken out from under us */
94 if (gDefaultConverter
!= NULL
) {
95 converter
= gDefaultConverter
;
96 gDefaultConverter
= NULL
;
101 /* if the cache was populated, flush it */
102 if(converter
!= NULL
) {
103 ucnv_close(converter
);
108 /* conversions between char* and UChar* ------------------------------------- */
110 /* maximum string length for u_uastrcpy() and u_austrcpy() implementations */
111 #define MAX_STRLEN 0x0FFFFFFF
114 returns the minimum of (the length of the null-terminated string) and n.
116 static int32_t u_astrnlen(const char *s1
, int32_t n
)
122 while (n
-- && *(s1
++))
130 U_CAPI UChar
* U_EXPORT2
131 u_uastrncpy(UChar
*ucs1
,
135 UChar
*target
= ucs1
;
136 UErrorCode err
= U_ZERO_ERROR
;
137 UConverter
*cnv
= u_getDefaultConverter(&err
);
138 if(U_SUCCESS(err
) && cnv
!= NULL
) {
144 s2
+u_astrnlen(s2
, n
),
148 ucnv_reset(cnv
); /* be good citizens */
149 u_releaseDefaultConverter(cnv
);
150 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
151 *ucs1
= 0; /* failure */
153 if(target
< (ucs1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
154 *target
= 0; /* terminate */
162 U_CAPI UChar
* U_EXPORT2
163 u_uastrcpy(UChar
*ucs1
,
166 UErrorCode err
= U_ZERO_ERROR
;
167 UConverter
*cnv
= u_getDefaultConverter(&err
);
168 if(U_SUCCESS(err
) && cnv
!= NULL
) {
173 (int32_t)uprv_strlen(s2
),
175 u_releaseDefaultConverter(cnv
);
186 returns the minimum of (the length of the null-terminated string) and n.
188 static int32_t u_ustrnlen(const UChar
*ucs1
, int32_t n
)
194 while (n
-- && *(ucs1
++))
202 U_CAPI
char* U_EXPORT2
203 u_austrncpy(char *s1
,
208 UErrorCode err
= U_ZERO_ERROR
;
209 UConverter
*cnv
= u_getDefaultConverter(&err
);
210 if(U_SUCCESS(err
) && cnv
!= NULL
) {
212 ucnv_fromUnicode(cnv
,
216 ucs2
+u_ustrnlen(ucs2
, n
),
220 ucnv_reset(cnv
); /* be good citizens */
221 u_releaseDefaultConverter(cnv
);
222 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
223 *s1
= 0; /* failure */
225 if(target
< (s1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
226 *target
= 0; /* terminate */
234 U_CAPI
char* U_EXPORT2
238 UErrorCode err
= U_ZERO_ERROR
;
239 UConverter
*cnv
= u_getDefaultConverter(&err
);
240 if(U_SUCCESS(err
) && cnv
!= NULL
) {
241 int32_t len
= ucnv_fromUChars(cnv
,
247 u_releaseDefaultConverter(cnv
);