str.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. StrCat (
  95. IN CHAR16 *Dest,
  96. IN CONST CHAR16 *Src
  97. )
  98. {
  99. RtStrCat(Dest, Src);
  100. }
  101. UINTN
  102. StrLen (
  103. IN CONST CHAR16 *s1
  104. )
  105. // string length
  106. {
  107. return RtStrLen(s1);
  108. }
  109. UINTN
  110. StrSize (
  111. IN CONST CHAR16 *s1
  112. )
  113. // string size
  114. {
  115. return RtStrSize(s1);
  116. }
  117. CHAR16 *
  118. StrDuplicate (
  119. IN CONST CHAR16 *Src
  120. )
  121. // duplicate a string
  122. {
  123. CHAR16 *Dest;
  124. UINTN Size;
  125. Size = StrSize(Src);
  126. Dest = AllocatePool (Size);
  127. if (Dest) {
  128. CopyMem (Dest, Src, Size);
  129. }
  130. return Dest;
  131. }
  132. UINTN
  133. strlena (
  134. IN CONST CHAR8 *s1
  135. )
  136. // string length
  137. {
  138. UINTN len;
  139. for (len=0; *s1; s1+=1, len+=1) ;
  140. return len;
  141. }
  142. UINTN
  143. strcmpa (
  144. IN CONST CHAR8 *s1,
  145. IN CONST CHAR8 *s2
  146. )
  147. // compare strings
  148. {
  149. while (*s1) {
  150. if (*s1 != *s2) {
  151. break;
  152. }
  153. s1 += 1;
  154. s2 += 1;
  155. }
  156. return *s1 - *s2;
  157. }
  158. UINTN
  159. strncmpa (
  160. IN CONST CHAR8 *s1,
  161. IN CONST CHAR8 *s2,
  162. IN UINTN len
  163. )
  164. // compare strings
  165. {
  166. while (*s1 && len) {
  167. if (*s1 != *s2) {
  168. break;
  169. }
  170. s1 += 1;
  171. s2 += 1;
  172. len -= 1;
  173. }
  174. return len ? *s1 - *s2 : 0;
  175. }
  176. UINTN
  177. xtoi (
  178. CONST CHAR16 *str
  179. )
  180. // convert hex string to uint
  181. {
  182. UINTN u;
  183. CHAR16 c;
  184. // skip preceeding white space
  185. while (*str && *str == ' ') {
  186. str += 1;
  187. }
  188. // convert hex digits
  189. u = 0;
  190. while ((c = *(str++))) {
  191. if (c >= 'a' && c <= 'f') {
  192. c -= 'a' - 'A';
  193. }
  194. if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
  195. u = (u << 4) | (c - (c >= 'A' ? 'A'-10 : '0'));
  196. } else {
  197. break;
  198. }
  199. }
  200. return u;
  201. }
  202. UINTN
  203. Atoi (
  204. CONST CHAR16 *str
  205. )
  206. // convert hex string to uint
  207. {
  208. UINTN u;
  209. CHAR16 c;
  210. // skip preceeding white space
  211. while (*str && *str == ' ') {
  212. str += 1;
  213. }
  214. // convert digits
  215. u = 0;
  216. while ((c = *(str++))) {
  217. if (c >= '0' && c <= '9') {
  218. u = (u * 10) + c - '0';
  219. } else {
  220. break;
  221. }
  222. }
  223. return u;
  224. }
  225. BOOLEAN
  226. MetaMatch (
  227. IN CHAR16 *String,
  228. IN CHAR16 *Pattern
  229. )
  230. {
  231. CHAR16 c, p, l;
  232. for (; ;) {
  233. p = *Pattern;
  234. Pattern += 1;
  235. switch (p) {
  236. case 0:
  237. // End of pattern. If end of string, TRUE match
  238. return *String ? FALSE : TRUE;
  239. case '*':
  240. // Match zero or more chars
  241. while (*String) {
  242. if (MetaMatch (String, Pattern)) {
  243. return TRUE;
  244. }
  245. String += 1;
  246. }
  247. return MetaMatch (String, Pattern);
  248. case '?':
  249. // Match any one char
  250. if (!*String) {
  251. return FALSE;
  252. }
  253. String += 1;
  254. break;
  255. case '[':
  256. // Match char set
  257. c = *String;
  258. if (!c) {
  259. return FALSE; // syntax problem
  260. }
  261. l = 0;
  262. while ((p = *Pattern++)) {
  263. if (p == ']') {
  264. return FALSE;
  265. }
  266. if (p == '-') { // if range of chars,
  267. p = *Pattern; // get high range
  268. if (p == 0 || p == ']') {
  269. return FALSE; // syntax problem
  270. }
  271. if (c >= l && c <= p) { // if in range,
  272. break; // it's a match
  273. }
  274. }
  275. l = p;
  276. if (c == p) { // if char matches
  277. break; // move on
  278. }
  279. }
  280. // skip to end of match char set
  281. while (p && p != ']') {
  282. p = *Pattern;
  283. Pattern += 1;
  284. }
  285. String += 1;
  286. break;
  287. default:
  288. c = *String;
  289. if (c != p) {
  290. return FALSE;
  291. }
  292. String += 1;
  293. break;
  294. }
  295. }
  296. }
  297. BOOLEAN EFIAPI
  298. LibStubMetaiMatch (
  299. IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
  300. IN CHAR16 *String,
  301. IN CHAR16 *Pattern
  302. )
  303. {
  304. return MetaMatch (String, Pattern);
  305. }
  306. BOOLEAN
  307. MetaiMatch (
  308. IN CHAR16 *String,
  309. IN CHAR16 *Pattern
  310. )
  311. {
  312. if (UnicodeInterface == &LibStubUnicodeInterface)
  313. return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
  314. else return uefi_call_wrapper(UnicodeInterface->MetaiMatch, 3, UnicodeInterface, String, Pattern);
  315. }