1 /*** 2 *mbscspn.c - Find first string char in charset (MBCS) 3 * 4 * Copyright (c) Microsoft Corporation. All rights reserved. 5 * 6 *Purpose: 7 * Find first string char in charset (MBCS) 8 * 9 *******************************************************************************/ 10 #ifndef _MBCS 11 #error This file should only be compiled with _MBCS defined 12 #endif 13 14 #include <corecrt_internal.h> 15 #include <corecrt_internal_mbstring.h> 16 #include <locale.h> 17 #include <stddef.h> 18 #include <string.h> 19 20 /*** 21 *ifndef _RETURN_PTR 22 * _mbscspn - Find first string char in charset (MBCS) 23 *else 24 * _mbspbrk - Find first string char in charset, pointer return (MBCS) 25 *endif 26 * 27 *Purpose: 28 * Returns maximum leading segment of string 29 * which consists solely of characters NOT from charset. 30 * Handles MBCS chars correctly. 31 * 32 *Entry: 33 * char *string = string to search in 34 * char *charset = set of characters to scan over 35 * 36 *Exit: 37 * 38 *ifndef _RETURN_PTR 39 * Returns the index of the first char in string 40 * that is in the set of characters specified by control. 41 * 42 * Returns 0, if string begins with a character in charset. 43 *else 44 * Returns pointer to first character in charset. 45 * 46 * Returns nullptr if string consists entirely of characters 47 * not from charset. 48 *endif 49 * 50 *Exceptions: 51 * Input parameters are validated. Refer to the validation section of the function. 52 * 53 *******************************************************************************/ 54 55 #ifndef _RETURN_PTR 56 57 extern "C" size_t __cdecl _mbscspn_l( 58 const unsigned char *string, 59 const unsigned char *charset, 60 _locale_t plocinfo 61 ) 62 #else /* _RETURN_PTR */ 63 64 extern "C" const unsigned char * __cdecl _mbspbrk_l( 65 const unsigned char *string, 66 const unsigned char *charset, 67 _locale_t plocinfo 68 ) 69 #endif /* _RETURN_PTR */ 70 71 { 72 unsigned char *p, *q; 73 _LocaleUpdate _loc_update(plocinfo); 74 75 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0) 76 #ifndef _RETURN_PTR 77 return strcspn((const char *)string, (const char *)charset); 78 #else /* _RETURN_PTR */ 79 return (const unsigned char *)strpbrk((const char *)string, (const char *)charset); 80 #endif /* _RETURN_PTR */ 81 82 /* validation section */ 83 #ifndef _RETURN_PTR 84 _VALIDATE_RETURN(string != nullptr, EINVAL, 0); 85 _VALIDATE_RETURN(charset != nullptr, EINVAL, 0); 86 #else /* _RETURN_PTR */ 87 _VALIDATE_RETURN(string != nullptr, EINVAL, nullptr); 88 _VALIDATE_RETURN(charset != nullptr, EINVAL, nullptr); 89 #endif /* _RETURN_PTR */ 90 91 /* loop through the string to be inspected */ 92 for (q = (unsigned char *)string; *q ; q++) { 93 94 /* loop through the charset */ 95 for (p = (unsigned char *)charset; *p ; p++) { 96 97 if ( _ismbblead_l(*p, _loc_update.GetLocaleT()) ) { 98 if (((*p == *q) && (p[1] == q[1])) || p[1] == '\0') 99 break; 100 p++; 101 } 102 else 103 if (*p == *q) 104 break; 105 } 106 107 if (*p != '\0') /* end of charset? */ 108 break; /* no, match on this char */ 109 110 if ( _ismbblead_l(*q, _loc_update.GetLocaleT()) ) 111 if (*++q == '\0') 112 break; 113 } 114 115 #ifndef _RETURN_PTR 116 return((size_t) (q - string)); /* index */ 117 #else /* _RETURN_PTR */ 118 return((*q) ? q : nullptr); /* pointer */ 119 #endif /* _RETURN_PTR */ 120 121 } 122 123 #ifndef _RETURN_PTR 124 125 extern "C" size_t (__cdecl _mbscspn)( 126 const unsigned char *string, 127 const unsigned char *charset 128 ) 129 #else /* _RETURN_PTR */ 130 131 extern "C" const unsigned char * (__cdecl _mbspbrk)( 132 const unsigned char *string, 133 const unsigned char *charset 134 ) 135 #endif /* _RETURN_PTR */ 136 137 { 138 #ifndef _RETURN_PTR 139 return _mbscspn_l(string, charset, nullptr); 140 #else /* _RETURN_PTR */ 141 return _mbspbrk_l(string, charset, nullptr); 142 #endif /* _RETURN_PTR */ 143 } 144