Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
3.9 KiB

  1. /***
  2. *mbscspn.c - Find first string char in charset (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Find first string char in charset (MBCS)
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  12. * 04-15-93 CFW Add _MB_CP_LOCK.
  13. * 05-09-94 CFW Optimize for SBCS.
  14. * 05-19-94 CFW Enable non-Win32.
  15. * 09-14-94 SKS Clean up preprocessor commands inside comments
  16. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  17. * 04-21-98 GJF Revised multithread support based on threadmbcinfo
  18. * structs
  19. *
  20. *******************************************************************************/
  21. #ifdef _MBCS
  22. #include <mtdll.h>
  23. #include <cruntime.h>
  24. #include <string.h>
  25. #include <mbdata.h>
  26. #include <mbctype.h>
  27. #include <mbstring.h>
  28. #include <stddef.h>
  29. /***
  30. *ifndef _RETURN_PTR
  31. * _mbscspn - Find first string char in charset (MBCS)
  32. *else
  33. * _mbspbrk - Find first string char in charset, pointer return (MBCS)
  34. *endif
  35. *
  36. *Purpose:
  37. * Returns maximum leading segment of string
  38. * which consists solely of characters NOT from charset.
  39. * Handles MBCS chars correctly.
  40. *
  41. *Entry:
  42. * char *string = string to search in
  43. * char *charset = set of characters to scan over
  44. *
  45. *Exit:
  46. *
  47. *ifndef _RETURN_PTR
  48. * Returns the index of the first char in string
  49. * that is in the set of characters specified by control.
  50. *
  51. * Returns 0, if string begins with a character in charset.
  52. *else
  53. * Returns pointer to first character in charset.
  54. *
  55. * Returns NULL if string consists entirely of characters
  56. * not from charset.
  57. *endif
  58. *
  59. *Exceptions:
  60. *
  61. *******************************************************************************/
  62. #ifndef _RETURN_PTR
  63. size_t __cdecl _mbscspn(
  64. const unsigned char *string,
  65. const unsigned char *charset
  66. )
  67. #else
  68. unsigned char * __cdecl _mbspbrk(
  69. const unsigned char *string,
  70. const unsigned char *charset
  71. )
  72. #endif
  73. {
  74. #ifdef _MT
  75. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  76. if ( ptmbci != __ptmbcinfo )
  77. ptmbci = __updatetmbcinfo();
  78. #ifndef _RETURN_PTR
  79. return __mbscspn_mt(ptmbci, string, charset);
  80. #else
  81. return __mbspbrk_mt(ptmbci, string, charset);
  82. #endif
  83. }
  84. #ifndef _RETURN_PTR
  85. size_t __cdecl __mbscspn_mt(
  86. pthreadmbcinfo ptmbci,
  87. const unsigned char *string,
  88. const unsigned char *charset
  89. )
  90. #else
  91. unsigned char * __cdecl __mbspbrk_mt(
  92. pthreadmbcinfo ptmbci,
  93. const unsigned char *string,
  94. const unsigned char *charset
  95. )
  96. #endif
  97. {
  98. #endif
  99. unsigned char *p, *q;
  100. #ifdef _MT
  101. if ( _ISNOTMBCP_MT(ptmbci) )
  102. #else
  103. if ( _ISNOTMBCP )
  104. #endif
  105. #ifndef _RETURN_PTR
  106. return strcspn(string, charset);
  107. #else
  108. return strpbrk(string, charset);
  109. #endif
  110. /* loop through the string to be inspected */
  111. for (q = (char *)string; *q ; q++) {
  112. /* loop through the charset */
  113. for (p = (char *)charset; *p ; p++) {
  114. #ifdef _MT
  115. if ( __ismbblead_mt(ptmbci, *p) ) {
  116. #else
  117. if ( _ismbblead(*p) ) {
  118. #endif
  119. if (((*p == *q) && (p[1] == q[1])) || p[1] == '\0')
  120. break;
  121. p++;
  122. }
  123. else
  124. if (*p == *q)
  125. break;
  126. }
  127. if (*p != '\0') /* end of charset? */
  128. break; /* no, match on this char */
  129. #ifdef _MT
  130. if ( __ismbblead_mt(ptmbci, *q) )
  131. #else
  132. if ( _ismbblead(*q) )
  133. #endif
  134. if (*++q == '\0')
  135. break;
  136. }
  137. #ifndef _RETURN_PTR
  138. return((size_t) (q - string)); /* index */
  139. #else
  140. return((*q) ? q : NULL); /* pointer */
  141. #endif
  142. }
  143. #endif /* _MBCS */