ICU-5882 use ucnv_getMinCharSize() to allocate space for trailing null character.
X-SVN-Rev: 22522
This commit is contained in:
parent
be37c4a5f1
commit
4d34e9654f
@ -1,6 +1,6 @@
|
||||
/*
|
||||
**********************************************************************
|
||||
* Copyright (C) 2005-2006, International Business Machines
|
||||
* Copyright (C) 2005-2007, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
*/
|
||||
@ -108,14 +108,22 @@ static UnicodeString *split(const UnicodeString &src, UChar ch, int32_t &splits)
|
||||
|
||||
static char *extractBytes(const UnicodeString &source, const char *codepage, int32_t &length)
|
||||
{
|
||||
int32_t sLength = source.length();
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UConverter *cnv = ucnv_open(codepage, &status);
|
||||
char *bytes = NULL;
|
||||
|
||||
length = source.extract(0, sLength, NULL, codepage);
|
||||
length = source.extract(NULL, 0, cnv, status);
|
||||
|
||||
if (length > 0) {
|
||||
bytes = NEW_ARRAY(char, length + 1);
|
||||
source.extract(0, sLength, bytes, codepage);
|
||||
if (status == U_BUFFER_OVERFLOW_ERROR) {
|
||||
status = U_ZERO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
if (U_SUCCESS(status) && length > 0) {
|
||||
int32_t capacity = length + ucnv_getMinCharSize(cnv);
|
||||
|
||||
bytes = NEW_ARRAY(char, capacity);
|
||||
source.extract(bytes, capacity, cnv, status);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
|
Loading…
Reference in New Issue
Block a user