#if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32) // TODO: We should use CharString for char buffers and UnicodeString for char16_t buffers. // Then we could change this to work only with wchar_t buffers. staticinline UBool
u_growAnyBufferFromStatic(void *context, void **pBuffer, int32_t *pCapacity, int32_t reqCapacity,
int32_t length, int32_t size) { // Use char* not void* to avoid the compiler's strict-aliasing assumptions // and related warnings. char *newBuffer=(char *)uprv_malloc(reqCapacity*size); if(newBuffer!=nullptr) { if(length>0) {
uprv_memcpy(newBuffer, *pBuffer, (size_t)length*size);
}
*pCapacity=reqCapacity;
} else {
*pCapacity=0;
}
/* release the old pBuffer if it was not statically allocated */ if(*pBuffer!=(char *)context) {
uprv_free(*pBuffer);
}
/* done with conversion null terminate the char buffer */ if(count>=tempBufCapacity){
tempBuf = saveBuf; /* we don't have enough room on the stack grow the buffer */ if(!u_growAnyBufferFromStatic(stackBuffer,(void**) &tempBuf, &tempBufCapacity,
count+1, count, 1)) { goto cleanup;
}
saveBuf = tempBuf;
}
saveBuf[count]=0;
/* allocate more space than required * here we assume that every char requires * no more than 2 wchar_ts
*/
intTargetCapacity = (count * _BUFFER_CAPACITY_MULTIPLIER + 1) /*for null termination */;
intTarget = (wchar_t*)uprv_malloc( intTargetCapacity * sizeof(wchar_t) );
/* we can call the system API since we are sure that * there is atleast 1 null in the input
*/
retVal = uprv_mbstowcs(pIntTarget,(tempBuf+nulLen),remaining);
if(retVal==-1){
*pErrorCode = U_INVALID_CHAR_FOUND; break;
}elseif(retVal== remaining){/* should never occur */ int numWritten = (pIntTarget-intTarget);
u_growAnyBufferFromStatic(nullptr,(void**) &intTarget,
&intTargetCapacity,
intTargetCapacity * _BUFFER_CAPACITY_MULTIPLIER,
numWritten, sizeof(wchar_t));
pIntTarget = intTarget;
remaining=intTargetCapacity;
if(nulLen!=count){ /*there are embedded nulls*/
pIntTarget+=numWritten;
remaining-=numWritten;
}
}else{
int32_t nulVal; /*scan for nulls */ /* we donot check for limit since tempBuf is null terminated */ while(tempBuf[nulLen++] != 0){
}
nulVal = (nulLen < srcLength) ? 1 : 0;
pIntTarget = pIntTarget + retVal+nulVal;
remaining -=(retVal+nulVal);
/* check if we have reached the source limit*/ if(nulLen>=(count)){ break;
}
}
}
count = (int32_t)(pIntTarget-intTarget);
/* free the allocated memory */
uprv_free(intTarget);
}else{
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
}
cleanup: /* are we still using stack buffer */ if(stackBuffer != saveBuf){
uprv_free(saveBuf);
}
u_terminateWChars(dest,destCapacity,count,pErrorCode);
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);
}else{ /* here the source is not null terminated * so it may have nulls embedded and we need to * do some extra processing
*/
int32_t remaining =cStackCap;
pSrcLimit = src + srcLength;
for(;;){
int32_t nulLen = 0;
/* find nulls in the string */ while(nulLen<srcLength && pSrc[nulLen++]!=0){
}
if((pSrc+nulLen) < pSrcLimit){ /* 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*/
u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap,
_BUFFER_CAPACITY_MULTIPLIER*cStackCap+(nulLen*MB_CUR_MAX),len,sizeof(char));
/* we have found a null so convert the * chunk from beginning 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;
}
pCSrc += retVal+1 /* already null terminated */;
pSrc += nulLen; /* skip past the null */
srcLength-=nulLen; /* decrement the srcLength */
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 occur */ /* allocate new buffer buffer */
pWStack =(wchar_t*) uprv_malloc(sizeof(wchar_t) * (nulLen + 1)); if(pWStack==nullptr){
*pErrorCode = U_MEMORY_ALLOCATION_ERROR; goto cleanup;
}
} if(nulLen>0){ /* copy the contents to tempStack */
uprv_memcpy(pWStack, pSrc, (size_t)nulLen*sizeof(wchar_t));
}
/* 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*/
u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap,
cStackCap+(nulLen*MB_CUR_MAX),len,sizeof(char));
/* OK..now we have converted from wchar_ts to chars now * convert chars to UChars
*/
pCSrcLimit = pCSrc;
pCSrc = pCSave;
pTarget = target= dest;
pTargetLimit = dest + destCapacity;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.