genstr.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Carl-Eric Codere,
  5. member of the Free Pascal development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$ifndef FPC_UNIT_HAS_STRLEN}
  13. function strlen(P : pchar) : StrLenInt;
  14. var
  15. counter : StrLenInt;
  16. Begin
  17. counter := 0;
  18. while P[counter] <> #0 do
  19. Inc(counter);
  20. strlen := counter;
  21. end;
  22. {$endif FPC_UNIT_HAS_STRLEN}
  23. {$ifndef FPC_UNIT_HAS_STREND}
  24. Function StrEnd(P: PChar): PChar;
  25. var
  26. counter: StrLenInt;
  27. begin
  28. counter := 0;
  29. while P[counter] <> #0 do
  30. Inc(counter);
  31. StrEnd := @(P[Counter]);
  32. end;
  33. {$endif FPC_UNIT_HAS_STREND}
  34. {$ifndef FPC_UNIT_HAS_STRCOPY}
  35. Function StrCopy(Dest, Source:PChar): PChar;
  36. var
  37. counter : StrLenInt;
  38. Begin
  39. counter := 0;
  40. while Source[counter] <> #0 do
  41. begin
  42. Dest[counter] := char(Source[counter]);
  43. Inc(counter);
  44. end;
  45. { terminate the string }
  46. Dest[counter] := #0;
  47. StrCopy := Dest;
  48. end;
  49. {$endif FPC_UNIT_HAS_STRCOPY}
  50. {$ifndef FPC_UNIT_HAS_STRUPPER}
  51. function StrUpper(P: PChar): PChar;
  52. var
  53. counter: StrLenInt;
  54. begin
  55. counter := 0;
  56. while (P[counter] <> #0) do
  57. begin
  58. if P[Counter] in [#97..#122,#128..#255] then
  59. P[counter] := Upcase(P[counter]);
  60. Inc(counter);
  61. end;
  62. StrUpper := P;
  63. end;
  64. {$endif FPC_UNIT_HAS_STRUPPER}
  65. {$ifndef FPC_UNIT_HAS_STRLOWER}
  66. function StrLower(P: PChar): PChar;
  67. var
  68. counter: StrLenInt;
  69. begin
  70. counter := 0;
  71. while (P[counter] <> #0) do
  72. begin
  73. if P[counter] in [#65..#90] then
  74. P[Counter] := chr(ord(P[Counter]) + 32);
  75. Inc(counter);
  76. end;
  77. StrLower := P;
  78. end;
  79. {$endif FPC_UNIT_HAS_STRLOWER}
  80. {$ifndef FPC_UNIT_HAS_STRSCAN}
  81. function StrScan(P: PChar; C: Char): PChar;
  82. Var
  83. count: StrLenInt;
  84. Begin
  85. count := 0;
  86. { As in Borland Pascal , if looking for NULL return null }
  87. if C = #0 then
  88. begin
  89. StrScan := @(P[StrLen(P)]);
  90. exit;
  91. end;
  92. { Find first matching character of Ch in Str }
  93. while P[count] <> #0 do
  94. begin
  95. if C = P[count] then
  96. begin
  97. StrScan := @(P[count]);
  98. exit;
  99. end;
  100. Inc(count);
  101. end;
  102. { nothing found. }
  103. StrScan := nil;
  104. end;
  105. {$endif FPC_UNIT_HAS_STRSCAN}
  106. {$ifndef FPC_UNIT_HAS_STRRSCAN}
  107. function StrRScan(P: PChar; C: Char): PChar;
  108. Var
  109. count: StrLenInt;
  110. index: StrLenInt;
  111. Begin
  112. count := Strlen(P);
  113. { As in Borland Pascal , if looking for NULL return null }
  114. if C = #0 then
  115. begin
  116. StrRScan := @(P[count]);
  117. exit;
  118. end;
  119. Dec(count);
  120. for index := count downto 0 do
  121. begin
  122. if C = P[index] then
  123. begin
  124. StrRScan := @(P[index]);
  125. exit;
  126. end;
  127. end;
  128. { nothing found. }
  129. StrRScan := nil;
  130. end;
  131. {$endif FPC_UNIT_HAS_STRRSCAN}
  132. {$ifndef FPC_UNIT_HAS_STRECOPY}
  133. Function StrECopy(Dest, Source: PChar): PChar;
  134. { Equivalent to the following: }
  135. { strcopy(Dest,Source); }
  136. { StrECopy := StrEnd(Dest); }
  137. var
  138. counter : StrLenInt;
  139. Begin
  140. counter := 0;
  141. while Source[counter] <> #0 do
  142. begin
  143. Dest[counter] := char(Source[counter]);
  144. Inc(counter);
  145. end;
  146. { terminate the string }
  147. Dest[counter] := #0;
  148. StrECopy:=@(Dest[counter]);
  149. end;
  150. {$endif FPC_UNIT_HAS_STRECOPY}
  151. {$ifndef FPC_UNIT_HAS_STRLCOPY}
  152. Function StrLCopy(Dest,Source: PChar; MaxLen: StrLenInt): PChar;
  153. var
  154. counter: StrLenInt;
  155. Begin
  156. counter := 0;
  157. { To be compatible with BP, on a null string, put two nulls }
  158. If Source[0] = #0 then
  159. Begin
  160. Dest[0]:=Source[0];
  161. Inc(counter);
  162. end;
  163. while (Source[counter] <> #0) and (counter < MaxLen) do
  164. Begin
  165. Dest[counter] := char(Source[counter]);
  166. Inc(counter);
  167. end;
  168. { terminate the string }
  169. Dest[counter] := #0;
  170. StrLCopy := Dest;
  171. end;
  172. {$endif FPC_UNIT_HAS_STRLCOPY}
  173. {$ifndef FPC_UNIT_HAS_STRCOMP}
  174. function StrComp(Str1, Str2 : PChar): StrLenInt;
  175. var
  176. counter: StrLenInt;
  177. Begin
  178. counter := 0;
  179. While str1[counter] = str2[counter] do
  180. Begin
  181. if (str2[counter] = #0) or (str1[counter] = #0) then
  182. break;
  183. Inc(counter);
  184. end;
  185. StrComp := ord(str1[counter]) - ord(str2[counter]);
  186. end;
  187. {$endif FPC_UNIT_HAS_STRCOMP}
  188. {$ifndef FPC_UNIT_HAS_STRICOMP}
  189. function StrIComp(Str1, Str2 : PChar): StrLenInt;
  190. var
  191. counter: StrLenInt;
  192. c1, c2: char;
  193. Begin
  194. counter := 0;
  195. c1 := upcase(str1[counter]);
  196. c2 := upcase(str2[counter]);
  197. While c1 = c2 do
  198. Begin
  199. if (c1 = #0) or (c2 = #0) then break;
  200. Inc(counter);
  201. c1 := upcase(str1[counter]);
  202. c2 := upcase(str2[counter]);
  203. end;
  204. StrIComp := ord(c1) - ord(c2);
  205. end;
  206. {$endif FPC_UNIT_HAS_STRICOMP}
  207. {$ifndef FPC_UNIT_HAS_STRLCOMP}
  208. function StrLComp(Str1, Str2 : PChar; L: StrLenInt): StrLenInt;
  209. var
  210. counter: StrLenInt;
  211. c1, c2: char;
  212. Begin
  213. counter := 0;
  214. if L = 0 then
  215. begin
  216. StrLComp := 0;
  217. exit;
  218. end;
  219. Repeat
  220. c1 := str1[counter];
  221. c2 := str2[counter];
  222. if (c1 = #0) or (c2 = #0) then break;
  223. Inc(counter);
  224. Until (c1 <> c2) or (counter >= L);
  225. StrLComp := ord(c1) - ord(c2);
  226. end;
  227. {$endif FPC_UNIT_HAS_STRLCOMP}
  228. {$ifndef FPC_UNIT_HAS_STRLICOMP}
  229. function StrLIComp(Str1, Str2 : PChar; L: StrLenInt): StrLenInt;
  230. var
  231. counter: StrLenInt;
  232. c1, c2: char;
  233. Begin
  234. counter := 0;
  235. if L = 0 then
  236. begin
  237. StrLIComp := 0;
  238. exit;
  239. end;
  240. Repeat
  241. c1 := upcase(str1[counter]);
  242. c2 := upcase(str2[counter]);
  243. if (c1 = #0) or (c2 = #0) then break;
  244. Inc(counter);
  245. Until (c1 <> c2) or (counter >= L);
  246. StrLIComp := ord(c1) - ord(c2);
  247. end;
  248. {$endif FPC_UNIT_HAS_STRLICOMP}
  249. {
  250. $Log$
  251. Revision 1.2 2003-07-07 20:22:05 peter
  252. * generic string routines added
  253. Revision 1.1 2003/04/30 16:36:39 florian
  254. + support for generic pchar routines added
  255. + some basic rtl stuff for x86-64 added
  256. }