From 9ebb3c1ae6af513bc30a2d17b8893191fe0aefbd Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Wed, 3 Oct 2001 23:14:31 +0000 Subject: [PATCH] ICU-1109 change NUL-termination semantics X-SVN-Rev: 6024 --- icu4c/source/common/ubidiwrt.c | 143 ++++++++++++++-------------- icu4c/source/common/unicode/ubidi.h | 8 +- 2 files changed, 75 insertions(+), 76 deletions(-) diff --git a/icu4c/source/common/ubidiwrt.c b/icu4c/source/common/ubidiwrt.c index e1c1291d19f..80e0a98316c 100644 --- a/icu4c/source/common/ubidiwrt.c +++ b/icu4c/source/common/ubidiwrt.c @@ -22,11 +22,12 @@ # define U_COMMON_IMPLEMENTATION #endif -#include "cmemory.h" #include "unicode/utypes.h" #include "unicode/ustring.h" #include "unicode/uchar.h" #include "unicode/ubidi.h" +#include "cmemory.h" +#include "ustr_imp.h" #include "ubidiimp.h" /* @@ -80,7 +81,7 @@ doWriteForward(const UChar *src, int32_t srcLength, int32_t length=srcLength; if(destSize0) { + c=*src++; + if(!IS_BIDI_CONTROL_CHAR(c)) { + --remaining; + } + } + return destSize-remaining; } *dest++=c; } @@ -133,7 +142,16 @@ doWriteForward(const UChar *src, int32_t srcLength, remaining-=i; if(remaining<0) { *pErrorCode=U_BUFFER_OVERFLOW_ERROR; - return 0; + + /* preflight the length */ + while(srcLength>0) { + c=*src++; + if(!IS_BIDI_CONTROL_CHAR(c)) { + --remaining; + } + --srcLength; + } + return destSize-remaining; } c=u_charMirror(c); UTF_APPEND_CHAR_UNSAFE(dest, j, c); @@ -181,7 +199,7 @@ doWriteReverse(const UChar *src, int32_t srcLength, */ if(destSize0); break; } /* end of switch */ + return destSize; } @@ -305,32 +324,40 @@ ubidi_writeReverse(const UChar *src, int32_t srcLength, UChar *dest, int32_t destSize, uint16_t options, UErrorCode *pErrorCode) { + int32_t destLength; + if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { return 0; } /* more error checking */ - if( src==NULL || srcLength<0 || - dest==NULL || destSize<=0) + if( src==NULL || srcLength<-1 || + destSize<0 || (destSize>0 && dest==NULL)) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* do input and output overlap? */ - if((src>=dest && src=src && dest=dest && src=src && dest0) { - return doWriteReverse(src, srcLength, dest, destSize, options, pErrorCode); + destLength=doWriteReverse(src, srcLength, dest, destSize, options, pErrorCode); } else { /* nothing to do */ - return 0; + destLength=0; } + + return u_terminateUChars(dest, destSize, destLength, pErrorCode); } #define MASK_R_AL (1UL<0 && dest==NULL)) + { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* do input and output overlap? */ - if((text>=dest && text=text && dest=dest && text=text && dest0 &&*/ u_charDirection(*src)!=U_LEFT_TO_RIGHT) { - if(destSize==0) { - *pErrorCode=U_BUFFER_OVERFLOW_ERROR; - return 0; + if(destSize>0) { + *dest++=LRM_CHAR; } - *dest++=LRM_CHAR; --destSize; } runLength=doWriteForward(src, runLength, dest, destSize, (uint16_t)(options&~UBIDI_DO_MIRRORING), pErrorCode); - if(U_FAILURE(*pErrorCode)) { - return 0; - } dest+=runLength; destSize-=runLength; if(/*run0) { + *dest++=LRM_CHAR; } - *dest++=LRM_CHAR; --destSize; } } else { if(/*run>0 &&*/ !(MASK_R_AL&1UL<0) { + *dest++=RLM_CHAR; } - *dest++=RLM_CHAR; --destSize; } runLength=doWriteReverse(src, runLength, dest, destSize, options, pErrorCode); - if(U_FAILURE(*pErrorCode)) { - return 0; - } dest+=runLength; destSize-=runLength; if(/*run0) { + *dest++=RLM_CHAR; } - *dest++=RLM_CHAR; --destSize; } } @@ -495,9 +511,6 @@ ubidi_writeReordered(UBiDi *pBiDi, } dest+=runLength; destSize-=runLength; - if(U_FAILURE(*pErrorCode)) { - return 0; - } } } else { /* insert BiDi controls for "inverse BiDi" */ @@ -511,56 +524,42 @@ ubidi_writeReordered(UBiDi *pBiDi, if(UBIDI_LTR==dir) { if(/*run0) { + *dest++=LRM_CHAR; } - *dest++=LRM_CHAR; --destSize; } runLength=doWriteReverse(src, runLength, dest, destSize, (uint16_t)(options&~UBIDI_DO_MIRRORING), pErrorCode); - if(U_FAILURE(*pErrorCode)) { - return 0; - } dest+=runLength; destSize-=runLength; if(/*run>0 &&*/ u_charDirection(*src)!=U_LEFT_TO_RIGHT) { - if(destSize==0) { - *pErrorCode=U_BUFFER_OVERFLOW_ERROR; - return 0; + if(destSize>0) { + *dest++=LRM_CHAR; } - *dest++=LRM_CHAR; --destSize; } } else { if(/*run0) { + *dest++=RLM_CHAR; } - *dest++=RLM_CHAR; --destSize; } runLength=doWriteForward(src, runLength, dest, destSize, options, pErrorCode); - if(U_FAILURE(*pErrorCode)) { - return 0; - } dest+=runLength; destSize-=runLength; if(/*run>0 &&*/ !(MASK_R_AL&1UL<0) { + *dest++=RLM_CHAR; } - *dest++=RLM_CHAR; --destSize; } } @@ -568,5 +567,5 @@ ubidi_writeReordered(UBiDi *pBiDi, } } - return dest-oldDest; + return u_terminateUChars(dest, destCapacity, destCapacity-destSize, pErrorCode); } diff --git a/icu4c/source/common/unicode/ubidi.h b/icu4c/source/common/unicode/ubidi.h index 59463d55256..2578974a2c4 100644 --- a/icu4c/source/common/unicode/ubidi.h +++ b/icu4c/source/common/unicode/ubidi.h @@ -1063,7 +1063,7 @@ ubidi_invertMap(const UTextOffset *srcMap, UTextOffset *destMap, UTextOffset len * @param pErrorCode must be a valid pointer to an error code value, * which must not indicate a failure before the function call. * - * @return The number of characters that were written to dest. + * @return The length of the output string. */ U_CAPI UTextOffset U_EXPORT2 ubidi_writeReordered(UBiDi *pBiDi, @@ -1087,8 +1087,8 @@ ubidi_writeReordered(UBiDi *pBiDi, * This function is the implementation for reversing RTL runs as part * of ubidi_writeReordered(). For detailed descriptions * of the parameters, see there. - * Since no BiDi controls are inserted here, this function will never - * write more than srcLength characters to dest. + * Since no BiDi controls are inserted here, the output string length + * will never exceed srcLength. * * @see ubidi_writeReordered * @@ -1115,7 +1115,7 @@ ubidi_writeReordered(UBiDi *pBiDi, * @param pErrorCode must be a valid pointer to an error code value, * which must not indicate a failure before the function call. * - * @return The number of characters that were written to dest. + * @return The length of the output string. */ U_CAPI UTextOffset U_EXPORT2 ubidi_writeReverse(const UChar *src, int32_t srcLength,