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.

98 lines
2.5 KiB

  1. /***
  2. *wcslwr.c - routine to map upper-case characters in a wchar_t string
  3. * to lower-case
  4. *
  5. * Copyright (c) 1985-1992, Microsoft Corporation. All rights reserved.
  6. *
  7. *Purpose:
  8. * Converts all the upper case characters in a wchar_t string
  9. * to lower case, in place.
  10. *
  11. *Revision History:
  12. * 09-09-91 ETC Created from strlwr.c.
  13. * 04-06-92 KRS Make work without _INTL also.
  14. * 08-19-92 KRS Activate NLS support.
  15. * 08-22-92 SRW Allow INTL definition to be conditional for building ntcrt.lib
  16. * 09-02-92 SRW Get _INTL definition via ..\crt32.def
  17. * 06-02-93 SRW ignore _INTL if _NTSUBSET_ defined.
  18. *
  19. *******************************************************************************/
  20. #include <windows.h>
  21. #include <cruntime.h>
  22. /***
  23. *wchar_t *_wcslwr(string) - map upper-case characters in a string to lower-case
  24. *
  25. *Purpose:
  26. * wcslwr converts upper-case characters in a null-terminated wchar_t
  27. * string to their lower-case equivalents. The result may be longer or
  28. * shorter than the original string. Assumes enough space in string
  29. * to hold the result.
  30. *
  31. *Entry:
  32. * wchar_t *wsrc - wchar_t string to change to lower case
  33. *
  34. *Exit:
  35. * input string address
  36. *
  37. *Exceptions:
  38. * on an error, the original string is unaltered
  39. *
  40. *******************************************************************************/
  41. wchar_t * _CALLTYPE1 _wcslwr (
  42. wchar_t * wsrc
  43. )
  44. {
  45. wchar_t * p;
  46. // Prescan the buffer. If all this is in the first 128 characters, it's just
  47. // ANSII, and we can do it ourselves...
  48. for (p = wsrc; *p; p++)
  49. {
  50. if (*p < 128)
  51. {
  52. *p = (wchar_t)CharLowerA((LPSTR)*p);
  53. }
  54. else
  55. {
  56. // We can't handle this character in place, so convert the remainder of the string
  57. // to MBS, lower-case it, and convert it back to Unicode.
  58. int wLength = lstrlenW (p);
  59. int mbsLength;
  60. CHAR * mbsBuffer = (CHAR *)LocalAlloc (LMEM_FIXED, 2 * sizeof (CHAR) * wLength + 1);
  61. if (mbsBuffer == NULL)
  62. {
  63. return NULL;
  64. }
  65. mbsLength = WideCharToMultiByte (CP_ACP,
  66. WC_COMPOSITECHECK,
  67. p,
  68. wLength,
  69. mbsBuffer,
  70. 2 * sizeof (CHAR) * wLength,
  71. NULL,
  72. NULL);
  73. if (mbsLength == 0)
  74. {
  75. // We don't know what happened, but it wasn't good
  76. return NULL;
  77. }
  78. mbsBuffer[mbsLength] = '\0';
  79. CharLowerA (mbsBuffer);
  80. if (MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, mbsBuffer, mbsLength, p, wLength) == 0)
  81. {
  82. return NULL;
  83. }
  84. break;
  85. }
  86. }
  87. return wsrc;
  88. }