str.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 CONST CHAR16 *s1,
  12. IN CONST CHAR16 *s2
  13. )
  14. // compare strings
  15. {
  16. return RtStrCmp(s1, s2);
  17. }
  18. INTN
  19. StrnCmp (
  20. IN CONST CHAR16 *s1,
  21. IN CONST 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 EFIAPI
  37. LibStubStriCmp (
  38. IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
  39. IN CHAR16 *s1,
  40. IN CHAR16 *s2
  41. )
  42. {
  43. return StrCmp (s1, s2);
  44. }
  45. VOID EFIAPI
  46. LibStubStrLwrUpr (
  47. IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
  48. IN CHAR16 *Str EFI_UNUSED
  49. )
  50. {
  51. }
  52. INTN
  53. StriCmp (
  54. IN CONST CHAR16 *s1,
  55. IN CONST CHAR16 *s2
  56. )
  57. // compare strings
  58. {
  59. if (UnicodeInterface == &LibStubUnicodeInterface)
  60. return UnicodeInterface->StriColl(UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
  61. else
  62. return uefi_call_wrapper(UnicodeInterface->StriColl, 3, UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
  63. }
  64. VOID
  65. StrLwr (
  66. IN CHAR16 *Str
  67. )
  68. // lwoer case string
  69. {
  70. if (UnicodeInterface == &LibStubUnicodeInterface)
  71. UnicodeInterface->StrLwr(UnicodeInterface, Str);
  72. else uefi_call_wrapper(UnicodeInterface->StrLwr, 2, UnicodeInterface, Str);
  73. }
  74. VOID
  75. StrUpr (
  76. IN CHAR16 *Str
  77. )
  78. // upper case string
  79. {
  80. if (UnicodeInterface == &LibStubUnicodeInterface)
  81. UnicodeInterface->StrUpr(UnicodeInterface, Str);
  82. else uefi_call_wrapper(UnicodeInterface->StrUpr, 2, UnicodeInterface, Str);
  83. }
  84. VOID
  85. StrCpy (
  86. IN CHAR16 *Dest,
  87. IN CONST CHAR16 *Src
  88. )
  89. // copy strings
  90. {
  91. RtStrCpy (Dest, Src);
  92. }
  93. VOID
  94. StrnCpy (
  95. IN CHAR16 *Dest,
  96. IN CONST CHAR16 *Src,
  97. IN UINTN Len
  98. )
  99. // copy strings
  100. {
  101. RtStrnCpy (Dest, Src, Len);
  102. }
  103. CHAR16 *
  104. StpCpy (
  105. IN CHAR16 *Dest,
  106. IN CONST CHAR16 *Src
  107. )
  108. // copy strings
  109. {
  110. return RtStpCpy (Dest, Src);
  111. }
  112. CHAR16 *
  113. StpnCpy (
  114. IN CHAR16 *Dest,
  115. IN CONST CHAR16 *Src,
  116. IN UINTN Len
  117. )
  118. // copy strings
  119. {
  120. return RtStpnCpy (Dest, Src, Len);
  121. }
  122. VOID
  123. StrCat (
  124. IN CHAR16 *Dest,
  125. IN CONST CHAR16 *Src
  126. )
  127. {
  128. RtStrCat(Dest, Src);
  129. }
  130. VOID
  131. StrnCat (
  132. IN CHAR16 *Dest,
  133. IN CONST CHAR16 *Src,
  134. IN UINTN Len
  135. )
  136. {
  137. RtStrnCat(Dest, Src, Len);
  138. }
  139. UINTN
  140. StrnLen (
  141. IN CONST CHAR16 *s1,
  142. IN UINTN Len
  143. )
  144. // string length
  145. {
  146. return RtStrnLen(s1, Len);
  147. }
  148. UINTN
  149. StrLen (
  150. IN CONST CHAR16 *s1
  151. )
  152. // string length
  153. {
  154. return RtStrLen(s1);
  155. }
  156. UINTN
  157. StrSize (
  158. IN CONST CHAR16 *s1
  159. )
  160. // string size
  161. {
  162. return RtStrSize(s1);
  163. }
  164. CHAR16 *
  165. StrDuplicate (
  166. IN CONST CHAR16 *Src
  167. )
  168. // duplicate a string
  169. {
  170. CHAR16 *Dest;
  171. UINTN Size;
  172. Size = StrSize(Src);
  173. Dest = AllocatePool (Size);
  174. if (Dest) {
  175. CopyMem (Dest, (void *)Src, Size);
  176. }
  177. return Dest;
  178. }
  179. UINTN
  180. strlena (
  181. IN CONST CHAR8 *s1
  182. )
  183. // string length
  184. {
  185. UINTN len;
  186. for (len=0; *s1; s1+=1, len+=1) ;
  187. return len;
  188. }
  189. UINTN
  190. strcmpa (
  191. IN CONST CHAR8 *s1,
  192. IN CONST CHAR8 *s2
  193. )
  194. // compare strings
  195. {
  196. while (*s1) {
  197. if (*s1 != *s2) {
  198. break;
  199. }
  200. s1 += 1;
  201. s2 += 1;
  202. }
  203. return *s1 - *s2;
  204. }
  205. UINTN
  206. strncmpa (
  207. IN CONST CHAR8 *s1,
  208. IN CONST CHAR8 *s2,
  209. IN UINTN len
  210. )
  211. // compare strings
  212. {
  213. while (*s1 && len) {
  214. if (*s1 != *s2) {
  215. break;
  216. }
  217. s1 += 1;
  218. s2 += 1;
  219. len -= 1;
  220. }
  221. return len ? *s1 - *s2 : 0;
  222. }
  223. UINTN
  224. xtoi (
  225. CONST CHAR16 *str
  226. )
  227. // convert hex string to uint
  228. {
  229. UINTN u;
  230. CHAR16 c;
  231. // skip preceeding white space
  232. while (*str == ' ') {
  233. str += 1;
  234. }
  235. // convert hex digits
  236. u = 0;
  237. while ((c = *(str++))) {
  238. if (c >= 'a' && c <= 'f') {
  239. c -= 'a' - 'A';
  240. }
  241. if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
  242. u = (u << 4) | ((UINTN)c - (c >= 'A' ? 'A'-10 : '0'));
  243. } else {
  244. break;
  245. }
  246. }
  247. return u;
  248. }
  249. UINTN
  250. Atoi (
  251. CONST CHAR16 *str
  252. )
  253. // convert hex string to uint
  254. {
  255. UINTN u;
  256. CHAR16 c;
  257. // skip preceeding white space
  258. while (*str == ' ') {
  259. str += 1;
  260. }
  261. // convert digits
  262. u = 0;
  263. while ((c = *(str++))) {
  264. if (c >= '0' && c <= '9') {
  265. u = (u * 10) + c - '0';
  266. } else {
  267. break;
  268. }
  269. }
  270. return u;
  271. }
  272. BOOLEAN
  273. MetaMatch (
  274. IN CHAR16 *String,
  275. IN CHAR16 *Pattern
  276. )
  277. {
  278. CHAR16 c, p, l;
  279. for (; ;) {
  280. p = *Pattern;
  281. Pattern += 1;
  282. switch (p) {
  283. case 0:
  284. // End of pattern. If end of string, TRUE match
  285. return *String ? FALSE : TRUE;
  286. case '*':
  287. // Match zero or more chars
  288. while (*String) {
  289. if (MetaMatch (String, Pattern)) {
  290. return TRUE;
  291. }
  292. String += 1;
  293. }
  294. return MetaMatch (String, Pattern);
  295. case '?':
  296. // Match any one char
  297. if (!*String) {
  298. return FALSE;
  299. }
  300. String += 1;
  301. break;
  302. case '[':
  303. // Match char set
  304. c = *String;
  305. if (!c) {
  306. return FALSE; // syntax problem
  307. }
  308. l = 0;
  309. while ((p = *Pattern++)) {
  310. if (p == ']') {
  311. return FALSE;
  312. }
  313. if (p == '-') { // if range of chars,
  314. p = *Pattern; // get high range
  315. if (p == 0 || p == ']') {
  316. return FALSE; // syntax problem
  317. }
  318. if (c >= l && c <= p) { // if in range,
  319. break; // it's a match
  320. }
  321. }
  322. l = p;
  323. if (c == p) { // if char matches
  324. break; // move on
  325. }
  326. }
  327. // skip to end of match char set
  328. while (p && p != ']') {
  329. p = *Pattern;
  330. Pattern += 1;
  331. }
  332. String += 1;
  333. break;
  334. default:
  335. c = *String;
  336. if (c != p) {
  337. return FALSE;
  338. }
  339. String += 1;
  340. break;
  341. }
  342. }
  343. }
  344. BOOLEAN EFIAPI
  345. LibStubMetaiMatch (
  346. IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
  347. IN CHAR16 *String,
  348. IN CHAR16 *Pattern
  349. )
  350. {
  351. return MetaMatch (String, Pattern);
  352. }
  353. BOOLEAN
  354. MetaiMatch (
  355. IN CHAR16 *String,
  356. IN CHAR16 *Pattern
  357. )
  358. {
  359. if (UnicodeInterface == &LibStubUnicodeInterface)
  360. return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
  361. else return uefi_call_wrapper(UnicodeInterface->MetaiMatch, 3, UnicodeInterface, String, Pattern);
  362. }