2020-08-11 09:10:23 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998-2014, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: ustr_cnv.cpp
|
|
|
|
* encoding: UTF-8
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2004aug24
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*
|
|
|
|
* Character conversion functions moved here from ustring.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_CONVERSION
|
|
|
|
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "umutex.h"
|
|
|
|
#include "ustr_cnv.h"
|
|
|
|
#include "ucnv_bld.h"
|
|
|
|
|
|
|
|
/* mutexed access to a shared default converter ----------------------------- */
|
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
static UConverter *gDefaultConverter = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
|
|
|
U_CAPI UConverter* U_EXPORT2
|
|
|
|
u_getDefaultConverter(UErrorCode *status)
|
|
|
|
{
|
2023-05-23 00:05:01 +00:00
|
|
|
UConverter *converter = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
if (gDefaultConverter != nullptr) {
|
|
|
|
icu::umtx_lock(nullptr);
|
2020-08-11 09:10:23 +00:00
|
|
|
|
|
|
|
/* need to check to make sure it wasn't taken out from under us */
|
2023-05-23 00:05:01 +00:00
|
|
|
if (gDefaultConverter != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
converter = gDefaultConverter;
|
2023-05-23 00:05:01 +00:00
|
|
|
gDefaultConverter = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
icu::umtx_unlock(nullptr);
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the cache was empty, create a converter */
|
2023-05-23 00:05:01 +00:00
|
|
|
if(converter == nullptr) {
|
|
|
|
converter = ucnv_open(nullptr, status);
|
2020-08-11 09:10:23 +00:00
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
ucnv_close(converter);
|
2023-05-23 00:05:01 +00:00
|
|
|
converter = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return converter;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
u_releaseDefaultConverter(UConverter *converter)
|
|
|
|
{
|
2023-05-23 00:05:01 +00:00
|
|
|
if(gDefaultConverter == nullptr) {
|
|
|
|
if (converter != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
ucnv_reset(converter);
|
|
|
|
}
|
|
|
|
ucnv_enableCleanup();
|
2023-05-23 00:05:01 +00:00
|
|
|
icu::umtx_lock(nullptr);
|
|
|
|
if(gDefaultConverter == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
gDefaultConverter = converter;
|
2023-05-23 00:05:01 +00:00
|
|
|
converter = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
icu::umtx_unlock(nullptr);
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
if(converter != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
ucnv_close(converter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
u_flushDefaultConverter()
|
|
|
|
{
|
2023-05-23 00:05:01 +00:00
|
|
|
UConverter *converter = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
if (gDefaultConverter != nullptr) {
|
|
|
|
icu::umtx_lock(nullptr);
|
2020-08-11 09:10:23 +00:00
|
|
|
|
|
|
|
/* need to check to make sure it wasn't taken out from under us */
|
2023-05-23 00:05:01 +00:00
|
|
|
if (gDefaultConverter != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
converter = gDefaultConverter;
|
2023-05-23 00:05:01 +00:00
|
|
|
gDefaultConverter = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
icu::umtx_unlock(nullptr);
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the cache was populated, flush it */
|
2023-05-23 00:05:01 +00:00
|
|
|
if(converter != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
ucnv_close(converter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
/* conversions between char* and char16_t* ------------------------------------- */
|
2020-08-11 09:10:23 +00:00
|
|
|
|
|
|
|
/* maximum string length for u_uastrcpy() and u_austrcpy() implementations */
|
|
|
|
#define MAX_STRLEN 0x0FFFFFFF
|
|
|
|
|
|
|
|
/*
|
|
|
|
returns the minimum of (the length of the null-terminated string) and n.
|
|
|
|
*/
|
|
|
|
static int32_t u_astrnlen(const char *s1, int32_t n)
|
|
|
|
{
|
|
|
|
int32_t len = 0;
|
|
|
|
|
|
|
|
if (s1)
|
|
|
|
{
|
|
|
|
while (n-- && *(s1++))
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
U_CAPI char16_t* U_EXPORT2
|
|
|
|
u_uastrncpy(char16_t *ucs1,
|
2020-08-11 09:10:23 +00:00
|
|
|
const char *s2,
|
|
|
|
int32_t n)
|
|
|
|
{
|
2023-05-23 00:05:01 +00:00
|
|
|
char16_t *target = ucs1;
|
2020-08-11 09:10:23 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
2023-05-23 00:05:01 +00:00
|
|
|
if(U_SUCCESS(err) && cnv != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
ucnv_reset(cnv);
|
|
|
|
ucnv_toUnicode(cnv,
|
|
|
|
&target,
|
|
|
|
ucs1+n,
|
|
|
|
&s2,
|
|
|
|
s2+u_astrnlen(s2, n),
|
2023-05-23 00:05:01 +00:00
|
|
|
nullptr,
|
2022-10-28 06:11:55 +00:00
|
|
|
true,
|
2020-08-11 09:10:23 +00:00
|
|
|
&err);
|
|
|
|
ucnv_reset(cnv); /* be good citizens */
|
|
|
|
u_releaseDefaultConverter(cnv);
|
|
|
|
if(U_FAILURE(err) && (err != U_BUFFER_OVERFLOW_ERROR) ) {
|
|
|
|
*ucs1 = 0; /* failure */
|
|
|
|
}
|
|
|
|
if(target < (ucs1+n)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
|
|
|
|
*target = 0; /* terminate */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*ucs1 = 0;
|
|
|
|
}
|
|
|
|
return ucs1;
|
|
|
|
}
|
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
U_CAPI char16_t* U_EXPORT2
|
|
|
|
u_uastrcpy(char16_t *ucs1,
|
2020-08-11 09:10:23 +00:00
|
|
|
const char *s2 )
|
|
|
|
{
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
2023-05-23 00:05:01 +00:00
|
|
|
if(U_SUCCESS(err) && cnv != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
ucnv_toUChars(cnv,
|
|
|
|
ucs1,
|
|
|
|
MAX_STRLEN,
|
|
|
|
s2,
|
|
|
|
(int32_t)uprv_strlen(s2),
|
|
|
|
&err);
|
|
|
|
u_releaseDefaultConverter(cnv);
|
|
|
|
if(U_FAILURE(err)) {
|
|
|
|
*ucs1 = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*ucs1 = 0;
|
|
|
|
}
|
|
|
|
return ucs1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
returns the minimum of (the length of the null-terminated string) and n.
|
|
|
|
*/
|
2023-05-23 00:05:01 +00:00
|
|
|
static int32_t u_ustrnlen(const char16_t *ucs1, int32_t n)
|
2020-08-11 09:10:23 +00:00
|
|
|
{
|
|
|
|
int32_t len = 0;
|
|
|
|
|
|
|
|
if (ucs1)
|
|
|
|
{
|
|
|
|
while (n-- && *(ucs1++))
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI char* U_EXPORT2
|
|
|
|
u_austrncpy(char *s1,
|
2023-05-23 00:05:01 +00:00
|
|
|
const char16_t *ucs2,
|
2020-08-11 09:10:23 +00:00
|
|
|
int32_t n)
|
|
|
|
{
|
|
|
|
char *target = s1;
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
2023-05-23 00:05:01 +00:00
|
|
|
if(U_SUCCESS(err) && cnv != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
ucnv_reset(cnv);
|
|
|
|
ucnv_fromUnicode(cnv,
|
|
|
|
&target,
|
|
|
|
s1+n,
|
|
|
|
&ucs2,
|
|
|
|
ucs2+u_ustrnlen(ucs2, n),
|
2023-05-23 00:05:01 +00:00
|
|
|
nullptr,
|
2022-10-28 06:11:55 +00:00
|
|
|
true,
|
2020-08-11 09:10:23 +00:00
|
|
|
&err);
|
|
|
|
ucnv_reset(cnv); /* be good citizens */
|
|
|
|
u_releaseDefaultConverter(cnv);
|
|
|
|
if(U_FAILURE(err) && (err != U_BUFFER_OVERFLOW_ERROR) ) {
|
|
|
|
*s1 = 0; /* failure */
|
|
|
|
}
|
|
|
|
if(target < (s1+n)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
|
|
|
|
*target = 0; /* terminate */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*s1 = 0;
|
|
|
|
}
|
|
|
|
return s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI char* U_EXPORT2
|
|
|
|
u_austrcpy(char *s1,
|
2023-05-23 00:05:01 +00:00
|
|
|
const char16_t *ucs2 )
|
2020-08-11 09:10:23 +00:00
|
|
|
{
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
2023-05-23 00:05:01 +00:00
|
|
|
if(U_SUCCESS(err) && cnv != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
int32_t len = ucnv_fromUChars(cnv,
|
|
|
|
s1,
|
|
|
|
MAX_STRLEN,
|
|
|
|
ucs2,
|
|
|
|
-1,
|
|
|
|
&err);
|
|
|
|
u_releaseDefaultConverter(cnv);
|
|
|
|
s1[len] = 0;
|
|
|
|
} else {
|
|
|
|
*s1 = 0;
|
|
|
|
}
|
|
|
|
return s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|