Leaked source code of windows server 2003
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.

99 lines
2.5 KiB

  1. /***
  2. *mbsrev.c - Reverse a string in place (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Reverse a string in place (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-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  16. * 04-17-98 GJF Revised multithread support based on threadmbcinfo
  17. * structs
  18. *
  19. *******************************************************************************/
  20. #ifdef _MBCS
  21. #include <mtdll.h>
  22. #include <cruntime.h>
  23. #include <string.h>
  24. #include <mbdata.h>
  25. #include <mbctype.h>
  26. #include <mbstring.h>
  27. /***
  28. * _mbsrev - Reverse a string in place (MBCS)
  29. *
  30. *Purpose:
  31. * Reverses the order of characters in the string. The terminating
  32. * null character remains in place. The order of MBCS characters
  33. * is not changed.
  34. *
  35. *Entry:
  36. * unsigned char *string = string to reverse
  37. *
  38. *Exit:
  39. * returns string - now with reversed characters
  40. *
  41. *Exceptions:
  42. *
  43. *******************************************************************************/
  44. unsigned char * __cdecl _mbsrev(
  45. unsigned char *string
  46. )
  47. {
  48. unsigned char *start = string;
  49. unsigned char *left = string;
  50. unsigned char c;
  51. #ifdef _MT
  52. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  53. if ( ptmbci != __ptmbcinfo )
  54. ptmbci = __updatetmbcinfo();
  55. if ( _ISNOTMBCP_MT(ptmbci) )
  56. #else
  57. if ( _ISNOTMBCP )
  58. #endif
  59. return _strrev(string);
  60. /* first go through and reverse the bytes in MBCS chars */
  61. while ( *string ) {
  62. #ifdef _MT
  63. if ( __ismbblead_mt(ptmbci, *string++) ) {
  64. #else
  65. if ( _ismbblead(*string++) ) {
  66. #endif
  67. if ( *string ) {
  68. c = *string;
  69. *string = *(string - 1);
  70. *(string - 1) = c;
  71. string++;
  72. }
  73. else /* second byte is EOS */
  74. break;
  75. }
  76. }
  77. /* now reverse the whole string */
  78. string--;
  79. while ( left < string ) {
  80. c = *left;
  81. *left++ = *string;
  82. *string-- = c;
  83. }
  84. return ( start );
  85. }
  86. #endif /* _MBCS */