diff --git a/icu4c/source/common/unicode/ustring.h b/icu4c/source/common/unicode/ustring.h index d245f2cd84..0f8786e9b3 100644 --- a/icu4c/source/common/unicode/ustring.h +++ b/icu4c/source/common/unicode/ustring.h @@ -327,13 +327,13 @@ U_CAPI char* U_EXPORT2 u_austrncpy(char *dst, /** - * synonym for uprv_memcpy + * Synonym for uprv_memcpy(), but with UChars only. * @draft */ UChar *u_memcpy(UChar *dest, const UChar *src, int32_t count); /** - * synonym for uprv_memmove + * Synonym for uprv_memmove(), but with UChars only. * @draft */ UChar *u_memmove(UChar *dest, const UChar *src, int32_t count); @@ -350,22 +350,45 @@ UChar *u_memmove(UChar *dest, const UChar *src, int32_t count); UChar *u_memset(UChar *dest, UChar c, int32_t count); /** - * synonym for u_strncmp + * Compare the first count UChars of each buffer. + * + * @param buf1 The first string to compare. + * @param buf2 The second string to compare. + * @param count The maximum number of UChars to compare. + * @return When buf1 < buf2, a negative number is returned. + * When buf1 == buf2, 0 is returned. + * When buf1 > buf2, a positive number is returned. * @draft */ -#define u_memcmp(buf1, buf2, count) u_strncmp(buf1, buf2, count) +int32_t u_memcmp(UChar *buf1, UChar *buf2, int32_t count); /** - * synonym for u_strchr + * Search for a UChar within a Unicode string until count + * is reached. + * + * @param src string to search in + * @param ch character to find + * @param count maximum number of UChars in srcto search for + * ch. + * @return A pointer within src, pointing to ch, or NULL if it + * was not found. * @draft */ -#define u_memchr(buf, c, count) u_strchr(buf, c, count) +UChar *u_memchr(UChar *src, UChar ch, int32_t count); /** - * synonym for u_strchr32 + * Search for a UChar32 within a Unicode string until count + * is reached. This also includes surrogates in UTF-16. + * + * @param src string to search in + * @param ch character to find + * @param count maximum number of UChars in srcto search for + * ch. + * @return A pointer within src, pointing to ch, or NULL if it + * was not found. * @draft */ -#define u_memchr32(buf, c, count) u_strchr32(buf, c, count) +UChar *u_memchr32(UChar *src, UChar32 ch, int32_t count); /** * Unicode String literals in C. diff --git a/icu4c/source/common/ustring.c b/icu4c/source/common/ustring.c index 9ddbd0711e..1ed85495de 100644 --- a/icu4c/source/common/ustring.c +++ b/icu4c/source/common/ustring.c @@ -444,12 +444,61 @@ u_memset(UChar *dest, UChar c, int32_t count) { UChar *ptr = dest; UChar *limit = dest + count; - while(ptr < limit) { + while (ptr < limit) { *(ptr++) = c; } return dest; } +int32_t +u_memcmp(UChar *buf1, UChar *buf2, int32_t count) { + UChar *limit = buf1 + count; + int32_t result; + + while (buf1 < limit) { + result = (int32_t)(uint16_t)*buf1 - (int32_t)(uint16_t)*buf2; + if (result != 0) { + return result; + } + buf1++; + buf2++; + } + + return 0; +} + +UChar * +u_memchr(UChar *src, UChar ch, int32_t count) { + UChar *ptr = src; + UChar *limit = src + count; + + while (ptr < limit) { + if (*ptr == ch) { + return ptr; + } + ptr++; + } + + return NULL; +} + +UChar * +u_memchr32(UChar *src, UChar32 ch, int32_t count) { + int32_t strItr = 0; + int32_t lastIndex; + UChar32 stringCh; + + while (strItr < count) { + lastIndex = strItr; + UTF_NEXT_CHAR_SAFE(src, strItr, count, stringCh, TRUE); + if (stringCh == ch) { + return src + (strItr - (strItr - lastIndex)); + } + } + + return NULL; +} + /* string casing ------------------------------------------------------------ */ /*