ICU-1109 add US::extract() with NUL-termination and error/warning code

X-SVN-Rev: 5695
This commit is contained in:
Markus Scherer 2001-09-05 23:37:56 +00:00
parent 2ee9b7af66
commit 06973e024e
2 changed files with 41 additions and 0 deletions

View file

@ -1137,6 +1137,30 @@ public:
UChar *dst,
UTextOffset dstStart = 0) const;
/**
* Copy the contents of the string into dest.
* This is a convenience function that
* checks if there is enough space in dest,
* extracts the entire string if possible,
* and NUL-terminates dest if possible.
*
* If the string fits into dest but cannot be NUL-terminated
* (length()==destCapacity) then the error code is set to U_STRING_NOT_TERMINATED_WARNING.
* If the string itself does not fit into dest
* (length()>destCapacity) then the error code is set to U_BUFFER_OVERFLOW_ERROR.
*
* If the string aliases to <code>dest</code> itself as an external buffer,
* then extract() will not copy the contents.
*
* @param dest Destination string buffer.
* @param destCapacity Number of UChars available at dest.
* @param errorCode ICU error code.
* @return length()
*/
int32_t
extract(UChar *dest, int32_t destCapacity,
UErrorCode &errorCode) const;
/**
* Copy the characters in the range
* [<tt>start</tt>, <tt>start + length</tt>) into the UnicodeString

View file

@ -652,6 +652,23 @@ UnicodeString::doExtract(UTextOffset start,
}
}
int32_t
UnicodeString::extract(UChar *dest, int32_t destCapacity,
UErrorCode &errorCode) const {
if(U_SUCCESS(errorCode)) {
if(isBogus() || destCapacity<0 || (destCapacity>0 && dest==NULL)) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
} else {
if(fLength<=destCapacity) {
doExtract(0, fLength, dest, 0);
}
return u_terminateUChars(dest, destCapacity, fLength, &errorCode);
}
}
return fLength;
}
UTextOffset
UnicodeString::indexOf(const UChar *srcChars,
UTextOffset srcStart,