1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2001-03-21 22:07:51 +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 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File uscanset.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 12/03/98 stephen Creation.
|
|
|
|
* 03/13/99 stephen Modified for new C API.
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uscanset.h"
|
|
|
|
|
|
|
|
|
2001-02-06 21:56:47 +00:00
|
|
|
static UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
u_scanf_scanset_add(u_scanf_scanset *scanset,
|
2002-08-26 20:53:53 +00:00
|
|
|
UChar c)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-08-26 20:53:53 +00:00
|
|
|
if(scanset->single_count == U_SCANF_MAX_SCANSET_SIZE - 1)
|
|
|
|
return FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
scanset->singles[scanset->single_count++] = c;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-02-06 21:56:47 +00:00
|
|
|
static UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
u_scanf_scanset_addrange(u_scanf_scanset *scanset,
|
2002-08-26 20:53:53 +00:00
|
|
|
UChar start,
|
|
|
|
UChar end)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-08-26 20:53:53 +00:00
|
|
|
if(scanset->pair_count == U_SCANF_MAX_SCANSET_SIZE - 1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (end < start) {
|
|
|
|
/* swap chars */
|
|
|
|
UChar temp = end;
|
|
|
|
end = start;
|
|
|
|
start = temp;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
scanset->pairs[scanset->pair_count].start = start;
|
|
|
|
scanset->pairs[scanset->pair_count].end = end;
|
|
|
|
scanset->pair_count++;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
u_scanf_scanset_init(u_scanf_scanset *scanset,
|
2002-08-26 20:53:53 +00:00
|
|
|
const UChar *s,
|
|
|
|
int32_t *len)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-08-26 20:53:53 +00:00
|
|
|
UChar c;
|
|
|
|
const UChar *limit;
|
|
|
|
int32_t count;
|
|
|
|
UBool result = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
/* set up parameters */
|
|
|
|
limit = s + *len;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* initialize to defaults */
|
|
|
|
scanset->single_count = 0;
|
|
|
|
scanset->pair_count = 0;
|
|
|
|
scanset->is_inclusive = TRUE;
|
|
|
|
|
|
|
|
/* check to see if this is an inclusive or exclusive scanset */
|
|
|
|
if(*s == 0x005E) { /* '^' */
|
|
|
|
scanset->is_inclusive = FALSE;
|
|
|
|
|
|
|
|
/* increment s and count */
|
|
|
|
++s;
|
|
|
|
++count;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
/* if ']' is the first character, add it */
|
|
|
|
else if(*s == 0x005D) {
|
|
|
|
result = u_scanf_scanset_add(scanset, *s++);
|
|
|
|
|
|
|
|
/* increment count */
|
|
|
|
++count;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
/* if the first character is '^' and the second is ']', add ']' */
|
|
|
|
if( ! scanset->is_inclusive && *s == 0x005D) {
|
|
|
|
result = u_scanf_scanset_add(scanset, *s++);
|
|
|
|
|
|
|
|
/* increment count */
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add characters until a ']' is seen, adding ranges as necessary */
|
|
|
|
while(s < limit) {
|
|
|
|
|
|
|
|
/* grab the current character */
|
|
|
|
c = *s++;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
/* if it's a ']', we're done */
|
|
|
|
if(c == 0x005D)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* check if this is a range */
|
|
|
|
if(*s == 0x002D && *(s+1) != 0x005D) {
|
|
|
|
result = u_scanf_scanset_addrange(scanset, c, *(s+1));
|
|
|
|
|
|
|
|
/* increment count and s */
|
|
|
|
s += 2;
|
|
|
|
count += 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* otherwise, just add the character */
|
|
|
|
result = u_scanf_scanset_add(scanset, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* increment count */
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update length to reflect # of characters consumed */
|
|
|
|
*len = count;
|
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
u_scanf_scanset_in(u_scanf_scanset *scanset,
|
2002-08-26 20:53:53 +00:00
|
|
|
UChar c)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-08-26 20:53:53 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* if this is an inclusive scanset, make sure c is in it */
|
|
|
|
if(scanset->is_inclusive) {
|
|
|
|
|
|
|
|
/* check the single chars first*/
|
|
|
|
for(i = 0; i < scanset->single_count; ++i) {
|
|
|
|
if(c == scanset->singles[i]) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the pairs */
|
|
|
|
for(i = 0; i < scanset->pair_count; ++i) {
|
|
|
|
if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* didn't find it, so c isn't in set */
|
|
|
|
return FALSE;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
/* otherwise, make sure c isn't in it */
|
|
|
|
else {
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-26 20:53:53 +00:00
|
|
|
/* check the single chars first*/
|
|
|
|
for(i = 0; i < scanset->single_count; ++i) {
|
|
|
|
if(c == scanset->singles[i]) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the pairs */
|
|
|
|
for(i = 0; i < scanset->pair_count; ++i) {
|
|
|
|
if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* didn't find it, so c is in set */
|
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|