rtstr.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*++
  2. Copyright (c) 1998 Intel Corporation
  3. Module Name:
  4. str.c
  5. Abstract:
  6. String runtime functions
  7. Revision History
  8. --*/
  9. #include "lib.h"
  10. #ifndef __GNUC__
  11. #pragma RUNTIME_CODE(RtAcquireLock)
  12. #endif
  13. INTN
  14. RUNTIMEFUNCTION
  15. RtStrCmp (
  16. IN CONST CHAR16 *s1,
  17. IN CONST CHAR16 *s2
  18. )
  19. // compare strings
  20. {
  21. while (*s1) {
  22. if (*s1 != *s2) {
  23. break;
  24. }
  25. s1 += 1;
  26. s2 += 1;
  27. }
  28. return *s1 - *s2;
  29. }
  30. #ifndef __GNUC__
  31. #pragma RUNTIME_CODE(RtStrCpy)
  32. #endif
  33. VOID
  34. RUNTIMEFUNCTION
  35. RtStrCpy (
  36. IN CHAR16 *Dest,
  37. IN CONST CHAR16 *Src
  38. )
  39. // copy strings
  40. {
  41. while (*Src) {
  42. *(Dest++) = *(Src++);
  43. }
  44. *Dest = 0;
  45. }
  46. #ifndef __GNUC__
  47. #pragma RUNTIME_CODE(RtStrnCpy)
  48. #endif
  49. VOID
  50. RUNTIMEFUNCTION
  51. RtStrnCpy (
  52. IN CHAR16 *Dest,
  53. IN CONST CHAR16 *Src,
  54. IN UINTN Len
  55. )
  56. // copy strings
  57. {
  58. UINTN Size = RtStrnLen(Src, Len);
  59. if (Size != Len)
  60. RtSetMem(Dest + Len, '\0', (Len - Size) * sizeof(CHAR16));
  61. RtCopyMem(Dest, Src, Size * sizeof(CHAR16));
  62. }
  63. #ifndef __GNUC__
  64. #pragma RUNTIME_CODE(RtStrCpy)
  65. #endif
  66. CHAR16 *
  67. RUNTIMEFUNCTION
  68. RtStpCpy (
  69. IN CHAR16 *Dest,
  70. IN CONST CHAR16 *Src
  71. )
  72. // copy strings
  73. {
  74. while (*Src) {
  75. *(Dest++) = *(Src++);
  76. }
  77. *Dest = 0;
  78. return Dest;
  79. }
  80. #ifndef __GNUC__
  81. #pragma RUNTIME_CODE(RtStrnCpy)
  82. #endif
  83. CHAR16 *
  84. RUNTIMEFUNCTION
  85. RtStpnCpy (
  86. IN CHAR16 *Dest,
  87. IN CONST CHAR16 *Src,
  88. IN UINTN Len
  89. )
  90. // copy strings
  91. {
  92. UINTN Size = RtStrnLen(Src, Len);
  93. if (Size != Len)
  94. RtSetMem(Dest + Len, '\0', (Len - Size) * sizeof(CHAR16));
  95. RtCopyMem(Dest, Src, Size * sizeof(CHAR16));
  96. return Dest + Size;
  97. }
  98. #ifndef __GNUC__
  99. #pragma RUNTIME_CODE(RtStrCat)
  100. #endif
  101. VOID
  102. RUNTIMEFUNCTION
  103. RtStrCat (
  104. IN CHAR16 *Dest,
  105. IN CONST CHAR16 *Src
  106. )
  107. {
  108. RtStrCpy(Dest+StrLen(Dest), Src);
  109. }
  110. #ifndef __GNUC__
  111. #pragma RUNTIME_CODE(RtStrCat)
  112. #endif
  113. VOID
  114. RUNTIMEFUNCTION
  115. RtStrnCat (
  116. IN CHAR16 *Dest,
  117. IN CONST CHAR16 *Src,
  118. IN UINTN Len
  119. )
  120. {
  121. RtStrnCpy(Dest+StrLen(Dest), Src, Len);
  122. }
  123. #ifndef __GNUC__
  124. #pragma RUNTIME_CODE(RtStrLen)
  125. #endif
  126. UINTN
  127. RUNTIMEFUNCTION
  128. RtStrLen (
  129. IN CONST CHAR16 *s1
  130. )
  131. // string length
  132. {
  133. UINTN len;
  134. for (len=0; *s1; s1+=1, len+=1) ;
  135. return len;
  136. }
  137. #ifndef __GNUC__
  138. #pragma RUNTIME_CODE(RtStrnLen)
  139. #endif
  140. UINTN
  141. RUNTIMEFUNCTION
  142. RtStrnLen (
  143. IN CONST CHAR16 *s1,
  144. IN UINTN Len
  145. )
  146. // copy strings
  147. {
  148. UINTN i;
  149. for (i = 0; *s1 && i < Len; i++)
  150. s1++;
  151. return i;
  152. }
  153. #ifndef __GNUC__
  154. #pragma RUNTIME_CODE(RtStrSize)
  155. #endif
  156. UINTN
  157. RUNTIMEFUNCTION
  158. RtStrSize (
  159. IN CONST CHAR16 *s1
  160. )
  161. // string size
  162. {
  163. UINTN len;
  164. for (len=0; *s1; s1+=1, len+=1) ;
  165. return (len + 1) * sizeof(CHAR16);
  166. }
  167. #ifndef __GNUC__
  168. #pragma RUNTIME_CODE(RtBCDtoDecimal)
  169. #endif
  170. UINT8
  171. RUNTIMEFUNCTION
  172. RtBCDtoDecimal(
  173. IN UINT8 BcdValue
  174. )
  175. {
  176. UINTN High, Low;
  177. High = BcdValue >> 4;
  178. Low = BcdValue - (High << 4);
  179. return ((UINT8)(Low + (High * 10)));
  180. }
  181. #ifndef __GNUC__
  182. #pragma RUNTIME_CODE(RtDecimaltoBCD)
  183. #endif
  184. UINT8
  185. RUNTIMEFUNCTION
  186. RtDecimaltoBCD (
  187. IN UINT8 DecValue
  188. )
  189. {
  190. UINTN High, Low;
  191. High = DecValue / 10;
  192. Low = DecValue - (High * 10);
  193. return ((UINT8)(Low + (High << 4)));
  194. }