]>
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"
33 /* mutexed access to a shared default converter ----------------------------- */
35 static UConverter
*gDefaultConverter
= NULL
;
37 U_CAPI UConverter
* U_EXPORT2
38 u_getDefaultConverter(UErrorCode
*status
)
40 UConverter
*converter
= NULL
;
42 if (gDefaultConverter
!= NULL
) {
45 /* need to check to make sure it wasn't taken out from under us */
46 if (gDefaultConverter
!= NULL
) {
47 converter
= gDefaultConverter
;
48 gDefaultConverter
= NULL
;
50 icu::umtx_unlock(NULL
);
53 /* if the cache was empty, create a converter */
54 if(converter
== NULL
) {
55 converter
= ucnv_open(NULL
, status
);
56 if(U_FAILURE(*status
)) {
57 ucnv_close(converter
);
66 u_releaseDefaultConverter(UConverter
*converter
)
68 if(gDefaultConverter
== NULL
) {
69 if (converter
!= NULL
) {
70 ucnv_reset(converter
);
74 if(gDefaultConverter
== NULL
) {
75 gDefaultConverter
= converter
;
78 icu::umtx_unlock(NULL
);
81 if(converter
!= NULL
) {
82 ucnv_close(converter
);
87 u_flushDefaultConverter()
89 UConverter
*converter
= NULL
;
91 if (gDefaultConverter
!= NULL
) {
94 /* need to check to make sure it wasn't taken out from under us */
95 if (gDefaultConverter
!= NULL
) {
96 converter
= gDefaultConverter
;
97 gDefaultConverter
= NULL
;
99 icu::umtx_unlock(NULL
);
102 /* if the cache was populated, flush it */
103 if(converter
!= NULL
) {
104 ucnv_close(converter
);
109 /* conversions between char* and UChar* ------------------------------------- */
111 /* maximum string length for u_uastrcpy() and u_austrcpy() implementations */
112 #define MAX_STRLEN 0x0FFFFFFF
115 returns the minimum of (the length of the null-terminated string) and n.
117 static int32_t u_astrnlen(const char *s1
, int32_t n
)
123 while (n
-- && *(s1
++))
131 U_CAPI UChar
* U_EXPORT2
132 u_uastrncpy(UChar
*ucs1
,
136 UChar
*target
= ucs1
;
137 UErrorCode err
= U_ZERO_ERROR
;
138 UConverter
*cnv
= u_getDefaultConverter(&err
);
139 if(U_SUCCESS(err
) && cnv
!= NULL
) {
145 s2
+u_astrnlen(s2
, n
),
149 ucnv_reset(cnv
); /* be good citizens */
150 u_releaseDefaultConverter(cnv
);
151 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
152 *ucs1
= 0; /* failure */
154 if(target
< (ucs1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
155 *target
= 0; /* terminate */
163 U_CAPI UChar
* U_EXPORT2
164 u_uastrcpy(UChar
*ucs1
,
167 UErrorCode err
= U_ZERO_ERROR
;
168 UConverter
*cnv
= u_getDefaultConverter(&err
);
169 if(U_SUCCESS(err
) && cnv
!= NULL
) {
174 (int32_t)uprv_strlen(s2
),
176 u_releaseDefaultConverter(cnv
);
187 returns the minimum of (the length of the null-terminated string) and n.
189 static int32_t u_ustrnlen(const UChar
*ucs1
, int32_t n
)
195 while (n
-- && *(ucs1
++))
203 U_CAPI
char* U_EXPORT2
204 u_austrncpy(char *s1
,
209 UErrorCode err
= U_ZERO_ERROR
;
210 UConverter
*cnv
= u_getDefaultConverter(&err
);
211 if(U_SUCCESS(err
) && cnv
!= NULL
) {
213 ucnv_fromUnicode(cnv
,
217 ucs2
+u_ustrnlen(ucs2
, n
),
221 ucnv_reset(cnv
); /* be good citizens */
222 u_releaseDefaultConverter(cnv
);
223 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
224 *s1
= 0; /* failure */
226 if(target
< (s1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
227 *target
= 0; /* terminate */
235 U_CAPI
char* U_EXPORT2
239 UErrorCode err
= U_ZERO_ERROR
;
240 UConverter
*cnv
= u_getDefaultConverter(&err
);
241 if(U_SUCCESS(err
) && cnv
!= NULL
) {
242 int32_t len
= ucnv_fromUChars(cnv
,
248 u_releaseDefaultConverter(cnv
);