sstrings.inc 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. subroutines for string handling
  12. ****************************************************************************}
  13. {$ifndef FPC_HAS_SHORTSTR_SETLENGTH}
  14. {$define FPC_HAS_SHORTSTR_SETLENGTH}
  15. procedure fpc_Shortstr_SetLength(var s:shortstring;len:SizeInt);[Public,Alias : 'FPC_SHORTSTR_SETLENGTH']; compilerproc;
  16. begin
  17. if Len>255 then
  18. Len:=255;
  19. s[0]:=chr(len);
  20. end;
  21. {$endif FPC_HAS_SHORTSTR_SETLENGTH}
  22. {$ifndef FPC_HAS_SHORTSTR_COPY}
  23. {$define FPC_HAS_SHORTSTR_COPY}
  24. function fpc_shortstr_copy(const s : shortstring;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  25. begin
  26. if count<0 then
  27. count:=0;
  28. if index>1 then
  29. dec(index)
  30. else
  31. index:=0;
  32. if index>length(s) then
  33. count:=0
  34. else
  35. if count>length(s)-index then
  36. count:=length(s)-index;
  37. fpc_shortstr_Copy[0]:=chr(Count);
  38. fpc_shortstr_shortstr_intern_charmove(s,Index+1,fpc_shortstr_Copy,1,Count);
  39. end;
  40. {$endif FPC_HAS_SHORTSTR_COPY}
  41. {$ifndef FPC_HAS_SHORTSTR_DELETE}
  42. {$define FPC_HAS_SHORTSTR_DELETE}
  43. procedure {$ifdef VER3_0}delete{$else}fpc_shortstr_delete{$endif}(var s : shortstring;index : SizeInt;count : SizeInt);
  44. begin
  45. if index<=0 then
  46. exit;
  47. if (Index<=Length(s)) and (Count>0) then
  48. begin
  49. if Count>length(s)-Index then
  50. Count:=length(s)-Index+1;
  51. s[0]:=Chr(length(s)-Count);
  52. if Index<=Length(s) then
  53. fpc_shortstr_shortstr_intern_charmove(s,Index+Count,s,Index,Length(s)-Index+1);
  54. end;
  55. end;
  56. {$endif FPC_HAS_SHORTSTR_DELETE}
  57. {$ifndef FPC_HAS_SHORTSTR_INSERT}
  58. {$define FPC_HAS_SHORTSTR_INSERT}
  59. procedure {$ifdef ver3_0}insert{$else}fpc_shortstr_insert{$endif}(const source : shortstring;var s : shortstring;index : SizeInt);
  60. var
  61. cut,srclen,indexlen : SizeInt;
  62. begin
  63. if index<1 then
  64. index:=1;
  65. if index>length(s) then
  66. begin
  67. index:=length(s)+1;
  68. if index>high(s) then
  69. exit;
  70. end;
  71. indexlen:=Length(s)-Index+1;
  72. srclen:=length(Source);
  73. if sizeInt(length(source))+sizeint(length(s))>=sizeof(s) then
  74. begin
  75. cut:=sizeInt(length(source))+sizeint(length(s))-sizeof(s)+1;
  76. if cut>indexlen then
  77. begin
  78. dec(srclen,cut-indexlen);
  79. indexlen:=0;
  80. end
  81. else
  82. dec(indexlen,cut);
  83. end;
  84. fpc_shortstr_shortstr_intern_charmove(s,Index,s,Index+srclen,indexlen);
  85. fpc_shortstr_shortstr_intern_charmove(Source,1,s,Index,srclen);
  86. s[0]:=chr(index+srclen+indexlen-1);
  87. end;
  88. {$endif FPC_HAS_SHORTSTR_INSERT}
  89. {$ifndef FPC_HAS_SHORTSTR_INSERT_CHAR}
  90. {$define FPC_HAS_SHORTSTR_INSERT_CHAR}
  91. procedure {$ifdef ver3_0}insert{$else}fpc_shortstr_insert_char{$endif}(source : Char;var s : shortstring;index : SizeInt);
  92. var
  93. indexlen : SizeInt;
  94. begin
  95. if index<1 then
  96. index:=1;
  97. if index>length(s) then
  98. begin
  99. index:=length(s)+1;
  100. if index>high(s) then
  101. exit;
  102. end;
  103. indexlen:=Length(s)-Index+1;
  104. if (sizeint(length(s))+1=sizeof(s)) and (indexlen>0) then
  105. dec(indexlen);
  106. fpc_shortstr_shortstr_intern_charmove(s,Index,s,Index+1,indexlen);
  107. s[Index]:=Source;
  108. s[0]:=chr(index+indexlen);
  109. end;
  110. {$endif FPC_HAS_SHORTSTR_INSERT_CHAR}
  111. {$ifndef FPC_HAS_SHORTSTR_POS_SHORTSTR}
  112. {$define FPC_HAS_SHORTSTR_POS_SHORTSTR}
  113. function pos(const substr : shortstring;const s : shortstring; Offset : Sizeint = 1):SizeInt;
  114. var
  115. i,MaxLen : SizeInt;
  116. pc : pchar;
  117. begin
  118. Pos:=0;
  119. if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(S)) then
  120. begin
  121. MaxLen:=sizeint(Length(s))-Length(SubStr);
  122. i:=Offset-1;
  123. pc:=@s[Offset];
  124. while (i<=MaxLen) do
  125. begin
  126. inc(i);
  127. if (SubStr[1]=pc^) and
  128. (CompareChar(Substr[1],pc^,Length(SubStr))=0) then
  129. begin
  130. Pos:=i;
  131. exit;
  132. end;
  133. inc(pc);
  134. end;
  135. end;
  136. end;
  137. {$endif FPC_HAS_SHORTSTR_POS_SHORTSTR}
  138. {$ifndef FPC_HAS_SHORTSTR_POS_CHAR}
  139. {$define FPC_HAS_SHORTSTR_POS_CHAR}
  140. {Faster when looking for a single char...}
  141. function pos(c:char;const s:shortstring; Offset : Sizeint = 1 ):SizeInt;
  142. var
  143. i : SizeInt;
  144. pc : pchar;
  145. begin
  146. Pos:=0;
  147. if (Offset<1) or (Offset>Length(S)) then
  148. exit;
  149. pc:=@s[Offset];
  150. for i:=Offset to length(s) do
  151. begin
  152. if pc^=c then
  153. begin
  154. pos:=i;
  155. exit;
  156. end;
  157. inc(pc);
  158. end;
  159. pos:=0;
  160. end;
  161. {$endif FPC_HAS_SHORTSTR_POS_CHAR}
  162. function fpc_char_copy(c:char;index : SizeInt;count : SizeInt): shortstring;compilerproc;
  163. begin
  164. if (index=1) and (Count>0) then
  165. fpc_char_Copy:=c
  166. else
  167. fpc_char_Copy:='';
  168. end;
  169. function pos(const substr : shortstring;c:char; Offset : Sizeint = 1): SizeInt;
  170. begin
  171. if (length(substr)=1) and (substr[1]=c) and (Offset=1) then
  172. Pos:=1
  173. else
  174. Pos:=0;
  175. end;
  176. {$if not defined(FPC_UPCASE_CHAR) or not defined(FPC_LOWERCASE_CHAR)}
  177. {$ifdef IBM_CHAR_SET}
  178. const
  179. UpCaseTbl : shortstring[7]=#154#142#153#144#128#143#165;
  180. LoCaseTbl : shortstring[7]=#129#132#148#130#135#134#164;
  181. {$endif}
  182. {$endif}
  183. {$ifndef FPC_UPCASE_CHAR}
  184. {$define FPC_UPCASE_CHAR}
  185. function upcase(c : char) : char;
  186. {$IFDEF IBM_CHAR_SET}
  187. var
  188. i : ObjpasInt;
  189. {$ENDIF}
  190. begin
  191. if (c in ['a'..'z']) then
  192. upcase:=char(byte(c)-32)
  193. else
  194. {$IFDEF IBM_CHAR_SET}
  195. begin
  196. i:=Pos(c,LoCaseTbl);
  197. if i>0 then
  198. upcase:=UpCaseTbl[i]
  199. else
  200. upcase:=c;
  201. end;
  202. {$ELSE}
  203. upcase:=c;
  204. {$ENDIF}
  205. end;
  206. {$endif FPC_UPCASE_CHAR}
  207. {$ifndef FPC_UPCASE_SHORTSTR}
  208. {$define FPC_UPCASE_SHORTSTR}
  209. function upcase(const s : shortstring) : shortstring;
  210. var
  211. i : ObjpasInt;
  212. begin
  213. upcase[0]:=s[0];
  214. for i := 1 to length (s) do
  215. upcase[i] := upcase (s[i]);
  216. end;
  217. {$endif FPC_UPCASE_SHORTSTR}
  218. {$ifndef FPC_LOWERCASE_CHAR}
  219. {$define FPC_LOWERCASE_CHAR}
  220. function lowercase(c : char) : char;overload;
  221. {$IFDEF IBM_CHAR_SET}
  222. var
  223. i : ObjpasInt;
  224. {$ENDIF}
  225. begin
  226. if (c in ['A'..'Z']) then
  227. lowercase:=char(byte(c)+32)
  228. else
  229. {$IFDEF IBM_CHAR_SET}
  230. begin
  231. i:=Pos(c,UpCaseTbl);
  232. if i>0 then
  233. lowercase:=LoCaseTbl[i]
  234. else
  235. lowercase:=c;
  236. end;
  237. {$ELSE}
  238. lowercase:=c;
  239. {$ENDIF}
  240. end;
  241. {$endif FPC_LOWERCASE_CHAR}
  242. {$ifndef FPC_LOWERCASE_SHORTSTR}
  243. {$define FPC_LOWERCASE_SHORTSTR}
  244. function lowercase(const s : shortstring) : shortstring; overload;
  245. var
  246. i : ObjpasInt;
  247. begin
  248. lowercase [0]:=s[0];
  249. for i:=1 to length(s) do
  250. lowercase[i]:=lowercase (s[i]);
  251. end;
  252. {$endif FPC_LOWERCASE_SHORTSTR}
  253. const
  254. HexTbl : array[0..15] of char='0123456789ABCDEF';
  255. function hexstr(val : longint;cnt : byte) : shortstring;
  256. var
  257. i : ObjpasInt;
  258. begin
  259. hexstr[0]:=char(cnt);
  260. for i:=cnt downto 1 do
  261. begin
  262. hexstr[i]:=hextbl[val and $f];
  263. val:=val shr 4;
  264. end;
  265. end;
  266. function octstr(val : longint;cnt : byte) : shortstring;
  267. var
  268. i : ObjpasInt;
  269. begin
  270. octstr[0]:=char(cnt);
  271. for i:=cnt downto 1 do
  272. begin
  273. octstr[i]:=hextbl[val and 7];
  274. val:=val shr 3;
  275. end;
  276. end;
  277. function binstr(val : longint;cnt : byte) : shortstring;
  278. var
  279. i : ObjpasInt;
  280. begin
  281. binstr[0]:=char(cnt);
  282. for i:=cnt downto 1 do
  283. begin
  284. binstr[i]:=char(48+val and 1);
  285. val:=val shr 1;
  286. end;
  287. end;
  288. function hexstr(val : int64;cnt : byte) : shortstring;
  289. var
  290. i : ObjpasInt;
  291. begin
  292. hexstr[0]:=char(cnt);
  293. for i:=cnt downto 1 do
  294. begin
  295. hexstr[i]:=hextbl[val and $f];
  296. val:=val shr 4;
  297. end;
  298. end;
  299. function octstr(val : int64;cnt : byte) : shortstring;
  300. var
  301. i : ObjpasInt;
  302. begin
  303. octstr[0]:=char(cnt);
  304. for i:=cnt downto 1 do
  305. begin
  306. octstr[i]:=hextbl[val and 7];
  307. val:=val shr 3;
  308. end;
  309. end;
  310. function binstr(val : int64;cnt : byte) : shortstring;
  311. var
  312. i : ObjpasInt;
  313. begin
  314. binstr[0]:=char(cnt);
  315. for i:=cnt downto 1 do
  316. begin
  317. binstr[i]:=char(48+val and 1);
  318. val:=val shr 1;
  319. end;
  320. end;
  321. {$ifndef FPC_HAS_QWORD_HEX_SHORTSTR}
  322. {$define FPC_HAS_QWORD_HEX_SHORTSTR}
  323. Function hexStr(Val:qword;cnt:byte):shortstring;
  324. begin
  325. hexStr:=hexStr(int64(Val),cnt);
  326. end;
  327. {$endif FPC_HAS_QWORD_HEX_SHORTSTR}
  328. {$ifndef FPC_HAS_QWORD_OCT_SHORTSTR}
  329. {$define FPC_HAS_QWORD_OCT_SHORTSTR}
  330. Function OctStr(Val:qword;cnt:byte):shortstring;
  331. begin
  332. OctStr:=OctStr(int64(Val),cnt);
  333. end;
  334. {$endif FPC_HAS_QWORD_OCT_SHORTSTR}
  335. {$ifndef FPC_HAS_QWORD_BIN_SHORTSTR}
  336. {$define FPC_HAS_QWORD_BIN_SHORTSTR}
  337. Function binStr(Val:qword;cnt:byte):shortstring;
  338. begin
  339. binStr:=binStr(int64(Val),cnt);
  340. end;
  341. {$endif FPC_HAS_QWORD_BIN_SHORTSTR}
  342. {$ifndef FPC_HAS_HEXSTR_POINTER_SHORTSTR}
  343. {$define FPC_HAS_HEXSTR_POINTER_SHORTSTR}
  344. function hexstr(val : pointer) : shortstring;
  345. var
  346. i : ObjpasInt;
  347. v : ptruint;
  348. begin
  349. v:=ptruint(val);
  350. hexstr[0]:=chr(sizeof(pointer)*2);
  351. for i:=sizeof(pointer)*2 downto 1 do
  352. begin
  353. hexstr[i]:=hextbl[v and $f];
  354. v:=v shr 4;
  355. end;
  356. end;
  357. {$endif FPC_HAS_HEXSTR_POINTER_SHORTSTR}
  358. {$ifndef FPC_HAS_SPACE_SHORTSTR}
  359. {$define FPC_HAS_SPACE_SHORTSTR}
  360. function space (b : byte): shortstring;
  361. begin
  362. space[0] := chr(b);
  363. FillChar (Space[1],b,' ');
  364. end;
  365. {$endif FPC_HAS_SPACE_SHORTSTR}
  366. {*****************************************************************************
  367. Str() Helpers
  368. *****************************************************************************}
  369. procedure fpc_shortstr_SInt(v : valSInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_SINT']; compilerproc;
  370. begin
  371. int_str(v,s);
  372. if length(s)<len then
  373. s:=space(len-length(s))+s;
  374. end;
  375. procedure fpc_shortstr_UInt(v : valUInt;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_UINT']; compilerproc;
  376. begin
  377. int_str_unsigned(v,s);
  378. if length(s)<len then
  379. s:=space(len-length(s))+s;
  380. end;
  381. {$ifndef CPU64}
  382. procedure fpc_shortstr_qword(v : qword;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_QWORD']; compilerproc;
  383. begin
  384. int_str_unsigned(v,s);
  385. if length(s)<len then
  386. s:=space(len-length(s))+s;
  387. end;
  388. procedure fpc_shortstr_int64(v : int64;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_INT64']; compilerproc;
  389. begin
  390. int_str(v,s);
  391. if length(s)<len then
  392. s:=space(len-length(s))+s;
  393. end;
  394. {$endif CPU64}
  395. {$if defined(CPU16) or defined(CPU8)}
  396. procedure fpc_shortstr_longword(v : longword;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_LONGWORD']; compilerproc;
  397. begin
  398. int_str_unsigned(v,s);
  399. if length(s)<len then
  400. s:=space(len-length(s))+s;
  401. end;
  402. procedure fpc_shortstr_longint(v : longint;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_LONGINT']; compilerproc;
  403. begin
  404. int_str(v,s);
  405. if length(s)<len then
  406. s:=space(len-length(s))+s;
  407. end;
  408. procedure fpc_shortstr_word(v : word;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_WORD']; compilerproc;
  409. begin
  410. int_str_unsigned(v,s);
  411. if length(s)<len then
  412. s:=space(len-length(s))+s;
  413. end;
  414. procedure fpc_shortstr_smallint(v : smallint;len : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_SMALLINT']; compilerproc;
  415. begin
  416. int_str(v,s);
  417. if length(s)<len then
  418. s:=space(len-length(s))+s;
  419. end;
  420. {$endif CPU16 or CPU8}
  421. { fpc_shortstr_sInt must appear before this file is included, because }
  422. { it's used inside real2str.inc and otherwise the searching via the }
  423. { compilerproc name will fail (JM) }
  424. {$ifndef FPUNONE}
  425. {$I flt_conv.inc}
  426. {$endif}
  427. {$ifndef FPUNONE}
  428. procedure fpc_shortstr_float(d : ValReal;len,fr,rt : SizeInt;out s : shortstring);[public,alias:'FPC_SHORTSTR_FLOAT']; compilerproc;
  429. begin
  430. str_real(len,fr,d,treal_type(rt),s);
  431. end;
  432. {$endif}
  433. {$ifndef FPC_STR_ENUM_INTERN}
  434. function fpc_shortstr_enum_intern(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring): longint;
  435. {$ifndef FPC_HAS_FEATURE_RTTI}
  436. begin
  437. int_str(ordinal,s);
  438. if length(s)<len then
  439. s:=space(len-length(s))+s;
  440. end;
  441. {$else with RTTI feature}
  442. { The following contains the TTypeInfo/TTypeData records from typinfo.pp
  443. specialized for the tkEnumeration case (and stripped of unused things). }
  444. type
  445. PPstring=^Pstring;
  446. Penum_typeinfo=^Tenum_typeinfo;
  447. Tenum_typeinfo={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  448. kind:TTypeKind; { always tkEnumeration }
  449. num_chars:byte;
  450. chars:array[0..0] of char; { variable length with size of num_chars }
  451. end;
  452. {$push}
  453. {$packrecords c}
  454. Penum_typedata=^Tenum_typedata;
  455. Tenum_typedata={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  456. {$if declared(TRttiDataCommon)}
  457. Common: TRttiDataCommon;
  458. {$endif}
  459. case TTypeKind of
  460. {$ifndef VER3_0}
  461. tkInt64,tkQWord,
  462. {$endif VER3_0}
  463. tkInteger,tkChar,tkEnumeration,tkBool,tkWChar: (
  464. OrdType : Byte;
  465. case TTypeKind of
  466. tkInteger,tkChar,tkEnumeration,tkBool,tkWChar: (
  467. MinValue,MaxValue : Longint;
  468. case TTypeKind of
  469. tkEnumeration: (
  470. BaseTypeRef : pointer
  471. );
  472. {$ifndef VER3_0}
  473. {tkBool with OrdType=otSQWord }
  474. tkInt64:
  475. (MinInt64Value, MaxInt64Value: Int64);
  476. {tkBool with OrdType=otUQWord }
  477. tkQWord:
  478. (MinQWordValue, MaxQWordValue: QWord);
  479. {$endif VER3_0}
  480. );
  481. );
  482. { more data here, but not needed }
  483. end;
  484. { Pascal data types for the ordinal enum value to string table. It consists of a header
  485. that indicates what type of data the table stores, either a direct lookup table (when
  486. o = lookup) or a set of ordered (ordinal value, string) tuples (when o = search). }
  487. { A single entry in the set of ordered tuples }
  488. Psearch_data=^Tsearch_data;
  489. Tsearch_data={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  490. value:longint;
  491. name:Pstring;
  492. end;
  493. Penum_ord_to_string=^Tenum_ord_to_string;
  494. Tenum_ord_to_string={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  495. o:(lookup,search);
  496. case integer of
  497. 0: (lookup_data:array[0..0] of Pstring);
  498. 1: (num_entries:longint;
  499. search_data:array[0..0] of Tsearch_data);
  500. end;
  501. {$pop}
  502. var
  503. enum_o2s : Penum_ord_to_string;
  504. header:Penum_typeinfo;
  505. body:Penum_typedata;
  506. res:Pshortstring;
  507. sorted_data:Psearch_data;
  508. spaces,i,m,h,l:longint;
  509. begin
  510. { set default return value }
  511. fpc_shortstr_enum_intern:=107;
  512. enum_o2s:=Penum_ord_to_string(ord2strindex);
  513. { depending on the type of table in ord2strindex retrieve the data }
  514. if (enum_o2s^.o=lookup) then
  515. begin
  516. { direct lookup table }
  517. header:=Penum_typeinfo(typinfo);
  518. { calculate address of enum rtti body: add the actual size of the
  519. enum_rtti_header, and then align. Use an alignment of 1 (which
  520. does nothing) in case FPC_REQUIRES_PROPER_ALIGNMENT is not set
  521. to avoid the need for an if in this situation }
  522. {$ifdef VER3_0}
  523. body:=Penum_typedata(aligntoptr(pointer(header) + 2 * sizeof(byte) { kind, num_chars } + header^.num_chars));
  524. {$else VER3_0}
  525. body:=Penum_typedata(aligntoqword(pointer(header) + 2 * sizeof(byte) { kind, num_chars } + header^.num_chars));
  526. {$endif VER3_0}
  527. with body^ do
  528. begin
  529. { Bounds check for the ordinal value for this enum }
  530. if (ordinal<minvalue) or (ordinal>maxvalue) then
  531. exit;
  532. { make the ordinal index for lookup zero-based }
  533. dec(ordinal,minvalue);
  534. end;
  535. { temporarily disable range checking because of the access to the array[0..0]
  536. member of Tenum_ord_to_string_lookup }
  537. {$push}{$R-}
  538. res:=enum_o2s^.lookup_data[ordinal];
  539. {$pop}
  540. if (not assigned(res)) then
  541. exit;
  542. s:=res^;
  543. end
  544. else
  545. begin
  546. { The compiler did generate a sorted array of (ordvalue,Pstring) tuples }
  547. sorted_data:=@enum_o2s^.search_data;
  548. { Use a binary search to get the string }
  549. l:=0;
  550. { temporarily disable range checking because of the access to the array[0..0]
  551. member of Tenum_ord_to_string_search }
  552. {$push}{$R-}
  553. h:=enum_o2s^.num_entries-1;
  554. repeat
  555. m:=(l+h) div 2;
  556. if ordinal>sorted_data[m].value then
  557. l:=m+1
  558. else if ordinal<sorted_data[m].value then
  559. h:=m-1
  560. else
  561. break;
  562. if l>h then
  563. exit; { Ordinal value not found? Exit }
  564. until false;
  565. {$pop}
  566. s:=sorted_data[m].name^;
  567. end;
  568. { Pad the string with spaces if necessary }
  569. if (len>length(s)) then
  570. begin
  571. spaces:=len-length(s);
  572. for i:=1 to spaces do
  573. s[length(s)+i]:=' ';
  574. inc(byte(s[0]),spaces);
  575. end;
  576. fpc_shortstr_enum_intern:=0;
  577. end;
  578. {$endif with RTTI feature}
  579. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);[public,alias:'FPC_SHORTSTR_ENUM'];compilerproc;
  580. var
  581. res: longint;
  582. begin
  583. res:=fpc_shortstr_enum_intern(ordinal,len,typinfo,ord2strindex,s);
  584. if (res<>0) then
  585. runerror(107);
  586. end;
  587. { also define alias for internal use in the system unit }
  588. procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:shortstring);external name 'FPC_SHORTSTR_ENUM';
  589. {$endif FPC_SHORTSTR_ENUM_INTERN}
  590. procedure fpc_shortstr_bool(b : boolean;len:sizeint;out s:shortstring);[public,alias:'FPC_SHORTSTR_BOOL'];compilerproc;
  591. begin
  592. if b then
  593. s:='TRUE'
  594. else
  595. s:='FALSE';
  596. if length(s)<len then
  597. s:=space(len-length(s))+s;
  598. end;
  599. { also define alias for internal use in the system unit }
  600. procedure fpc_shortstr_bool(b : boolean;len:sizeint;out s:shortstring);external {$ifndef cpujvm}name 'FPC_SHORTSTR_BOOL'{$endif};
  601. procedure fpc_shortstr_currency({$ifdef cpujvm}constref{$endif} c : currency; len,f : SizeInt; out s : shortstring);[public,alias:'FPC_SHORTSTR_CURRENCY']; compilerproc;
  602. const
  603. MinLen = 8; { Minimal string length in scientific format }
  604. var
  605. buf : array[1..19] of char;
  606. i,j,k,reslen,tlen,sign,r,point : ObjpasInt;
  607. ic : qword;
  608. begin
  609. fillchar(buf,length(buf),'0');
  610. { default value for length is -32767 }
  611. if len=-32767 then
  612. len:=25;
  613. if PInt64(@c)^ >= 0 then
  614. begin
  615. ic:=QWord(PInt64(@c)^);
  616. sign:=0;
  617. end
  618. else
  619. begin
  620. sign:=1;
  621. ic:=QWord(-PInt64(@c)^);
  622. end;
  623. { converting to integer string }
  624. tlen:=0;
  625. repeat
  626. Inc(tlen);
  627. buf[tlen]:=Chr(ic mod 10 + $30);
  628. ic:=ic div 10;
  629. until ic = 0;
  630. { calculating:
  631. reslen - length of result string,
  632. r - rounding or appending zeroes,
  633. point - place of decimal point }
  634. reslen:=tlen;
  635. if f <> 0 then
  636. Inc(reslen); { adding decimal point length }
  637. if f < 0 then
  638. begin
  639. { scientific format }
  640. Inc(reslen,5); { adding length of sign and exponent }
  641. if len < MinLen then
  642. len:=MinLen;
  643. r:=reslen-len;
  644. if reslen < len then
  645. reslen:=len;
  646. if r > 0 then
  647. begin
  648. reslen:=len;
  649. point:=tlen - r;
  650. end
  651. else
  652. point:=tlen;
  653. end
  654. else
  655. begin
  656. { fixed format }
  657. Inc(reslen, sign);
  658. { prepending fractional part with zeroes }
  659. while tlen < 5 do
  660. begin
  661. Inc(reslen);
  662. Inc(tlen);
  663. buf[tlen]:='0';
  664. end;
  665. { Currency have 4 digits in fractional part }
  666. r:=4 - f;
  667. point:=f;
  668. if point <> 0 then
  669. begin
  670. if point > 4 then
  671. point:=4;
  672. Inc(point);
  673. end;
  674. Dec(reslen,r);
  675. end;
  676. { rounding string if r > 0 }
  677. if r > 0 then
  678. begin
  679. k := 0;
  680. i := r+2;
  681. if i > tlen then
  682. i := tlen+1;
  683. if buf[i-2] >= '5' then
  684. begin
  685. if buf[i-1] < '9' then
  686. buf[i-1] := chr(ord(buf[i-1])+1)
  687. else
  688. begin
  689. buf[i-1] := '0';
  690. k := 1;
  691. end;
  692. end;
  693. If (k=1) and (buf[i-1]='0') then
  694. begin
  695. { 1.9996 rounded to two decimal digits after the decimal separator must result in
  696. 2.00, i.e. the rounding is propagated
  697. }
  698. while buf[i]='9' do
  699. begin
  700. buf[i]:='0';
  701. inc(i);
  702. end;
  703. buf[i]:=chr(Ord(buf[i])+1);
  704. { did we add another digit? This happens when rounding
  705. e.g. 99.9996 to two decimal digits after the decimal separator which should result in
  706. 100.00
  707. }
  708. if i>tlen then
  709. begin
  710. inc(reslen);
  711. inc(tlen);
  712. end;
  713. end;
  714. end;
  715. { preparing result string }
  716. if reslen<len then
  717. reslen:=len;
  718. if reslen>High(s) then
  719. begin
  720. if r < 0 then
  721. Inc(r, reslen - High(s));
  722. reslen:=High(s);
  723. end;
  724. SetLength(s,reslen);
  725. j:=reslen;
  726. if f<0 then
  727. begin
  728. { writing power of 10 part }
  729. if PInt64(@c)^ = 0 then
  730. k:=0
  731. else
  732. k:=tlen-5;
  733. if k >= 0 then
  734. s[j-2]:='+'
  735. else
  736. begin
  737. s[j-2]:='-';
  738. k:=-k;
  739. end;
  740. s[j]:=Chr(k mod 10 + $30);
  741. Dec(j);
  742. s[j]:=Chr(k div 10 + $30);
  743. Dec(j,2);
  744. s[j]:='E';
  745. Dec(j);
  746. end;
  747. { writing extra zeroes if r < 0 }
  748. while r < 0 do
  749. begin
  750. s[j]:='0';
  751. Dec(j);
  752. Inc(r);
  753. end;
  754. { writing digits and decimal point }
  755. for i:=r + 1 to tlen do
  756. begin
  757. Dec(point);
  758. if point = 0 then
  759. begin
  760. s[j]:='.';
  761. Dec(j);
  762. end;
  763. s[j]:=buf[i];
  764. Dec(j);
  765. end;
  766. { writing sign }
  767. if sign = 1 then
  768. begin
  769. s[j]:='-';
  770. Dec(j);
  771. end;
  772. { writing spaces }
  773. while j > 0 do
  774. begin
  775. s[j]:=' ';
  776. Dec(j);
  777. end;
  778. end;
  779. {
  780. Array Of Char Str() helpers
  781. }
  782. procedure fpc_chararray_sint(v : valsint;len : SizeInt;out a:array of char);compilerproc;
  783. var
  784. ss : shortstring;
  785. maxlen : SizeInt;
  786. begin
  787. int_str(v,ss);
  788. if length(ss)<len then
  789. ss:=space(len-length(ss))+ss;
  790. if length(ss)<high(a)+1 then
  791. maxlen:=length(ss)
  792. else
  793. maxlen:=high(a)+1;
  794. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  795. end;
  796. procedure fpc_chararray_uint(v : valuint;len : SizeInt;out a : array of char);compilerproc;
  797. var
  798. ss : shortstring;
  799. maxlen : SizeInt;
  800. begin
  801. int_str_unsigned(v,ss);
  802. if length(ss)<len then
  803. ss:=space(len-length(ss))+ss;
  804. if length(ss)<high(a)+1 then
  805. maxlen:=length(ss)
  806. else
  807. maxlen:=high(a)+1;
  808. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  809. end;
  810. {$ifndef CPU64}
  811. procedure fpc_chararray_qword(v : qword;len : SizeInt;out a : array of char);compilerproc;
  812. {$ifdef EXCLUDE_COMPLEX_PROCS}
  813. begin
  814. runerror(219);
  815. end;
  816. {$else EXCLUDE_COMPLEX_PROCS}
  817. var
  818. ss : shortstring;
  819. maxlen : SizeInt;
  820. begin
  821. int_str_unsigned(v,ss);
  822. if length(ss)<len then
  823. ss:=space(len-length(ss))+ss;
  824. if length(ss)<high(a)+1 then
  825. maxlen:=length(ss)
  826. else
  827. maxlen:=high(a)+1;
  828. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  829. end;
  830. {$endif EXCLUDE_COMPLEX_PROCS}
  831. procedure fpc_chararray_int64(v : int64;len : SizeInt;out a : array of char);compilerproc;
  832. {$ifdef EXCLUDE_COMPLEX_PROCS}
  833. begin
  834. runerror(219);
  835. end;
  836. {$else EXCLUDE_COMPLEX_PROCS}
  837. var
  838. ss : shortstring;
  839. maxlen : SizeInt;
  840. begin
  841. int_str(v,ss);
  842. if length(ss)<len then
  843. ss:=space(len-length(ss))+ss;
  844. if length(ss)<high(a)+1 then
  845. maxlen:=length(ss)
  846. else
  847. maxlen:=high(a)+1;
  848. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  849. end;
  850. {$endif EXCLUDE_COMPLEX_PROCS}
  851. {$endif CPU64}
  852. {$if defined(CPU16) or defined(CPU8)}
  853. procedure fpc_chararray_longword(v : longword;len : SizeInt;out a : array of char);compilerproc;
  854. var
  855. ss : shortstring;
  856. maxlen : SizeInt;
  857. begin
  858. int_str_unsigned(v,ss);
  859. if length(ss)<len then
  860. ss:=space(len-length(ss))+ss;
  861. if length(ss)<high(a)+1 then
  862. maxlen:=length(ss)
  863. else
  864. maxlen:=high(a)+1;
  865. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  866. end;
  867. procedure fpc_chararray_longint(v : longint;len : SizeInt;out a : array of char);compilerproc;
  868. var
  869. ss : shortstring;
  870. maxlen : SizeInt;
  871. begin
  872. int_str(v,ss);
  873. if length(ss)<len then
  874. ss:=space(len-length(ss))+ss;
  875. if length(ss)<high(a)+1 then
  876. maxlen:=length(ss)
  877. else
  878. maxlen:=high(a)+1;
  879. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  880. end;
  881. procedure fpc_chararray_word(v : word;len : SizeInt;out a : array of char);compilerproc;
  882. var
  883. ss : shortstring;
  884. maxlen : SizeInt;
  885. begin
  886. int_str_unsigned(v,ss);
  887. if length(ss)<len then
  888. ss:=space(len-length(ss))+ss;
  889. if length(ss)<high(a)+1 then
  890. maxlen:=length(ss)
  891. else
  892. maxlen:=high(a)+1;
  893. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  894. end;
  895. procedure fpc_chararray_smallint(v : smallint;len : SizeInt;out a : array of char);compilerproc;
  896. var
  897. ss : shortstring;
  898. maxlen : SizeInt;
  899. begin
  900. int_str(v,ss);
  901. if length(ss)<len then
  902. ss:=space(len-length(ss))+ss;
  903. if length(ss)<high(a)+1 then
  904. maxlen:=length(ss)
  905. else
  906. maxlen:=high(a)+1;
  907. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  908. end;
  909. {$endif CPU16 or CPU8}
  910. {$ifndef FPUNONE}
  911. procedure fpc_chararray_Float(d : ValReal;len,fr,rt : SizeInt;out a : array of char);compilerproc;
  912. var
  913. ss : shortstring;
  914. maxlen : SizeInt;
  915. begin
  916. str_real(len,fr,d,treal_type(rt),ss);
  917. if length(ss)<high(a)+1 then
  918. maxlen:=length(ss)
  919. else
  920. maxlen:=high(a)+1;
  921. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  922. end;
  923. {$endif}
  924. {$ifndef FPC_STR_ENUM_INTERN}
  925. { currently, the avr code generator fails on this procedure, so we disable it,
  926. this is not a good solution but fixing compilation of this procedure for
  927. avr is hard, requires significant changes to the register allocator to take
  928. care of different register classes }
  929. procedure fpc_chararray_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out a : array of char);compilerproc;
  930. var
  931. ss : shortstring;
  932. maxlen : SizeInt;
  933. begin
  934. {$ifdef EXCLUDE_COMPLEX_PROCS}
  935. runerror(219);
  936. {$else EXCLUDE_COMPLEX_PROCS}
  937. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  938. if length(ss)<high(a)+1 then
  939. maxlen:=length(ss)
  940. else
  941. maxlen:=high(a)+1;
  942. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  943. {$endif EXCLUDE_COMPLEX_PROCS}
  944. end;
  945. {$endif not FPC_STR_ENUM_INTERN}
  946. procedure fpc_chararray_bool(b : boolean;len:sizeint;out a : array of char);compilerproc;
  947. var
  948. ss : shortstring;
  949. maxlen : SizeInt;
  950. begin
  951. fpc_shortstr_bool(b,len,ss);
  952. if length(ss)<high(a)+1 then
  953. maxlen:=length(ss)
  954. else
  955. maxlen:=high(a)+1;
  956. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  957. end;
  958. {$ifndef FPC_HAS_CHARARRAY_CURRENCY}
  959. {$define FPC_HAS_CHARARRAY_CURRENCY}
  960. procedure fpc_chararray_Currency(c : Currency;len,fr : SizeInt;out a : array of char);compilerproc;
  961. {$ifdef EXCLUDE_COMPLEX_PROCS}
  962. begin
  963. runerror(217);
  964. end;
  965. {$else EXCLUDE_COMPLEX_PROCS}
  966. var
  967. ss : shortstring;
  968. maxlen : SizeInt;
  969. begin
  970. str(c:len:fr,ss);
  971. if length(ss)<high(a)+1 then
  972. maxlen:=length(ss)
  973. else
  974. maxlen:=high(a)+1;
  975. fpc_shortstr_chararray_intern_charmove(ss,a,maxlen);
  976. end;
  977. {$endif EXCLUDE_COMPLEX_PROCS}
  978. {$endif FPC_HAS_CHARARRAY_CURRENCY}
  979. {*****************************************************************************
  980. Val() Functions
  981. *****************************************************************************}
  982. Function InitVal(const s:shortstring;out negativ:boolean;out base:byte):ValSInt;
  983. var
  984. Code : SizeInt;
  985. begin
  986. code:=1;
  987. negativ:=false;
  988. base:=10;
  989. if length(s)=0 then
  990. begin
  991. InitVal:=code;
  992. Exit;
  993. end;
  994. {Skip Spaces and Tab}
  995. while (code<=length(s)) and (s[code] in [' ',#9]) do
  996. inc(code);
  997. {Sign}
  998. case s[code] of
  999. '-' : begin
  1000. negativ:=true;
  1001. inc(code);
  1002. end;
  1003. '+' : inc(code);
  1004. end;
  1005. {Base}
  1006. if code<=length(s) then
  1007. begin
  1008. case s[code] of
  1009. '$',
  1010. 'X',
  1011. 'x' : begin
  1012. base:=16;
  1013. inc(code);
  1014. end;
  1015. '%' : begin
  1016. base:=2;
  1017. inc(code);
  1018. end;
  1019. '&' : begin
  1020. Base:=8;
  1021. inc(code);
  1022. end;
  1023. '0' : begin
  1024. if (code < length(s)) and (s[code+1] in ['x', 'X']) then
  1025. begin
  1026. inc(code, 2);
  1027. base := 16;
  1028. end;
  1029. end;
  1030. end;
  1031. end;
  1032. { strip leading zeros }
  1033. while ((code < length(s)) and (s[code] = '0')) do begin
  1034. inc(code);
  1035. end;
  1036. InitVal:=code;
  1037. end;
  1038. Function fpc_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_SHORTSTR']; compilerproc;
  1039. var
  1040. temp, prev, maxPrevValue: ValUInt;
  1041. base,u : byte;
  1042. negative: boolean;
  1043. UnsignedUpperLimit: ValUInt;
  1044. const
  1045. ValueArray : array['0'..'f'] of byte = (0,1,2,3,4,5,6,7,8,9,$FF,$FF,$FF,$FF,$FF,$FF,$FF,10,11,12,13,14,15,
  1046. $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
  1047. 10,11,12,13,14,15);
  1048. begin
  1049. fpc_Val_SInt_ShortStr := 0;
  1050. Temp:=0;
  1051. Code:=InitVal(s,negative,base);
  1052. if (base=10) or negative then
  1053. begin //always limit to either Low(DestType) or High(DestType)
  1054. case DestSize of
  1055. 1: UnsignedUpperLimit := ValUInt(High(ShortInt))+Ord(negative);
  1056. 2: UnsignedUpperLimit := ValUInt(High(SmallInt))+Ord(negative);
  1057. 4: UnsignedUpperLimit := ValUInt(High(LongInt))+Ord(negative);
  1058. {$ifdef CPU64}
  1059. 8: UnsignedUpperLimit := ValUInt(High(Int64))+Ord(negative);
  1060. {$endif CPU64}
  1061. end;
  1062. end
  1063. else
  1064. begin //not decimal and not negative
  1065. case DestSize of
  1066. 1: UnsignedUpperLimit := High(Byte);
  1067. 2: UnsignedUpperLimit := High(Word);
  1068. 4: UnsignedUpperLimit := High(DWord);
  1069. {$ifdef CPU64}
  1070. 8: UnsignedUpperLimit := High(UInt64);
  1071. {$endif CPU64}
  1072. end;
  1073. end;
  1074. if Code>length(s) then
  1075. exit;
  1076. if (s[Code]=#0) then
  1077. begin
  1078. if (Code>1) and (s[Code-1]='0') then
  1079. Code:=0;
  1080. exit;
  1081. end;
  1082. maxPrevValue := ValUInt(MaxUIntValue) div ValUInt(Base);
  1083. while Code<=Length(s) do
  1084. begin
  1085. u:=16;
  1086. case s[code] of
  1087. '0'..'f' : u:=ValueArray[S[Code]];
  1088. #0 : break;
  1089. else
  1090. ;
  1091. end;
  1092. Prev := Temp;
  1093. Temp := Temp*ValUInt(base);
  1094. If (u >= base) or
  1095. (prev > maxPrevValue)
  1096. or ((Temp)>(UnsignedUpperLimit-u)) Then
  1097. Begin
  1098. fpc_Val_SInt_ShortStr := 0;
  1099. Exit
  1100. End;
  1101. Temp:=Temp+u;
  1102. inc(code);
  1103. end;
  1104. code := 0;
  1105. fpc_Val_SInt_ShortStr := ValSInt(Temp);
  1106. If Negative Then
  1107. fpc_Val_SInt_ShortStr := -fpc_Val_SInt_ShortStr;
  1108. If Not(Negative) and (base <> 10) Then
  1109. {sign extend the result to allow proper range checking}
  1110. Case DestSize of
  1111. 1: fpc_Val_SInt_ShortStr := shortint(fpc_Val_SInt_ShortStr);
  1112. 2: fpc_Val_SInt_ShortStr := smallint(fpc_Val_SInt_ShortStr);
  1113. {$ifdef cpu64}
  1114. 4: fpc_Val_SInt_ShortStr := longint(fpc_Val_SInt_ShortStr);
  1115. {$endif cpu64}
  1116. End;
  1117. end;
  1118. {$ifndef FPC_HAS_INT_VAL_SINT_SHORTSTR}
  1119. {$define FPC_HAS_INT_VAL_SINT_SHORTSTR}
  1120. { we need this for fpc_Val_SInt_Ansistr and fpc_Val_SInt_WideStr because }
  1121. { we have to pass the DestSize parameter on (JM) }
  1122. Function int_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [external name 'FPC_VAL_SINT_SHORTSTR'];
  1123. {$endif FPC_HAS_INT_VAL_SINT_SHORTSTR}
  1124. Function fpc_Val_UInt_Shortstr(Const S: ShortString; out Code: ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_SHORTSTR']; compilerproc;
  1125. var
  1126. base,u : byte;
  1127. negative : boolean;
  1128. begin
  1129. fpc_Val_UInt_Shortstr:=0;
  1130. Code:=InitVal(s,negative,base);
  1131. If Negative or (Code>length(s)) Then
  1132. Exit;
  1133. if (s[Code]=#0) then
  1134. begin
  1135. if (Code>1) and (s[Code-1]='0') then
  1136. Code:=0;
  1137. exit;
  1138. end;
  1139. while Code<=Length(s) do
  1140. begin
  1141. case s[Code] of
  1142. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1143. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1144. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1145. #0 : break;
  1146. else
  1147. u:=16;
  1148. end;
  1149. If (u>=base) or
  1150. (ValUInt(MaxUIntValue-u) div ValUInt(Base)<fpc_val_uint_shortstr) then
  1151. begin
  1152. fpc_Val_UInt_Shortstr:=0;
  1153. exit;
  1154. end;
  1155. fpc_Val_UInt_Shortstr:=fpc_Val_UInt_Shortstr*ValUInt(base) + u;
  1156. inc(code);
  1157. end;
  1158. code := 0;
  1159. end;
  1160. {$ifndef CPU64}
  1161. Function fpc_val_int64_shortstr(Const S: ShortString; out Code: ValSInt): Int64; [public, alias:'FPC_VAL_INT64_SHORTSTR']; compilerproc;
  1162. var u : sizeuint;
  1163. temp, prev, maxprevvalue, maxnewvalue : qword;
  1164. base : byte;
  1165. negative : boolean;
  1166. const maxint64=qword($7fffffffffffffff);
  1167. minint64_unsigned=qword($8000000000000000);
  1168. maxqword=qword($ffffffffffffffff);
  1169. begin
  1170. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1171. runerror(219);
  1172. {$else EXCLUDE_COMPLEX_PROCS}
  1173. fpc_val_int64_shortstr := 0;
  1174. Temp:=0;
  1175. Code:=InitVal(s,negative,base);
  1176. if Code>length(s) then
  1177. exit;
  1178. if (s[Code]=#0) then
  1179. begin
  1180. if (Code>1) and (s[Code-1]='0') then
  1181. Code:=0;
  1182. exit;
  1183. end;
  1184. maxprevvalue := maxqword div base;
  1185. if (base = 10) then
  1186. maxnewvalue := maxint64 + ord(negative)
  1187. else
  1188. maxnewvalue := maxqword;
  1189. while Code<=Length(s) do
  1190. begin
  1191. case s[Code] of
  1192. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1193. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1194. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1195. #0 : break;
  1196. else
  1197. u:=16;
  1198. end;
  1199. Prev:=Temp;
  1200. Temp:=Temp*qword(base);
  1201. If (u >= base) or
  1202. (qword(maxnewvalue-u) < temp) or
  1203. (prev > maxprevvalue) or
  1204. ((base<>10) and (negative) and ((Temp+u)>minint64_unsigned)) Then
  1205. Begin
  1206. fpc_val_int64_shortstr := 0;
  1207. Exit
  1208. End;
  1209. Temp:=Temp+u;
  1210. inc(code);
  1211. end;
  1212. code:=0;
  1213. fpc_val_int64_shortstr:=int64(Temp);
  1214. If Negative Then
  1215. fpc_val_int64_shortstr:=-fpc_val_int64_shortstr;
  1216. {$endif EXCLUDE_COMPLEX_PROCS}
  1217. end;
  1218. Function fpc_val_qword_shortstr(Const S: ShortString; out Code: ValSInt): QWord; [public, alias:'FPC_VAL_QWORD_SHORTSTR']; compilerproc;
  1219. var u : sizeuint;
  1220. base : byte;
  1221. negative : boolean;
  1222. const maxqword=qword($ffffffffffffffff);
  1223. begin
  1224. fpc_val_qword_shortstr:=0;
  1225. Code:=InitVal(s,negative,base);
  1226. If Negative or (Code>length(s)) Then
  1227. Exit;
  1228. if (s[Code]=#0) then
  1229. begin
  1230. if (Code>1) and (s[Code-1]='0') then
  1231. Code:=0;
  1232. exit;
  1233. end;
  1234. while Code<=Length(s) do
  1235. begin
  1236. case s[Code] of
  1237. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1238. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1239. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1240. #0 : break;
  1241. else
  1242. u:=16;
  1243. end;
  1244. If (u>=base) or
  1245. ((QWord(maxqword-u) div QWord(base))<fpc_val_qword_shortstr) then
  1246. Begin
  1247. fpc_val_qword_shortstr := 0;
  1248. Exit
  1249. End;
  1250. fpc_val_qword_shortstr:=fpc_val_qword_shortstr*QWord(base) + u;
  1251. inc(code);
  1252. end;
  1253. code := 0;
  1254. end;
  1255. {$endif CPU64}
  1256. {$if defined(CPU16) or defined(CPU8)}
  1257. Function fpc_val_longint_shortstr(Const S: ShortString; out Code: ValSInt): LongInt; [public, alias:'FPC_VAL_LONGINT_SHORTSTR']; compilerproc;
  1258. var u, temp, prev, maxprevvalue, maxnewvalue : longword;
  1259. base : byte;
  1260. negative : boolean;
  1261. const maxlongint=longword($7fffffff);
  1262. maxlongword=longword($ffffffff);
  1263. begin
  1264. fpc_val_longint_shortstr := 0;
  1265. Temp:=0;
  1266. Code:=InitVal(s,negative,base);
  1267. if Code>length(s) then
  1268. exit;
  1269. if (s[Code]=#0) then
  1270. begin
  1271. if (Code>1) and (s[Code-1]='0') then
  1272. Code:=0;
  1273. exit;
  1274. end;
  1275. maxprevvalue := maxlongword div base;
  1276. if (base = 10) then
  1277. maxnewvalue := maxlongint + ord(negative)
  1278. else
  1279. maxnewvalue := maxlongword;
  1280. while Code<=Length(s) do
  1281. begin
  1282. case s[Code] of
  1283. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1284. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1285. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1286. #0 : break;
  1287. else
  1288. u:=16;
  1289. end;
  1290. Prev:=Temp;
  1291. Temp:=Temp*longword(base);
  1292. If (u >= base) or
  1293. (longword(maxnewvalue-u) < temp) or
  1294. (prev > maxprevvalue) Then
  1295. Begin
  1296. fpc_val_longint_shortstr := 0;
  1297. Exit
  1298. End;
  1299. Temp:=Temp+u;
  1300. inc(code);
  1301. end;
  1302. code:=0;
  1303. fpc_val_longint_shortstr:=longint(Temp);
  1304. If Negative Then
  1305. fpc_val_longint_shortstr:=-fpc_val_longint_shortstr;
  1306. end;
  1307. Function fpc_val_longword_shortstr(Const S: ShortString; out Code: ValSInt): LongWord; [public, alias:'FPC_VAL_LONGWORD_SHORTSTR']; compilerproc;
  1308. var u, prev: LongWord;
  1309. base : byte;
  1310. negative : boolean;
  1311. const maxlongword=longword($ffffffff);
  1312. begin
  1313. fpc_val_longword_shortstr:=0;
  1314. Code:=InitVal(s,negative,base);
  1315. If Negative or (Code>length(s)) Then
  1316. Exit;
  1317. if (s[Code]=#0) then
  1318. begin
  1319. if (Code>1) and (s[Code-1]='0') then
  1320. Code:=0;
  1321. exit;
  1322. end;
  1323. while Code<=Length(s) do
  1324. begin
  1325. case s[Code] of
  1326. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1327. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1328. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1329. #0 : break;
  1330. else
  1331. u:=16;
  1332. end;
  1333. prev := fpc_val_longword_shortstr;
  1334. If (u>=base) or
  1335. ((LongWord(maxlongword-u) div LongWord(base))<prev) then
  1336. Begin
  1337. fpc_val_longword_shortstr := 0;
  1338. Exit
  1339. End;
  1340. fpc_val_longword_shortstr:=fpc_val_longword_shortstr*LongWord(base) + u;
  1341. inc(code);
  1342. end;
  1343. code := 0;
  1344. end;
  1345. Function fpc_val_smallint_shortstr(Const S: ShortString; out Code: ValSInt): SmallInt; [public, alias:'FPC_VAL_SMALLINT_SHORTSTR']; compilerproc;
  1346. var u, temp, prev, maxprevvalue, maxnewvalue : word;
  1347. base : byte;
  1348. negative : boolean;
  1349. const maxlongint=longword($7fffffff);
  1350. maxlongword=longword($ffffffff);
  1351. begin
  1352. fpc_val_smallint_shortstr := 0;
  1353. Temp:=0;
  1354. Code:=InitVal(s,negative,base);
  1355. if Code>length(s) then
  1356. exit;
  1357. if (s[Code]=#0) then
  1358. begin
  1359. if (Code>1) and (s[Code-1]='0') then
  1360. Code:=0;
  1361. exit;
  1362. end;
  1363. maxprevvalue := maxlongword div base;
  1364. if (base = 10) then
  1365. maxnewvalue := maxlongint + ord(negative)
  1366. else
  1367. maxnewvalue := maxlongword;
  1368. while Code<=Length(s) do
  1369. begin
  1370. case s[Code] of
  1371. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1372. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1373. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1374. #0 : break;
  1375. else
  1376. u:=16;
  1377. end;
  1378. Prev:=Temp;
  1379. Temp:=Temp*longword(base);
  1380. If (u >= base) or
  1381. (longword(maxnewvalue-u) < temp) or
  1382. (prev > maxprevvalue) Then
  1383. Begin
  1384. fpc_val_smallint_shortstr := 0;
  1385. Exit
  1386. End;
  1387. Temp:=Temp+u;
  1388. inc(code);
  1389. end;
  1390. code:=0;
  1391. fpc_val_smallint_shortstr:=longint(Temp);
  1392. If Negative Then
  1393. fpc_val_smallint_shortstr:=-fpc_val_smallint_shortstr;
  1394. end;
  1395. Function fpc_val_word_shortstr(Const S: ShortString; out Code: ValSInt): Word; [public, alias:'FPC_VAL_WORD_SHORTSTR']; compilerproc;
  1396. var u, prev: word;
  1397. base : byte;
  1398. negative : boolean;
  1399. const maxlongword=longword($ffffffff);
  1400. begin
  1401. fpc_val_word_shortstr:=0;
  1402. Code:=InitVal(s,negative,base);
  1403. If Negative or (Code>length(s)) Then
  1404. Exit;
  1405. if (s[Code]=#0) then
  1406. begin
  1407. if (Code>1) and (s[Code-1]='0') then
  1408. Code:=0;
  1409. exit;
  1410. end;
  1411. while Code<=Length(s) do
  1412. begin
  1413. case s[Code] of
  1414. '0'..'9' : u:=Ord(S[Code])-Ord('0');
  1415. 'A'..'F' : u:=Ord(S[Code])-(Ord('A')-10);
  1416. 'a'..'f' : u:=Ord(S[Code])-(Ord('a')-10);
  1417. #0 : break;
  1418. else
  1419. u:=16;
  1420. end;
  1421. prev := fpc_val_word_shortstr;
  1422. If (u>=base) or
  1423. ((LongWord(maxlongword-u) div LongWord(base))<prev) then
  1424. Begin
  1425. fpc_val_word_shortstr := 0;
  1426. Exit
  1427. End;
  1428. fpc_val_word_shortstr:=fpc_val_word_shortstr*LongWord(base) + u;
  1429. inc(code);
  1430. end;
  1431. code := 0;
  1432. end;
  1433. {$endif CPU16 or CPU8}
  1434. {$ifndef FPUNONE}
  1435. Function fpc_Val_Real_ShortStr(const s : shortstring; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_SHORTSTR']; compilerproc;
  1436. begin
  1437. fpc_Val_Real_ShortStr := val_real( s, code );
  1438. end;
  1439. {$endif FPUNONE}
  1440. {$ifndef FPC_STR_ENUM_INTERN}
  1441. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint; [public, alias:'FPC_VAL_ENUM_SHORTSTR']; compilerproc;
  1442. function string_compare(const s1,s2:shortstring):sizeint;
  1443. {We cannot use the > and < operators to compare a string here, because we if the string is
  1444. not found in the enum, we need to return the position of error in "code". Code equals the
  1445. highest matching character of all string compares, which is only known inside the string
  1446. comparison.}
  1447. var i,l:byte;
  1448. c1,c2:char;
  1449. begin
  1450. l:=length(s1);
  1451. if length(s1)>length(s2) then
  1452. l:=length(s2);
  1453. i:=1;
  1454. while i<=l do
  1455. begin
  1456. c1:=s1[i];
  1457. c2:=s2[i];
  1458. if c1<>c2 then
  1459. break;
  1460. inc(i);
  1461. end;
  1462. if i>code then
  1463. code:=i;
  1464. if i<=l then
  1465. string_compare:=byte(c1)-byte(c2)
  1466. else
  1467. string_compare:=length(s1)-length(s2);
  1468. end;
  1469. type Psorted_array=^Tsorted_array;
  1470. Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  1471. o:longint;
  1472. s:Pstring;
  1473. end;
  1474. Pstring_to_ord=^Tstring_to_ord;
  1475. Tstring_to_ord={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  1476. count:longint;
  1477. data:array[0..0] of Tsorted_array;
  1478. end;
  1479. var l,h,m:cardinal;
  1480. c:sizeint;
  1481. sorted_array:^Tsorted_array;
  1482. spaces:byte;
  1483. t:shortstring;
  1484. begin
  1485. {Val for numbers accepts spaces at the start, so lets do the same
  1486. for enums. Skip spaces at the start of the string.}
  1487. spaces:=1;
  1488. code:=1;
  1489. while (spaces<=length(s)) and (s[spaces]=' ') do
  1490. inc(spaces);
  1491. t:=upcase(copy(s,spaces,255));
  1492. sorted_array:=pointer(@Pstring_to_ord(str2ordindex)^.data);
  1493. {Use a binary search to get the string.}
  1494. l:=1;
  1495. h:=Pstring_to_ord(str2ordindex)^.count;
  1496. repeat
  1497. m:=(l+h) div 2;
  1498. c:=string_compare(t,upcase(sorted_array[m-1].s^));
  1499. if c>0 then
  1500. l:=m+1
  1501. else if c<0 then
  1502. h:=m-1
  1503. else
  1504. break;
  1505. if l>h then
  1506. begin
  1507. {Not found...}
  1508. inc(code,spaces-1); {Add skipped spaces again.}
  1509. {The result of val in case of error is undefined, don't assign a function result.}
  1510. exit;
  1511. end;
  1512. until false;
  1513. code:=0;
  1514. fpc_val_enum_shortstr:=sorted_array[m-1].o;
  1515. end;
  1516. {Redeclare fpc_val_enum_shortstr for internal use in the system unit.}
  1517. function fpc_val_enum_shortstr(str2ordindex:pointer;const s:shortstring;out code:valsint):longint;external name 'FPC_VAL_ENUM_SHORTSTR';
  1518. {$endif FPC_STR_ENUM_INTERN}
  1519. function fpc_Val_Currency_ShortStr(const s : shortstring; out Code : ValSInt): currency; [public, alias:'FPC_VAL_CURRENCY_SHORTSTR']; compilerproc;
  1520. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1521. begin
  1522. runerror(217);
  1523. end;
  1524. {$else EXCLUDE_COMPLEX_PROCS}
  1525. const
  1526. MinInt64 : Int64 =-$8000000000000000;
  1527. MinInt64Edge : Int64 = (-$8000000000000000 + 10) div 10;
  1528. var
  1529. { to enable taking the address on the JVM target }
  1530. res : array[0..0] of Int64;
  1531. i,j,power,sign,len : longint;
  1532. FracOverflow : boolean;
  1533. begin
  1534. fpc_Val_Currency_ShortStr:=0;
  1535. res[0]:=0;
  1536. len:=Length(s);
  1537. Code:=1;
  1538. sign:=-1;
  1539. power:=0;
  1540. while True do
  1541. if Code > len then
  1542. exit
  1543. else
  1544. if s[Code] in [' ', #9] then
  1545. Inc(Code)
  1546. else
  1547. break;
  1548. { Read sign }
  1549. case s[Code] of
  1550. '+' : begin
  1551. Inc(Code);
  1552. end;
  1553. '-' : begin
  1554. sign:=+1;
  1555. Inc(Code);
  1556. end;
  1557. end;
  1558. { Read digits }
  1559. FracOverflow:=False;
  1560. i:=0;
  1561. while Code <= len do
  1562. begin
  1563. case s[Code] of
  1564. '0'..'9':
  1565. begin
  1566. j:=Ord(s[code])-Ord('0');
  1567. { check overflow }
  1568. if (res[0] >= MinInt64Edge) or (res[0] >= (MinInt64 + j) div 10) then
  1569. begin
  1570. res[0]:=res[0]*10 - j;
  1571. Inc(i);
  1572. end
  1573. else
  1574. if power = 0 then
  1575. { exit if integer part overflow }
  1576. exit
  1577. else
  1578. begin
  1579. if not FracOverflow and (j >= 5) and (res[0] > MinInt64) then
  1580. { round if first digit of fractional part overflow }
  1581. Dec(res[0]);
  1582. FracOverflow:=True;
  1583. end;
  1584. end;
  1585. '.':
  1586. begin
  1587. if power = 0 then
  1588. begin
  1589. power:=1;
  1590. i:=0;
  1591. end
  1592. else
  1593. exit;
  1594. end;
  1595. else
  1596. break;
  1597. end;
  1598. Inc(Code);
  1599. end;
  1600. if (i = 0) and (power = 0) then
  1601. exit;
  1602. if power <> 0 then
  1603. power:=i;
  1604. power:=4 - power;
  1605. { Exponent? }
  1606. if Code <= len then
  1607. if s[Code] in ['E', 'e'] then
  1608. begin
  1609. Inc(Code);
  1610. if Code > len then
  1611. exit;
  1612. i:=1;
  1613. case s[Code] of
  1614. '+':
  1615. Inc(Code);
  1616. '-':
  1617. begin
  1618. i:=-1;
  1619. Inc(Code);
  1620. end;
  1621. end;
  1622. { read exponent }
  1623. j:=0;
  1624. while Code <= len do
  1625. if s[Code] in ['0'..'9'] then
  1626. begin
  1627. if j > 4951 then
  1628. exit;
  1629. j:=j*10 + (Ord(s[code])-Ord('0'));
  1630. Inc(Code);
  1631. end
  1632. else
  1633. exit;
  1634. power:=power + j*i;
  1635. end
  1636. else
  1637. exit;
  1638. if power > 0 then
  1639. begin
  1640. for i:=1 to power do
  1641. if res[0] >= MinInt64 div 10 then
  1642. res[0]:=res[0]*10
  1643. else
  1644. exit;
  1645. end
  1646. else
  1647. for i:=1 to -power do
  1648. begin
  1649. if res[0] >= MinInt64 + 5 then
  1650. Dec(res[0], 5);
  1651. res[0]:=res[0] div 10;
  1652. end;
  1653. if sign <> 1 then
  1654. if res[0] > MinInt64 then
  1655. res[0]:=res[0]*sign
  1656. else
  1657. exit;
  1658. fpc_Val_Currency_ShortStr:=PCurrency(@res[0])^;
  1659. Code:=0;
  1660. end;
  1661. {$endif EXCLUDE_COMPLEX_PROCS}
  1662. {$ifndef FPC_HAS_SETSTRING_SHORTSTR}
  1663. {$define FPC_HAS_SETSTRING_SHORTSTR}
  1664. Procedure {$ifdef FPC_HAS_CPSTRING}fpc_setstring_shortstr{$else}SetString{$endif}(Out S : Shortstring; Buf : PChar; Len : SizeInt); {$ifdef FPC_HAS_CPSTRING} compilerproc; {$endif FPC_HAS_CPSTRING}
  1665. begin
  1666. If Len > High(S) then
  1667. Len := High(S);
  1668. SetLength(S,Len);
  1669. If Buf<>Nil then
  1670. begin
  1671. Move (Buf[0],S[1],Len);
  1672. end;
  1673. end;
  1674. {$endif FPC_HAS_SETSTRING_SHORTSTR}
  1675. {$ifndef FPC_HAS_COMPARETEXT_SHORTSTR}
  1676. {$define FPC_HAS_COMPARETEXT_SHORTSTR}
  1677. function ShortCompareText(const S1, S2: shortstring): SizeInt;
  1678. var
  1679. c1, c2: Byte;
  1680. i: SizeInt;
  1681. L1, L2, Count: SizeInt;
  1682. P1, P2: PChar;
  1683. begin
  1684. L1 := Length(S1);
  1685. L2 := Length(S2);
  1686. if L1 > L2 then
  1687. Count := L2
  1688. else
  1689. Count := L1;
  1690. i := 0;
  1691. P1 := @S1[1];
  1692. P2 := @S2[1];
  1693. while i < count do
  1694. begin
  1695. c1 := byte(p1^);
  1696. c2 := byte(p2^);
  1697. if c1 <> c2 then
  1698. begin
  1699. if c1 in [97..122] then
  1700. Dec(c1, 32);
  1701. if c2 in [97..122] then
  1702. Dec(c2, 32);
  1703. if c1 <> c2 then
  1704. Break;
  1705. end;
  1706. Inc(P1); Inc(P2); Inc(I);
  1707. end;
  1708. if i < count then
  1709. ShortCompareText := c1 - c2
  1710. else
  1711. ShortCompareText := L1 - L2;
  1712. end;
  1713. {$endif FPC_HAS_COMPARETEXT_SHORTSTR}