ICU-1109 add u_terminateUChars()

X-SVN-Rev: 5694
This commit is contained in:
Markus Scherer 2001-09-05 23:37:16 +00:00
parent 0cf4543b4a
commit 2ee9b7af66
2 changed files with 42 additions and 2 deletions

View file

@ -116,7 +116,7 @@ u_internalGetCombiningClass(UChar32 c);
* Remember to use the u_releaseDefaultConverter when you are done.
* @internal
*/
U_CAPI UConverter*
U_CAPI UConverter* U_EXPORT2
u_getDefaultConverter(UErrorCode *status);
@ -124,7 +124,23 @@ u_getDefaultConverter(UErrorCode *status);
* Release the default converter to the converter cache.
* @internal
*/
U_CAPI void
U_CAPI void U_EXPORT2
u_releaseDefaultConverter(UConverter *converter);
/**
* NUL-terminate a UChar * string if possible.
* If length < destCapacity then NUL-terminate.
* If length == destCapacity then do not terminate but set U_STRING_NOT_TERMINATED_WARNING.
* If length > destCapacity then do not terminate but set U_BUFFER_OVERFLOW_ERROR.
*
* @param dest Destination buffer, can be NULL if destCapacity==0.
* @param destCapacity Number of UChars available at dest.
* @param length Number of UChars that were (to be) written to dest.
* @param pErrorCode ICU error code.
* @return length
* @internal
*/
U_CAPI int32_t U_EXPORT2
u_terminateUChars(UChar *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode);
#endif

View file

@ -1284,3 +1284,27 @@ u_growBufferFromStatic(void *context,
*pBuffer=newBuffer;
return (UBool)(newBuffer!=NULL);
}
/* NUL-termination of strings ----------------------------------------------- */
U_CAPI int32_t U_EXPORT2
u_terminateUChars(UChar *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode) {
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return length;
}
/* not a public function, so no complete argument checking */
if(length<destCapacity) {
/* NUL-terminate the string, the NUL fits */
dest[length]=0;
} else if(length==destCapacity) {
/* unable to NUL-terminate, but the string itself fit - set a warning code */
*pErrorCode=U_STRING_NOT_TERMINATED_WARNING;
} else /* length>destCapacity */ {
/* even the string itself did not fit - set an error code */
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
return length;
}