summaryrefslogtreecommitdiff
path: root/StdLib/LibC/Wchar/Searching.c
blob: 12556bd7572bb00b7c0285f5254270497b170ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/** @file
    Search Functions for <wchar.h>.

  Unless explicitly stated otherwise, the functions defined in this file order
  two wide characters the same way as two integers of the underlying integer
  type designated by wchar_t.

  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials are licensed and made available under
  the terms and conditions of the BSD License that accompanies this distribution.
  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include  <Uefi.h>
#include  <Library/BaseLib.h>
#include  <Library/BaseMemoryLib.h>
#include  <Library/MemoryAllocationLib.h>

#include  <LibConfig.h>

#include  <wchar.h>

/* Data initialized by the library constructor */
extern  UINT8  *__wchar_bitmap;
extern  UINTN   __wchar_bitmap_size;
extern  UINTN   __wchar_bitmap_64;

/** The wcschr function locates the first occurrence of c in the wide string
    pointed to by s.  The terminating null wide character is considered to be
    part of the wide string.

    @return   The wcschr function returns a pointer to the located wide
              character, or a null pointer if the wide character does not occur
              in the wide string.
**/
wchar_t *wcschr(const wchar_t *s, wchar_t c)
{
  do {
    if( *s == c) {
      return (wchar_t *)s;
    }
  } while(*s++ != 0);
  return NULL;
}

static UINT8  BitMask[] = {
  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
  };

#define WHICH8(c)     ((unsigned short)(c) >> 3)
#define WHICH_BIT(c)  (BitMask[((c) & 0x7)])
#define BITMAP64      ((UINT64 *)bitmap)

static
void
BuildBitmap(unsigned char * bitmap, const wchar_t *s2, UINTN n)
{
  UINT8 bit;
  UINTN index;

  //// Initialize bitmap.  Bit 0 is always 1 which corresponds to '\0'
  //for (BITMAP64[0] = index = 1; index < n; index++)
  //  BITMAP64[index] = 0;
  (void)wmemset( (wchar_t *)bitmap, 0, n / sizeof(wchar_t));
  bitmap[0] = 1;

  // Set bits in bitmap corresponding to the characters in s2
  for (; *s2 != 0; ++s2) {
    index = WHICH8(*s2);
    bit = WHICH_BIT(*s2);
    bitmap[index] |= bit;
  }
}

/** The wcscspn function computes the length of the maximum initial segment of
    the wide string pointed to by s1 which consists entirely of wide characters
    not from the wide string pointed to by s2.

    @return   The wcscspn function returns the length of the segment.
**/
size_t wcscspn(const wchar_t *s1, const wchar_t *s2)
{
  const wchar_t *str;
  UINT8 bit;
  int index;
  size_t s1len;

  if(*s1 == 0)   return 0;
  s1len = wcslen(s1);

  BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);

  for(str = s1; str < &s1[s1len] ; str++) {
    index = WHICH8(*str);
    bit = WHICH_BIT(*str);
    if ((__wchar_bitmap[index] & bit) != 0)
      break;
  }
  return (str - s1);
}

/** The wcspbrk function locates the first occurrence in the wide string
    pointed to by s1 of any wide character from the wide string
    pointed to by s2.

    @return   The wcspbrk function returns a pointer to the wide character
              in s1, or a null pointer if no wide character from s2 occurs
              in s1.
**/
wchar_t *wcspbrk(const wchar_t *s1, const wchar_t *s2)
{
  UINT8 bit;
  int index;

  BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);

  for( ; *s1 != 0; ++s1) {
    index = WHICH8(*s1);
    bit = WHICH_BIT(*s1);
    if( (__wchar_bitmap[index] & bit) != 0) {
      return (wchar_t *)s1;
    }
  }
  return NULL;
}

/** The wcsrchr function locates the last occurrence of c in the wide string
    pointed to by s. The terminating null wide character is considered to be
    part of the wide string.

    @return   The wcsrchr function returns a pointer to the wide character,
              or a null pointer if c does not occur in the wide string.
**/
wchar_t *wcsrchr(const wchar_t *s, wchar_t c)
{
  wchar_t  *found  = NULL;

  do {
    if( *s == c)  found = (wchar_t *)s;
  } while( *s++ != 0);

  return found;
}

/** The wcsspn function computes the length of the maximum initial segment of
    the wide string pointed to by s1 which consists entirely of wide characters
    from the wide string pointed to by s2.

    @return   The wcsspn function returns the length of the segment.
**/
size_t wcsspn(const wchar_t *s1, const wchar_t *s2)
{
  size_t  length = 0;
  int     index;
  UINT8   bit;

  BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);

  for( ; *s1 != 0; ++s1) {
    index = WHICH8(*s1);
    bit = WHICH_BIT(*s1);
    if( (__wchar_bitmap[index] & bit) == 0)   break;
    ++length;
  }
  return length;
}

/** The wcsstr function locates the first occurrence in the wide string pointed
    to by s1 of the sequence of wide characters (excluding the terminating null
    wide character) in the wide string pointed to by s2.

    @return   The wcsstr function returns a pointer to the located wide string,
              or a null pointer if the wide string is not found. If s2 points
              to a wide string with zero length, the function returns s1.
**/
wchar_t *wcsstr(const wchar_t *s1, const wchar_t *s2)
{
  return (wchar_t *)StrStr( (CONST CHAR16 *)s1, (CONST CHAR16 *)s2);
}

/** A sequence of calls to the wcstok function breaks the wide string pointed
    to by s1 into a sequence of tokens, each of which is delimited by a wide
    character from the wide string pointed to by s2. The third argument points
    to a caller-provided wchar_t pointer into which the wcstok function stores
    information necessary for it to continue scanning the same wide string.

    The first call in a sequence has a non-null first argument and stores an
    initial value in the object pointed to by ptr. Subsequent calls in the
    sequence have a null first argument and the object pointed to by ptr is
    required to have the value stored by the previous call in the sequence,
    which is then updated. The separator wide string pointed to by s2 may be
    different from call to call.

    The first call in the sequence searches the wide string pointed to by s1
    for the first wide character that is not contained in the current separator
    wide string pointed to by s2. If no such wide character is found, then
    there are no tokens in the wide string pointed to by s1 and the wcstok
    function returns a null pointer. If such a wide character is found, it is
    the start of the first token.

    The wcstok function then searches from there for a wide character that is
    contained in the current separator wide string. If no such wide character
    is found, the current token extends to the end of the wide string pointed
    to by s1, and subsequent searches in the same wide string for a token
    return a null pointer. If such a wide character is found, it is overwritten
    by a null wide character, which terminates the current token.

    In all cases, the wcstok function stores sufficient information in the
    pointer pointed to by ptr so that subsequent calls, with a null pointer for
    s1 and the unmodified pointer value for ptr, shall start searching just
    past the element overwritten by a null wide character (if any).

    @return   The wcstok function returns a pointer to the first wide character
              of a token, or a null pointer if there is no token.
**/
wchar_t *wcstok(wchar_t * __restrict s1, const wchar_t * __restrict s2, wchar_t ** __restrict ptr)
{
  wchar_t        *Token = NULL;
  int             index;
  UINT8           bit;

  if(     (s1 == NULL)
      &&  ((s1 = *ptr) == NULL))
  {
    return  NULL;
  }

  // s2 can be different on each call, so build the bitmap each time.
  BuildBitmap( __wchar_bitmap, s2, __wchar_bitmap_size);

  // skip leading delimiters: all chars in s2
  for( ; *s1 != 0; ++s1) {
    index = WHICH8(*s1);
    bit = WHICH_BIT(*s1);
    if( (__wchar_bitmap[index] & bit) == 0)   break;
  }
  if( *s1 != 0)
  {
    // Remember this point, it is the start of the token
    Token = s1++;

    // find the next delimiter and replace it with a '\0'
    for( ; *s1 != 0; ++s1) {
      index = WHICH8(*s1);
      bit = WHICH_BIT(*s1);
      if( (__wchar_bitmap[index] & bit) != 0) {
        *s1++ = 0;
        *ptr = s1;
        return Token;
      }
    }
  }
  *ptr = NULL;
  return Token;
}

/** The wmemchr function locates the first occurrence of c in the initial n
    wide characters of the object pointed to by s.

    @return   The wmemchr function returns a pointer to the located wide
              character, or a null pointer if the wide character does not occur
              in the object.
**/
wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n)
{
  return (wchar_t *)ScanMem16( s, (UINTN)(n * sizeof(wchar_t)), (UINT16)c);
}