genstr.inc 7.2 KB

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