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.

234 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. pastring.c
  5. Abstract:
  6. Routines that manage the pascal strings
  7. Author:
  8. Matthew Vanderzee (matthewv) 13-Aug-1999
  9. Revision History:
  10. --*/
  11. #include "pch.h"
  12. #include "memdbp.h"
  13. PPASTR
  14. StringPasConvertTo (
  15. IN OUT PWSTR str
  16. )
  17. /*++
  18. Converts a string in place from a zero-terminated
  19. string to a pascal-style string.
  20. --*/
  21. {
  22. WCHAR Len;
  23. MYASSERT(str);
  24. Len = (WORD) CharCountW (str);
  25. MoveMemory (str + 1, str, Len * sizeof(WCHAR));
  26. *str = Len;
  27. return str;
  28. }
  29. PWSTR
  30. StringPasConvertFrom (
  31. IN OUT PPASTR str
  32. )
  33. /*++
  34. Converts a string in place from a pascal-style string
  35. to a null-terminated string.
  36. --*/
  37. {
  38. WCHAR Len;
  39. MYASSERT(str);
  40. Len = *str;
  41. MoveMemory (str, str + 1, Len * sizeof(WCHAR));
  42. *(str + Len) = 0;
  43. return str;
  44. }
  45. PPASTR
  46. StringPasCopyConvertTo (
  47. OUT PPASTR str1,
  48. IN PCWSTR str2
  49. )
  50. /*++
  51. Converts a string from a zero-terminated
  52. string to a pascal-style string in a new buffer.
  53. --*/
  54. {
  55. MYASSERT(str1);
  56. MYASSERT(str2);
  57. *str1 = (WORD) CharCountW (str2);
  58. CopyMemory (str1 + 1, str2, *str1 * sizeof(WCHAR));
  59. return str1;
  60. }
  61. PWSTR
  62. StringPasCopyConvertFrom (
  63. OUT PWSTR str1,
  64. IN PCPASTR str2
  65. )
  66. /*++
  67. Converts a string from a pascal-style string
  68. to a null-terminated string in a new buffer.
  69. --*/
  70. {
  71. MYASSERT(str1);
  72. MYASSERT(str2);
  73. CopyMemory (str1, str2 + 1, *str2 * sizeof(WCHAR));
  74. *(str1 + *str2) = 0;
  75. return str1;
  76. }
  77. PPASTR
  78. StringPasCopy (
  79. OUT PPASTR str1,
  80. IN PCPASTR str2
  81. )
  82. /*++
  83. Copys a pascal string to a new buffer.
  84. --*/
  85. {
  86. MYASSERT(str1);
  87. MYASSERT(str2);
  88. CopyMemory (str1, str2, (*str2+1) * sizeof(WCHAR));
  89. return str1;
  90. }
  91. UINT
  92. StringPasCharCount (
  93. IN PCPASTR str
  94. )
  95. /*++
  96. Returns the number of characters in a string.
  97. --*/
  98. {
  99. MYASSERT(str);
  100. return (UINT)(*str);
  101. }
  102. INT
  103. StringPasCompare (
  104. IN PCPASTR str1,
  105. IN PCPASTR str2
  106. )
  107. /*++
  108. Compares two pascal-style strings, returns values
  109. in the same fashion as strcmp().
  110. --*/
  111. {
  112. INT equal;
  113. INT diff;
  114. MYASSERT(str1);
  115. MYASSERT(str2);
  116. //
  117. // diff is < 0 if str1 is shorter, = 0 if
  118. // strings are same length, otherwise > 0
  119. //
  120. diff = *str1 - *str2;
  121. equal = wcsncmp(str1+1, str2+1, (diff < 0) ? *str1 : *str2);
  122. if (equal != 0) {
  123. return equal;
  124. }
  125. return diff;
  126. }
  127. BOOL
  128. StringPasMatch (
  129. IN PCPASTR str1,
  130. IN PCPASTR str2
  131. )
  132. /*++
  133. Returns TRUE if the two strings match
  134. --*/
  135. {
  136. MYASSERT(str1);
  137. MYASSERT(str2);
  138. if (*str1 != *str2) {
  139. return FALSE;
  140. }
  141. return wcsncmp(str1+1, str2+1, *str2)==0;
  142. }
  143. INT
  144. StringPasICompare (
  145. IN PCPASTR str1,
  146. IN PCPASTR str2
  147. )
  148. /*++
  149. Compares two pascal-style strings, returns values
  150. in the same fashion as strcmp(). (CASE INSENSITIVE)
  151. --*/
  152. {
  153. INT equal;
  154. INT diff;
  155. MYASSERT(str1);
  156. MYASSERT(str2);
  157. //
  158. // diff is < 0 if str1 is shorter, = 0 if
  159. // strings are same length, otherwise > 0
  160. //
  161. diff = *str1 - *str2;
  162. equal = _wcsnicmp(str1+1, str2+1, (diff < 0) ? *str1 : *str2);
  163. if (equal != 0) {
  164. return equal;
  165. }
  166. return diff;
  167. }
  168. BOOL
  169. StringPasIMatch (
  170. IN PCPASTR str1,
  171. IN PCPASTR str2
  172. )
  173. /*++
  174. Returns TRUE if the two strings match (CASE INSENSITIVE)
  175. --*/
  176. {
  177. MYASSERT(str1);
  178. MYASSERT(str2);
  179. if (*str1 != *str2) {
  180. return FALSE;
  181. }
  182. return _wcsnicmp(str1+1, str2+1, *str2)==0;
  183. }