2001-09-18 03:41:09 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2003, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* File ustrtrns.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 9/10/2001 Ram Creation.
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* u_strTo* and u_strFrom* APIs
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "unicode/putil.h"
|
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "cwchar.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "ustr_imp.h"
|
|
|
|
|
|
|
|
|
2001-12-03 19:55:31 +00:00
|
|
|
U_INLINE static UBool
|
2001-09-18 03:41:09 +00:00
|
|
|
u_growAnyBufferFromStatic(void *context,
|
|
|
|
void **pBuffer, int32_t *pCapacity, int32_t reqCapacity,
|
|
|
|
int32_t length, int32_t size) {
|
|
|
|
|
|
|
|
void *newBuffer=uprv_malloc(reqCapacity*size);
|
|
|
|
if(newBuffer!=NULL) {
|
|
|
|
if(length>0) {
|
|
|
|
uprv_memcpy(newBuffer, *pBuffer, length*size);
|
|
|
|
}
|
|
|
|
*pCapacity=reqCapacity;
|
|
|
|
} else {
|
|
|
|
*pCapacity=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release the old pBuffer if it was not statically allocated */
|
|
|
|
if(*pBuffer!=(void *)context) {
|
|
|
|
uprv_free(*pBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pBuffer=newBuffer;
|
|
|
|
return (UBool)(newBuffer!=NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define _STACK_BUFFER_CAPACITY 1000
|
|
|
|
|
|
|
|
U_CAPI UChar* U_EXPORT2
|
|
|
|
u_strFromUTF32(UChar *dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
2001-10-05 23:13:25 +00:00
|
|
|
const UChar32 *src,
|
2001-09-18 03:41:09 +00:00
|
|
|
int32_t srcLength,
|
2001-10-05 23:13:25 +00:00
|
|
|
UErrorCode *pErrorCode)
|
|
|
|
{
|
2001-09-18 03:41:09 +00:00
|
|
|
int32_t reqLength = 0;
|
|
|
|
uint32_t ch =0;
|
|
|
|
UChar *pDestLimit =dest+destCapacity;
|
|
|
|
UChar *pDest = dest;
|
2001-10-05 23:13:25 +00:00
|
|
|
const uint32_t *pSrc = (const uint32_t *)src;
|
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
/* args check */
|
2001-11-30 05:20:18 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){
|
2001-09-18 03:41:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the source is null terminated */
|
|
|
|
if(srcLength == -1 ){
|
2001-11-30 05:20:18 +00:00
|
|
|
while(((ch=*pSrc)!=0) && (pDest < pDestLimit)){
|
|
|
|
++pSrc;
|
|
|
|
if(ch<=0xFFFF){
|
|
|
|
*(pDest++)=(UChar)ch;
|
|
|
|
}else if(ch<=0x10ffff){
|
|
|
|
*(pDest++)=UTF16_LEAD(ch);
|
|
|
|
if(pDest<pDestLimit){
|
|
|
|
*(pDest++)=UTF16_TRAIL(ch);
|
2001-09-18 03:41:09 +00:00
|
|
|
}else{
|
2001-11-30 05:20:18 +00:00
|
|
|
reqLength++;
|
|
|
|
break;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}else{
|
2001-11-30 05:20:18 +00:00
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while((ch=*pSrc++) != 0){
|
|
|
|
reqLength+=UTF_CHAR_LENGTH(ch);
|
|
|
|
}
|
|
|
|
}else{
|
2001-10-05 23:13:25 +00:00
|
|
|
const uint32_t* pSrcLimit = ((const uint32_t*)pSrc) + srcLength;
|
2001-11-30 22:15:36 +00:00
|
|
|
while((pSrc < pSrcLimit) && (pDest < pDestLimit)){
|
2001-11-30 05:20:18 +00:00
|
|
|
ch = *pSrc++;
|
|
|
|
if(ch<=0xFFFF){
|
|
|
|
*(pDest++)=(UChar)ch;
|
|
|
|
}else if(ch<=0x10FFFF){
|
|
|
|
*(pDest++)=UTF16_LEAD(ch);
|
|
|
|
if(pDest<pDestLimit){
|
|
|
|
*(pDest++)=UTF16_TRAIL(ch);
|
2001-09-18 03:41:09 +00:00
|
|
|
}else{
|
2001-11-30 05:20:18 +00:00
|
|
|
reqLength++;
|
|
|
|
break;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}else{
|
2001-11-30 05:20:18 +00:00
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while(pSrc <pSrcLimit){
|
|
|
|
ch = *pSrc++;
|
|
|
|
reqLength+=UTF_CHAR_LENGTH(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-30 22:15:36 +00:00
|
|
|
reqLength += pDest - dest;
|
2001-09-18 03:41:09 +00:00
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength = reqLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the buffer */
|
|
|
|
u_terminateUChars(dest,destCapacity,reqLength,pErrorCode);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-05 23:13:25 +00:00
|
|
|
U_CAPI UChar32* U_EXPORT2
|
|
|
|
u_strToUTF32(UChar32 *dest,
|
2001-09-18 03:41:09 +00:00
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
|
|
|
const UChar *src,
|
|
|
|
int32_t srcLength,
|
2001-10-05 23:13:25 +00:00
|
|
|
UErrorCode *pErrorCode)
|
|
|
|
{
|
2001-09-18 03:41:09 +00:00
|
|
|
const UChar* pSrc = src;
|
2001-11-30 05:20:18 +00:00
|
|
|
const UChar* pSrcLimit;
|
2001-09-18 03:41:09 +00:00
|
|
|
int32_t reqLength=0;
|
|
|
|
uint32_t ch=0;
|
2001-10-05 23:13:25 +00:00
|
|
|
uint32_t *pDest = (uint32_t *)dest;
|
|
|
|
uint32_t *pDestLimit = pDest + destCapacity;
|
2001-11-30 05:20:18 +00:00
|
|
|
UChar ch2=0;
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
/* args check */
|
2001-11-30 05:20:18 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){
|
2001-09-18 03:41:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
if((srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
if(srcLength==-1) {
|
|
|
|
while((ch=*pSrc)!=0 && pDest!=pDestLimit) {
|
|
|
|
++pSrc;
|
|
|
|
/*need not check for NUL because NUL fails UTF_IS_TRAIL() anyway*/
|
|
|
|
if(UTF_IS_LEAD(ch) && UTF_IS_TRAIL(ch2=*pSrc)) {
|
|
|
|
++pSrc;
|
|
|
|
ch=UTF16_GET_PAIR_VALUE(ch, ch2);
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
*(pDest++)= ch;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
while((ch=*pSrc++)!=0) {
|
|
|
|
if(UTF_IS_LEAD(ch) && UTF_IS_TRAIL(ch2=*pSrc)) {
|
|
|
|
++pSrc;
|
|
|
|
}
|
|
|
|
++reqLength;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pSrcLimit = pSrc+srcLength;
|
|
|
|
while(pSrc<pSrcLimit && pDest<pDestLimit) {
|
|
|
|
ch=*pSrc++;
|
|
|
|
if(UTF_IS_LEAD(ch) && pSrc<pSrcLimit && UTF_IS_TRAIL(ch2=*pSrc)) {
|
|
|
|
++pSrc;
|
|
|
|
ch=UTF16_GET_PAIR_VALUE(ch, ch2);
|
|
|
|
}
|
|
|
|
*(pDest++)= ch;
|
|
|
|
}
|
|
|
|
while(pSrc!=pSrcLimit) {
|
|
|
|
ch=*pSrc++;
|
|
|
|
if(UTF_IS_LEAD(ch) && pSrc<pSrcLimit && UTF_IS_TRAIL(ch2=*pSrc)) {
|
|
|
|
++pSrc;
|
|
|
|
}
|
|
|
|
++reqLength;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-05 23:13:25 +00:00
|
|
|
reqLength+=(pDest - (uint32_t *)dest);
|
2001-09-18 03:41:09 +00:00
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength = reqLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the buffer */
|
|
|
|
u_terminateUChar32s(dest,destCapacity,reqLength,pErrorCode);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UChar* U_EXPORT2
|
|
|
|
u_strFromUTF8(UChar *dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
2001-09-22 03:00:46 +00:00
|
|
|
const char* src,
|
2001-09-18 03:41:09 +00:00
|
|
|
int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode){
|
|
|
|
|
|
|
|
UChar *pDest = dest;
|
|
|
|
UChar *pDestLimit = dest+destCapacity;
|
|
|
|
UChar32 ch=0;
|
|
|
|
int32_t index = 0;
|
|
|
|
int32_t reqLength = 0;
|
2001-09-22 03:00:46 +00:00
|
|
|
uint8_t* pSrc = (uint8_t*) src;
|
2002-08-03 00:54:39 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
/* args check */
|
2001-11-30 05:20:18 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){
|
2001-09-18 03:41:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
if((srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(srcLength == -1){
|
|
|
|
srcLength = uprv_strlen((char*)pSrc);
|
|
|
|
}
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
while((index < srcLength)&&(pDest<pDestLimit)){
|
|
|
|
ch = pSrc[index++];
|
|
|
|
if(ch <=0x7f){
|
|
|
|
*pDest++=(UChar)ch;
|
|
|
|
}else{
|
2002-09-30 04:00:17 +00:00
|
|
|
ch=utf8_nextCharSafeBody(pSrc, &index, srcLength, ch, -1);
|
|
|
|
if(ch<0){
|
2002-08-03 00:54:39 +00:00
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
|
|
|
}else if(ch<=0xFFFF){
|
2001-11-30 05:20:18 +00:00
|
|
|
*(pDest++)=(UChar)ch;
|
2002-08-03 00:54:39 +00:00
|
|
|
}else{
|
2001-11-30 05:20:18 +00:00
|
|
|
*(pDest++)=UTF16_LEAD(ch);
|
|
|
|
if(pDest<pDestLimit){
|
|
|
|
*(pDest++)=UTF16_TRAIL(ch);
|
2001-09-18 03:41:09 +00:00
|
|
|
}else{
|
2001-11-30 05:20:18 +00:00
|
|
|
reqLength++;
|
|
|
|
break;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* donot fill the dest buffer just count the UChars needed */
|
|
|
|
while(index < srcLength){
|
|
|
|
ch = pSrc[index++];
|
|
|
|
if(ch <= 0x7f){
|
|
|
|
reqLength++;
|
|
|
|
}else{
|
2002-09-30 04:00:17 +00:00
|
|
|
ch=utf8_nextCharSafeBody(pSrc, &index, srcLength, ch, -1);
|
|
|
|
if(ch<0){
|
2002-08-03 00:54:39 +00:00
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-18 03:41:09 +00:00
|
|
|
reqLength+=UTF_CHAR_LENGTH(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reqLength+=(pDest - dest);
|
|
|
|
|
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength = reqLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the buffer */
|
|
|
|
u_terminateUChars(dest,destCapacity,reqLength,pErrorCode);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2001-11-30 22:15:36 +00:00
|
|
|
|
2001-12-03 19:55:31 +00:00
|
|
|
U_INLINE static uint8_t *
|
2001-11-30 22:15:36 +00:00
|
|
|
_appendUTF8(uint8_t *pDest, UChar32 c) {
|
|
|
|
/* c<=0x7f is handled by the caller, here it is 0x80<=c<=0x10ffff */
|
|
|
|
if((c)<=0x7ff) {
|
2001-11-30 05:20:18 +00:00
|
|
|
*pDest++=(uint8_t)((c>>6)|0xc0);
|
|
|
|
*pDest++=(uint8_t)((c&0x3f)|0x80);
|
|
|
|
} else if((uint32_t)(c)<=0xffff) {
|
|
|
|
*pDest++=(uint8_t)((c>>12)|0xe0);
|
|
|
|
*pDest++=(uint8_t)(((c>>6)&0x3f)|0x80);
|
|
|
|
*pDest++=(uint8_t)(((c)&0x3f)|0x80);
|
2001-11-30 22:15:36 +00:00
|
|
|
} else /* if((uint32_t)(c)<=0x10ffff) */ {
|
2001-11-30 05:20:18 +00:00
|
|
|
*pDest++=(uint8_t)(((c)>>18)|0xf0);
|
|
|
|
*pDest++=(uint8_t)((((c)>>12)&0x3f)|0x80);
|
|
|
|
*pDest++=(uint8_t)((((c)>>6)&0x3f)|0x80);
|
|
|
|
*pDest++=(uint8_t)(((c)&0x3f)|0x80);
|
|
|
|
}
|
2001-11-30 22:15:36 +00:00
|
|
|
return pDest;
|
2001-11-30 05:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-22 03:00:46 +00:00
|
|
|
U_CAPI char* U_EXPORT2
|
2001-09-22 03:12:14 +00:00
|
|
|
u_strToUTF8(char *dest,
|
2001-09-18 03:41:09 +00:00
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
|
|
|
const UChar *pSrc,
|
|
|
|
int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode){
|
|
|
|
|
|
|
|
int32_t reqLength=0;
|
|
|
|
const UChar *pSrcLimit;
|
2001-11-30 05:20:18 +00:00
|
|
|
uint32_t ch=0,ch2=0;
|
|
|
|
uint8_t *pDest = (uint8_t *)dest;
|
|
|
|
uint8_t *pDestLimit = pDest + destCapacity;
|
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
/* args check */
|
2001-11-30 05:20:18 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){
|
2001-09-18 03:41:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
if((srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
if(srcLength==-1) {
|
|
|
|
while((ch=*pSrc)!=0 && pDest!=pDestLimit) {
|
|
|
|
++pSrc;
|
2001-11-30 22:15:36 +00:00
|
|
|
if(ch <= 0x7f) {
|
|
|
|
*pDest++ = (char)ch;
|
|
|
|
++reqLength;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
/*need not check for NUL because NUL fails UTF_IS_TRAIL() anyway*/
|
2002-07-13 00:46:18 +00:00
|
|
|
if(UTF_IS_SURROGATE(ch)) {
|
|
|
|
if(UTF_IS_SURROGATE_FIRST(ch) && UTF_IS_TRAIL(ch2=*pSrc)) {
|
|
|
|
++pSrc;
|
|
|
|
ch=UTF16_GET_PAIR_VALUE(ch, ch2);
|
|
|
|
} else {
|
|
|
|
/* Unicode 3.2 forbids surrogate code points in UTF-8 */
|
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
}
|
|
|
|
reqLength += UTF8_CHAR_LENGTH(ch);
|
|
|
|
/* do we have enough room in destination? */
|
|
|
|
if(destCapacity< reqLength){
|
2001-09-18 03:41:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
/* convert and append*/
|
2001-11-30 22:15:36 +00:00
|
|
|
pDest=_appendUTF8(pDest, ch);
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
while((ch=*pSrc++)!=0) {
|
2002-07-13 00:46:18 +00:00
|
|
|
if(ch<=0x7f) {
|
|
|
|
++reqLength;
|
|
|
|
} else if(ch<=0x7ff) {
|
|
|
|
reqLength+=2;
|
|
|
|
} else if(!UTF_IS_SURROGATE(ch)) {
|
|
|
|
reqLength+=3;
|
|
|
|
} else if(UTF_IS_SURROGATE_FIRST(ch) && UTF_IS_TRAIL(ch2=*pSrc)) {
|
2001-11-30 05:20:18 +00:00
|
|
|
++pSrc;
|
|
|
|
reqLength+=4;
|
|
|
|
} else {
|
2002-07-13 00:46:18 +00:00
|
|
|
/* Unicode 3.2 forbids surrogate code points in UTF-8 */
|
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
2001-11-30 05:20:18 +00:00
|
|
|
}
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
} else {
|
|
|
|
pSrcLimit = pSrc+srcLength;
|
|
|
|
while(pSrc<pSrcLimit && pDest<pDestLimit) {
|
|
|
|
ch=*pSrc++;
|
2001-11-30 22:15:36 +00:00
|
|
|
if(ch <= 0x7f) {
|
|
|
|
*pDest++ = (char)ch;
|
|
|
|
++reqLength;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-07-13 00:46:18 +00:00
|
|
|
if(UTF_IS_SURROGATE(ch)) {
|
|
|
|
if(UTF_IS_SURROGATE_FIRST(ch) && pSrc<pSrcLimit && UTF_IS_TRAIL(ch2=*pSrc)) {
|
|
|
|
++pSrc;
|
|
|
|
ch=UTF16_GET_PAIR_VALUE(ch, ch2);
|
|
|
|
} else {
|
|
|
|
/* Unicode 3.2 forbids surrogate code points in UTF-8 */
|
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
}
|
|
|
|
reqLength += UTF8_CHAR_LENGTH(ch);
|
|
|
|
/* do we have enough room in destination? */
|
|
|
|
if(destCapacity< reqLength){
|
2001-09-18 03:41:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
/* convert and append*/
|
2001-11-30 22:15:36 +00:00
|
|
|
pDest=_appendUTF8(pDest, ch);
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
while(pSrc<pSrcLimit) {
|
|
|
|
ch=*pSrc++;
|
2002-07-13 00:46:18 +00:00
|
|
|
if(ch<=0x7f) {
|
|
|
|
++reqLength;
|
|
|
|
} else if(ch<=0x7ff) {
|
|
|
|
reqLength+=2;
|
|
|
|
} else if(!UTF_IS_SURROGATE(ch)) {
|
|
|
|
reqLength+=3;
|
|
|
|
} else if(UTF_IS_SURROGATE_FIRST(ch) && pSrc<pSrcLimit && UTF_IS_TRAIL(ch2=*pSrc)) {
|
2001-11-30 05:20:18 +00:00
|
|
|
++pSrc;
|
|
|
|
reqLength+=4;
|
|
|
|
} else {
|
2002-07-13 00:46:18 +00:00
|
|
|
/* Unicode 3.2 forbids surrogate code points in UTF-8 */
|
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
return NULL;
|
2001-11-30 05:20:18 +00:00
|
|
|
}
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength = reqLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the buffer */
|
|
|
|
u_terminateChars((char*)dest,destCapacity,reqLength,pErrorCode);
|
|
|
|
|
2001-09-22 03:00:46 +00:00
|
|
|
return (char*)dest;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 04:22:32 +00:00
|
|
|
#if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32)
|
2001-09-18 03:41:09 +00:00
|
|
|
/* helper function */
|
|
|
|
static wchar_t*
|
|
|
|
_strToWCS(wchar_t *dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
|
|
|
const UChar *src,
|
|
|
|
int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode){
|
|
|
|
|
|
|
|
char stackBuffer [_STACK_BUFFER_CAPACITY];
|
|
|
|
char* tempBuf = stackBuffer;
|
|
|
|
int32_t tempBufCapacity = _STACK_BUFFER_CAPACITY;
|
|
|
|
char* tempBufLimit = stackBuffer + tempBufCapacity;
|
|
|
|
UConverter* conv = NULL;
|
|
|
|
char* saveBuf = tempBuf;
|
|
|
|
wchar_t* intTarget=NULL;
|
2001-11-30 05:20:18 +00:00
|
|
|
int32_t intTargetCapacity=0;
|
2001-09-18 03:41:09 +00:00
|
|
|
int count=0,retVal=0;
|
|
|
|
|
|
|
|
const UChar *pSrcLimit =NULL;
|
|
|
|
const UChar *pSrc = src;
|
2001-12-03 19:55:31 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
conv = u_getDefaultConverter(pErrorCode);
|
2001-11-30 05:20:18 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*pErrorCode)){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-30 22:15:36 +00:00
|
|
|
if(srcLength == -1){
|
|
|
|
srcLength = u_strlen(pSrc);
|
|
|
|
}
|
2001-12-03 19:55:31 +00:00
|
|
|
|
|
|
|
pSrcLimit = pSrc + srcLength;
|
2001-11-30 22:15:36 +00:00
|
|
|
|
|
|
|
for(;;) {
|
2001-11-30 05:20:18 +00:00
|
|
|
/* reset the error state */
|
|
|
|
*pErrorCode = U_ZERO_ERROR;
|
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
/* convert to chars using default converter */
|
2001-11-30 05:20:18 +00:00
|
|
|
ucnv_fromUnicode(conv,&tempBuf,tempBufLimit,&pSrc,pSrcLimit,NULL,(UBool)(pSrc==pSrcLimit),pErrorCode);
|
|
|
|
count =(tempBuf - saveBuf);
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
/* This should rarely occur */
|
|
|
|
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
tempBuf = saveBuf;
|
2001-11-30 05:20:18 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
/* we dont have enough room on the stack grow the buffer */
|
2001-11-30 05:20:18 +00:00
|
|
|
if(!u_growAnyBufferFromStatic(stackBuffer,(void**) &tempBuf, &tempBufCapacity,
|
|
|
|
(2*(pSrcLimit-pSrc)+100), count,sizeof(char))){
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
saveBuf = tempBuf;
|
|
|
|
tempBufLimit = tempBuf + tempBufCapacity;
|
2001-11-30 05:20:18 +00:00
|
|
|
tempBuf = tempBuf + count;
|
2001-09-18 03:41:09 +00:00
|
|
|
|
2001-11-30 22:15:36 +00:00
|
|
|
} else {
|
|
|
|
break;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
2001-11-30 22:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(U_FAILURE(*pErrorCode)){
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2001-09-18 03:41:09 +00:00
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
/* done with conversion null terminate the char buffer */
|
|
|
|
if(count>=tempBufCapacity){
|
|
|
|
tempBuf = saveBuf;
|
|
|
|
/* we dont have enough room on the stack grow the buffer */
|
|
|
|
if(!u_growAnyBufferFromStatic(stackBuffer,(void**) &tempBuf, &tempBufCapacity,
|
|
|
|
tempBufCapacity-count+1, count,sizeof(char))){
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
saveBuf = tempBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveBuf[count]=0;
|
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
/* allocate more space than required
|
|
|
|
* here we assume that every char requires
|
|
|
|
* no more than 2 wchar_ts
|
|
|
|
*/
|
2001-11-30 05:20:18 +00:00
|
|
|
intTargetCapacity = (count*2+1) /*for null termination */;
|
|
|
|
intTarget = (wchar_t*)uprv_malloc( intTargetCapacity * sizeof(wchar_t) );
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
if(intTarget){
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
int32_t nulLen = 0;
|
|
|
|
int32_t remaining = intTargetCapacity;
|
2001-09-18 03:41:09 +00:00
|
|
|
wchar_t* pIntTarget=intTarget;
|
|
|
|
tempBuf = saveBuf;
|
|
|
|
|
|
|
|
/* now convert the mbs to wcs */
|
|
|
|
for(;;){
|
|
|
|
|
|
|
|
/* we can call the system API since we are sure that
|
|
|
|
* there is atleast 1 null in the input
|
|
|
|
*/
|
2001-11-30 05:20:18 +00:00
|
|
|
retVal = uprv_mbstowcs(pIntTarget,(tempBuf+nulLen),remaining);
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
if(retVal==-1){
|
|
|
|
*pErrorCode = U_INVALID_CHAR_FOUND;
|
|
|
|
break;
|
2001-11-30 05:20:18 +00:00
|
|
|
}else if(retVal== remaining){/* should never occur */
|
|
|
|
int numWritten = (pIntTarget-intTarget);
|
2001-11-30 20:11:33 +00:00
|
|
|
u_growAnyBufferFromStatic(NULL,(void**) &intTarget,
|
2001-11-30 05:20:18 +00:00
|
|
|
&intTargetCapacity,
|
|
|
|
intTargetCapacity*2,
|
|
|
|
numWritten,
|
|
|
|
sizeof(wchar_t));
|
|
|
|
pIntTarget = intTarget;
|
|
|
|
remaining=intTargetCapacity;
|
|
|
|
|
|
|
|
if(nulLen!=count){ /*there are embedded nulls*/
|
|
|
|
pIntTarget+=numWritten;
|
|
|
|
remaining-=numWritten;
|
|
|
|
}
|
2001-09-18 03:41:09 +00:00
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
}else{
|
|
|
|
/*scan for nulls */
|
|
|
|
/* we donot check for limit since tempBuf is null terminated */
|
|
|
|
while(tempBuf[nulLen++] != 0){
|
|
|
|
}
|
|
|
|
pIntTarget = pIntTarget + retVal+1;
|
|
|
|
remaining -=(retVal+1);
|
2001-09-18 03:41:09 +00:00
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
/* check if we have reached the source limit*/
|
|
|
|
if(nulLen>=(count)){
|
|
|
|
break;
|
|
|
|
}
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
count = (int32_t)(pIntTarget-intTarget);
|
|
|
|
|
2001-11-30 22:15:36 +00:00
|
|
|
if(0 < count && count <= destCapacity){
|
2001-09-18 03:41:09 +00:00
|
|
|
uprv_memcpy(dest,intTarget,count*sizeof(wchar_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free the allocated memory */
|
|
|
|
uprv_free(intTarget);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
cleanup:
|
2001-09-18 03:41:09 +00:00
|
|
|
/* are we still using stack buffer */
|
|
|
|
if(stackBuffer != saveBuf){
|
|
|
|
uprv_free(saveBuf);
|
|
|
|
}
|
|
|
|
u_terminateWChars(dest,destCapacity,count,pErrorCode);
|
|
|
|
|
|
|
|
u_releaseDefaultConverter(conv);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2002-02-07 04:22:32 +00:00
|
|
|
#endif
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
U_CAPI wchar_t* U_EXPORT2
|
|
|
|
u_strToWCS(wchar_t *dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
|
|
|
const UChar *src,
|
|
|
|
int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode){
|
|
|
|
|
|
|
|
/* args check */
|
2001-11-30 05:20:18 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){
|
2001-09-18 03:41:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
if((srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef U_WCHAR_IS_UTF16
|
|
|
|
/* wchar_t is UTF-16 just do a memcpy */
|
2001-11-30 22:15:36 +00:00
|
|
|
if(srcLength == -1){
|
|
|
|
srcLength = u_strlen(src);
|
|
|
|
}
|
|
|
|
if(0 < srcLength && srcLength <= destCapacity){
|
2001-09-18 03:41:09 +00:00
|
|
|
uprv_memcpy(dest,src,srcLength*U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength = srcLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_terminateUChars(dest,destCapacity,srcLength,pErrorCode);
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
return dest;
|
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
#elif defined U_WCHAR_IS_UTF32
|
|
|
|
|
|
|
|
return u_strToUTF32(dest,destCapacity,pDestLength,src,srcLength,pErrorCode);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return _strToWCS(dest,destCapacity,pDestLength,src,srcLength, pErrorCode);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-02-07 04:22:32 +00:00
|
|
|
#if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32)
|
2001-09-18 03:41:09 +00:00
|
|
|
/* helper function */
|
|
|
|
static UChar*
|
|
|
|
_strFromWCS( UChar *dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
|
|
|
const wchar_t *src,
|
|
|
|
int32_t srcLength,
|
|
|
|
UErrorCode *pErrorCode){
|
|
|
|
|
|
|
|
int32_t retVal =0, count =0 ;
|
|
|
|
UConverter* conv = NULL;
|
|
|
|
UChar* pTarget = NULL;
|
|
|
|
UChar* pTargetLimit = NULL;
|
2001-11-30 05:20:18 +00:00
|
|
|
UChar* target = NULL;
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
UChar uStack [_STACK_BUFFER_CAPACITY];
|
|
|
|
|
|
|
|
wchar_t wStack[_STACK_BUFFER_CAPACITY];
|
|
|
|
wchar_t* pWStack = wStack;
|
|
|
|
|
|
|
|
|
|
|
|
char cStack[_STACK_BUFFER_CAPACITY];
|
|
|
|
int32_t cStackCap = _STACK_BUFFER_CAPACITY;
|
|
|
|
char* pCSrc=cStack;
|
|
|
|
char* pCSave=pCSrc;
|
|
|
|
char* pCSrcLimit=NULL;
|
|
|
|
|
|
|
|
const wchar_t* pSrc = src;
|
|
|
|
const wchar_t* pSrcLimit = NULL;
|
|
|
|
|
|
|
|
if(srcLength ==-1){
|
|
|
|
/* if the wchar_t source is null terminated we can safely
|
|
|
|
* assume that there are no embedded nulls, this is a fast
|
|
|
|
* path for null terminated strings.
|
|
|
|
*/
|
|
|
|
for(;;){
|
|
|
|
/* convert wchars to chars */
|
|
|
|
retVal = uprv_wcstombs(pCSrc,src, cStackCap);
|
|
|
|
|
|
|
|
if(retVal == -1){
|
|
|
|
*pErrorCode = U_ILLEGAL_CHAR_FOUND;
|
|
|
|
goto cleanup;
|
|
|
|
}else if(retVal == cStackCap){
|
|
|
|
/* Should rarely occur */
|
2001-09-22 01:54:21 +00:00
|
|
|
u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap,
|
2001-09-18 03:41:09 +00:00
|
|
|
cStackCap*2,0,sizeof(char));
|
|
|
|
pCSave = pCSrc;
|
|
|
|
}else{
|
|
|
|
/* converted every thing */
|
|
|
|
pCSrc = pCSrc+retVal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
/* here the source is not null terminated
|
|
|
|
* so it may have nulls embeded and we need to
|
|
|
|
* do some extra processing
|
|
|
|
*/
|
|
|
|
int32_t remaining =cStackCap;
|
|
|
|
|
|
|
|
pSrcLimit = src + srcLength;
|
|
|
|
|
|
|
|
for(;;){
|
2001-11-30 05:20:18 +00:00
|
|
|
register int32_t nulLen = 0;
|
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
/* find nulls in the string */
|
2001-11-30 05:20:18 +00:00
|
|
|
while(nulLen<srcLength && pSrc[nulLen++]!=0){
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
if((pSrc+nulLen) < pSrcLimit){
|
2001-09-18 03:41:09 +00:00
|
|
|
/* check if we have enough room in pCSrc */
|
|
|
|
if(remaining < (nulLen * MB_CUR_MAX)){
|
|
|
|
/* should rarely occur */
|
|
|
|
int32_t len = (pCSrc-pCSave);
|
|
|
|
pCSrc = pCSave;
|
|
|
|
/* we do not have enough room so grow the buffer*/
|
2001-09-22 01:54:21 +00:00
|
|
|
u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap,
|
2001-09-18 03:41:09 +00:00
|
|
|
2*cStackCap+(nulLen*MB_CUR_MAX),len,sizeof(char));
|
|
|
|
|
|
|
|
pCSave = pCSrc;
|
|
|
|
pCSrc = pCSave+len;
|
|
|
|
remaining = cStackCap-(pCSrc - pCSave);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have found a null so convert the
|
|
|
|
* chunk from begining of non-null char to null
|
|
|
|
*/
|
|
|
|
retVal = uprv_wcstombs(pCSrc,pSrc,remaining);
|
|
|
|
|
|
|
|
if(retVal==-1){
|
|
|
|
/* an error occurred bail out */
|
|
|
|
*pErrorCode = U_ILLEGAL_CHAR_FOUND;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
pCSrc += retVal+1 /* already null terminated */;
|
2001-09-18 03:41:09 +00:00
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
pSrc += nulLen; /* skip past the null */
|
|
|
|
srcLength-=nulLen; /* decrement the srcLength */
|
2001-09-18 03:41:09 +00:00
|
|
|
remaining -= (pCSrc-pCSave);
|
|
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
/* the source is not null terminated and we are
|
|
|
|
* end of source so we copy the source to a temp buffer
|
|
|
|
* null terminate it and convert wchar_ts to chars
|
|
|
|
*/
|
|
|
|
if(nulLen > _STACK_BUFFER_CAPACITY){
|
|
|
|
/* Should rarely occcur */
|
|
|
|
/* allocate new buffer buffer */
|
|
|
|
pWStack =(wchar_t*) uprv_malloc(sizeof(wchar_t) * nulLen);
|
2001-11-30 05:20:18 +00:00
|
|
|
if(pWStack==NULL){
|
|
|
|
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(nulLen>0){
|
|
|
|
/* copy the contents to tempStack */
|
|
|
|
uprv_memcpy(pWStack,pSrc,nulLen*sizeof(wchar_t));
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* null terminate the tempBuffer */
|
|
|
|
pWStack[nulLen] =0 ;
|
|
|
|
|
|
|
|
if(remaining < (nulLen * MB_CUR_MAX)){
|
|
|
|
/* Should rarely occur */
|
|
|
|
int32_t len = (pCSrc-pCSave);
|
|
|
|
pCSrc = pCSave;
|
|
|
|
/* we do not have enough room so grow the buffer*/
|
2001-09-22 01:54:21 +00:00
|
|
|
u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap,
|
2001-09-18 03:41:09 +00:00
|
|
|
cStackCap+(nulLen*MB_CUR_MAX),len,sizeof(char));
|
|
|
|
|
|
|
|
pCSave = pCSrc;
|
|
|
|
pCSrc = pCSave+len;
|
|
|
|
remaining = cStackCap-(pCSrc - pCSave);
|
|
|
|
}
|
|
|
|
/* convert to chars */
|
|
|
|
retVal = uprv_wcstombs(pCSrc,pWStack,remaining);
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
pCSrc += retVal +1;
|
|
|
|
pSrc += nulLen;
|
|
|
|
srcLength-=nulLen; /* decrement the srcLength */
|
2001-09-18 03:41:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK..now we have converted from wchar_ts to chars now
|
|
|
|
* convert chars to UChars
|
|
|
|
*/
|
|
|
|
pCSrcLimit = pCSrc;
|
|
|
|
pCSrc = pCSave;
|
2001-11-30 05:20:18 +00:00
|
|
|
pTarget = target= dest;
|
2001-09-18 03:41:09 +00:00
|
|
|
pTargetLimit = dest + destCapacity;
|
|
|
|
|
|
|
|
conv= u_getDefaultConverter(pErrorCode);
|
|
|
|
|
2001-11-30 05:20:18 +00:00
|
|
|
if(U_FAILURE(*pErrorCode)|| conv==NULL){
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2001-11-30 22:15:36 +00:00
|
|
|
for(;;) {
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
*pErrorCode = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
/* convert to stack buffer*/
|
2001-11-30 05:20:18 +00:00
|
|
|
ucnv_toUnicode(conv,&pTarget,pTargetLimit,(const char**)&pCSrc,pCSrcLimit,NULL,(UBool)(pCSrc==pCSrcLimit),pErrorCode);
|
2002-07-09 12:47:45 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
/* increment count to number written to stack */
|
2001-11-30 05:20:18 +00:00
|
|
|
count+= pTarget - target;
|
|
|
|
|
|
|
|
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR){
|
|
|
|
target = uStack;
|
|
|
|
pTarget = uStack;
|
|
|
|
pTargetLimit = uStack + _STACK_BUFFER_CAPACITY;
|
2001-11-30 22:15:36 +00:00
|
|
|
} else {
|
|
|
|
break;
|
2001-11-30 05:20:18 +00:00
|
|
|
}
|
|
|
|
|
2001-11-30 22:15:36 +00:00
|
|
|
}
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength =count;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_terminateUChars(dest,destCapacity,count,pErrorCode);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
if(cStack != pCSave){
|
|
|
|
uprv_free(pCSave);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wStack != pWStack){
|
|
|
|
uprv_free(pWStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_releaseDefaultConverter(conv);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2002-02-07 04:22:32 +00:00
|
|
|
#endif
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
U_CAPI UChar* U_EXPORT2
|
|
|
|
u_strFromWCS(UChar *dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *pDestLength,
|
|
|
|
const wchar_t *src,
|
|
|
|
int32_t srcLength,
|
2001-10-05 23:13:25 +00:00
|
|
|
UErrorCode *pErrorCode)
|
|
|
|
{
|
2001-09-18 03:41:09 +00:00
|
|
|
|
|
|
|
/* args check */
|
2001-11-30 05:20:18 +00:00
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){
|
2001-09-18 03:41:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-30 05:20:18 +00:00
|
|
|
|
2001-09-18 03:41:09 +00:00
|
|
|
if((srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
|
|
|
|
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef U_WCHAR_IS_UTF16
|
2001-11-30 22:15:36 +00:00
|
|
|
/* wchar_t is UTF-16 just do a memcpy */
|
|
|
|
if(srcLength == -1){
|
|
|
|
srcLength = u_strlen(src);
|
|
|
|
}
|
|
|
|
if(0 < srcLength && srcLength <= destCapacity){
|
|
|
|
uprv_memcpy(dest,src,srcLength*U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
if(pDestLength){
|
|
|
|
*pDestLength = srcLength;
|
2001-09-18 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u_terminateUChars(dest,destCapacity,srcLength,pErrorCode);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
|
|
|
|
#elif defined U_WCHAR_IS_UTF32
|
|
|
|
|
|
|
|
return u_strFromUTF32(dest,destCapacity,pDestLength,src,srcLength,pErrorCode);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return _strFromWCS(dest,destCapacity,pDestLength,src,srcLength,pErrorCode);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|