ICU-11129 simplify string buffer handling

X-SVN-Rev: 36461
This commit is contained in:
Markus Scherer 2014-09-11 15:34:38 +00:00
parent 04df1aa651
commit 459c88cc14

View File

@ -20,7 +20,7 @@
#include "unicode/usprep.h"
#include "unicode/unorm.h"
#include "unicode/normalizer2.h"
#include "unicode/ustring.h"
#include "unicode/uchar.h"
#include "unicode/uversion.h"
@ -33,6 +33,7 @@
#include "udataswp.h"
#include "ucln_cmn.h"
#include "ubidi_props.h"
#include "uprops.h"
U_NAMESPACE_USE
@ -502,8 +503,7 @@ getValues(uint16_t trieWord, int16_t& value, UBool& isIndex){
return type;
}
// TODO: change to writing to UnicodeString not UChar *
static int32_t
usprep_map( const UStringPrepProfile* profile,
const UChar* src, int32_t srcLength,
@ -598,20 +598,7 @@ usprep_map( const UStringPrepProfile* profile,
return u_terminateUChars(dest, destCapacity, destIndex, status);
}
static int32_t
usprep_normalize( const UChar* src, int32_t srcLength,
UChar* dest, int32_t destCapacity,
UErrorCode* status ){
return unorm_normalize(
src, srcLength,
UNORM_NFKC, UNORM_UNICODE_3_2,
dest, destCapacity,
status);
}
/*
/*
1) Map -- For each character in the input, check if it has a mapping
and, if so, replace it with its mapping.
@ -649,10 +636,6 @@ usprep_normalize( const UChar* src, int32_t srcLength,
character MUST be the first character of the string, and a
RandALCat character MUST be the last character of the string.
*/
#define MAX_STACK_BUFFER_SIZE 300
U_CAPI int32_t U_EXPORT2
usprep_prepare( const UStringPrepProfile* profile,
const UChar* src, int32_t srcLength,
@ -662,100 +645,91 @@ usprep_prepare( const UStringPrepProfile* profile,
UErrorCode* status ){
// check error status
if(status == NULL || U_FAILURE(*status)){
if(U_FAILURE(*status)){
return 0;
}
//check arguments
if(profile==NULL || src==NULL || srcLength<-1 || (dest==NULL && destCapacity!=0)) {
if(profile==NULL ||
(src==NULL ? srcLength!=0 : srcLength<-1) ||
(dest==NULL ? destCapacity!=0 : destCapacity<0)) {
*status=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UChar b1Stack[MAX_STACK_BUFFER_SIZE], b2Stack[MAX_STACK_BUFFER_SIZE];
UChar *b1 = b1Stack, *b2 = b2Stack;
int32_t b1Len, b2Len=0,
b1Capacity = MAX_STACK_BUFFER_SIZE ,
b2Capacity = MAX_STACK_BUFFER_SIZE;
uint16_t result;
int32_t b2Index = 0;
UCharDirection direction=U_CHAR_DIRECTION_COUNT, firstCharDir=U_CHAR_DIRECTION_COUNT;
UBool leftToRight=FALSE, rightToLeft=FALSE;
int32_t rtlPos =-1, ltrPos =-1;
//get the string length
if(srcLength == -1){
if(srcLength < 0){
srcLength = u_strlen(src);
}
// map
b1Len = usprep_map(profile, src, srcLength, b1, b1Capacity, options, parseError, status);
UnicodeString s1;
UChar *b1 = s1.getBuffer(srcLength);
if(b1==NULL){
*status = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
int32_t b1Len = usprep_map(profile, src, srcLength,
b1, s1.getCapacity(), options, parseError, status);
s1.releaseBuffer(U_SUCCESS(*status) ? b1Len : 0);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
b1 = s1.getBuffer(b1Len);
if(b1==NULL){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
return 0;
}
*status = U_ZERO_ERROR; // reset error
b1Len = usprep_map(profile, src, srcLength, b1, b1Len, options, parseError, status);
b1Len = usprep_map(profile, src, srcLength,
b1, s1.getCapacity(), options, parseError, status);
s1.releaseBuffer(U_SUCCESS(*status) ? b1Len : 0);
}
if(U_FAILURE(*status)){
return 0;
}
// normalize
if(profile->doNFKC == TRUE){
b2Len = usprep_normalize(b1,b1Len, b2,b2Capacity,status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
if(b2==NULL){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b2Len = usprep_normalize(b1,b1Len, b2,b2Len,status);
UnicodeString s2;
if(profile->doNFKC){
const Normalizer2 *n2 = Normalizer2::getNFKCInstance(*status);
FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*status));
if(U_FAILURE(*status)){
return 0;
}
fn2.normalize(s1, s2, *status);
}else{
b2 = b1;
b2Len = b1Len;
s2.fastCopyFrom(s1);
}
if(U_FAILURE(*status)){
goto CLEANUP;
return 0;
}
UChar32 ch;
UStringPrepType type;
int16_t value;
UBool isIndex;
// Prohibit and checkBiDi in one pass
for(b2Index=0; b2Index<b2Len;){
ch = 0;
const UChar *b2 = s2.getBuffer();
int32_t b2Len = s2.length();
UCharDirection direction=U_CHAR_DIRECTION_COUNT, firstCharDir=U_CHAR_DIRECTION_COUNT;
UBool leftToRight=FALSE, rightToLeft=FALSE;
int32_t rtlPos =-1, ltrPos =-1;
for(int32_t b2Index=0; b2Index<b2Len;){
UChar32 ch = 0;
U16_NEXT(b2, b2Index, b2Len, ch);
uint16_t result;
UTRIE_GET16(&profile->sprepTrie,ch,result);
type = getValues(result, value, isIndex);
int16_t value;
UBool isIndex;
UStringPrepType type = getValues(result, value, isIndex);
if( type == USPREP_PROHIBITED ||
((result < _SPREP_TYPE_THRESHOLD) && (result & 0x01) /* first bit says it the code point is prohibited*/)
){
*status = U_STRINGPREP_PROHIBITED_ERROR;
uprv_syntaxError(b1, b2Index-U16_LENGTH(ch), b2Len, parseError);
goto CLEANUP;
return 0;
}
if(profile->checkBiDi) {
@ -772,13 +746,13 @@ usprep_prepare( const UStringPrepProfile* profile,
rtlPos = b2Index-1;
}
}
}
}
if(profile->checkBiDi == TRUE){
// satisfy 2
if( leftToRight == TRUE && rightToLeft == TRUE){
*status = U_STRINGPREP_CHECK_BIDI_ERROR;
uprv_syntaxError(b2,(rtlPos>ltrPos) ? rtlPos : ltrPos, b2Len, parseError);
goto CLEANUP;
return 0;
}
//satisfy 3
@ -791,21 +765,7 @@ usprep_prepare( const UStringPrepProfile* profile,
return FALSE;
}
}
if(b2Len>0 && b2Len <= destCapacity){
uprv_memmove(dest,b2, b2Len*U_SIZEOF_UCHAR);
}
CLEANUP:
if(b1!=b1Stack){
uprv_free(b1);
b1=NULL;
}
if(b2!=b1Stack && b2!=b2Stack && b2!=b1 /* b1 should not be freed twice */){
uprv_free(b2);
b2=NULL;
}
return u_terminateUChars(dest, destCapacity, b2Len, status);
return s2.extract(dest, destCapacity, *status);
}