1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
* Copyright (C) 1998-2001, International Business Machines
|
2000-01-13 23:54:23 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File ustring.h
|
|
|
|
*
|
|
|
|
* 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"
|
2002-02-21 04:48:21 +00:00
|
|
|
#include "unicode/ustring.h"
|
2001-01-23 02:40:22 +00:00
|
|
|
#include "unicode/putil.h"
|
2001-01-31 18:20:26 +00:00
|
|
|
#include "unicode/ucnv.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"
|
1999-12-09 01:03:31 +00:00
|
|
|
#include "umutex.h"
|
2001-01-31 18:20:26 +00:00
|
|
|
#include "ustr_imp.h"
|
2001-08-11 00:29:58 +00:00
|
|
|
#include "ucln_cmn.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-09 01:03:31 +00:00
|
|
|
/* forward declaractions of definitions for the shared default converter */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
static UConverter *gDefaultConverter = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-09 01:03:31 +00:00
|
|
|
/* ANSI string.h - style functions ------------------------------------------ */
|
|
|
|
|
|
|
|
#define MAX_STRLEN 0x0FFFFFFF
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-01-30 20:58:38 +00:00
|
|
|
/* ---- String searching functions ---- */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
2000-10-12 22:00:33 +00:00
|
|
|
u_strchr(const UChar *s, UChar c)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-10-12 22:00:33 +00:00
|
|
|
while (*s && *s != c) {
|
1999-12-08 00:15:46 +00:00
|
|
|
++s;
|
|
|
|
}
|
2000-10-12 22:00:33 +00:00
|
|
|
if (*s == c)
|
|
|
|
return (UChar *)s;
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-07-17 20:47:02 +00:00
|
|
|
/* A Boyer-Moore algorithm would be better, but that would require a hashtable
|
|
|
|
because UChar is so big. This algorithm doesn't use a lot of extra memory.
|
|
|
|
*/
|
2000-06-29 18:52:11 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strstr(const UChar *s, const UChar *substring) {
|
2000-07-17 20:47:02 +00:00
|
|
|
|
|
|
|
UChar *strItr, *subItr;
|
|
|
|
|
|
|
|
if (*substring == 0) {
|
2000-06-29 18:52:11 +00:00
|
|
|
return (UChar *)s;
|
|
|
|
}
|
2000-07-17 20:47:02 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
strItr = (UChar *)s;
|
|
|
|
subItr = (UChar *)substring;
|
|
|
|
|
|
|
|
/* Only one string iterator needs checking for null terminator */
|
|
|
|
while ((*strItr != 0) && (*strItr == *subItr)) {
|
|
|
|
strItr++;
|
2000-08-11 21:24:17 +00:00
|
|
|
subItr++;
|
|
|
|
}
|
2000-07-17 20:47:02 +00:00
|
|
|
|
|
|
|
if (*subItr == 0) { /* Was the end of the substring reached? */
|
|
|
|
return (UChar *)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
s++;
|
|
|
|
} while (*strItr != 0); /* Was the end of the string reached? */
|
|
|
|
|
|
|
|
return NULL; /* No match */
|
2000-06-29 18:52:11 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 23:19:41 +00:00
|
|
|
/**
|
|
|
|
* Check if there is an unmatched surrogate c in a string [start..limit[ at s.
|
|
|
|
* start<=s<limit or limit==NULL
|
|
|
|
* @return TRUE if *s is unmatched
|
|
|
|
*/
|
|
|
|
static U_INLINE UBool
|
|
|
|
uprv_isSingleSurrogate(const UChar *start, const UChar *s, UChar c, const UChar *limit) {
|
|
|
|
if(UTF_IS_SURROGATE_FIRST(c)) {
|
|
|
|
++s;
|
|
|
|
return (UBool)(s==limit || !UTF_IS_TRAIL(*s));
|
|
|
|
} else {
|
|
|
|
return (UBool)(s==start || !UTF_IS_LEAD(*(s-1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC const UChar *
|
|
|
|
uprv_strFindSurrogate(const UChar *s, int32_t length, UChar surrogate) {
|
|
|
|
const UChar *limit, *t;
|
|
|
|
UChar c;
|
|
|
|
|
|
|
|
if(length>=0) {
|
|
|
|
limit=s+length;
|
|
|
|
} else {
|
|
|
|
limit=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(t=s; t!=limit && ((c=*t)!=0 || limit!=NULL); ++t) {
|
|
|
|
if(c==surrogate && uprv_isSingleSurrogate(s, t, c, limit)) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC const UChar *
|
|
|
|
uprv_strFindLastSurrogate(const UChar *s, int32_t length, UChar surrogate) {
|
|
|
|
const UChar *limit, *t;
|
|
|
|
UChar c;
|
|
|
|
|
|
|
|
if(length>=0) {
|
|
|
|
limit=s+length;
|
|
|
|
} else {
|
|
|
|
limit=s+u_strlen(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(t=limit; t!=s;) {
|
|
|
|
c=*--t;
|
|
|
|
if(c==surrogate && uprv_isSingleSurrogate(s, t, c, limit)) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-06-29 19:00:55 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
|
|
|
u_strchr32(const UChar *s, UChar32 c) {
|
2002-02-22 02:00:42 +00:00
|
|
|
if(c < 0xd800) {
|
|
|
|
/* non-surrogate BMP code point */
|
|
|
|
return u_strchr(s, (UChar)c);
|
|
|
|
} else if(c <= 0xdfff) {
|
|
|
|
/* surrogate code point */
|
2002-03-18 23:19:41 +00:00
|
|
|
return (UChar *)uprv_strFindSurrogate(s, -1, (UChar)c);
|
2002-02-22 02:00:42 +00:00
|
|
|
} else if(c <= 0xffff) {
|
|
|
|
/* non-surrogate BMP code point */
|
2000-06-29 19:00:55 +00:00
|
|
|
return u_strchr(s, (UChar)c);
|
|
|
|
} else {
|
2002-02-22 02:00:42 +00:00
|
|
|
/* supplementary code point, search for string */
|
|
|
|
UChar buffer[3];
|
|
|
|
|
|
|
|
buffer[0] = UTF16_LEAD(c);
|
|
|
|
buffer[1] = UTF16_TRAIL(c);
|
|
|
|
buffer[2] = 0;
|
2000-06-29 19:00:55 +00:00
|
|
|
return u_strstr(s, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 UChar * U_EXPORT2
|
2001-01-30 03:12:01 +00:00
|
|
|
u_strpbrk(const UChar *string, const UChar *matchSet)
|
|
|
|
{
|
|
|
|
int32_t matchLen;
|
|
|
|
UBool single = TRUE;
|
|
|
|
|
|
|
|
for (matchLen = 0; matchSet[matchLen]; matchLen++)
|
|
|
|
{
|
|
|
|
if (!UTF_IS_SINGLE(matchSet[matchLen]))
|
|
|
|
{
|
|
|
|
single = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (single)
|
|
|
|
{
|
|
|
|
const UChar *matchItr;
|
|
|
|
const UChar *strItr;
|
|
|
|
|
|
|
|
for (strItr = string; *strItr; strItr++)
|
|
|
|
{
|
|
|
|
for (matchItr = matchSet; *matchItr; matchItr++)
|
|
|
|
{
|
|
|
|
if (*matchItr == *strItr)
|
|
|
|
{
|
|
|
|
return (UChar *)strItr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int32_t matchItr;
|
|
|
|
int32_t strItr;
|
|
|
|
UChar32 stringCh, matchSetCh;
|
|
|
|
int32_t stringLen = u_strlen(string);
|
|
|
|
|
|
|
|
for (strItr = 0; strItr < stringLen; strItr++)
|
|
|
|
{
|
|
|
|
UTF_GET_CHAR_SAFE(string, 0, strItr, stringLen, stringCh, TRUE);
|
|
|
|
for (matchItr = 0; matchItr < matchLen; matchItr++)
|
|
|
|
{
|
|
|
|
UTF_GET_CHAR_SAFE(matchSet, 0, matchItr, matchLen, matchSetCh, TRUE);
|
|
|
|
if (stringCh == matchSetCh && (stringCh != UTF_ERROR_VALUE
|
2001-01-30 18:56:26 +00:00
|
|
|
|| string[strItr] == UTF_ERROR_VALUE
|
2001-03-21 20:44:20 +00:00
|
|
|
|| (matchSetCh == UTF_ERROR_VALUE && !UTF_IS_SINGLE(matchSet[matchItr]))))
|
2001-01-30 03:12:01 +00:00
|
|
|
{
|
|
|
|
return (UChar *)string + strItr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Didn't find it. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
const UChar *foundStr = u_strpbrk(string, matchSet);
|
|
|
|
if (foundStr == NULL)
|
|
|
|
{
|
|
|
|
return u_strlen(string);
|
|
|
|
}
|
|
|
|
return foundStr - string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
UBool single = TRUE;
|
|
|
|
UBool match = TRUE;
|
|
|
|
int32_t matchLen;
|
|
|
|
int32_t retValue;
|
|
|
|
|
|
|
|
for (matchLen = 0; matchSet[matchLen]; matchLen++)
|
|
|
|
{
|
|
|
|
if (!UTF_IS_SINGLE(matchSet[matchLen]))
|
|
|
|
{
|
|
|
|
single = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (single)
|
|
|
|
{
|
|
|
|
const UChar *matchItr;
|
|
|
|
const UChar *strItr;
|
|
|
|
|
|
|
|
for (strItr = string; *strItr && match; strItr++)
|
|
|
|
{
|
|
|
|
match = FALSE;
|
|
|
|
for (matchItr = matchSet; *matchItr; matchItr++)
|
|
|
|
{
|
|
|
|
if (*matchItr == *strItr)
|
|
|
|
{
|
|
|
|
match = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retValue = strItr - string - (match == FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int32_t matchItr;
|
|
|
|
int32_t strItr;
|
|
|
|
UChar32 stringCh, matchSetCh;
|
|
|
|
int32_t stringLen = u_strlen(string);
|
|
|
|
|
|
|
|
for (strItr = 0; strItr < stringLen && match; strItr++)
|
|
|
|
{
|
|
|
|
match = FALSE;
|
|
|
|
UTF_GET_CHAR_SAFE(string, 0, strItr, stringLen, stringCh, TRUE);
|
|
|
|
for (matchItr = 0; matchItr < matchLen; matchItr++)
|
|
|
|
{
|
|
|
|
UTF_GET_CHAR_SAFE(matchSet, 0, matchItr, matchLen, matchSetCh, TRUE);
|
|
|
|
if (stringCh == matchSetCh && (stringCh != UTF_ERROR_VALUE
|
2001-01-30 18:56:26 +00:00
|
|
|
|| string[strItr] == UTF_ERROR_VALUE
|
2001-03-21 20:44:20 +00:00
|
|
|
|| (matchSetCh == UTF_ERROR_VALUE && !UTF_IS_SINGLE(matchSet[matchItr]))))
|
2001-01-30 03:12:01 +00:00
|
|
|
{
|
|
|
|
match = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retValue = strItr - (match == FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Found a mismatch or didn't find it. */
|
|
|
|
return retValue;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-05-22 00:32:42 +00:00
|
|
|
U_CAPI 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(
|
2002-05-14 00:45:47 +00:00
|
|
|
(c1<=0xdbff && (s1+1)!=limit1 && UTF_IS_TRAIL(*(s1+1))) ||
|
2002-05-13 23:03:09 +00:00
|
|
|
(UTF_IS_TRAIL(c1) && start1!=s1 && UTF_IS_LEAD(*(s1-1)))
|
|
|
|
) {
|
|
|
|
/* part of a surrogate pair, leave >=d800 */
|
|
|
|
} else {
|
|
|
|
/* BMP code point - may be surrogate code point - make <d800 */
|
|
|
|
c1-=0x2800;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(
|
2002-05-14 00:45:47 +00:00
|
|
|
(c2<=0xdbff && (s2+1)!=limit2 && UTF_IS_TRAIL(*(s2+1))) ||
|
2002-05-13 23:03:09 +00:00
|
|
|
(UTF_IS_TRAIL(c2) && start2!=s2 && UTF_IS_LEAD(*(s2-1)))
|
|
|
|
) {
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* now c1 and c2 are in UTF-32-compatible order */
|
|
|
|
return (int32_t)c1-(int32_t)c2;
|
2001-01-30 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
2001-01-30 20:58:38 +00:00
|
|
|
return uprv_wcslen(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;
|
|
|
|
if(UTF_IS_LEAD(*s) && length>=2 && UTF_IS_TRAIL(*(s+1))) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
if(UTF_IS_LEAD(c) && UTF_IS_TRAIL(*s)) {
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return (UChar *)uprv_memcpy(dest, src, count*U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return (UChar *)uprv_memmove(dest, src, count*U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
2002-03-12 22:59:04 +00:00
|
|
|
u_memchr(const UChar *src, UChar ch, int32_t count) {
|
2001-11-28 23:49:32 +00:00
|
|
|
if(count > 0) {
|
2002-03-12 22:59:04 +00:00
|
|
|
const UChar *ptr = src;
|
|
|
|
const UChar *limit = src + count;
|
2001-02-21 22:27:39 +00:00
|
|
|
|
2001-11-28 23:49:32 +00:00
|
|
|
do {
|
|
|
|
if (*ptr == ch) {
|
2002-03-12 22:59:04 +00:00
|
|
|
return (UChar *)ptr;
|
2001-11-28 23:49:32 +00:00
|
|
|
}
|
|
|
|
} while (++ptr < limit);
|
2001-02-21 22:27:39 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar * U_EXPORT2
|
2002-03-12 22:59:04 +00:00
|
|
|
u_memchr32(const UChar *src, UChar32 ch, int32_t count) {
|
2001-11-28 23:49:32 +00:00
|
|
|
if(count<=0 || (uint32_t)ch>0x10ffff) {
|
|
|
|
return NULL; /* no string, or illegal arguments */
|
2001-02-21 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 23:19:41 +00:00
|
|
|
if(ch<0xd800) {
|
|
|
|
/* non-surrogate BMP code point */
|
|
|
|
return u_memchr(src, (UChar)ch, count); /* BMP, single UChar */
|
|
|
|
} else if(ch<=0xdfff) {
|
|
|
|
/* surrogate code point */
|
|
|
|
return (UChar *)uprv_strFindSurrogate(src, count, (UChar)ch);
|
|
|
|
} else if(ch<=0xffff) {
|
2001-11-28 23:49:32 +00:00
|
|
|
return u_memchr(src, (UChar)ch, count); /* BMP, single UChar */
|
|
|
|
} else if(count<2) {
|
|
|
|
return NULL; /* too short for a surrogate pair */
|
|
|
|
} else {
|
|
|
|
const UChar *limit=src+count-1; /* -1 so that we do not need a separate check for the trail unit */
|
|
|
|
UChar lead=UTF16_LEAD(ch), trail=UTF16_TRAIL(ch);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(*src==lead && *(src+1)==trail) {
|
2002-03-12 22:59:04 +00:00
|
|
|
return (UChar *)src;
|
2001-11-28 23:49:32 +00:00
|
|
|
}
|
|
|
|
} while(++src<limit);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-21 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
1999-12-09 01:03:31 +00:00
|
|
|
/* conversions between char* and UChar* ------------------------------------- */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-02-08 03:00:09 +00:00
|
|
|
/*
|
|
|
|
returns the minimum of (the length of the null-terminated string) and n.
|
|
|
|
*/
|
|
|
|
static int32_t u_astrnlen(const char *s1, int32_t n)
|
|
|
|
{
|
|
|
|
int32_t len = 0;
|
|
|
|
|
|
|
|
if (s1)
|
|
|
|
{
|
|
|
|
while (*(s1++) && n--)
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
|
|
|
u_uastrncpy(UChar *ucs1,
|
2001-02-08 03:00:09 +00:00
|
|
|
const char *s2,
|
|
|
|
int32_t n)
|
|
|
|
{
|
|
|
|
UChar *target = ucs1;
|
2001-08-11 00:29:58 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
|
|
|
if(U_SUCCESS(err) && cnv != NULL) {
|
2001-02-08 03:00:09 +00:00
|
|
|
ucnv_reset(cnv);
|
|
|
|
ucnv_toUnicode(cnv,
|
|
|
|
&target,
|
|
|
|
ucs1+n,
|
|
|
|
&s2,
|
|
|
|
s2+u_astrnlen(s2, n),
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
&err);
|
|
|
|
ucnv_reset(cnv); /* be good citizens */
|
2001-08-11 00:29:58 +00:00
|
|
|
u_releaseDefaultConverter(cnv);
|
2001-02-08 03:00:09 +00:00
|
|
|
if(U_FAILURE(err) && (err != U_BUFFER_OVERFLOW_ERROR) ) {
|
|
|
|
*ucs1 = 0; /* failure */
|
|
|
|
}
|
|
|
|
if(target < (ucs1+n)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
|
|
|
|
*target = 0; /* terminate */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*ucs1 = 0;
|
|
|
|
}
|
|
|
|
return ucs1;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UChar* U_EXPORT2
|
|
|
|
u_uastrcpy(UChar *ucs1,
|
1999-08-16 21:50:52 +00:00
|
|
|
const char *s2 )
|
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
|
|
|
if(U_SUCCESS(err) && cnv != NULL) {
|
1999-12-09 01:03:31 +00:00
|
|
|
ucnv_toUChars(cnv,
|
|
|
|
ucs1,
|
|
|
|
MAX_STRLEN,
|
|
|
|
s2,
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_strlen(s2),
|
1999-12-09 01:03:31 +00:00
|
|
|
&err);
|
2001-08-11 00:29:58 +00:00
|
|
|
u_releaseDefaultConverter(cnv);
|
1999-12-09 01:03:31 +00:00
|
|
|
if(U_FAILURE(err)) {
|
|
|
|
*ucs1 = 0;
|
|
|
|
}
|
|
|
|
} else {
|
1999-12-08 00:15:46 +00:00
|
|
|
*ucs1 = 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
return ucs1;
|
|
|
|
}
|
|
|
|
|
2000-08-08 00:18:02 +00:00
|
|
|
/*
|
|
|
|
returns the minimum of (the length of the null-terminated string) and n.
|
|
|
|
*/
|
2001-02-08 03:00:09 +00:00
|
|
|
static int32_t u_ustrnlen(const UChar *ucs1, int32_t n)
|
2000-08-08 00:18:02 +00:00
|
|
|
{
|
|
|
|
int32_t len = 0;
|
|
|
|
|
|
|
|
if (ucs1)
|
|
|
|
{
|
2002-08-27 22:45:05 +00:00
|
|
|
while (n-- && *(ucs1++))
|
2000-08-08 00:18:02 +00:00
|
|
|
{
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI char* U_EXPORT2
|
|
|
|
u_austrncpy(char *s1,
|
2001-02-08 03:00:09 +00:00
|
|
|
const UChar *ucs2,
|
|
|
|
int32_t n)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-02-08 03:00:09 +00:00
|
|
|
char *target = s1;
|
2001-08-11 00:29:58 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
|
|
|
if(U_SUCCESS(err) && cnv != NULL) {
|
2000-06-29 01:41:04 +00:00
|
|
|
ucnv_reset(cnv);
|
2001-02-08 03:00:09 +00:00
|
|
|
ucnv_fromUnicode(cnv,
|
|
|
|
&target,
|
|
|
|
s1+n,
|
|
|
|
&ucs2,
|
|
|
|
ucs2+u_ustrnlen(ucs2, n),
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
&err);
|
2000-06-29 01:41:04 +00:00
|
|
|
ucnv_reset(cnv); /* be good citizens */
|
2001-08-11 00:29:58 +00:00
|
|
|
u_releaseDefaultConverter(cnv);
|
2000-08-11 19:23:50 +00:00
|
|
|
if(U_FAILURE(err) && (err != U_BUFFER_OVERFLOW_ERROR) ) {
|
2001-02-08 03:00:09 +00:00
|
|
|
*s1 = 0; /* failure */
|
2000-06-29 01:41:04 +00:00
|
|
|
}
|
2001-02-08 03:00:09 +00:00
|
|
|
if(target < (s1+n)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
|
2000-06-29 01:41:04 +00:00
|
|
|
*target = 0; /* terminate */
|
1999-12-09 01:03:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2001-02-08 03:00:09 +00:00
|
|
|
*s1 = 0;
|
1999-12-08 00:15:46 +00:00
|
|
|
}
|
2001-02-08 03:00:09 +00:00
|
|
|
return s1;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI char* U_EXPORT2
|
|
|
|
u_austrcpy(char *s1,
|
1999-08-16 21:50:52 +00:00
|
|
|
const UChar *ucs2 )
|
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = u_getDefaultConverter(&err);
|
|
|
|
if(U_SUCCESS(err) && cnv != NULL) {
|
1999-12-09 01:03:31 +00:00
|
|
|
int32_t len = ucnv_fromUChars(cnv,
|
|
|
|
s1,
|
|
|
|
MAX_STRLEN,
|
|
|
|
ucs2,
|
2000-03-07 01:42:40 +00:00
|
|
|
-1,
|
1999-12-09 01:03:31 +00:00
|
|
|
&err);
|
2001-08-11 00:29:58 +00:00
|
|
|
u_releaseDefaultConverter(cnv);
|
1999-12-09 01:03:31 +00:00
|
|
|
s1[len] = 0;
|
|
|
|
} else {
|
|
|
|
*s1 = 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
return s1;
|
|
|
|
}
|
1999-12-09 01:03:31 +00:00
|
|
|
|
|
|
|
/* mutexed access to a shared default converter ----------------------------- */
|
|
|
|
|
2001-08-25 01:09:11 +00:00
|
|
|
UBool ustring_cleanup(void) {
|
2001-08-11 00:29:58 +00:00
|
|
|
if (gDefaultConverter) {
|
2001-08-31 02:10:39 +00:00
|
|
|
ucnv_close(gDefaultConverter);
|
|
|
|
gDefaultConverter = NULL;
|
2001-08-11 00:29:58 +00:00
|
|
|
}
|
|
|
|
|
2001-08-15 23:51:26 +00:00
|
|
|
/* it's safe to close a 0 converter */
|
2001-08-11 00:29:58 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
1999-12-09 01:03:31 +00:00
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI UConverter* U_EXPORT2
|
2001-08-11 00:29:58 +00:00
|
|
|
u_getDefaultConverter(UErrorCode *status)
|
|
|
|
{
|
|
|
|
UConverter *converter = NULL;
|
|
|
|
|
|
|
|
if (gDefaultConverter != NULL) {
|
|
|
|
umtx_lock(NULL);
|
|
|
|
|
|
|
|
/* need to check to make sure it wasn't taken out from under us */
|
|
|
|
if (gDefaultConverter != NULL) {
|
|
|
|
converter = gDefaultConverter;
|
|
|
|
gDefaultConverter = NULL;
|
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
1999-12-09 01:03:31 +00:00
|
|
|
}
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
/* if the cache was empty, create a converter */
|
|
|
|
if(converter == NULL) {
|
|
|
|
converter = ucnv_open(NULL, status);
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-12-09 01:03:31 +00:00
|
|
|
}
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
return converter;
|
1999-12-09 01:03:31 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2001-08-11 00:29:58 +00:00
|
|
|
u_releaseDefaultConverter(UConverter *converter)
|
1999-12-09 01:03:31 +00:00
|
|
|
{
|
2001-08-11 00:29:58 +00:00
|
|
|
if(gDefaultConverter == NULL) {
|
|
|
|
if (converter != NULL) {
|
|
|
|
ucnv_reset(converter);
|
|
|
|
}
|
1999-12-09 01:03:31 +00:00
|
|
|
umtx_lock(NULL);
|
|
|
|
|
2001-08-11 00:29:58 +00:00
|
|
|
if(gDefaultConverter == NULL) {
|
|
|
|
gDefaultConverter = converter;
|
1999-12-09 01:03:31 +00:00
|
|
|
converter = NULL;
|
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(converter != NULL) {
|
|
|
|
ucnv_close(converter);
|
|
|
|
}
|
|
|
|
}
|
2000-06-29 19:36:20 +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,
|
|
|
|
/*f*/ 0x66, 0x0c,
|
|
|
|
/*n*/ 0x6E, 0x0a,
|
|
|
|
/*r*/ 0x72, 0x0d,
|
|
|
|
/*t*/ 0x74, 0x09,
|
|
|
|
/*v*/ 0x76, 0x0b
|
|
|
|
};
|
|
|
|
enum { UNESCAPE_MAP_LENGTH = sizeof(UNESCAPE_MAP) / sizeof(UNESCAPE_MAP[0]) };
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
maxDig = 2;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (UTF_IS_FIRST_SURROGATE(c) && *offset < length) {
|
|
|
|
UChar c2 = charAt(*offset, context);
|
|
|
|
if (UTF_IS_SECOND_SURROGATE(c2)) {
|
|
|
|
++(*offset);
|
|
|
|
return UTF16_GET_PAIR_VALUE(c, c2);
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
segment, src - segment);
|
|
|
|
}
|
2000-08-16 23:31:50 +00:00
|
|
|
i += src - segment;
|
2000-07-17 21:02:18 +00:00
|
|
|
}
|
|
|
|
++src; /* advance past '\\' */
|
|
|
|
c32 = u_unescapeAt(_charPtr_charAt, &lenParsed, uprv_strlen(src), (void*)src);
|
|
|
|
if (lenParsed == 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
src += lenParsed; /* advance past escape seq. */
|
2000-08-16 23:31:50 +00:00
|
|
|
if (dest != NULL && UTF_CHAR_LENGTH(c32) <= (destCapacity - i)) {
|
|
|
|
UTF_APPEND_CHAR_UNSAFE(dest, i, c32);
|
|
|
|
} else {
|
|
|
|
i += UTF_CHAR_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,
|
|
|
|
segment, src - segment);
|
|
|
|
}
|
2000-08-16 23:31:50 +00:00
|
|
|
i += 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
|
|
|
}
|
2000-08-16 23:31:50 +00:00
|
|
|
return i + 1; /* add 1 for zero term */
|
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-08-17 00:18:25 +00:00
|
|
|
/* C UGrowBuffer implementation --------------------------------------------- */
|
2001-06-25 22:59:23 +00:00
|
|
|
|
2001-07-10 16:43:51 +00:00
|
|
|
U_CAPI UBool /* U_CALLCONV U_EXPORT2 */
|
2001-06-25 22:59:23 +00:00
|
|
|
u_growBufferFromStatic(void *context,
|
|
|
|
UChar **pBuffer, int32_t *pCapacity, int32_t reqCapacity,
|
|
|
|
int32_t length) {
|
|
|
|
UChar *newBuffer=(UChar *)uprv_malloc(reqCapacity*U_SIZEOF_UCHAR);
|
|
|
|
if(newBuffer!=NULL) {
|
|
|
|
if(length>0) {
|
|
|
|
uprv_memcpy(newBuffer, *pBuffer, length*U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
*pCapacity=reqCapacity;
|
|
|
|
} else {
|
|
|
|
*pCapacity=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release the old pBuffer if it was not statically allocated */
|
|
|
|
if(*pBuffer!=(UChar *)context) {
|
|
|
|
uprv_free(*pBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pBuffer=newBuffer;
|
|
|
|
return (UBool)(newBuffer!=NULL);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
#define __TERMINATE_STRING(dest, destCapacity, length, pErrorCode) \
|
|
|
|
if(pErrorCode!=NULL && U_SUCCESS(*pErrorCode)) { \
|
|
|
|
/* not a public function, so no complete argument checking */ \
|
|
|
|
\
|
2001-11-28 23:59:09 +00:00
|
|
|
if(length<0) { \
|
|
|
|
/* assume that the caller handles this */ \
|
|
|
|
} else if(length<destCapacity) { \
|
2001-09-18 00:56:43 +00:00
|
|
|
/* NUL-terminate the string, the NUL fits */ \
|
|
|
|
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; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
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) {
|
2001-09-18 00:56:43 +00:00
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
|
|
|
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_terminateChars(char *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode) {
|
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
|
|
|
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) {
|
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
|
|
|
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) {
|
|
|
|
__TERMINATE_STRING(dest, destCapacity, length, pErrorCode);
|
2001-09-05 23:37:16 +00:00
|
|
|
return length;
|
|
|
|
}
|