EDK2 doxygen online documents - Firmware Encoding Index 1
EDK2 doxygen online documents - Firmware Encoding Index

StdLib/LibC/Wchar/Searching.c

Go to the documentation of this file.
00001 
00017 #include  <Uefi.h>
00018 #include  <Library/BaseLib.h>
00019 #include  <Library/BaseMemoryLib.h>
00020 #include  <Library/MemoryAllocationLib.h>
00021 
00022 #include  <LibConfig.h>
00023 
00024 #include  <wchar.h>
00025 
00026 /* Data initialized by the library constructor */
00027 extern  UINT8  *__wchar_bitmap;
00028 extern  UINTN   __wchar_bitmap_size;
00029 extern  UINTN   __wchar_bitmap_64;
00030 
00039 wchar_t *wcschr(const wchar_t *s, wchar_t c)
00040 {
00041   do {
00042     if( *s == c) {
00043       return (wchar_t *)s;
00044     }
00045   } while(*s++ != 0);
00046   return NULL;
00047 }
00048 
00049 static UINT8  BitMask[] = {
00050   0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
00051   };
00052 
00053 #define WHICH8(c)     ((unsigned short)(c) >> 3)
00054 #define WHICH_BIT(c)  (BitMask[((c) & 0x7)])
00055 #define BITMAP64      ((UINT64 *)bitmap)
00056 
00057 static
00058 void
00059 BuildBitmap(unsigned char * bitmap, const wchar_t *s2, UINTN n)
00060 {
00061   UINT8 bit;
00062   UINTN index;
00063 
00065   //for (BITMAP64[0] = index = 1; index < n; index++)
00066   //  BITMAP64[index] = 0;
00067   (void)wmemset( (wchar_t *)bitmap, 0, n / sizeof(wchar_t));
00068   bitmap[0] = 1;
00069 
00070   // Set bits in bitmap corresponding to the characters in s2
00071   for (; *s2 != 0; ++s2) {
00072     index = WHICH8(*s2);
00073     bit = WHICH_BIT(*s2);
00074     bitmap[index] |= bit;
00075   }
00076 }
00077 
00084 size_t wcscspn(const wchar_t *s1, const wchar_t *s2)
00085 {
00086   const wchar_t *str;
00087   UINT8 bit;
00088   int index;
00089   size_t s1len;
00090 
00091   if(*s1 == 0)   return 0;
00092   s1len = wcslen(s1);
00093 
00094   BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);
00095 
00096   for(str = s1; str < &s1[s1len] ; str++) {
00097     index = WHICH8(*str);
00098     bit = WHICH_BIT(*str);
00099     if ((__wchar_bitmap[index] & bit) != 0)
00100       break;
00101   }
00102   return (str - s1);
00103 }
00104 
00113 wchar_t *wcspbrk(const wchar_t *s1, const wchar_t *s2)
00114 {
00115   UINT8 bit;
00116   int index;
00117 
00118   BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);
00119 
00120   for( ; *s1 != 0; ++s1) {
00121     index = WHICH8(*s1);
00122     bit = WHICH_BIT(*s1);
00123     if( (__wchar_bitmap[index] & bit) != 0) {
00124       return (wchar_t *)s1;
00125     }
00126   }
00127   return NULL;
00128 }
00129 
00137 wchar_t *wcsrchr(const wchar_t *s, wchar_t c)
00138 {
00139   wchar_t  *found  = NULL;
00140 
00141   do {
00142     if( *s == c)  found = (wchar_t *)s;
00143   } while( *s++ != 0);
00144 
00145   return found;
00146 }
00147 
00154 size_t wcsspn(const wchar_t *s1, const wchar_t *s2)
00155 {
00156   size_t  length = 0;
00157   int     index;
00158   UINT8   bit;
00159 
00160   BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);
00161 
00162   for( ; *s1 != 0; ++s1) {
00163     index = WHICH8(*s1);
00164     bit = WHICH_BIT(*s1);
00165     if( (__wchar_bitmap[index] & bit) == 0)   break;
00166     ++length;
00167   }
00168   return length;
00169 }
00170 
00179 wchar_t *wcsstr(const wchar_t *s1, const wchar_t *s2)
00180 {
00181   return (wchar_t *)StrStr( (CONST CHAR16 *)s1, (CONST CHAR16 *)s2);
00182 }
00183 
00219 wchar_t *wcstok(wchar_t * __restrict s1, const wchar_t * __restrict s2, wchar_t ** __restrict ptr)
00220 {
00221   wchar_t        *Token = NULL;
00222   int             index;
00223   UINT8           bit;
00224 
00225   if(     (s1 == NULL)
00226       &&  ((s1 = *ptr) == NULL))
00227   {
00228     return  NULL;
00229   }
00230 
00231   // s2 can be different on each call, so build the bitmap each time.
00232   BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);
00233 
00234   // skip leading delimiters: all chars in s2
00235   for( ; *s1 != 0; ++s1) {
00236     index = WHICH8(*s1);
00237     bit = WHICH_BIT(*s1);
00238     if( (__wchar_bitmap[index] & bit) == 0)   break;
00239   }
00240   if( *s1 != 0)
00241   {
00242     // Remember this point, it is the start of the token
00243     Token = s1++;
00244 
00245     // find the next delimiter and replace it with a '\0'
00246     for( ; *s1 != 0; ++s1) {
00247       index = WHICH8(*s1);
00248       bit = WHICH_BIT(*s1);
00249       if( (__wchar_bitmap[index] & bit) != 0) {
00250         *s1++ = 0;
00251         *ptr = s1;
00252         return Token;
00253       }
00254     }
00255   }
00256   *ptr = NULL;
00257   return Token;
00258 }
00259 
00267 wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n)
00268 {
00269   return (wchar_t *)ScanMem16( s, (UINTN)(n * sizeof(wchar_t)), (UINT16)c);
00270 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines