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.

175 lines
4.2 KiB

  1. /***
  2. *mbsspn.c - Search for init substring of chars from control string (MBCS).
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Search for init substring of chars from control string (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. #include <tchar.h>
  30. /***
  31. *ifndef _RETURN_PTR
  32. * _mbsspn - Find first string char not in charset (MBCS)
  33. *else
  34. * _mbsspnp - Find first string char not in charset, return pointer (MBCS)
  35. *endif
  36. *
  37. *Purpose:
  38. * Returns maximum leading segment of string consisting solely
  39. * of characters from charset. Handles MBCS characters correctly.
  40. *
  41. *Entry:
  42. * unsigned char *string = string to search in
  43. * unsigned char *charset = set of characters to scan over
  44. *
  45. *Exit:
  46. *
  47. *ifndef _RETURN_PTR
  48. * Returns index of first char in string not in control.
  49. * Returns 0, if string begins with a character not in charset.
  50. *else
  51. * Returns pointer to first character not in charset.
  52. * Returns NULL if string consists entirely of characters from charset.
  53. *endif
  54. *
  55. *Exceptions:
  56. *
  57. *******************************************************************************/
  58. #ifndef _RETURN_PTR
  59. size_t __cdecl _mbsspn(
  60. const unsigned char *string,
  61. const unsigned char *charset
  62. )
  63. #else
  64. unsigned char * __cdecl _mbsspnp(
  65. const unsigned char *string,
  66. const unsigned char *charset
  67. )
  68. #endif
  69. {
  70. #ifdef _MT
  71. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  72. if ( ptmbci != __ptmbcinfo )
  73. ptmbci = __updatetmbcinfo();
  74. #ifndef _RETURN_PTR
  75. return __mbsspn_mt(ptmbci, string, charset);
  76. #else
  77. return __mbsspnp_mt(ptmbci, string, charset);
  78. #endif
  79. }
  80. #ifndef _RETURN_PTR
  81. size_t __cdecl __mbsspn_mt(
  82. pthreadmbcinfo ptmbci,
  83. const unsigned char *string,
  84. const unsigned char *charset
  85. )
  86. #else
  87. unsigned char * __cdecl __mbsspnp_mt(
  88. pthreadmbcinfo ptmbci,
  89. const unsigned char *string,
  90. const unsigned char *charset
  91. )
  92. #endif
  93. {
  94. #endif
  95. unsigned char *p, *q;
  96. #ifndef _RETURN_PTR
  97. #ifdef _MT
  98. if ( _ISNOTMBCP_MT(ptmbci) )
  99. #else
  100. if ( _ISNOTMBCP )
  101. #endif
  102. return strspn(string, charset);
  103. #else
  104. #ifdef _MT
  105. if ( _ISNOTMBCP_MT(ptmbci) )
  106. #else
  107. if ( _ISNOTMBCP )
  108. #endif
  109. {
  110. size_t retval;
  111. retval = strspn(string, charset);
  112. return (unsigned char *)(*(string + retval) ? string + retval : NULL);
  113. }
  114. #endif
  115. /* loop through the string to be inspected */
  116. for (q = (char *)string; *q; q++) {
  117. /* loop through the charset */
  118. for (p = (char *)charset; *p; p++) {
  119. #ifdef _MT
  120. if ( __ismbblead_mt(ptmbci, *p) ) {
  121. #else
  122. if ( _ismbblead(*p) ) {
  123. #endif
  124. if (((*p == *q) && (p[1] == q[1])) || p[1] == '\0')
  125. break;
  126. p++;
  127. }
  128. else
  129. if (*p == *q)
  130. break;
  131. }
  132. if (*p == '\0') /* end of charset? */
  133. break; /* yes, no match on this char */
  134. #ifdef _MT
  135. if ( __ismbblead_mt(ptmbci, *q) )
  136. #else
  137. if ( _ismbblead(*q) )
  138. #endif
  139. if (*++q == '\0')
  140. break;
  141. }
  142. #ifndef _RETURN_PTR
  143. return((size_t) (q - string)); /* index */
  144. #else
  145. return((*q) ? q : NULL); /* pointer */
  146. #endif
  147. }
  148. #endif /* _MBCS */