2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1998-2016, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
2011-05-26 20:05:39 +00:00
|
|
|
* File ustring.cpp
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 12/07/98 bertrand Creation.
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
2004-10-18 03:03:13 +00:00
|
|
|
#include "unicode/putil.h"
|
2017-02-09 21:15:34 +00:00
|
|
|
#include "unicode/uchar.h"
|
2002-02-21 04:48:21 +00:00
|
|
|
#include "unicode/ustring.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cstring.h"
|
2001-05-25 17:57:14 +00:00
|
|
|
#include "cwchar.h"
|
2001-01-31 18:20:26 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "ustr_imp.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-09 01:03:31 +00:00
|
|
|
/* ANSI string.h - style functions ------------------------------------------ */
|
|
|
|
|
2002-11-25 22:44:48 +00:00
|
|
|
/* U+ffff is the highest BMP code point, the highest one that fits into a 16-bit UChar */
|
|
|
|
#define U_BMP_MAX 0xffff
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
/* Forward binary string search functions ----------------------------------- */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
/*
|
|
|
|
* Test if a substring match inside a string is at code point boundaries.
|
|
|
|
* All pointers refer to the same buffer.
|
|
|
|
* The limit pointer may be NULL, all others must be real pointers.
|
|
|
|
*/
|
2011-05-26 20:05:39 +00:00
|
|
|
static inline UBool
|
2002-10-24 01:49:58 +00:00
|
|
|
isMatchAtCPBoundary(const UChar *start, const UChar *match, const UChar *matchLimit, const UChar *limit) {
|
|
|
|
if(U16_IS_TRAIL(*match) && start!=match && U16_IS_LEAD(*(match-1))) {
|
|
|
|
/* the leading edge of the match is in the middle of a surrogate pair */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if(U16_IS_LEAD(*(matchLimit-1)) && match!=limit && U16_IS_TRAIL(*matchLimit)) {
|
|
|
|
/* the trailing edge of the match is in the middle of a surrogate pair */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-06-29 18:52:11 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
2002-10-24 01:49:58 +00:00
|
|
|
u_strFindFirst(const UChar *s, int32_t length,
|
|
|
|
const UChar *sub, int32_t subLength) {
|
|
|
|
const UChar *start, *p, *q, *subLimit;
|
|
|
|
UChar c, cs, cq;
|
2000-07-17 20:47:02 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
if(sub==NULL || subLength<-1) {
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
if(s==NULL || length<-1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-07-17 20:47:02 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
start=s;
|
2000-07-17 20:47:02 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
if(length<0 && subLength<0) {
|
|
|
|
/* both strings are NUL-terminated */
|
|
|
|
if((cs=*sub++)==0) {
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
if(*sub==0 && !U16_IS_SURROGATE(cs)) {
|
|
|
|
/* the substring consists of a single, non-surrogate BMP code point */
|
|
|
|
return u_strchr(s, cs);
|
|
|
|
}
|
2000-07-17 20:47:02 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
while((c=*s++)!=0) {
|
|
|
|
if(c==cs) {
|
|
|
|
/* found first substring UChar, compare rest */
|
|
|
|
p=s;
|
|
|
|
q=sub;
|
|
|
|
for(;;) {
|
|
|
|
if((cq=*q)==0) {
|
|
|
|
if(isMatchAtCPBoundary(start, s-1, p, NULL)) {
|
|
|
|
return (UChar *)(s-1); /* well-formed match */
|
|
|
|
} else {
|
|
|
|
break; /* no match because surrogate pair is split */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((c=*p)==0) {
|
|
|
|
return NULL; /* no match, and none possible after s */
|
|
|
|
}
|
|
|
|
if(c!=cq) {
|
|
|
|
break; /* no match */
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
return NULL;
|
2000-08-11 21:24:17 +00:00
|
|
|
}
|
2000-07-17 20:47:02 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
if(subLength<0) {
|
|
|
|
subLength=u_strlen(sub);
|
|
|
|
}
|
|
|
|
if(subLength==0) {
|
|
|
|
return (UChar *)s;
|
2000-07-17 20:47:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
/* get sub[0] to search for it fast */
|
|
|
|
cs=*sub++;
|
|
|
|
--subLength;
|
|
|
|
subLimit=sub+subLength;
|
2000-07-17 20:47:02 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
if(subLength==0 && !U16_IS_SURROGATE(cs)) {
|
|
|
|
/* the substring consists of a single, non-surrogate BMP code point */
|
|
|
|
return length<0 ? u_strchr(s, cs) : u_memchr(s, cs, length);
|
|
|
|
}
|
2000-06-29 18:52:11 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
if(length<0) {
|
|
|
|
/* s is NUL-terminated */
|
|
|
|
while((c=*s++)!=0) {
|
|
|
|
if(c==cs) {
|
|
|
|
/* found first substring UChar, compare rest */
|
|
|
|
p=s;
|
|
|
|
q=sub;
|
|
|
|
for(;;) {
|
|
|
|
if(q==subLimit) {
|
|
|
|
if(isMatchAtCPBoundary(start, s-1, p, NULL)) {
|
|
|
|
return (UChar *)(s-1); /* well-formed match */
|
|
|
|
} else {
|
|
|
|
break; /* no match because surrogate pair is split */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((c=*p)==0) {
|
|
|
|
return NULL; /* no match, and none possible after s */
|
|
|
|
}
|
|
|
|
if(c!=*q) {
|
|
|
|
break; /* no match */
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
} else {
|
2002-10-24 01:49:58 +00:00
|
|
|
const UChar *limit, *preLimit;
|
|
|
|
|
|
|
|
/* subLength was decremented above */
|
|
|
|
if(length<=subLength) {
|
|
|
|
return NULL; /* s is shorter than sub */
|
|
|
|
}
|
|
|
|
|
|
|
|
limit=s+length;
|
|
|
|
|
|
|
|
/* the substring must start before preLimit */
|
|
|
|
preLimit=limit-subLength;
|
|
|
|
|
|
|
|
while(s!=preLimit) {
|
|
|
|
c=*s++;
|
|
|
|
if(c==cs) {
|
|
|
|
/* found first substring UChar, compare rest */
|
|
|
|
p=s;
|
|
|
|
q=sub;
|
|
|
|
for(;;) {
|
|
|
|
if(q==subLimit) {
|
|
|
|
if(isMatchAtCPBoundary(start, s-1, p, limit)) {
|
|
|
|
return (UChar *)(s-1); /* well-formed match */
|
|
|
|
} else {
|
|
|
|
break; /* no match because surrogate pair is split */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(*p!=*q) {
|
|
|
|
break; /* no match */
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
2002-10-24 01:49:58 +00:00
|
|
|
|
|
|
|
/* not found */
|
|
|
|
return NULL;
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strstr(const UChar *s, const UChar *substring) {
|
|
|
|
return u_strFindFirst(s, -1, substring, -1);
|
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strchr(const UChar *s, UChar c) {
|
|
|
|
if(U16_IS_SURROGATE(c)) {
|
|
|
|
/* make sure to not find half of a surrogate pair */
|
|
|
|
return u_strFindFirst(s, -1, &c, 1);
|
2002-03-18 23:19:41 +00:00
|
|
|
} else {
|
2002-10-24 01:49:58 +00:00
|
|
|
UChar cs;
|
|
|
|
|
|
|
|
/* trivial search for a BMP code point */
|
|
|
|
for(;;) {
|
|
|
|
if((cs=*s)==c) {
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
if(cs==0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
++s;
|
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
2002-10-24 01:49:58 +00:00
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strchr32(const UChar *s, UChar32 c) {
|
2002-11-25 22:44:48 +00:00
|
|
|
if((uint32_t)c<=U_BMP_MAX) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find BMP code point */
|
|
|
|
return u_strchr(s, (UChar)c);
|
2002-11-25 22:44:48 +00:00
|
|
|
} else if((uint32_t)c<=UCHAR_MAX_VALUE) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find supplementary code point as surrogate pair */
|
|
|
|
UChar cs, lead=U16_LEAD(c), trail=U16_TRAIL(c);
|
|
|
|
|
|
|
|
while((cs=*s++)!=0) {
|
|
|
|
if(cs==lead && *s==trail) {
|
|
|
|
return (UChar *)(s-1);
|
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
2002-10-24 01:49:58 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* not a Unicode code point, not findable */
|
|
|
|
return NULL;
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
2002-10-24 01:49:58 +00:00
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_memchr(const UChar *s, UChar c, int32_t count) {
|
|
|
|
if(count<=0) {
|
|
|
|
return NULL; /* no string */
|
|
|
|
} else if(U16_IS_SURROGATE(c)) {
|
|
|
|
/* make sure to not find half of a surrogate pair */
|
|
|
|
return u_strFindFirst(s, count, &c, 1);
|
|
|
|
} else {
|
|
|
|
/* trivial search for a BMP code point */
|
|
|
|
const UChar *limit=s+count;
|
|
|
|
do {
|
|
|
|
if(*s==c) {
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
} while(++s!=limit);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_memchr32(const UChar *s, UChar32 c, int32_t count) {
|
2002-11-25 22:44:48 +00:00
|
|
|
if((uint32_t)c<=U_BMP_MAX) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find BMP code point */
|
|
|
|
return u_memchr(s, (UChar)c, count);
|
|
|
|
} else if(count<2) {
|
|
|
|
/* too short for a surrogate pair */
|
|
|
|
return NULL;
|
2002-11-25 22:44:48 +00:00
|
|
|
} else if((uint32_t)c<=UCHAR_MAX_VALUE) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find supplementary code point as surrogate pair */
|
|
|
|
const UChar *limit=s+count-1; /* -1 so that we do not need a separate check for the trail unit */
|
|
|
|
UChar lead=U16_LEAD(c), trail=U16_TRAIL(c);
|
2002-03-18 23:19:41 +00:00
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
do {
|
|
|
|
if(*s==lead && *(s+1)==trail) {
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
} while(++s!=limit);
|
|
|
|
return NULL;
|
2002-03-18 23:19:41 +00:00
|
|
|
} else {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* not a Unicode code point, not findable */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Backward binary string search functions ---------------------------------- */
|
|
|
|
|
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strFindLast(const UChar *s, int32_t length,
|
|
|
|
const UChar *sub, int32_t subLength) {
|
|
|
|
const UChar *start, *limit, *p, *q, *subLimit;
|
|
|
|
UChar c, cs;
|
|
|
|
|
|
|
|
if(sub==NULL || subLength<-1) {
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
if(s==NULL || length<-1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This implementation is more lazy than the one for u_strFindFirst():
|
|
|
|
* There is no special search code for NUL-terminated strings.
|
|
|
|
* It does not seem to be worth it for searching substrings to
|
|
|
|
* search forward and find all matches like in u_strrchr() and similar.
|
|
|
|
* Therefore, we simply get both string lengths and search backward.
|
|
|
|
*
|
|
|
|
* markus 2002oct23
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(subLength<0) {
|
|
|
|
subLength=u_strlen(sub);
|
|
|
|
}
|
|
|
|
if(subLength==0) {
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get sub[subLength-1] to search for it fast */
|
|
|
|
subLimit=sub+subLength;
|
|
|
|
cs=*(--subLimit);
|
|
|
|
--subLength;
|
|
|
|
|
|
|
|
if(subLength==0 && !U16_IS_SURROGATE(cs)) {
|
|
|
|
/* the substring consists of a single, non-surrogate BMP code point */
|
|
|
|
return length<0 ? u_strrchr(s, cs) : u_memrchr(s, cs, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(length<0) {
|
|
|
|
length=u_strlen(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* subLength was decremented above */
|
|
|
|
if(length<=subLength) {
|
|
|
|
return NULL; /* s is shorter than sub */
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
start=s;
|
|
|
|
limit=s+length;
|
|
|
|
|
|
|
|
/* the substring must start no later than s+subLength */
|
|
|
|
s+=subLength;
|
|
|
|
|
|
|
|
while(s!=limit) {
|
|
|
|
c=*(--limit);
|
|
|
|
if(c==cs) {
|
|
|
|
/* found last substring UChar, compare rest */
|
|
|
|
p=limit;
|
|
|
|
q=subLimit;
|
|
|
|
for(;;) {
|
|
|
|
if(q==sub) {
|
2002-10-24 17:29:35 +00:00
|
|
|
if(isMatchAtCPBoundary(start, p, limit+1, start+length)) {
|
2002-10-24 01:49:58 +00:00
|
|
|
return (UChar *)p; /* well-formed match */
|
|
|
|
} else {
|
|
|
|
break; /* no match because surrogate pair is split */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(*(--p)!=*(--q)) {
|
|
|
|
break; /* no match */
|
|
|
|
}
|
|
|
|
}
|
2002-03-18 23:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
/* not found */
|
2002-03-18 23:19:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-24 17:29:35 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strrstr(const UChar *s, const UChar *substring) {
|
|
|
|
return u_strFindLast(s, -1, substring, -1);
|
|
|
|
}
|
|
|
|
|
2000-06-29 19:00:55 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
2002-10-24 01:49:58 +00:00
|
|
|
u_strrchr(const UChar *s, UChar c) {
|
|
|
|
if(U16_IS_SURROGATE(c)) {
|
|
|
|
/* make sure to not find half of a surrogate pair */
|
|
|
|
return u_strFindLast(s, -1, &c, 1);
|
|
|
|
} else {
|
|
|
|
const UChar *result=NULL;
|
|
|
|
UChar cs;
|
|
|
|
|
|
|
|
/* trivial search for a BMP code point */
|
|
|
|
for(;;) {
|
|
|
|
if((cs=*s)==c) {
|
|
|
|
result=s;
|
|
|
|
}
|
|
|
|
if(cs==0) {
|
|
|
|
return (UChar *)result;
|
|
|
|
}
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strrchr32(const UChar *s, UChar32 c) {
|
2002-11-25 22:44:48 +00:00
|
|
|
if((uint32_t)c<=U_BMP_MAX) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find BMP code point */
|
|
|
|
return u_strrchr(s, (UChar)c);
|
2002-11-25 22:44:48 +00:00
|
|
|
} else if((uint32_t)c<=UCHAR_MAX_VALUE) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find supplementary code point as surrogate pair */
|
|
|
|
const UChar *result=NULL;
|
|
|
|
UChar cs, lead=U16_LEAD(c), trail=U16_TRAIL(c);
|
|
|
|
|
|
|
|
while((cs=*s++)!=0) {
|
|
|
|
if(cs==lead && *s==trail) {
|
|
|
|
result=s-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (UChar *)result;
|
|
|
|
} else {
|
|
|
|
/* not a Unicode code point, not findable */
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-29 19:00:55 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_memrchr(const UChar *s, UChar c, int32_t count) {
|
|
|
|
if(count<=0) {
|
|
|
|
return NULL; /* no string */
|
|
|
|
} else if(U16_IS_SURROGATE(c)) {
|
|
|
|
/* make sure to not find half of a surrogate pair */
|
|
|
|
return u_strFindLast(s, count, &c, 1);
|
|
|
|
} else {
|
|
|
|
/* trivial search for a BMP code point */
|
|
|
|
const UChar *limit=s+count;
|
|
|
|
do {
|
|
|
|
if(*(--limit)==c) {
|
|
|
|
return (UChar *)limit;
|
|
|
|
}
|
|
|
|
} while(s!=limit);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_memrchr32(const UChar *s, UChar32 c, int32_t count) {
|
2002-11-25 22:44:48 +00:00
|
|
|
if((uint32_t)c<=U_BMP_MAX) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find BMP code point */
|
|
|
|
return u_memrchr(s, (UChar)c, count);
|
|
|
|
} else if(count<2) {
|
|
|
|
/* too short for a surrogate pair */
|
|
|
|
return NULL;
|
2002-11-25 22:44:48 +00:00
|
|
|
} else if((uint32_t)c<=UCHAR_MAX_VALUE) {
|
2002-10-24 01:49:58 +00:00
|
|
|
/* find supplementary code point as surrogate pair */
|
|
|
|
const UChar *limit=s+count-1;
|
|
|
|
UChar lead=U16_LEAD(c), trail=U16_TRAIL(c);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(*limit==trail && *(limit-1)==lead) {
|
|
|
|
return (UChar *)(limit-1);
|
|
|
|
}
|
|
|
|
} while(s!=--limit);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* not a Unicode code point, not findable */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tokenization functions --------------------------------------------------- */
|
|
|
|
|
2002-09-30 04:00:17 +00:00
|
|
|
/*
|
|
|
|
* Match each code point in a string against each code point in the matchSet.
|
|
|
|
* Return the index of the first string code point that
|
|
|
|
* is (polarity==TRUE) or is not (FALSE) contained in the matchSet.
|
|
|
|
* Return -(string length)-1 if there is no such code point.
|
|
|
|
*/
|
|
|
|
static int32_t
|
|
|
|
_matchFromSet(const UChar *string, const UChar *matchSet, UBool polarity) {
|
|
|
|
int32_t matchLen, matchBMPLen, strItr, matchItr;
|
|
|
|
UChar32 stringCh, matchCh;
|
|
|
|
UChar c, c2;
|
|
|
|
|
|
|
|
/* first part of matchSet contains only BMP code points */
|
|
|
|
matchBMPLen = 0;
|
|
|
|
while((c = matchSet[matchBMPLen]) != 0 && U16_IS_SINGLE(c)) {
|
|
|
|
++matchBMPLen;
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
|
|
|
|
2002-09-30 04:00:17 +00:00
|
|
|
/* second part of matchSet contains BMP and supplementary code points */
|
|
|
|
matchLen = matchBMPLen;
|
|
|
|
while(matchSet[matchLen] != 0) {
|
|
|
|
++matchLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(strItr = 0; (c = string[strItr]) != 0;) {
|
|
|
|
++strItr;
|
|
|
|
if(U16_IS_SINGLE(c)) {
|
|
|
|
if(polarity) {
|
|
|
|
for(matchItr = 0; matchItr < matchLen; ++matchItr) {
|
|
|
|
if(c == matchSet[matchItr]) {
|
|
|
|
return strItr - 1; /* one matches */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(matchItr = 0; matchItr < matchLen; ++matchItr) {
|
|
|
|
if(c == matchSet[matchItr]) {
|
|
|
|
goto endloop;
|
|
|
|
}
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
return strItr - 1; /* none matches */
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* No need to check for string length before U16_IS_TRAIL
|
|
|
|
* because c2 could at worst be the terminating NUL.
|
|
|
|
*/
|
|
|
|
if(U16_IS_SURROGATE_LEAD(c) && U16_IS_TRAIL(c2 = string[strItr])) {
|
|
|
|
++strItr;
|
|
|
|
stringCh = U16_GET_SUPPLEMENTARY(c, c2);
|
|
|
|
} else {
|
|
|
|
stringCh = c; /* unpaired trail surrogate */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(polarity) {
|
|
|
|
for(matchItr = matchBMPLen; matchItr < matchLen;) {
|
|
|
|
U16_NEXT(matchSet, matchItr, matchLen, matchCh);
|
|
|
|
if(stringCh == matchCh) {
|
|
|
|
return strItr - U16_LENGTH(stringCh); /* one matches */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(matchItr = matchBMPLen; matchItr < matchLen;) {
|
|
|
|
U16_NEXT(matchSet, matchItr, matchLen, matchCh);
|
|
|
|
if(stringCh == matchCh) {
|
|
|
|
goto endloop;
|
|
|
|
}
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
return strItr - U16_LENGTH(stringCh); /* none matches */
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
endloop:
|
|
|
|
/* wish C had continue with labels like Java... */;
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Didn't find it. */
|
2002-09-30 04:00:17 +00:00
|
|
|
return -strItr-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for a codepoint in a string that matches one of the matchSet codepoints. */
|
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strpbrk(const UChar *string, const UChar *matchSet)
|
|
|
|
{
|
2009-04-15 01:57:02 +00:00
|
|
|
int32_t idx = _matchFromSet(string, matchSet, TRUE);
|
|
|
|
if(idx >= 0) {
|
|
|
|
return (UChar *)string + idx;
|
2002-09-30 04:00:17 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for a codepoint in a string that matches one of the matchSet codepoints. */
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-01-30 03:12:01 +00:00
|
|
|
u_strcspn(const UChar *string, const UChar *matchSet)
|
|
|
|
{
|
2009-04-15 01:57:02 +00:00
|
|
|
int32_t idx = _matchFromSet(string, matchSet, TRUE);
|
|
|
|
if(idx >= 0) {
|
|
|
|
return idx;
|
2002-09-30 04:00:17 +00:00
|
|
|
} else {
|
2009-04-15 01:57:02 +00:00
|
|
|
return -idx - 1; /* == u_strlen(string) */
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for a codepoint in a string that does not match one of the matchSet codepoints. */
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-01-30 03:12:01 +00:00
|
|
|
u_strspn(const UChar *string, const UChar *matchSet)
|
|
|
|
{
|
2009-04-15 01:57:02 +00:00
|
|
|
int32_t idx = _matchFromSet(string, matchSet, FALSE);
|
|
|
|
if(idx >= 0) {
|
|
|
|
return idx;
|
2002-09-30 04:00:17 +00:00
|
|
|
} else {
|
2009-04-15 01:57:02 +00:00
|
|
|
return -idx - 1; /* == u_strlen(string) */
|
2001-01-30 03:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-30 20:58:38 +00:00
|
|
|
/* ----- Text manipulation functions --- */
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strtok_r(UChar *src,
|
|
|
|
const UChar *delim,
|
|
|
|
UChar **saveState)
|
|
|
|
{
|
|
|
|
UChar *tokSource;
|
|
|
|
UChar *nextToken;
|
|
|
|
uint32_t nonDelimIdx;
|
|
|
|
|
2002-03-23 00:29:03 +00:00
|
|
|
/* If saveState is NULL, the user messed up. */
|
2001-01-30 20:58:38 +00:00
|
|
|
if (src != NULL) {
|
|
|
|
tokSource = src;
|
2001-12-27 00:13:20 +00:00
|
|
|
*saveState = src; /* Set to "src" in case there are no delimiters */
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
2002-03-23 00:29:03 +00:00
|
|
|
else if (*saveState) {
|
2001-01-30 20:58:38 +00:00
|
|
|
tokSource = *saveState;
|
|
|
|
}
|
|
|
|
else {
|
2002-03-23 00:29:03 +00:00
|
|
|
/* src == NULL && *saveState == NULL */
|
|
|
|
/* This shouldn't happen. We already finished tokenizing. */
|
2001-01-30 20:58:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip initial delimiters */
|
|
|
|
nonDelimIdx = u_strspn(tokSource, delim);
|
|
|
|
tokSource = &tokSource[nonDelimIdx];
|
|
|
|
|
2001-07-16 22:57:39 +00:00
|
|
|
if (*tokSource) {
|
|
|
|
nextToken = u_strpbrk(tokSource, delim);
|
|
|
|
if (nextToken != NULL) {
|
|
|
|
/* Create a token */
|
|
|
|
*(nextToken++) = 0;
|
|
|
|
*saveState = nextToken;
|
|
|
|
return tokSource;
|
|
|
|
}
|
2002-03-23 00:29:03 +00:00
|
|
|
else if (*saveState) {
|
2001-07-16 22:57:39 +00:00
|
|
|
/* Return the last token */
|
|
|
|
*saveState = NULL;
|
|
|
|
return tokSource;
|
|
|
|
}
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
2001-07-16 22:57:39 +00:00
|
|
|
else {
|
|
|
|
/* No tokens were found. Only delimiters were left. */
|
2001-01-30 20:58:38 +00:00
|
|
|
*saveState = NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-24 01:49:58 +00:00
|
|
|
/* Miscellaneous functions -------------------------------------------------- */
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strcat(UChar *dst,
|
|
|
|
const UChar *src)
|
|
|
|
{
|
2001-03-15 23:45:54 +00:00
|
|
|
UChar *anchor = dst; /* save a pointer to start of dst */
|
2001-01-30 20:58:38 +00:00
|
|
|
|
2001-03-15 23:45:54 +00:00
|
|
|
while(*dst != 0) { /* To end of first string */
|
|
|
|
++dst;
|
|
|
|
}
|
|
|
|
while((*(dst++) = *(src++)) != 0) { /* copy string 2 over */
|
|
|
|
}
|
2001-01-30 20:58:38 +00:00
|
|
|
|
2001-03-15 23:45:54 +00:00
|
|
|
return anchor;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strncat(UChar *dst,
|
|
|
|
const UChar *src,
|
|
|
|
int32_t n )
|
|
|
|
{
|
2001-03-15 23:45:54 +00:00
|
|
|
if(n > 0) {
|
|
|
|
UChar *anchor = dst; /* save a pointer to start of dst */
|
2001-01-30 20:58:38 +00:00
|
|
|
|
2001-03-15 23:45:54 +00:00
|
|
|
while(*dst != 0) { /* To end of first string */
|
|
|
|
++dst;
|
|
|
|
}
|
|
|
|
while((*dst = *src) != 0) { /* copy string 2 over */
|
|
|
|
++dst;
|
|
|
|
if(--n == 0) {
|
|
|
|
*dst = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
|
|
|
|
return anchor;
|
|
|
|
} else {
|
|
|
|
return dst;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----- Text property functions --- */
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strcmp(const UChar *s1,
|
|
|
|
const UChar *s2)
|
|
|
|
{
|
2001-06-26 21:33:04 +00:00
|
|
|
UChar c1, c2;
|
2001-07-09 19:44:54 +00:00
|
|
|
|
2001-03-15 23:45:54 +00:00
|
|
|
for(;;) {
|
2001-07-09 19:44:54 +00:00
|
|
|
c1=*s1++;
|
|
|
|
c2=*s2++;
|
|
|
|
if (c1 != c2 || c1 == 0) {
|
|
|
|
break;
|
2001-03-15 23:45:54 +00:00
|
|
|
}
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
2001-07-09 19:44:54 +00:00
|
|
|
return (int32_t)c1 - (int32_t)c2;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 08:45:10 +00:00
|
|
|
U_CFUNC int32_t U_EXPORT2
|
2002-05-24 17:05:31 +00:00
|
|
|
uprv_strCompare(const UChar *s1, int32_t length1,
|
|
|
|
const UChar *s2, int32_t length2,
|
|
|
|
UBool strncmpStyle, UBool codePointOrder) {
|
2002-05-13 23:03:09 +00:00
|
|
|
const UChar *start1, *start2, *limit1, *limit2;
|
2001-03-15 23:45:54 +00:00
|
|
|
UChar c1, c2;
|
|
|
|
|
2002-05-13 23:03:09 +00:00
|
|
|
/* setup for fix-up */
|
|
|
|
start1=s1;
|
|
|
|
start2=s2;
|
|
|
|
|
2001-06-25 23:44:21 +00:00
|
|
|
/* compare identical prefixes - they do not need to be fixed up */
|
2002-05-13 23:03:09 +00:00
|
|
|
if(length1<0 && length2<0) {
|
|
|
|
/* strcmp style, both NUL-terminated */
|
|
|
|
if(s1==s2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
c1=*s1;
|
|
|
|
c2=*s2;
|
|
|
|
if(c1!=c2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(c1==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
++s1;
|
|
|
|
++s2;
|
2001-03-15 23:45:54 +00:00
|
|
|
}
|
2002-05-13 23:03:09 +00:00
|
|
|
|
|
|
|
/* setup for fix-up */
|
|
|
|
limit1=limit2=NULL;
|
|
|
|
} else if(strncmpStyle) {
|
|
|
|
/* special handling for strncmp, assume length1==length2>=0 but also check for NUL */
|
|
|
|
if(s1==s2) {
|
2001-11-28 23:49:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-13 23:03:09 +00:00
|
|
|
|
|
|
|
limit1=start1+length1;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
/* both lengths are same, check only one limit */
|
|
|
|
if(s1==limit1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
c1=*s1;
|
|
|
|
c2=*s2;
|
|
|
|
if(c1!=c2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(c1==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
++s1;
|
|
|
|
++s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup for fix-up */
|
|
|
|
limit2=start2+length1; /* use length1 here, too, to enforce assumption */
|
|
|
|
} else {
|
|
|
|
/* memcmp/UnicodeString style, both length-specified */
|
|
|
|
int32_t lengthResult;
|
|
|
|
|
|
|
|
if(length1<0) {
|
|
|
|
length1=u_strlen(s1);
|
|
|
|
}
|
|
|
|
if(length2<0) {
|
|
|
|
length2=u_strlen(s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* limit1=start1+min(lenght1, length2) */
|
|
|
|
if(length1<length2) {
|
|
|
|
lengthResult=-1;
|
|
|
|
limit1=start1+length1;
|
|
|
|
} else if(length1==length2) {
|
|
|
|
lengthResult=0;
|
|
|
|
limit1=start1+length1;
|
|
|
|
} else /* length1>length2 */ {
|
|
|
|
lengthResult=1;
|
|
|
|
limit1=start1+length2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s1==s2) {
|
|
|
|
return lengthResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
/* check pseudo-limit */
|
|
|
|
if(s1==limit1) {
|
|
|
|
return lengthResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
c1=*s1;
|
|
|
|
c2=*s2;
|
|
|
|
if(c1!=c2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++s1;
|
|
|
|
++s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup for fix-up */
|
|
|
|
limit1=start1+length1;
|
|
|
|
limit2=start2+length2;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
2001-06-25 23:44:21 +00:00
|
|
|
|
2002-05-13 23:03:09 +00:00
|
|
|
/* if both values are in or above the surrogate range, fix them up */
|
2002-05-22 00:32:42 +00:00
|
|
|
if(c1>=0xd800 && c2>=0xd800 && codePointOrder) {
|
2002-05-13 23:03:09 +00:00
|
|
|
/* subtract 0x2800 from BMP code points to make them smaller than supplementary ones */
|
|
|
|
if(
|
2011-07-27 05:53:56 +00:00
|
|
|
(c1<=0xdbff && (s1+1)!=limit1 && U16_IS_TRAIL(*(s1+1))) ||
|
|
|
|
(U16_IS_TRAIL(c1) && start1!=s1 && U16_IS_LEAD(*(s1-1)))
|
2002-05-13 23:03:09 +00:00
|
|
|
) {
|
|
|
|
/* part of a surrogate pair, leave >=d800 */
|
|
|
|
} else {
|
|
|
|
/* BMP code point - may be surrogate code point - make <d800 */
|
|
|
|
c1-=0x2800;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(
|
2011-07-27 05:53:56 +00:00
|
|
|
(c2<=0xdbff && (s2+1)!=limit2 && U16_IS_TRAIL(*(s2+1))) ||
|
|
|
|
(U16_IS_TRAIL(c2) && start2!=s2 && U16_IS_LEAD(*(s2-1)))
|
2002-05-13 23:03:09 +00:00
|
|
|
) {
|
|
|
|
/* part of a surrogate pair, leave >=d800 */
|
|
|
|
} else {
|
|
|
|
/* BMP code point - may be surrogate code point - make <d800 */
|
|
|
|
c2-=0x2800;
|
|
|
|
}
|
2001-06-26 21:33:04 +00:00
|
|
|
}
|
2001-06-25 23:44:21 +00:00
|
|
|
|
2003-01-14 21:18:52 +00:00
|
|
|
/* now c1 and c2 are in the requested (code unit or code point) order */
|
2001-06-25 23:44:21 +00:00
|
|
|
return (int32_t)c1-(int32_t)c2;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 21:18:52 +00:00
|
|
|
/*
|
|
|
|
* Compare two strings as presented by UCharIterators.
|
|
|
|
* Use code unit or code point order.
|
|
|
|
* When the function returns, it is undefined where the iterators
|
|
|
|
* have stopped.
|
|
|
|
*/
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_strCompareIter(UCharIterator *iter1, UCharIterator *iter2, UBool codePointOrder) {
|
|
|
|
UChar32 c1, c2;
|
|
|
|
|
|
|
|
/* argument checking */
|
|
|
|
if(iter1==NULL || iter2==NULL) {
|
|
|
|
return 0; /* bad arguments */
|
|
|
|
}
|
|
|
|
if(iter1==iter2) {
|
|
|
|
return 0; /* identical iterators */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset iterators to start? */
|
|
|
|
iter1->move(iter1, 0, UITER_START);
|
|
|
|
iter2->move(iter2, 0, UITER_START);
|
|
|
|
|
|
|
|
/* compare identical prefixes - they do not need to be fixed up */
|
|
|
|
for(;;) {
|
|
|
|
c1=iter1->next(iter1);
|
|
|
|
c2=iter2->next(iter2);
|
|
|
|
if(c1!=c2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(c1==-1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if both values are in or above the surrogate range, fix them up */
|
|
|
|
if(c1>=0xd800 && c2>=0xd800 && codePointOrder) {
|
|
|
|
/* subtract 0x2800 from BMP code points to make them smaller than supplementary ones */
|
|
|
|
if(
|
2011-07-27 05:53:56 +00:00
|
|
|
(c1<=0xdbff && U16_IS_TRAIL(iter1->current(iter1))) ||
|
|
|
|
(U16_IS_TRAIL(c1) && (iter1->previous(iter1), U16_IS_LEAD(iter1->previous(iter1))))
|
2003-01-14 21:18:52 +00:00
|
|
|
) {
|
|
|
|
/* part of a surrogate pair, leave >=d800 */
|
|
|
|
} else {
|
|
|
|
/* BMP code point - may be surrogate code point - make <d800 */
|
|
|
|
c1-=0x2800;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(
|
2011-07-27 05:53:56 +00:00
|
|
|
(c2<=0xdbff && U16_IS_TRAIL(iter2->current(iter2))) ||
|
|
|
|
(U16_IS_TRAIL(c2) && (iter2->previous(iter2), U16_IS_LEAD(iter2->previous(iter2))))
|
2003-01-14 21:18:52 +00:00
|
|
|
) {
|
|
|
|
/* part of a surrogate pair, leave >=d800 */
|
|
|
|
} else {
|
|
|
|
/* BMP code point - may be surrogate code point - make <d800 */
|
|
|
|
c2-=0x2800;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now c1 and c2 are in the requested (code unit or code point) order */
|
|
|
|
return (int32_t)c1-(int32_t)c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* u_strCompareIter() does not leave the iterators _on_ the different units.
|
|
|
|
* This is possible but would cost a few extra indirect function calls to back
|
|
|
|
* up if the last unit (c1 or c2 respectively) was >=0.
|
|
|
|
*
|
|
|
|
* Consistently leaving them _behind_ the different units is not an option
|
|
|
|
* because the current "unit" is the end of the string if that is reached,
|
|
|
|
* and in such a case the iterator does not move.
|
|
|
|
* For example, when comparing "ab" with "abc", both iterators rest _on_ the end
|
|
|
|
* of their strings. Calling previous() on each does not move them to where
|
|
|
|
* the comparison fails.
|
|
|
|
*
|
|
|
|
* So the simplest semantics is to not define where the iterators end up.
|
|
|
|
*
|
|
|
|
* The following fragment is part of what would need to be done for backing up.
|
|
|
|
*/
|
|
|
|
void fragment {
|
|
|
|
/* iff a surrogate is part of a surrogate pair, leave >=d800 */
|
|
|
|
if(c1<=0xdbff) {
|
2011-07-27 05:53:56 +00:00
|
|
|
if(!U16_IS_TRAIL(iter1->current(iter1))) {
|
2003-01-14 21:18:52 +00:00
|
|
|
/* lead surrogate code point - make <d800 */
|
|
|
|
c1-=0x2800;
|
|
|
|
}
|
|
|
|
} else if(c1<=0xdfff) {
|
2009-04-15 01:57:02 +00:00
|
|
|
int32_t idx=iter1->getIndex(iter1, UITER_CURRENT);
|
2003-01-14 21:18:52 +00:00
|
|
|
iter1->previous(iter1); /* ==c1 */
|
2011-07-27 05:53:56 +00:00
|
|
|
if(!U16_IS_LEAD(iter1->previous(iter1))) {
|
2003-01-14 21:18:52 +00:00
|
|
|
/* trail surrogate code point - make <d800 */
|
|
|
|
c1-=0x2800;
|
|
|
|
}
|
|
|
|
/* go back to behind where the difference is */
|
2009-04-15 01:57:02 +00:00
|
|
|
iter1->move(iter1, idx, UITER_ZERO);
|
2003-01-14 21:18:52 +00:00
|
|
|
} else /* 0xe000<=c1<=0xffff */ {
|
|
|
|
/* BMP code point - make <d800 */
|
|
|
|
c1-=0x2800;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-05-24 17:05:31 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_strCompare(const UChar *s1, int32_t length1,
|
|
|
|
const UChar *s2, int32_t length2,
|
|
|
|
UBool codePointOrder) {
|
|
|
|
/* argument checking */
|
|
|
|
if(s1==NULL || length1<-1 || s2==NULL || length2<-1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return uprv_strCompare(s1, length1, s2, length2, FALSE, codePointOrder);
|
|
|
|
}
|
|
|
|
|
2002-05-13 23:03:09 +00:00
|
|
|
/* String compare in code point order - u_strcmp() compares in code unit order. */
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_strcmpCodePointOrder(const UChar *s1, const UChar *s2) {
|
2002-05-24 17:05:31 +00:00
|
|
|
return uprv_strCompare(s1, -1, s2, -1, FALSE, TRUE);
|
2002-05-13 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strncmp(const UChar *s1,
|
|
|
|
const UChar *s2,
|
|
|
|
int32_t n)
|
|
|
|
{
|
2001-03-15 23:45:54 +00:00
|
|
|
if(n > 0) {
|
|
|
|
int32_t rc;
|
|
|
|
for(;;) {
|
|
|
|
rc = (int32_t)*s1 - (int32_t)*s2;
|
|
|
|
if(rc != 0 || *s1 == 0 || --n == 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
++s1;
|
|
|
|
++s2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-25 23:44:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_strncmpCodePointOrder(const UChar *s1, const UChar *s2, int32_t n) {
|
2002-05-24 17:05:31 +00:00
|
|
|
return uprv_strCompare(s1, n, s2, n, TRUE, TRUE);
|
2001-06-25 23:44:21 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strcpy(UChar *dst,
|
|
|
|
const UChar *src)
|
|
|
|
{
|
2001-03-15 23:45:54 +00:00
|
|
|
UChar *anchor = dst; /* save a pointer to start of dst */
|
2001-01-30 20:58:38 +00:00
|
|
|
|
2001-03-15 23:45:54 +00:00
|
|
|
while((*(dst++) = *(src++)) != 0) { /* copy string 2 over */
|
|
|
|
}
|
2001-01-30 20:58:38 +00:00
|
|
|
|
2001-03-15 23:45:54 +00:00
|
|
|
return anchor;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strncpy(UChar *dst,
|
|
|
|
const UChar *src,
|
|
|
|
int32_t n)
|
|
|
|
{
|
2001-03-15 23:45:54 +00:00
|
|
|
UChar *anchor = dst; /* save a pointer to start of dst */
|
|
|
|
|
2001-11-28 23:20:08 +00:00
|
|
|
/* copy string 2 over */
|
|
|
|
while(n > 0 && (*(dst++) = *(src++)) != 0) {
|
|
|
|
--n;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2001-03-15 23:45:54 +00:00
|
|
|
return anchor;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-01-30 20:58:38 +00:00
|
|
|
u_strlen(const UChar *s)
|
|
|
|
{
|
2001-03-15 23:45:54 +00:00
|
|
|
#if U_SIZEOF_WCHAR_T == U_SIZEOF_UCHAR
|
2016-09-13 21:37:40 +00:00
|
|
|
return (int32_t)uprv_wcslen((const wchar_t *)s);
|
2001-03-15 23:45:54 +00:00
|
|
|
#else
|
2001-01-30 20:58:38 +00:00
|
|
|
const UChar *t = s;
|
|
|
|
while(*t != 0) {
|
|
|
|
++t;
|
|
|
|
}
|
|
|
|
return t - s;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-10-11 21:39:09 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_countChar32(const UChar *s, int32_t length) {
|
|
|
|
int32_t count;
|
|
|
|
|
|
|
|
if(s==NULL || length<-1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
count=0;
|
|
|
|
if(length>=0) {
|
|
|
|
while(length>0) {
|
|
|
|
++count;
|
2011-07-27 05:53:56 +00:00
|
|
|
if(U16_IS_LEAD(*s) && length>=2 && U16_IS_TRAIL(*(s+1))) {
|
2001-10-11 21:39:09 +00:00
|
|
|
s+=2;
|
|
|
|
length-=2;
|
|
|
|
} else {
|
|
|
|
++s;
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else /* length==-1 */ {
|
|
|
|
UChar c;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if((c=*s++)==0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sufficient to look ahead one because of UTF-16;
|
|
|
|
* safe to look ahead one because at worst that would be the terminating NUL
|
|
|
|
*/
|
2011-07-27 05:53:56 +00:00
|
|
|
if(U16_IS_LEAD(c) && U16_IS_TRAIL(*s)) {
|
2001-10-11 21:39:09 +00:00
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2002-10-09 20:40:05 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
u_strHasMoreChar32Than(const UChar *s, int32_t length, int32_t number) {
|
|
|
|
|
|
|
|
if(number<0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if(s==NULL || length<-1) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(length==-1) {
|
|
|
|
/* s is NUL-terminated */
|
|
|
|
UChar c;
|
|
|
|
|
|
|
|
/* count code points until they exceed */
|
|
|
|
for(;;) {
|
|
|
|
if((c=*s++)==0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if(number==0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if(U16_IS_LEAD(c) && U16_IS_TRAIL(*s)) {
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
--number;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* length>=0 known */
|
|
|
|
const UChar *limit;
|
|
|
|
int32_t maxSupplementary;
|
|
|
|
|
|
|
|
/* s contains at least (length+1)/2 code points: <=2 UChars per cp */
|
|
|
|
if(((length+1)/2)>number) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if s does not even contain enough UChars */
|
|
|
|
maxSupplementary=length-number;
|
|
|
|
if(maxSupplementary<=0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* there are maxSupplementary=length-number more UChars than asked-for code points */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* count code points until they exceed and also check that there are
|
|
|
|
* no more than maxSupplementary supplementary code points (UChar pairs)
|
|
|
|
*/
|
|
|
|
limit=s+length;
|
|
|
|
for(;;) {
|
|
|
|
if(s==limit) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if(number==0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if(U16_IS_LEAD(*s++) && s!=limit && U16_IS_TRAIL(*s)) {
|
|
|
|
++s;
|
|
|
|
if(--maxSupplementary<=0) {
|
|
|
|
/* too many pairs - too few code points */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
2001-02-15 03:34:29 +00:00
|
|
|
u_memcpy(UChar *dest, const UChar *src, int32_t count) {
|
2012-06-07 00:14:54 +00:00
|
|
|
if(count > 0) {
|
2016-09-20 01:10:36 +00:00
|
|
|
uprv_memcpy(dest, src, (size_t)count*U_SIZEOF_UCHAR);
|
2012-06-07 00:14:54 +00:00
|
|
|
}
|
|
|
|
return dest;
|
2001-02-15 03:34:29 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
2001-02-15 03:34:29 +00:00
|
|
|
u_memmove(UChar *dest, const UChar *src, int32_t count) {
|
2012-06-07 00:14:54 +00:00
|
|
|
if(count > 0) {
|
2016-09-20 01:10:36 +00:00
|
|
|
uprv_memmove(dest, src, (size_t)count*U_SIZEOF_UCHAR);
|
2012-06-07 00:14:54 +00:00
|
|
|
}
|
|
|
|
return dest;
|
2001-02-15 03:34:29 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
2001-02-15 03:34:29 +00:00
|
|
|
u_memset(UChar *dest, UChar c, int32_t count) {
|
2001-11-28 23:49:32 +00:00
|
|
|
if(count > 0) {
|
|
|
|
UChar *ptr = dest;
|
|
|
|
UChar *limit = dest + count;
|
2001-02-15 03:34:29 +00:00
|
|
|
|
2001-11-28 23:49:32 +00:00
|
|
|
while (ptr < limit) {
|
|
|
|
*(ptr++) = c;
|
|
|
|
}
|
2001-02-15 03:34:29 +00:00
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2002-03-12 22:59:04 +00:00
|
|
|
u_memcmp(const UChar *buf1, const UChar *buf2, int32_t count) {
|
2001-11-28 23:49:32 +00:00
|
|
|
if(count > 0) {
|
2002-03-12 22:59:04 +00:00
|
|
|
const UChar *limit = buf1 + count;
|
2001-11-28 23:49:32 +00:00
|
|
|
int32_t result;
|
|
|
|
|
|
|
|
while (buf1 < limit) {
|
|
|
|
result = (int32_t)(uint16_t)*buf1 - (int32_t)(uint16_t)*buf2;
|
|
|
|
if (result != 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
buf1++;
|
|
|
|
buf2++;
|
2001-02-21 22:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-25 23:44:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_memcmpCodePointOrder(const UChar *s1, const UChar *s2, int32_t count) {
|
2002-05-24 17:05:31 +00:00
|
|
|
return uprv_strCompare(s1, count, s2, count, FALSE, TRUE);
|
2001-06-25 23:44:21 +00:00
|
|
|
}
|
|
|
|
|
2000-07-17 21:02:18 +00:00
|
|
|
/* u_unescape & support fns ------------------------------------------------- */
|
|
|
|
|
2000-08-16 20:24:57 +00:00
|
|
|
/* This map must be in ASCENDING ORDER OF THE ESCAPE CODE */
|
2000-07-17 21:02:18 +00:00
|
|
|
static const UChar UNESCAPE_MAP[] = {
|
|
|
|
/*" 0x22, 0x22 */
|
|
|
|
/*' 0x27, 0x27 */
|
|
|
|
/*? 0x3F, 0x3F */
|
|
|
|
/*\ 0x5C, 0x5C */
|
|
|
|
/*a*/ 0x61, 0x07,
|
|
|
|
/*b*/ 0x62, 0x08,
|
2002-11-01 14:36:38 +00:00
|
|
|
/*e*/ 0x65, 0x1b,
|
2000-07-17 21:02:18 +00:00
|
|
|
/*f*/ 0x66, 0x0c,
|
|
|
|
/*n*/ 0x6E, 0x0a,
|
|
|
|
/*r*/ 0x72, 0x0d,
|
|
|
|
/*t*/ 0x74, 0x09,
|
|
|
|
/*v*/ 0x76, 0x0b
|
|
|
|
};
|
2016-02-23 10:40:09 +00:00
|
|
|
enum { UNESCAPE_MAP_LENGTH = UPRV_LENGTHOF(UNESCAPE_MAP) };
|
2000-07-17 21:02:18 +00:00
|
|
|
|
|
|
|
/* Convert one octal digit to a numeric value 0..7, or -1 on failure */
|
|
|
|
static int8_t _digit8(UChar c) {
|
|
|
|
if (c >= 0x0030 && c <= 0x0037) {
|
2000-08-10 01:59:18 +00:00
|
|
|
return (int8_t)(c - 0x0030);
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert one hex digit to a numeric value 0..F, or -1 on failure */
|
|
|
|
static int8_t _digit16(UChar c) {
|
|
|
|
if (c >= 0x0030 && c <= 0x0039) {
|
2000-08-10 01:59:18 +00:00
|
|
|
return (int8_t)(c - 0x0030);
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
if (c >= 0x0041 && c <= 0x0046) {
|
2000-08-10 01:59:18 +00:00
|
|
|
return (int8_t)(c - (0x0041 - 10));
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
if (c >= 0x0061 && c <= 0x0066) {
|
2000-08-10 01:59:18 +00:00
|
|
|
return (int8_t)(c - (0x0061 - 10));
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse a single escape sequence. Although this method deals in
|
|
|
|
* UChars, it does not use C++ or UnicodeString. This allows it to
|
|
|
|
* be used from C contexts. */
|
2000-08-16 23:31:50 +00:00
|
|
|
U_CAPI UChar32 U_EXPORT2
|
2000-07-17 21:02:18 +00:00
|
|
|
u_unescapeAt(UNESCAPE_CHAR_AT charAt,
|
|
|
|
int32_t *offset,
|
|
|
|
int32_t length,
|
|
|
|
void *context) {
|
|
|
|
|
|
|
|
int32_t start = *offset;
|
|
|
|
UChar c;
|
|
|
|
UChar32 result = 0;
|
|
|
|
int8_t n = 0;
|
|
|
|
int8_t minDig = 0;
|
|
|
|
int8_t maxDig = 0;
|
|
|
|
int8_t bitsPerDigit = 4;
|
|
|
|
int8_t dig;
|
|
|
|
int32_t i;
|
2003-05-09 00:22:25 +00:00
|
|
|
UBool braces = FALSE;
|
2000-07-17 21:02:18 +00:00
|
|
|
|
|
|
|
/* Check that offset is in range */
|
|
|
|
if (*offset < 0 || *offset >= length) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetch first UChar after '\\' */
|
|
|
|
c = charAt((*offset)++, context);
|
|
|
|
|
|
|
|
/* Convert hexadecimal and octal escapes */
|
|
|
|
switch (c) {
|
|
|
|
case 0x0075 /*'u'*/:
|
|
|
|
minDig = maxDig = 4;
|
|
|
|
break;
|
|
|
|
case 0x0055 /*'U'*/:
|
|
|
|
minDig = maxDig = 8;
|
|
|
|
break;
|
|
|
|
case 0x0078 /*'x'*/:
|
|
|
|
minDig = 1;
|
2003-05-09 00:22:25 +00:00
|
|
|
if (*offset < length && charAt(*offset, context) == 0x7B /*{*/) {
|
|
|
|
++(*offset);
|
|
|
|
braces = TRUE;
|
|
|
|
maxDig = 8;
|
|
|
|
} else {
|
|
|
|
maxDig = 2;
|
|
|
|
}
|
2000-07-17 21:02:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dig = _digit8(c);
|
|
|
|
if (dig >= 0) {
|
|
|
|
minDig = 1;
|
|
|
|
maxDig = 3;
|
|
|
|
n = 1; /* Already have first octal digit */
|
|
|
|
bitsPerDigit = 3;
|
|
|
|
result = dig;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (minDig != 0) {
|
|
|
|
while (*offset < length && n < maxDig) {
|
|
|
|
c = charAt(*offset, context);
|
2000-08-10 01:59:18 +00:00
|
|
|
dig = (int8_t)((bitsPerDigit == 3) ? _digit8(c) : _digit16(c));
|
2000-07-17 21:02:18 +00:00
|
|
|
if (dig < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = (result << bitsPerDigit) | dig;
|
|
|
|
++(*offset);
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
if (n < minDig) {
|
|
|
|
goto err;
|
|
|
|
}
|
2003-05-09 00:22:25 +00:00
|
|
|
if (braces) {
|
|
|
|
if (c != 0x7D /*}*/) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
++(*offset);
|
|
|
|
}
|
2003-09-29 20:42:21 +00:00
|
|
|
if (result < 0 || result >= 0x110000) {
|
|
|
|
goto err;
|
|
|
|
}
|
2003-10-07 21:01:48 +00:00
|
|
|
/* If an escape sequence specifies a lead surrogate, see if
|
|
|
|
* there is a trail surrogate after it, either as an escape or
|
|
|
|
* as a literal. If so, join them up into a supplementary.
|
|
|
|
*/
|
2003-10-07 18:10:37 +00:00
|
|
|
if (*offset < length && U16_IS_LEAD(result)) {
|
|
|
|
int32_t ahead = *offset + 1;
|
|
|
|
c = charAt(*offset, context);
|
|
|
|
if (c == 0x5C /*'\\'*/ && ahead < length) {
|
|
|
|
c = (UChar) u_unescapeAt(charAt, &ahead, length, context);
|
|
|
|
}
|
|
|
|
if (U16_IS_TRAIL(c)) {
|
|
|
|
*offset = ahead;
|
|
|
|
result = U16_GET_SUPPLEMENTARY(result, c);
|
|
|
|
}
|
|
|
|
}
|
2000-07-17 21:02:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert C-style escapes in table */
|
|
|
|
for (i=0; i<UNESCAPE_MAP_LENGTH; i+=2) {
|
|
|
|
if (c == UNESCAPE_MAP[i]) {
|
|
|
|
return UNESCAPE_MAP[i+1];
|
2000-08-16 20:24:57 +00:00
|
|
|
} else if (c < UNESCAPE_MAP[i]) {
|
2000-07-17 21:02:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-01 14:36:38 +00:00
|
|
|
/* Map \cX to control-X: X & 0x1F */
|
|
|
|
if (c == 0x0063 /*'c'*/ && *offset < length) {
|
|
|
|
c = charAt((*offset)++, context);
|
2011-07-27 05:53:56 +00:00
|
|
|
if (U16_IS_LEAD(c) && *offset < length) {
|
2002-11-01 14:36:38 +00:00
|
|
|
UChar c2 = charAt(*offset, context);
|
2011-07-27 05:53:56 +00:00
|
|
|
if (U16_IS_TRAIL(c2)) {
|
2002-11-01 14:36:38 +00:00
|
|
|
++(*offset);
|
2011-07-27 05:53:56 +00:00
|
|
|
c = (UChar) U16_GET_SUPPLEMENTARY(c, c2); /* [sic] */
|
2002-11-01 14:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0x1F & c;
|
|
|
|
}
|
|
|
|
|
2000-07-17 21:02:18 +00:00
|
|
|
/* If no special forms are recognized, then consider
|
2000-08-16 23:54:42 +00:00
|
|
|
* the backslash to generically escape the next character.
|
|
|
|
* Deal with surrogate pairs. */
|
2011-07-27 05:53:56 +00:00
|
|
|
if (U16_IS_LEAD(c) && *offset < length) {
|
2000-08-16 23:54:42 +00:00
|
|
|
UChar c2 = charAt(*offset, context);
|
2011-07-27 05:53:56 +00:00
|
|
|
if (U16_IS_TRAIL(c2)) {
|
2000-08-16 23:54:42 +00:00
|
|
|
++(*offset);
|
2011-07-27 05:53:56 +00:00
|
|
|
return U16_GET_SUPPLEMENTARY(c, c2);
|
2000-08-16 23:54:42 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-17 21:02:18 +00:00
|
|
|
return c;
|
|
|
|
|
|
|
|
err:
|
|
|
|
/* Invalid escape sequence */
|
|
|
|
*offset = start; /* Reset to initial value */
|
|
|
|
return (UChar32)0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* u_unescapeAt() callback to return a UChar from a char* */
|
2002-07-23 23:01:08 +00:00
|
|
|
static UChar U_CALLCONV
|
|
|
|
_charPtr_charAt(int32_t offset, void *context) {
|
2000-07-17 21:02:18 +00:00
|
|
|
UChar c16;
|
|
|
|
/* It would be more efficient to access the invariant tables
|
|
|
|
* directly but there is no API for that. */
|
|
|
|
u_charsToUChars(((char*) context) + offset, &c16, 1);
|
|
|
|
return c16;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append an escape-free segment of the text; used by u_unescape() */
|
|
|
|
static void _appendUChars(UChar *dest, int32_t destCapacity,
|
|
|
|
const char *src, int32_t srcLen) {
|
|
|
|
if (destCapacity < 0) {
|
|
|
|
destCapacity = 0;
|
|
|
|
}
|
|
|
|
if (srcLen > destCapacity) {
|
|
|
|
srcLen = destCapacity;
|
|
|
|
}
|
|
|
|
u_charsToUChars(src, dest, srcLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do an invariant conversion of char* -> UChar*, with escape parsing */
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_unescape(const char *src, UChar *dest, int32_t destCapacity) {
|
|
|
|
const char *segment = src;
|
2000-08-16 23:31:50 +00:00
|
|
|
int32_t i = 0;
|
2000-07-17 21:02:18 +00:00
|
|
|
char c;
|
|
|
|
|
|
|
|
while ((c=*src) != 0) {
|
|
|
|
/* '\\' intentionally written as compiler-specific
|
|
|
|
* character constant to correspond to compiler-specific
|
|
|
|
* char* constants. */
|
|
|
|
if (c == '\\') {
|
|
|
|
int32_t lenParsed = 0;
|
|
|
|
UChar32 c32;
|
|
|
|
if (src != segment) {
|
2000-08-17 17:12:38 +00:00
|
|
|
if (dest != NULL) {
|
|
|
|
_appendUChars(dest + i, destCapacity - i,
|
2009-11-11 15:47:22 +00:00
|
|
|
segment, (int32_t)(src - segment));
|
2000-08-17 17:12:38 +00:00
|
|
|
}
|
2009-11-11 15:47:22 +00:00
|
|
|
i += (int32_t)(src - segment);
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
++src; /* advance past '\\' */
|
2009-11-11 15:47:22 +00:00
|
|
|
c32 = (UChar32)u_unescapeAt(_charPtr_charAt, &lenParsed, (int32_t)uprv_strlen(src), (void*)src);
|
2000-07-17 21:02:18 +00:00
|
|
|
if (lenParsed == 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
src += lenParsed; /* advance past escape seq. */
|
2011-07-27 05:53:56 +00:00
|
|
|
if (dest != NULL && U16_LENGTH(c32) <= (destCapacity - i)) {
|
|
|
|
U16_APPEND_UNSAFE(dest, i, c32);
|
2000-08-16 23:31:50 +00:00
|
|
|
} else {
|
2011-07-27 05:53:56 +00:00
|
|
|
i += U16_LENGTH(c32);
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
segment = src;
|
|
|
|
} else {
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (src != segment) {
|
2000-08-17 17:12:38 +00:00
|
|
|
if (dest != NULL) {
|
|
|
|
_appendUChars(dest + i, destCapacity - i,
|
2009-11-11 15:47:22 +00:00
|
|
|
segment, (int32_t)(src - segment));
|
2000-08-17 17:12:38 +00:00
|
|
|
}
|
2009-11-11 15:47:22 +00:00
|
|
|
i += (int32_t)(src - segment);
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
2000-08-17 17:12:38 +00:00
|
|
|
if (dest != NULL && i < destCapacity) {
|
2000-08-16 23:31:50 +00:00
|
|
|
dest[i] = 0;
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
2002-09-06 04:31:03 +00:00
|
|
|
return i;
|
2000-07-17 21:02:18 +00:00
|
|
|
|
|
|
|
err:
|
2000-08-16 23:31:50 +00:00
|
|
|
if (dest != NULL && destCapacity > 0) {
|
|
|
|
*dest = 0;
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2000-06-29 19:36:20 +00:00
|
|
|
}
|
2001-06-25 22:59:23 +00:00
|
|
|
|
2001-09-05 23:37:16 +00:00
|
|
|
/* NUL-termination of strings ----------------------------------------------- */
|
|
|
|
|
2001-09-18 00:56:43 +00:00
|
|
|
/**
|
|
|
|
* NUL-terminate a string no matter what its type.
|
|
|
|
* Set warning and error codes accordingly.
|
|
|
|
*/
|
2019-08-14 21:05:37 +00:00
|
|
|
#define __TERMINATE_STRING(dest, destCapacity, length, pErrorCode) UPRV_BLOCK_MACRO_BEGIN { \
|
2001-09-18 00:56:43 +00:00
|
|
|
if(pErrorCode!=NULL && U_SUCCESS(*pErrorCode)) { \
|
|
|
|
/* not a public function, so no complete argument checking */ \
|
2007-08-31 19:39:14 +00:00
|
|
|
\
|
2001-11-28 23:59:09 +00:00
|
|
|
if(length<0) { \
|
|
|
|
/* assume that the caller handles this */ \
|
2007-08-31 19:39:14 +00:00
|
|
|
} else if(length<destCapacity) { \
|
2001-09-18 00:56:43 +00:00
|
|
|
/* NUL-terminate the string, the NUL fits */ \
|
2007-08-31 19:39:14 +00:00
|
|
|
dest[length]=0; \
|
2001-10-03 23:15:15 +00:00
|
|
|
/* unset the not-terminated warning but leave all others */ \
|
|
|
|
if(*pErrorCode==U_STRING_NOT_TERMINATED_WARNING) { \
|
|
|
|
*pErrorCode=U_ZERO_ERROR; \
|
|
|
|
} \
|
2001-09-18 00:56:43 +00:00
|
|
|
} 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; \
|
|
|
|
} \
|
2019-08-14 21:05:37 +00:00
|
|
|
} \
|
|
|
|
} UPRV_BLOCK_MACRO_END
|
2001-09-18 00:56:43 +00:00
|
|
|
|
2001-09-05 23:37:16 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_terminateUChars(UChar *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode) {
|
2007-08-31 19:39:14 +00:00
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
2001-09-18 00:56:43 +00:00
|
|
|
return length;
|
|
|
|
}
|
2001-09-05 23:37:16 +00:00
|
|
|
|
2001-09-18 00:56:43 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2007-08-31 19:39:14 +00:00
|
|
|
u_terminateChars(char *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode) {
|
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
2001-09-18 00:56:43 +00:00
|
|
|
return length;
|
|
|
|
}
|
2001-09-05 23:37:16 +00:00
|
|
|
|
2001-09-18 00:56:43 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_terminateUChar32s(UChar32 *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode) {
|
2007-08-31 19:39:14 +00:00
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
2001-09-18 00:56:43 +00:00
|
|
|
return length;
|
|
|
|
}
|
2001-09-05 23:37:16 +00:00
|
|
|
|
2001-09-18 00:56:43 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_terminateWChars(wchar_t *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode) {
|
2007-08-31 19:39:14 +00:00
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
2001-09-05 23:37:16 +00:00
|
|
|
return length;
|
|
|
|
}
|
2011-06-03 05:23:57 +00:00
|
|
|
|
|
|
|
// Compute the hash code for a string -------------------------------------- ***
|
|
|
|
|
|
|
|
// Moved here from uhash.c so that UnicodeString::hashCode() does not depend
|
|
|
|
// on UHashtable code.
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the hash by iterating sparsely over about 32 (up to 63)
|
|
|
|
characters spaced evenly through the string. For each character,
|
|
|
|
multiply the previous hash value by a prime number and add the new
|
|
|
|
character in, like a linear congruential random number generator,
|
|
|
|
producing a pseudorandom deterministic value well distributed over
|
|
|
|
the output range. [LIU]
|
|
|
|
*/
|
|
|
|
|
2019-08-14 21:05:37 +00:00
|
|
|
#define STRING_HASH(TYPE, STR, STRLEN, DEREF) UPRV_BLOCK_MACRO_BEGIN { \
|
2014-07-09 22:02:52 +00:00
|
|
|
uint32_t hash = 0; \
|
2011-06-03 05:23:57 +00:00
|
|
|
const TYPE *p = (const TYPE*) STR; \
|
|
|
|
if (p != NULL) { \
|
|
|
|
int32_t len = (int32_t)(STRLEN); \
|
|
|
|
int32_t inc = ((len - 32) / 32) + 1; \
|
|
|
|
const TYPE *limit = p + len; \
|
|
|
|
while (p<limit) { \
|
|
|
|
hash = (hash * 37) + DEREF; \
|
|
|
|
p += inc; \
|
|
|
|
} \
|
|
|
|
} \
|
2019-08-14 21:05:37 +00:00
|
|
|
return static_cast<int32_t>(hash); \
|
|
|
|
} UPRV_BLOCK_MACRO_END
|
2011-06-03 05:23:57 +00:00
|
|
|
|
|
|
|
/* Used by UnicodeString to compute its hashcode - Not public API. */
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
ustr_hashUCharsN(const UChar *str, int32_t length) {
|
|
|
|
STRING_HASH(UChar, str, length, *p);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
ustr_hashCharsN(const char *str, int32_t length) {
|
|
|
|
STRING_HASH(uint8_t, str, length, *p);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
ustr_hashICharsN(const char *str, int32_t length) {
|
|
|
|
STRING_HASH(char, str, length, (uint8_t)uprv_tolower(*p));
|
|
|
|
}
|