~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/extra/ustdio/uscanset.c

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
******************************************************************************
 
3
*
 
4
*   Copyright (C) 1998-2001, International Business Machines
 
5
*   Corporation and others.  All Rights Reserved.
 
6
*
 
7
******************************************************************************
 
8
*
 
9
* File uscanset.c
 
10
*
 
11
* Modification History:
 
12
*
 
13
*   Date        Name        Description
 
14
*   12/03/98    stephen        Creation.
 
15
*   03/13/99    stephen     Modified for new C API.
 
16
******************************************************************************
 
17
*/
 
18
 
 
19
#include "uscanset.h"
 
20
 
 
21
 
 
22
static UBool
 
23
u_scanf_scanset_add(u_scanf_scanset     *scanset,
 
24
            UChar         c)
 
25
{
 
26
  if(scanset->single_count == U_SCANF_MAX_SCANSET_SIZE - 1)
 
27
    return FALSE;
 
28
 
 
29
  scanset->singles[ scanset->single_count ] = c;
 
30
  (scanset->single_count)++;
 
31
 
 
32
  return TRUE;
 
33
}
 
34
 
 
35
static UBool
 
36
u_scanf_scanset_addrange(u_scanf_scanset     *scanset,
 
37
             UChar             start, 
 
38
             UChar             end)
 
39
{
 
40
  if(scanset->pair_count == U_SCANF_MAX_SCANSET_SIZE - 1)
 
41
    return FALSE;
 
42
  
 
43
  scanset->pairs[ scanset->pair_count ].start     = start;
 
44
  scanset->pairs[ scanset->pair_count ].end     = end;
 
45
 
 
46
  (scanset->pair_count)++;
 
47
 
 
48
  return TRUE;
 
49
}
 
50
 
 
51
UBool
 
52
u_scanf_scanset_init(u_scanf_scanset     *scanset,
 
53
             const UChar    *s,
 
54
             int32_t        *len)
 
55
{
 
56
  UChar        c;
 
57
  const UChar     *limit;
 
58
  int32_t     count;
 
59
  UBool    result = FALSE;
 
60
 
 
61
 
 
62
  /* set up parameters */
 
63
  limit = s + *len;
 
64
  count = 0;
 
65
 
 
66
  /* initialize to defaults */
 
67
  scanset->single_count = 0;
 
68
  scanset->pair_count     = 0;
 
69
  scanset->is_inclusive = TRUE;
 
70
 
 
71
  /* check to see if this is an inclusive or exclusive scanset */
 
72
  if(*s == 0x005E) { /* '^' */
 
73
    scanset->is_inclusive = FALSE;
 
74
 
 
75
    /* increment s and count */
 
76
    ++s;
 
77
    ++count;
 
78
  }
 
79
 
 
80
  /* if ']' is the first character, add it */
 
81
  else if(*s == 0x005D) {
 
82
    result = u_scanf_scanset_add(scanset, *s++);
 
83
 
 
84
    /* increment count */
 
85
    ++count;
 
86
  }
 
87
 
 
88
  /* if the first character is '^' and the second is ']', add ']' */
 
89
  if( ! scanset->is_inclusive && *s == 0x005D) {
 
90
    result = u_scanf_scanset_add(scanset, *s++);
 
91
 
 
92
    /* increment count */
 
93
    ++count;
 
94
  }
 
95
 
 
96
  /* add characters until a ']' is seen, adding ranges as necessary */
 
97
  while(s < limit) {
 
98
    
 
99
    /* grab the current character */
 
100
    c = *s++;
 
101
 
 
102
    /* if it's a ']', we're done */
 
103
    if(c == 0x005D)
 
104
      break;
 
105
 
 
106
    /* check if this is a range */
 
107
    if(*s == 0x002D && *(s+1) != 0x005D) {
 
108
      result = u_scanf_scanset_addrange(scanset, c, *(s+1));
 
109
      
 
110
      /* increment count and s */
 
111
      s        += 2;
 
112
      count     += 2;
 
113
    }
 
114
 
 
115
    /* otherwise, just add the character */
 
116
    else {
 
117
      result = u_scanf_scanset_add(scanset, c);
 
118
    }
 
119
 
 
120
      /* increment count */
 
121
      ++count;
 
122
  }
 
123
 
 
124
  /* update length to reflect # of characters consumed */
 
125
  *len = count;
 
126
  return result;
 
127
}
 
128
 
 
129
UBool
 
130
u_scanf_scanset_in(u_scanf_scanset     *scanset,
 
131
           UChar         c)
 
132
{
 
133
  int i;
 
134
 
 
135
  /* if this is an inclusive scanset, make sure c is in it */
 
136
  if(scanset->is_inclusive) {
 
137
 
 
138
    /* check the single chars first*/
 
139
    for(i = 0; i < scanset->single_count; ++i) {
 
140
      if(c == scanset->singles[i])
 
141
    return TRUE;
 
142
    }
 
143
    
 
144
    /* check the pairs */
 
145
    for(i = 0; i < scanset->pair_count; ++i) {
 
146
      if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end)
 
147
    return TRUE;
 
148
    }
 
149
   
 
150
    /* didn't find it, so c isn't in set */
 
151
    return FALSE;
 
152
  }
 
153
 
 
154
  /* otherwise, make sure c isn't in it */
 
155
  else {
 
156
 
 
157
    /* check the single chars first*/
 
158
    for(i = 0; i < scanset->single_count; ++i) {
 
159
      if(c == scanset->singles[i])
 
160
    return FALSE;
 
161
    }
 
162
    
 
163
    /* check the pairs */
 
164
    for(i = 0; i < scanset->pair_count; ++i) {
 
165
      if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end)
 
166
    return FALSE;
 
167
    }
 
168
    
 
169
    /* didn't find it, so c is in set */
 
170
    return TRUE;
 
171
  }
 
172
}
 
173
 
 
174
 
 
175
 
 
176
 
 
177
 
 
178
 
 
179
 
 
180