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.

370 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1998 Intel Corporation
  3. Module Name:
  4. str.c
  5. Abstract:
  6. Revision History
  7. --*/
  8. #include "lib.h"
  9. INTN
  10. StrCmp (
  11. IN CHAR16 *s1,
  12. IN CHAR16 *s2
  13. )
  14. /* compare strings */
  15. {
  16. return RtStrCmp(s1, s2);
  17. }
  18. INTN
  19. StrnCmp (
  20. IN CHAR16 *s1,
  21. IN CHAR16 *s2,
  22. IN UINTN len
  23. )
  24. /* compare strings */
  25. {
  26. while (*s1 && len) {
  27. if (*s1 != *s2) {
  28. break;
  29. }
  30. s1 += 1;
  31. s2 += 1;
  32. len -= 1;
  33. }
  34. return len ? *s1 - *s2 : 0;
  35. }
  36. INTN
  37. LibStubStriCmp (
  38. IN EFI_UNICODE_COLLATION_INTERFACE *This,
  39. IN CHAR16 *s1,
  40. IN CHAR16 *s2
  41. )
  42. {
  43. return StrCmp (s1, s2);
  44. }
  45. VOID
  46. LibStubStrLwrUpr (
  47. IN EFI_UNICODE_COLLATION_INTERFACE *This,
  48. IN CHAR16 *Str
  49. )
  50. {
  51. }
  52. INTN
  53. StriCmp (
  54. IN CHAR16 *s1,
  55. IN CHAR16 *s2
  56. )
  57. /* compare strings */
  58. {
  59. return UnicodeInterface->StriColl(UnicodeInterface, s1, s2);
  60. }
  61. VOID
  62. StrLwr (
  63. IN CHAR16 *Str
  64. )
  65. /* lwoer case string */
  66. {
  67. UnicodeInterface->StrLwr(UnicodeInterface, Str);
  68. }
  69. VOID
  70. StrUpr (
  71. IN CHAR16 *Str
  72. )
  73. /* upper case string */
  74. {
  75. UnicodeInterface->StrUpr(UnicodeInterface, Str);
  76. }
  77. VOID
  78. StrCpy (
  79. IN CHAR16 *Dest,
  80. IN CHAR16 *Src
  81. )
  82. /* copy strings */
  83. {
  84. RtStrCpy (Dest, Src);
  85. }
  86. VOID
  87. StrCat (
  88. IN CHAR16 *Dest,
  89. IN CHAR16 *Src
  90. )
  91. {
  92. RtStrCat(Dest, Src);
  93. }
  94. UINTN
  95. StrLen (
  96. IN CHAR16 *s1
  97. )
  98. /* string length */
  99. {
  100. return RtStrLen(s1);
  101. }
  102. UINTN
  103. StrSize (
  104. IN CHAR16 *s1
  105. )
  106. /* string size */
  107. {
  108. return RtStrSize(s1);
  109. }
  110. CHAR16 *
  111. StrDuplicate (
  112. IN CHAR16 *Src
  113. )
  114. /* duplicate a string */
  115. {
  116. CHAR16 *Dest;
  117. UINTN Size;
  118. Size = StrSize(Src);
  119. Dest = AllocatePool (Size);
  120. if (Dest) {
  121. CopyMem (Dest, Src, Size);
  122. }
  123. return Dest;
  124. }
  125. UINTN
  126. strlena (
  127. IN CHAR8 *s1
  128. )
  129. /* string length */
  130. {
  131. UINTN len;
  132. for (len=0; *s1; s1+=1, len+=1) ;
  133. return len;
  134. }
  135. UINTN
  136. strcmpa (
  137. IN CHAR8 *s1,
  138. IN CHAR8 *s2
  139. )
  140. /* compare strings */
  141. {
  142. while (*s1) {
  143. if (*s1 != *s2) {
  144. break;
  145. }
  146. s1 += 1;
  147. s2 += 1;
  148. }
  149. return *s1 - *s2;
  150. }
  151. UINTN
  152. strncmpa (
  153. IN CHAR8 *s1,
  154. IN CHAR8 *s2,
  155. IN UINTN len
  156. )
  157. /* compare strings */
  158. {
  159. while (*s1 && len) {
  160. if (*s1 != *s2) {
  161. break;
  162. }
  163. s1 += 1;
  164. s2 += 1;
  165. len -= 1;
  166. }
  167. return len ? *s1 - *s2 : 0;
  168. }
  169. UINTN
  170. xtoi (
  171. CHAR16 *str
  172. )
  173. /* convert hex string to uint */
  174. {
  175. UINTN u;
  176. CHAR16 c;
  177. /* skip preceeding white space */
  178. while (*str && *str == ' ') {
  179. str += 1;
  180. }
  181. /* convert hex digits */
  182. u = 0;
  183. while (c = *(str++)) {
  184. if (c >= 'a' && c <= 'f') {
  185. c -= 'a' - 'A';
  186. }
  187. if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
  188. u = u << 4 | c - (c >= 'A' ? 'A'-10 : '0');
  189. } else {
  190. break;
  191. }
  192. }
  193. return u;
  194. }
  195. UINTN
  196. Atoi (
  197. CHAR16 *str
  198. )
  199. /* convert hex string to uint */
  200. {
  201. UINTN u;
  202. CHAR16 c;
  203. /* skip preceeding white space */
  204. while (*str && *str == ' ') {
  205. str += 1;
  206. }
  207. /* convert digits */
  208. u = 0;
  209. while (c = *(str++)) {
  210. if (c >= '0' && c <= '9') {
  211. u = (u * 10) + c - '0';
  212. } else {
  213. break;
  214. }
  215. }
  216. return u;
  217. }
  218. BOOLEAN
  219. MetaMatch (
  220. IN CHAR16 *String,
  221. IN CHAR16 *Pattern
  222. )
  223. {
  224. CHAR16 c, p, l;
  225. for (; ;) {
  226. p = *Pattern;
  227. Pattern += 1;
  228. switch (p) {
  229. case 0:
  230. /* End of pattern. If end of string, TRUE match */
  231. return *String ? FALSE : TRUE;
  232. case '*':
  233. /* Match zero or more chars */
  234. while (*String) {
  235. if (MetaMatch (String, Pattern)) {
  236. return TRUE;
  237. }
  238. String += 1;
  239. }
  240. return MetaMatch (String, Pattern);
  241. case '?':
  242. /* Match any one char */
  243. if (!*String) {
  244. return FALSE;
  245. }
  246. String += 1;
  247. break;
  248. case '[':
  249. /* Match char set */
  250. c = *String;
  251. if (!c) {
  252. return FALSE; /* syntax problem */
  253. }
  254. l = 0;
  255. while ( p = *Pattern++ ) {
  256. if (p == ']') {
  257. return FALSE;
  258. }
  259. if (p == '-') { /* if range of chars, */
  260. p = *Pattern; /* get high range */
  261. if (p == 0 || p == ']') {
  262. return FALSE; /* syntax problem */
  263. }
  264. if (c >= l && c <= p) { /* if in range, */
  265. break; /* it's a match */
  266. }
  267. }
  268. l = p;
  269. if (c == p) { /* if char matches */
  270. break; /* move on */
  271. }
  272. }
  273. /* skip to end of match char set */
  274. while (p && p != ']') {
  275. p = *Pattern;
  276. Pattern += 1;
  277. }
  278. String += 1;
  279. break;
  280. default:
  281. c = *String;
  282. if (c != p) {
  283. return FALSE;
  284. }
  285. String += 1;
  286. break;
  287. }
  288. }
  289. }
  290. BOOLEAN
  291. LibStubMetaiMatch (
  292. IN EFI_UNICODE_COLLATION_INTERFACE *This,
  293. IN CHAR16 *String,
  294. IN CHAR16 *Pattern
  295. )
  296. {
  297. return MetaMatch (String, Pattern);
  298. }
  299. BOOLEAN
  300. MetaiMatch (
  301. IN CHAR16 *String,
  302. IN CHAR16 *Pattern
  303. )
  304. {
  305. return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
  306. }