rtstr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(RtStrCmp)
  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 + Size, (Len - Size) * sizeof(CHAR16), '\0');
  61. RtCopyMem(Dest, Src, Size * sizeof(CHAR16));
  62. }
  63. #ifndef __GNUC__
  64. #pragma RUNTIME_CODE(RtStpCpy)
  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(RtStpnCpy)
  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 + Size, (Len - Size) * sizeof(CHAR16), '\0');
  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+RtStrLen(Dest), Src);
  109. }
  110. #ifndef __GNUC__
  111. #pragma RUNTIME_CODE(RtStrnCat)
  112. #endif
  113. VOID
  114. RUNTIMEFUNCTION
  115. RtStrnCat (
  116. IN CHAR16 *Dest,
  117. IN CONST CHAR16 *Src,
  118. IN UINTN Len
  119. )
  120. {
  121. UINTN DestSize, Size;
  122. DestSize = RtStrLen(Dest);
  123. Size = RtStrnLen(Src, Len);
  124. RtCopyMem(Dest + DestSize, Src, Size * sizeof(CHAR16));
  125. Dest[DestSize + Size] = '\0';
  126. }
  127. #ifndef __GNUC__
  128. #pragma RUNTIME_CODE(RtStrLen)
  129. #endif
  130. UINTN
  131. RUNTIMEFUNCTION
  132. RtStrLen (
  133. IN CONST CHAR16 *s1
  134. )
  135. // string length
  136. {
  137. UINTN len;
  138. for (len=0; *s1; s1+=1, len+=1) ;
  139. return len;
  140. }
  141. #ifndef __GNUC__
  142. #pragma RUNTIME_CODE(RtStrnLen)
  143. #endif
  144. UINTN
  145. RUNTIMEFUNCTION
  146. RtStrnLen (
  147. IN CONST CHAR16 *s1,
  148. IN UINTN Len
  149. )
  150. // string length
  151. {
  152. UINTN i;
  153. for (i = 0; *s1 && i < Len; i++)
  154. s1++;
  155. return i;
  156. }
  157. #ifndef __GNUC__
  158. #pragma RUNTIME_CODE(RtStrSize)
  159. #endif
  160. UINTN
  161. RUNTIMEFUNCTION
  162. RtStrSize (
  163. IN CONST CHAR16 *s1
  164. )
  165. // string size
  166. {
  167. UINTN len;
  168. for (len=0; *s1; s1+=1, len+=1) ;
  169. return (len + 1) * sizeof(CHAR16);
  170. }
  171. #ifndef __GNUC__
  172. #pragma RUNTIME_CODE(RtBCDtoDecimal)
  173. #endif
  174. UINT8
  175. RUNTIMEFUNCTION
  176. RtBCDtoDecimal(
  177. IN UINT8 BcdValue
  178. )
  179. {
  180. UINTN High, Low;
  181. High = BcdValue >> 4;
  182. Low = BcdValue - (High << 4);
  183. return ((UINT8)(Low + (High * 10)));
  184. }
  185. #ifndef __GNUC__
  186. #pragma RUNTIME_CODE(RtDecimaltoBCD)
  187. #endif
  188. UINT8
  189. RUNTIMEFUNCTION
  190. RtDecimaltoBCD (
  191. IN UINT8 DecValue
  192. )
  193. {
  194. UINTN High, Low;
  195. High = DecValue / 10;
  196. Low = DecValue - (High * 10);
  197. return ((UINT8)(Low + (High << 4)));
  198. }