Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

uscanset.c

00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1998-1999, International Business Machines
00005 *   Corporation and others.  All Rights Reserved.
00006 *
00007 *******************************************************************************
00008 *
00009 * File uscanset.c
00010 *
00011 * Modification History:
00012 *
00013 *   Date        Name        Description
00014 *   12/03/98    stephen        Creation.
00015 *   03/13/99    stephen     Modified for new C API.
00016 *******************************************************************************
00017 */
00018 
00019 #include "uscanset.h"
00020 
00021 
00022 UBool
00023 u_scanf_scanset_add(u_scanf_scanset     *scanset,
00024             UChar         c)
00025 {
00026   if(scanset->single_count == U_SCANF_MAX_SCANSET_SIZE - 1)
00027     return FALSE;
00028 
00029   scanset->singles[ scanset->single_count ] = c;
00030   (scanset->single_count)++;
00031 
00032   return TRUE;
00033 }
00034 
00035 UBool
00036 u_scanf_scanset_addrange(u_scanf_scanset     *scanset,
00037              UChar             start, 
00038              UChar             end)
00039 {
00040   if(scanset->pair_count == U_SCANF_MAX_SCANSET_SIZE - 1)
00041     return FALSE;
00042   
00043   scanset->pairs[ scanset->pair_count ].start     = start;
00044   scanset->pairs[ scanset->pair_count ].end     = end;
00045 
00046   (scanset->pair_count)++;
00047 
00048   return TRUE;
00049 }
00050 
00051 UBool
00052 u_scanf_scanset_init(u_scanf_scanset     *scanset,
00053              const UChar    *s,
00054              int32_t        *len)
00055 {
00056   UChar        c;
00057   const UChar     *limit;
00058   int32_t     count;
00059   UBool    result;
00060 
00061 
00062   /* set up parameters */
00063   limit = s + *len;
00064   count = 0;
00065 
00066   /* initialize to defaults */
00067   scanset->single_count = 0;
00068   scanset->pair_count     = 0;
00069   scanset->is_inclusive = TRUE;
00070 
00071   /* check to see if this is an inclusive or exclusive scanset */
00072   if(*s == 0x005E) { /* '^' */
00073     scanset->is_inclusive = FALSE;
00074 
00075     /* increment s and count */
00076     ++s;
00077     ++count;
00078   }
00079 
00080   /* if ']' is the first character, add it */
00081   else if(*s == 0x005D) {
00082     result = u_scanf_scanset_add(scanset, *s++);
00083 
00084     /* increment count */
00085     ++count;
00086   }
00087 
00088   /* if the first character is '^' and the second is ']', add ']' */
00089   if( ! scanset->is_inclusive && *s == 0x005D) {
00090     result = u_scanf_scanset_add(scanset, *s++);
00091 
00092     /* increment count */
00093     ++count;
00094   }
00095 
00096   /* add characters until a ']' is seen, adding ranges as necessary */
00097   while(s < limit) {
00098     
00099     /* grab the current character */
00100     c = *s++;
00101 
00102     /* if it's a ']', we're done */
00103     if(c == 0x005D)
00104       break;
00105 
00106     /* check if this is a range */
00107     if(*s == 0x002D && *(s+1) != 0x005D) {
00108       result = u_scanf_scanset_addrange(scanset, c, *(s+1));
00109       
00110       /* increment count and s */
00111       s        += 2;
00112       count     += 2;
00113     }
00114 
00115     /* otherwise, just add the character */
00116     else {
00117       result = u_scanf_scanset_add(scanset, c);
00118     }
00119 
00120       /* increment count */
00121       ++count;
00122   }
00123 
00124   /* update length to reflect # of characters consumed */
00125   *len = count;
00126   return result;
00127 }
00128 
00129 UBool
00130 u_scanf_scanset_in(u_scanf_scanset     *scanset,
00131            UChar         c)
00132 {
00133   int i;
00134 
00135   /* if this is an inclusive scanset, make sure c is in it */
00136   if(scanset->is_inclusive) {
00137 
00138     /* check the single chars first*/
00139     for(i = 0; i < scanset->single_count; ++i) {
00140       if(c == scanset->singles[i])
00141     return TRUE;
00142     }
00143     
00144     /* check the pairs */
00145     for(i = 0; i < scanset->pair_count; ++i) {
00146       if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end)
00147     return TRUE;
00148     }
00149    
00150     /* didn't find it, so c isn't in set */
00151     return FALSE;
00152   }
00153 
00154   /* otherwise, make sure c isn't in it */
00155   else {
00156 
00157     /* check the single chars first*/
00158     for(i = 0; i < scanset->single_count; ++i) {
00159       if(c == scanset->singles[i])
00160     return FALSE;
00161     }
00162     
00163     /* check the pairs */
00164     for(i = 0; i < scanset->pair_count; ++i) {
00165       if(c >= scanset->pairs[i].start && c <= scanset->pairs[i].end)
00166     return FALSE;
00167     }
00168     
00169     /* didn't find it, so c is in set */
00170     return TRUE;
00171   }
00172 }
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 

Generated at Tue Dec 5 10:48:12 2000 for ICU by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000