Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: third_party/minicrt/string.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/minicrt/string.c ('k') | third_party/minicrt/struplwr.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <windows.h>
2 #include <limits.h>
3 #include <stdlib.h>
4
5 #include "libctiny.h"
6
7 #ifndef _CONST_RETURN
8 #define _CONST_RETURN
9 #endif
10
11 /***
12 *wcsstr.c - search for one wide-character string inside another
13 *
14 * Copyright (c) Microsoft Corporation. All rights reserved.
15 *
16 *Purpose:
17 * defines wcsstr() - search for one wchar_t string inside another
18 *
19 *******************************************************************************/
20
21 /***
22 *wchar_t *wcsstr(string1, string2) - search for string2 in string1
23 * (wide strings)
24 *
25 *Purpose:
26 * finds the first occurrence of string2 in string1 (wide strings)
27 *
28 *Entry:
29 * wchar_t *string1 - string to search in
30 * wchar_t *string2 - string to search for
31 *
32 *Exit:
33 * returns a pointer to the first occurrence of string2 in
34 * string1, or NULL if string2 does not occur in string1
35 *
36 *Uses:
37 *
38 *Exceptions:
39 *
40 *******************************************************************************/
41
42 _CONST_RETURN wchar_t * __cdecl wcsstr(const wchar_t * wcs1,
43 const wchar_t * wcs2) {
44 wchar_t *cp = (wchar_t *) wcs1;
45 wchar_t *s1, *s2;
46
47 if (!*wcs2)
48 return (wchar_t *)wcs1;
49
50 while (*cp)
51 {
52 s1 = cp;
53 s2 = (wchar_t *) wcs2;
54
55 while (*s1 && *s2 && !(*s1-*s2))
56 s1++, s2++;
57
58 if (!*s2)
59 return(cp);
60
61 cp++;
62 }
63
64 return(NULL);
65 }
66
67 /***
68 *wcsrchr.c - find last occurrence of wchar_t character in wide string
69 *
70 * Copyright (c) Microsoft Corporation. All rights reserved.
71 *
72 *Purpose:
73 * defines wcsrchr() - find the last occurrence of a given character
74 * in a string (wide-characters).
75 *
76 *******************************************************************************/
77
78 /***
79 *wchar_t *wcsrchr(string, ch) - find last occurrence of ch in wide string
80 *
81 *Purpose:
82 * Finds the last occurrence of ch in string. The terminating
83 * null character is used as part of the search (wide-characters).
84 *
85 *Entry:
86 * wchar_t *string - string to search in
87 * wchar_t ch - character to search for
88 *
89 *Exit:
90 * returns a pointer to the last occurrence of ch in the given
91 * string
92 * returns NULL if ch does not occurr in the string
93 *
94 *Exceptions:
95 *
96 *******************************************************************************/
97
98 _CONST_RETURN wchar_t * __cdecl wcsrchr(const wchar_t * string, wchar_t ch) {
99 wchar_t *start = (wchar_t *)string;
100
101 while (*string++) /* find end of string */
102 ;
103 /* search towards front */
104 while (--string != start && *string != (wchar_t)ch)
105 ;
106
107 if (*string == (wchar_t)ch) /* wchar_t found ? */
108 return( (wchar_t *)string );
109
110 return(NULL);
111 }
112
113 /***
114 *wcschr.c - search a wchar_t string for a given wchar_t character
115 *
116 * Copyright (c) Microsoft Corporation. All rights reserved.
117 *
118 *Purpose:
119 * defines wcschr() - search a wchar_t string for a wchar_t character
120 *
121 *******************************************************************************/
122
123 /***
124 *wchar_t *wcschr(string, c) - search a string for a wchar_t character
125 *
126 *Purpose:
127 * Searches a wchar_t string for a given wchar_t character,
128 * which may be the null character L'\0'.
129 *
130 *Entry:
131 * wchar_t *string - wchar_t string to search in
132 * wchar_t c - wchar_t character to search for
133 *
134 *Exit:
135 * returns pointer to the first occurence of c in string
136 * returns NULL if c does not occur in string
137 *
138 *Exceptions:
139 *
140 *******************************************************************************/
141
142 _CONST_RETURN wchar_t * __cdecl wcschr(const wchar_t * string, wchar_t ch) {
143 while (*string && *string != (wchar_t)ch)
144 string++;
145
146 if (*string == (wchar_t)ch)
147 return((wchar_t *)string);
148 return(NULL);
149 }
150
151 /***
152 *xtoa.c - convert integers/longs to ASCII string
153 *
154 * Copyright (c) Microsoft Corporation. All rights reserved.
155 *
156 *Purpose:
157 * The module has code to convert integers/longs to ASCII strings. See
158 *
159 *******************************************************************************/
160
161 /***
162 *char *_itoa, *_ltoa, *_ultoa(val, buf, radix) - convert binary int to ASCII
163 * string
164 *
165 *Purpose:
166 * Converts an int to a character string.
167 *
168 *Entry:
169 * val - number to be converted (int, long or unsigned long)
170 * int radix - base to convert into
171 * char *buf - ptr to buffer to place result
172 *
173 *Exit:
174 * fills in space pointed to by buf with string result
175 * returns a pointer to this buffer
176 *
177 *Exceptions:
178 *
179 *******************************************************************************/
180
181 /* helper routine that does the main job. */
182
183 static void __cdecl xtoa(unsigned long val, char *buf, unsigned radix, int is_ne g) {
184 char *p; /* pointer to traverse string */
185 char *firstdig; /* pointer to first digit */
186 char temp; /* temp char */
187 unsigned digval; /* value of digit */
188
189 p = buf;
190
191 if (is_neg) {
192 /* negative, so output '-' and negate */
193 *p++ = '-';
194 val = (unsigned long)(-(long)val);
195 }
196
197 firstdig = p; /* save pointer to first digit */
198
199 do {
200 digval = (unsigned) (val % radix);
201 val /= radix; /* get next digit */
202
203 /* convert to ascii and store */
204 if (digval > 9)
205 *p++ = (char) (digval - 10 + 'a'); /* a letter */
206 else
207 *p++ = (char) (digval + '0'); /* a digit */
208 } while (val > 0);
209
210 /* We now have the digit of the number in the buffer, but in reverse
211 order. Thus we reverse them now. */
212
213 *p-- = '\0'; /* terminate string; p points to last digit */
214
215 do {
216 temp = *p;
217 *p = *firstdig;
218 *firstdig = temp; /* swap *p and *firstdig */
219 --p;
220 ++firstdig; /* advance to next two digits */
221 } while (firstdig < p); /* repeat until halfway */
222 }
223
224 /* Actual functions just call conversion helper with neg flag set correctly,
225 and return pointer to buffer. */
226
227 extern "C" char * __cdecl _itoa(int val, char *buf, int radix) {
228 if (radix == 10 && val < 0)
229 xtoa((unsigned long)val, buf, radix, 1);
230 else
231 xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
232 return buf;
233 }
234
235 /***
236 *strlen.c - contains strlen() routine
237 *
238 * Copyright (c) Microsoft Corporation. All rights reserved.
239 *
240 *Purpose:
241 * strlen returns the length of a null-terminated string,
242 * not including the null byte itself.
243 *
244 *******************************************************************************/
245
246 #ifdef _MSC_VER
247 #pragma function(strlen)
248 #endif /* _MSC_VER */
249
250 /***
251 *strlen - return the length of a null-terminated string
252 *
253 *Purpose:
254 * Finds the length in bytes of the given string, not including
255 * the final null character.
256 *
257 *Entry:
258 * const char * str - string whose length is to be computed
259 *
260 *Exit:
261 * length of the string "str", exclusive of the final null byte
262 *
263 *Exceptions:
264 *
265 *******************************************************************************/
266
267 size_t __cdecl strlen(const char * str) {
268 const char *eos = str;
269
270 while (*eos++) ;
271
272 return( (int)(eos - str - 1) );
273 }
274
275 /***
276 *wcsncpy.c - copy at most n characters of wide-character string
277 *
278 * Copyright (c) Microsoft Corporation. All rights reserved.
279 *
280 *Purpose:
281 * defines wcsncpy() - copy at most n characters of wchar_t string
282 *
283 *******************************************************************************/
284
285 /***
286 *wchar_t *wcsncpy(dest, source, count) - copy at most n wide characters
287 *
288 *Purpose:
289 * Copies count characters from the source string to the
290 * destination. If count is less than the length of source,
291 * NO NULL CHARACTER is put onto the end of the copied string.
292 * If count is greater than the length of sources, dest is padded
293 * with null characters to length count (wide-characters).
294 *
295 *
296 *Entry:
297 * wchar_t *dest - pointer to destination
298 * wchar_t *source - source string for copy
299 * size_t count - max number of characters to copy
300 *
301 *Exit:
302 * returns dest
303 *
304 *Exceptions:
305 *
306 *******************************************************************************/
307
308 wchar_t * __cdecl wcsncpy(wchar_t * dest, const wchar_t * source, size_t count) {
309 wchar_t *start = dest;
310
311 while (count && (*dest++ = *source++)) /* copy string */
312 count--;
313
314 if (count) /* pad out with zeroes */
315 while (--count)
316 *dest++ = L'\0';
317
318 return(start);
319 }
320
321 /***
322 *wcscmp.c - routine to compare two wchar_t strings (for equal, less, or greater)
323 *
324 * Copyright (c) Microsoft Corporation. All rights reserved.
325 *
326 *Purpose:
327 * Compares two wide-character strings, determining their lexical order.
328 *
329 *******************************************************************************/
330
331 /***
332 *wcscmp - compare two wchar_t strings,
333 * returning less than, equal to, or greater than
334 *
335 *Purpose:
336 * wcscmp compares two wide-character strings and returns an integer
337 * to indicate whether the first is less than the second, the two are
338 * equal, or whether the first is greater than the second.
339 *
340 * Comparison is done wchar_t by wchar_t on an UNSIGNED basis, which is to
341 * say that Null wchar_t(0) is less than any other character.
342 *
343 *Entry:
344 * const wchar_t * src - string for left-hand side of comparison
345 * const wchar_t * dst - string for right-hand side of comparison
346 *
347 *Exit:
348 * returns -1 if src < dst
349 * returns 0 if src == dst
350 * returns +1 if src > dst
351 *
352 *Exceptions:
353 *
354 *******************************************************************************/
355
356 int __cdecl wcscmp(const wchar_t * src, const wchar_t * dst) {
357 int ret = 0 ;
358
359 while (! (ret = (int)(*src - *dst)) && *dst)
360 ++src, ++dst;
361
362 if (ret < 0)
363 ret = -1 ;
364 else if (ret > 0)
365 ret = 1 ;
366
367 return( ret );
368 }
369
370 /***
371 *wcslen.c - contains wcslen() routine
372 *
373 * Copyright (c) Microsoft Corporation. All rights reserved.
374 *
375 *Purpose:
376 * wcslen returns the length of a null-terminated wide-character string,
377 * not including the null wchar_t itself.
378 *
379 *******************************************************************************/
380
381 /***
382 *wcslen - return the length of a null-terminated wide-character string
383 *
384 *Purpose:
385 * Finds the length in wchar_t's of the given string, not including
386 * the final null wchar_t (wide-characters).
387 *
388 *Entry:
389 * const wchar_t * wcs - string whose length is to be computed
390 *
391 *Exit:
392 * length of the string "wcs", exclusive of the final null wchar_t
393 *
394 *Exceptions:
395 *
396 *******************************************************************************/
397
398 size_t __cdecl wcslen(
399 const wchar_t * wcs
400 ) {
401 const wchar_t *eos = wcs;
402
403 while (*eos++) ;
404
405 return( (size_t)(eos - wcs - 1) );
406 }
407
408 /***
409 *strstr.c - search for one string inside another
410 *
411 * Copyright (c) Microsoft Corporation. All rights reserved.
412 *
413 *Purpose:
414 * defines strstr() - search for one string inside another
415 *
416 *******************************************************************************/
417
418 /***
419 *char *strstr(string1, string2) - search for string2 in string1
420 *
421 *Purpose:
422 * finds the first occurrence of string2 in string1
423 *
424 *Entry:
425 * char *string1 - string to search in
426 * char *string2 - string to search for
427 *
428 *Exit:
429 * returns a pointer to the first occurrence of string2 in
430 * string1, or NULL if string2 does not occur in string1
431 *
432 *Uses:
433 *
434 *Exceptions:
435 *
436 *******************************************************************************/
437
438 _CONST_RETURN char * __cdecl strstr(
439 const char * str1,
440 const char * str2
441 ) {
442 char *cp = (char *) str1;
443 char *s1, *s2;
444
445 if (!*str2)
446 return((char *)str1);
447
448 while (*cp)
449 {
450 s1 = cp;
451 s2 = (char *) str2;
452
453 while (*s1 && *s2 && !(*s1-*s2))
454 s1++, s2++;
455
456 if (!*s2)
457 return(cp);
458
459 cp++;
460 }
461
462 return(NULL);
463
464 }
465
466 /***
467 *strcmp.c - routine to compare two strings (for equal, less, or greater)
468 *
469 * Copyright (c) Microsoft Corporation. All rights reserved.
470 *
471 *Purpose:
472 * Compares two string, determining their lexical order.
473 *
474 *******************************************************************************/
475
476 #ifdef _MSC_VER
477 #pragma function(strcmp)
478 #endif /* _MSC_VER */
479
480 /***
481 *strcmp - compare two strings, returning less than, equal to, or greater than
482 *
483 *Purpose:
484 * STRCMP compares two strings and returns an integer
485 * to indicate whether the first is less than the second, the two are
486 * equal, or whether the first is greater than the second.
487 *
488 * Comparison is done byte by byte on an UNSIGNED basis, which is to
489 * say that Null (0) is less than any other character (1-255).
490 *
491 *Entry:
492 * const char * src - string for left-hand side of comparison
493 * const char * dst - string for right-hand side of comparison
494 *
495 *Exit:
496 * returns -1 if src < dst
497 * returns 0 if src == dst
498 * returns +1 if src > dst
499 *
500 *Exceptions:
501 *
502 *******************************************************************************/
503
504 int __cdecl strcmp(
505 const char * src,
506 const char * dst
507 ) {
508 int ret = 0 ;
509
510 while (! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
511 ++src, ++dst;
512
513 if (ret < 0)
514 ret = -1 ;
515 else if (ret > 0)
516 ret = 1 ;
517
518 return( ret );
519 }
520
521 #ifndef _MBSCAT
522 #ifdef _MSC_VER
523 #pragma function(strcpy)
524 #endif /* _MSC_VER */
525 #endif /* _MBSCAT */
526
527 /***
528 *char *strcpy(dst, src) - copy one string over another
529 *
530 *Purpose:
531 * Copies the string src into the spot specified by
532 * dest; assumes enough room.
533 *
534 *Entry:
535 * char * dst - string over which "src" is to be copied
536 * const char * src - string to be copied over "dst"
537 *
538 *Exit:
539 * The address of "dst"
540 *
541 *Exceptions:
542 *******************************************************************************/
543
544 char * __cdecl strcpy(char * dst, const char * src) {
545 char * cp = dst;
546
547 while (*cp++ = *src++)
548 ; /* Copy src over dst */
549
550 return( dst );
551 }
552
553 /***
554 *strncmp.c - compare first n characters of two strings
555 *
556 * Copyright (c) Microsoft Corporation. All rights reserved.
557 *
558 *Purpose:
559 * defines strncmp() - compare first n characters of two strings
560 * for lexical order.
561 *
562 *******************************************************************************/
563
564 /***
565 *int strncmp(first, last, count) - compare first count chars of strings
566 *
567 *Purpose:
568 * Compares two strings for lexical order. The comparison stops
569 * after: (1) a difference between the strings is found, (2) the end
570 * of the strings is reached, or (3) count characters have been
571 * compared.
572 *
573 *Entry:
574 * char *first, *last - strings to compare
575 * unsigned count - maximum number of characters to compare
576 *
577 *Exit:
578 * returns <0 if first < last
579 * returns 0 if first == last
580 * returns >0 if first > last
581 *
582 *Exceptions:
583 *
584 *******************************************************************************/
585
586 int __cdecl strncmp(
587 const char * first,
588 const char * last,
589 size_t count
590 ) {
591 if (!count)
592 return(0);
593
594 while (--count && *first && *first == *last)
595 {
596 first++;
597 last++;
598 }
599
600 return( *(unsigned char *)first - *(unsigned char *)last );
601 }
602
603 /***
604 *strchr.c - search a string for a given character
605 *
606 * Copyright (c) Microsoft Corporation. All rights reserved.
607 *
608 *Purpose:
609 * defines strchr() - search a string for a character
610 *
611 *******************************************************************************/
612
613 /***
614 *char *strchr(string, c) - search a string for a character
615 *
616 *Purpose:
617 * Searches a string for a given character, which may be the
618 * null character '\0'.
619 *
620 *Entry:
621 * char *string - string to search in
622 * char c - character to search for
623 *
624 *Exit:
625 * returns pointer to the first occurence of c in string
626 * returns NULL if c does not occur in string
627 *
628 *Exceptions:
629 *
630 *******************************************************************************/
631
632 _CONST_RETURN char * __cdecl strchr(
633 const char * string,
634 int ch
635 ) {
636 while (*string && *string != (char)ch)
637 string++;
638
639 if (*string == (char)ch)
640 return((char *)string);
641 return(NULL);
642 }
643
644 /***
645 *wcsncmp.c - compare first n characters of two wide-character strings
646 *
647 * Copyright (c) Microsoft Corporation. All rights reserved.
648 *
649 *Purpose:
650 * defines wcsncmp() - compare first n characters of two wchar_t strings
651 * for lexical order.
652 *
653 *******************************************************************************/
654
655 /***
656 *int wcsncmp(first, last, count) - compare first count chars of wchar_t strings
657 *
658 *Purpose:
659 * Compares two strings for lexical order. The comparison stops
660 * after: (1) a difference between the strings is found, (2) the end
661 * of the strings is reached, or (3) count characters have been
662 * compared (wide-character strings).
663 *
664 *Entry:
665 * wchar_t *first, *last - strings to compare
666 * size_t count - maximum number of characters to compare
667 *
668 *Exit:
669 * returns <0 if first < last
670 * returns 0 if first == last
671 * returns >0 if first > last
672 *
673 *Exceptions:
674 *
675 *******************************************************************************/
676
677 int __cdecl wcsncmp(
678 const wchar_t * first,
679 const wchar_t * last,
680 size_t count
681 ) {
682 if (!count)
683 return(0);
684
685 while (--count && *first && *first == *last)
686 {
687 first++;
688 last++;
689 }
690
691 return((int)(*first - *last));
692 }
693
694 /***
695 *wcsspn.c - find length of initial substring of chars from a control string
696 * (wide-character strings)
697 *
698 * Copyright (c) Microsoft Corporation. All rights reserved.
699 *
700 *Purpose:
701 * defines wcsspn() - finds the length of the initial substring of
702 * a string consisting entirely of characters from a control string
703 * (wide-character strings).
704 *
705 *******************************************************************************/
706
707 /***
708 *int wcsspn(string, control) - find init substring of control chars
709 *
710 *Purpose:
711 * Finds the index of the first character in string that does belong
712 * to the set of characters specified by control. This is
713 * equivalent to the length of the initial substring of string that
714 * consists entirely of characters from control. The L'\0' character
715 * that terminates control is not considered in the matching process
716 * (wide-character strings).
717 *
718 *Entry:
719 * wchar_t *string - string to search
720 * wchar_t *control - string containing characters not to search for
721 *
722 *Exit:
723 * returns index of first wchar_t in string not in control
724 *
725 *Exceptions:
726 *
727 *******************************************************************************/
728
729 size_t __cdecl wcsspn(
730 const wchar_t * string,
731 const wchar_t * control
732 ) {
733 wchar_t *str = (wchar_t *) string;
734 wchar_t *ctl;
735
736 /* 1st char not in control string stops search */
737 while (*str) {
738 for (ctl = (wchar_t *)control; *ctl != *str; ctl++) {
739 if (*ctl == (wchar_t)0) {
740 /*
741 * reached end of control string without finding a match
742 */
743 return (size_t)(str - string);
744 }
745 }
746 str++;
747 }
748 /*
749 * The whole string consisted of characters from control
750 */
751 return (size_t)(str - string);
752 }
753
754 /***
755 *wcscspn.c - find length of initial substring of wide characters
756 * not in a control string
757 *
758 * Copyright (c) Microsoft Corporation. All rights reserved.
759 *
760 *Purpose:
761 * defines wcscspn()- finds the length of the initial substring of
762 * a string consisting entirely of characters not in a control string
763 * (wide-character strings).
764 *
765 *******************************************************************************/
766
767 /***
768 *size_t wcscspn(string, control) - search for init substring w/o control wchars
769 *
770 *Purpose:
771 * returns the index of the first character in string that belongs
772 * to the set of characters specified by control. This is equivalent
773 * to the length of the length of the initial substring of string
774 * composed entirely of characters not in control. Null chars not
775 * considered (wide-character strings).
776 *
777 *Entry:
778 * wchar_t *string - string to search
779 * wchar_t *control - set of characters not allowed in init substring
780 *
781 *Exit:
782 * returns the index of the first wchar_t in string
783 * that is in the set of characters specified by control.
784 *
785 *Exceptions:
786 *
787 *******************************************************************************/
788
789 size_t __cdecl wcscspn(
790 const wchar_t * string,
791 const wchar_t * control
792 ) {
793 wchar_t *str = (wchar_t *) string;
794 wchar_t *wcset;
795
796 /* 1st char in control string stops search */
797 while (*str) {
798 for (wcset = (wchar_t *)control; *wcset; wcset++) {
799 if (*wcset == *str) {
800 return (size_t)(str - string);
801 }
802 }
803 str++;
804 }
805 return (size_t)(str - string);
806 }
807
808 /***
809 *wchar_t *wcscpy(dst, src) - copy one wchar_t string over another
810 *
811 *Purpose:
812 * Copies the wchar_t string src into the spot specified by
813 * dest; assumes enough room.
814 *
815 *Entry:
816 * wchar_t * dst - wchar_t string over which "src" is to be copied
817 * const wchar_t * src - wchar_t string to be copied over "dst"
818 *
819 *Exit:
820 * The address of "dst"
821 *
822 *Exceptions:
823 *******************************************************************************/
824
825 wchar_t * __cdecl wcscpy(wchar_t * dst, const wchar_t * src)
826 {
827 wchar_t * cp = dst;
828
829 while( *cp++ = *src++ )
830 ; /* Copy src over dst */
831
832 return( dst );
833 }
834
835 /***
836 *strtol, strtoul(nptr,endptr,ibase) - Convert ascii string to long un/signed
837 * int.
838 *
839 *Purpose:
840 * Convert an ascii string to a long 32-bit value. The base
841 * used for the caculations is supplied by the caller. The base
842 * must be in the range 0, 2-36. If a base of 0 is supplied, the
843 * ascii string must be examined to determine the base of the
844 * number:
845 * (a) First char = '0', second char = 'x' or 'X',
846 * use base 16.
847 * (b) First char = '0', use base 8
848 * (c) First char in range '1' - '9', use base 10.
849 *
850 * If the 'endptr' value is non-NULL, then strtol/strtoul places
851 * a pointer to the terminating character in this value.
852 * See ANSI standard for details
853 *
854 *Entry:
855 * nptr == NEAR/FAR pointer to the start of string.
856 * endptr == NEAR/FAR pointer to the end of the string.
857 * ibase == integer base to use for the calculations.
858 *
859 * string format: [whitespace] [sign] [0] [x] [digits/letters]
860 *
861 *Exit:
862 * Good return:
863 * result
864 *
865 * Overflow return:
866 * strtol -- LONG_MAX or LONG_MIN
867 * strtoul -- ULONG_MAX
868 * strtol/strtoul -- errno == ERANGE
869 *
870 * No digits or bad base return:
871 * 0
872 * endptr = nptr*
873 *
874 *Exceptions:
875 * None.
876 *******************************************************************************/
877
878 /* flag values */
879 #define FL_UNSIGNED 1 /* strtoul called */
880 #define FL_NEG 2 /* negative sign found */
881 #define FL_OVERFLOW 4 /* overflow occured */
882 #define FL_READDIGIT 8 /* we've read at least one correct digit */
883
884 // __ascii_isdigit returns a non-zero value if c is a decimal digit (0 – 9).
885 int __ascii_isdigit(int c)
886 {
887 return (c >= '0' && c <= '9');
888 }
889
890 // __ascii_isalpha returns a nonzero value if c is within
891 // the ranges A – Z or a – z.
892 int __ascii_isalpha(int c)
893 {
894 return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
895 }
896
897 // __ascii_toupper converts lowercase character to uppercase.
898 int __ascii_toupper(int c)
899 {
900 if (c >= 'a' && c <= 'z') return (c - ('a' - 'A'));
901 return c;
902 }
903
904 int isspace(int c)
905 {
906 static bool spaces[256] =
907 {
908 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // 0-9
909 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 10-19
910 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-29
911 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 30-39
912 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-49
913 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-59
914 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-69
915 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-79
916 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-89
917 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-99
918 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 100-109
919 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 110-119
920 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 120-129
921 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 130-139
922 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 140-149
923 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 150-159
924 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160-169
925 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 170-179
926 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 180-189
927 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 190-199
928 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 200-209
929 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 210-219
930 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 220-229
931 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 230-239
932 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 240-249
933 0, 0, 0, 0, 0, 1, // 250-255
934 };
935
936 return spaces[static_cast<unsigned char>(c)] == 1;
937 }
938
939 static unsigned long __cdecl strtoxl (
940 const char *nptr,
941 const char **endptr,
942 int ibase,
943 int flags
944 )
945 {
946 const char *p;
947 char c;
948 unsigned long number;
949 unsigned digval;
950 unsigned long maxval;
951
952 p = nptr; /* p is our scanning pointer */
953 number = 0; /* start with zero */
954
955 c = *p++; /* read char */
956 while ( isspace((int)(unsigned char)c) )
957 c = *p++; /* skip whitespace */
958
959 if (c == '-') {
960 flags |= FL_NEG; /* remember minus sign */
961 c = *p++;
962 }
963 else if (c == '+')
964 c = *p++; /* skip sign */
965
966 if (ibase < 0 || ibase == 1 || ibase > 36) {
967 /* bad base! */
968 if (endptr)
969 /* store beginning of string in endptr */
970 *endptr = nptr;
971 return 0L; /* return 0 */
972 }
973 else if (ibase == 0) {
974 /* determine base free-lance, based on first two chars of
975 string */
976 if (c != '0')
977 ibase = 10;
978 else if (*p == 'x' || *p == 'X')
979 ibase = 16;
980 else
981 ibase = 8;
982 }
983
984 if (ibase == 16) {
985 /* we might have 0x in front of number; remove if there */
986 if (c == '0' && (*p == 'x' || *p == 'X')) {
987 ++p;
988 c = *p++; /* advance past prefix */
989 }
990 }
991
992 /* if our number exceeds this, we will overflow on multiply */
993 maxval = ULONG_MAX / ibase;
994
995
996 for (;;) { /* exit in middle of loop */
997 /* convert c to value */
998 if ( __ascii_isdigit((int)(unsigned char)c) )
999 digval = c - '0';
1000 else if ( __ascii_isalpha((int)(unsigned char)c) )
1001 digval = __ascii_toupper(c) - 'A' + 10;
1002 else
1003 break;
1004 if (digval >= (unsigned)ibase)
1005 break; /* exit loop if bad digit found */
1006
1007 /* record the fact we have read one digit */
1008 flags |= FL_READDIGIT;
1009
1010 /* we now need to compute number = number * base + digval,
1011 but we need to know if overflow occured. This requires
1012 a tricky pre-check. */
1013
1014 if (number < maxval || (number == maxval &&
1015 (unsigned long)digval <= ULONG_MAX % ibase)) {
1016 /* we won't overflow, go ahead and multiply */
1017 number = number * ibase + digval;
1018 }
1019 else {
1020 /* we would have overflowed -- set the overflow flag */
1021 flags |= FL_OVERFLOW;
1022 }
1023
1024 c = *p++; /* read next digit */
1025 }
1026
1027 --p; /* point to place that stopped scan */
1028
1029 if (!(flags & FL_READDIGIT)) {
1030 /* no number there; return 0 and point to beginning of
1031 string */
1032 if (endptr)
1033 /* store beginning of string in endptr later on */
1034 p = nptr;
1035 number = 0L; /* return 0 */
1036 }
1037 else if ( (flags & FL_OVERFLOW) ||
1038 ( !(flags & FL_UNSIGNED) &&
1039 ( ( (flags & FL_NEG) && (number > -LONG_MIN) ) ||
1040 ( !(flags & FL_NEG) && (number > LONG_MAX) ) ) ) )
1041 {
1042 /* overflow or signed overflow occurred */
1043 // errno = ERANGE;
1044 if ( flags & FL_UNSIGNED )
1045 number = ULONG_MAX;
1046 else if ( flags & FL_NEG )
1047 number = (unsigned long)(-LONG_MIN);
1048 else
1049 number = LONG_MAX;
1050 }
1051
1052 if (endptr != NULL)
1053 /* store pointer to char that stopped the scan */
1054 *endptr = p;
1055
1056 if (flags & FL_NEG)
1057 /* negate result if there was a neg sign */
1058 number = (unsigned long)(-(long)number);
1059
1060 return number; /* done. */
1061 }
1062
1063 long __cdecl strtol (
1064 const char *nptr,
1065 char **endptr,
1066 int ibase
1067 )
1068 {
1069 return (long) strtoxl(nptr, (const char**)endptr, ibase, 0);
1070 }
1071
1072 unsigned long __cdecl strtoul (
1073 const char *nptr,
1074 char **endptr,
1075 int ibase
1076 )
1077 {
1078 return strtoxl(nptr, (const char**)endptr, ibase, FL_UNSIGNED);
1079 }
1080
OLDNEW
« no previous file with comments | « third_party/minicrt/string.c ('k') | third_party/minicrt/struplwr.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698