sysstr.inc 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  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. System Utilities For Free Pascal
  11. }
  12. { NewStr creates a new PString and assigns S to it
  13. if length(s) = 0 NewStr returns Nil }
  14. function NewStr(const S: string): PString;
  15. begin
  16. if (S='') then
  17. Result:=nil
  18. else
  19. begin
  20. new(result);
  21. if (Result<>nil) then
  22. Result^:=s;
  23. end;
  24. end;
  25. {$ifdef dummy}
  26. { declaring this breaks delphi compatibility and e.g. tw3721.pp }
  27. FUNCTION NewStr (Const S: ShortString): PShortString;
  28. VAR P: PShortString;
  29. BEGIN
  30. If (S = '') Then
  31. P := Nil
  32. Else
  33. Begin { Return nil }
  34. GetMem(P, Length(S) + 1); { Allocate memory }
  35. If (P<>Nil) Then P^ := S; { Hold string }
  36. End;
  37. NewStr := P; { Return result }
  38. END;
  39. {$endif dummy}
  40. { DisposeStr frees the memory occupied by S }
  41. procedure DisposeStr(S: PString);
  42. begin
  43. if S <> Nil then
  44. begin
  45. dispose(s);
  46. S:=nil;
  47. end;
  48. end;
  49. PROCEDURE DisposeStr (S: PShortString);
  50. BEGIN
  51. If (S <> Nil) Then FreeMem(S, Length(S^) + 1); { Release memory }
  52. END;
  53. { AssignStr assigns S to P^ }
  54. procedure AssignStr(var P: PString; const S: string);
  55. begin
  56. P^ := s;
  57. end ;
  58. { AppendStr appends S to Dest }
  59. procedure AppendStr(var Dest: String; const S: string);
  60. begin
  61. Dest := Dest + S;
  62. end ;
  63. Function InternalChangeCase(Const S : AnsiString; const Chars: TSysCharSet; const Adjustment: Longint): AnsiString;
  64. var
  65. i : Integer;
  66. P : PChar;
  67. Unique : Boolean;
  68. begin
  69. Result := S;
  70. if Result='' then
  71. exit;
  72. Unique:=false;
  73. P:=PChar(Result);
  74. for i:=1 to Length(Result) do
  75. begin
  76. if CharInSet(P^,Chars) then
  77. begin
  78. if not Unique then
  79. begin
  80. UniqueString(Result);
  81. p:=@Result[i];
  82. Unique:=true;
  83. end;
  84. P^:=Char(Ord(P^)+Adjustment);
  85. end;
  86. Inc(P);
  87. end;
  88. end;
  89. { UpperCase returns a copy of S where all lowercase characters ( from a to z )
  90. have been converted to uppercase }
  91. Function UpperCase(Const S : AnsiString) : AnsiString;
  92. begin
  93. Result:=InternalChangeCase(S,['a'..'z'],-32);
  94. end;
  95. { LowerCase returns a copy of S where all uppercase characters ( from A to Z )
  96. have been converted to lowercase }
  97. Function Lowercase(Const S : AnsiString) : AnsiString;
  98. begin
  99. Result:=InternalChangeCase(S,['A'..'Z'],32);
  100. end;
  101. function LowerCase(const V: variant): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
  102. begin
  103. result:=LowerCase(ansistring(V));
  104. end;
  105. { CompareStr compares S1 and S2, the result is the based on
  106. substraction of the ascii values of the characters in S1 and S2
  107. case result
  108. S1 < S2 < 0
  109. S1 > S2 > 0
  110. S1 = S2 = 0 }
  111. {$IF SIZEOF(SIZEINT)>SIZEOF(INTEGER)}
  112. Function DoCapSizeInt(SI : SizeInt) : Integer; inline;
  113. begin
  114. if (SI<0) then
  115. result:=-1
  116. else if (SI>0) then
  117. result:=1
  118. else
  119. result:=0;
  120. end;
  121. {$DEFINE CAPSIZEINT:=DoCapSizeInt}
  122. {$ELSE}
  123. {$DEFINE CAPSIZEINT:=}
  124. {$ENDIF}
  125. function CompareStr(const S1, S2: string): Integer;
  126. var res,count, count1, count2: SizeInt;
  127. begin
  128. result := 0;
  129. Count1 := Length(S1);
  130. Count2 := Length(S2);
  131. if Count1>Count2 then
  132. Count:=Count2
  133. else
  134. Count:=Count1;
  135. result := CompareMemRange(Pointer(S1),Pointer(S2), Count);
  136. if result=0 then
  137. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  138. result:=CAPSIZEINT(Count1-Count2);
  139. end;
  140. { CompareMemRange returns the result of comparison of Length bytes at P1 and P2
  141. case result
  142. P1 < P2 < 0
  143. P1 > P2 > 0
  144. P1 = P2 = 0 }
  145. function CompareMemRange(P1, P2: Pointer; Length: PtrUInt): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  146. begin
  147. Result:=CompareByte(P1^,P2^,Length);
  148. end;
  149. function CompareMem(P1, P2: Pointer; Length: PtrUInt): Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  150. begin
  151. Result:=CompareByte(P1^,P2^,Length)=0;
  152. end;
  153. { CompareText compares S1 and S2, the result is the based on
  154. substraction of the ascii values of characters in S1 and S2
  155. comparison is case-insensitive
  156. case result
  157. S1 < S2 < 0
  158. S1 > S2 > 0
  159. S1 = S2 = 0 }
  160. function CompareText(const S1, S2: string): Integer;
  161. var
  162. i, count, count1, count2: sizeint;
  163. Chr1, Chr2: byte;
  164. P1, P2: PChar;
  165. begin
  166. Count1 := Length(S1);
  167. Count2 := Length(S2);
  168. if (Count1>Count2) then
  169. Count := Count2
  170. else
  171. Count := Count1;
  172. i := 0;
  173. if count>0 then
  174. begin
  175. P1 := @S1[1];
  176. P2 := @S2[1];
  177. while i < Count do
  178. begin
  179. Chr1 := byte(p1^);
  180. Chr2 := byte(p2^);
  181. if Chr1 <> Chr2 then
  182. begin
  183. if Chr1 in [97..122] then
  184. dec(Chr1,32);
  185. if Chr2 in [97..122] then
  186. dec(Chr2,32);
  187. if Chr1 <> Chr2 then
  188. Break;
  189. end;
  190. Inc(P1); Inc(P2); Inc(I);
  191. end;
  192. end;
  193. if i < Count then
  194. result := Chr1-Chr2
  195. else
  196. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  197. result:=CAPSIZEINT(Count1-Count2);
  198. end;
  199. function SameText(const s1,s2:String):Boolean;
  200. begin
  201. Result:=CompareText(S1,S2)=0;
  202. end;
  203. function SameStr(const s1,s2:String):Boolean;
  204. begin
  205. Result:=CompareStr(S1,S2)=0;
  206. end;
  207. {$ifndef FPC_NOGENERICANSIROUTINES}
  208. {==============================================================================}
  209. { Ansi string functions }
  210. { these functions rely on the character set loaded by the OS }
  211. {==============================================================================}
  212. type
  213. TCaseTranslationTable = array[0..255] of char;
  214. var
  215. { Tables with upper and lowercase forms of character sets.
  216. MUST be initialized with the correct code-pages }
  217. UpperCaseTable: TCaseTranslationTable;
  218. LowerCaseTable: TCaseTranslationTable;
  219. function GenericAnsiUpperCase(const s: string): string;
  220. var
  221. len, i: integer;
  222. begin
  223. len := length(s);
  224. SetLength(result, len);
  225. for i := 1 to len do
  226. result[i] := UpperCaseTable[ord(s[i])];
  227. end;
  228. function GenericAnsiLowerCase(const s: string): string;
  229. var
  230. len, i: integer;
  231. begin
  232. len := length(s);
  233. SetLength(result, len);
  234. for i := 1 to len do
  235. result[i] := LowerCaseTable[ord(s[i])];
  236. end;
  237. function GenericAnsiCompareStr(const S1, S2: string): PtrInt;
  238. Var
  239. I,L1,L2 : SizeInt;
  240. begin
  241. Result:=0;
  242. L1:=Length(S1);
  243. L2:=Length(S2);
  244. I:=1;
  245. While (Result=0) and ((I<=L1) and (I<=L2)) do
  246. begin
  247. Result:=Ord(S1[I])-Ord(S2[I]); //!! Must be replaced by ansi characters !!
  248. Inc(I);
  249. end;
  250. If Result=0 Then
  251. Result:=L1-L2;
  252. end;
  253. function GenericAnsiCompareText(const S1, S2: string): PtrInt;
  254. Var
  255. I,L1,L2 : SizeInt;
  256. begin
  257. Result:=0;
  258. L1:=Length(S1);
  259. L2:=Length(S2);
  260. I:=1;
  261. While (Result=0) and ((I<=L1) and (I<=L2)) do
  262. begin
  263. Result:=Ord(LowerCaseTable[Ord(S1[I])])-Ord(LowerCaseTable[Ord(S2[I])]); //!! Must be replaced by ansi characters !!
  264. Inc(I);
  265. end;
  266. If Result=0 Then
  267. Result:=L1-L2;
  268. end;
  269. function GenericAnsiStrComp(S1, S2: PChar): PtrInt;
  270. begin
  271. Result:=0;
  272. If S1=Nil then
  273. begin
  274. If S2=Nil Then Exit;
  275. result:=-1;
  276. exit;
  277. end;
  278. If S2=Nil then
  279. begin
  280. Result:=1;
  281. exit;
  282. end;
  283. While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
  284. Result:=Ord(S1^)-Ord(S2^); //!! Must be replaced by ansi characters !!
  285. Inc(S1);
  286. Inc(S2);
  287. end;
  288. if (Result=0) and (S1^<>S2^) then // loop ended because exactly one has #0
  289. if S1^=#0 then // shorter string is smaller
  290. result:=-1
  291. else
  292. result:=1;
  293. end;
  294. function GenericAnsiStrIComp(S1, S2: PChar): PtrInt;
  295. begin
  296. Result:=0;
  297. If S1=Nil then
  298. begin
  299. If S2=Nil Then Exit;
  300. result:=-1;
  301. exit;
  302. end;
  303. If S2=Nil then
  304. begin
  305. Result:=1;
  306. exit;
  307. end;
  308. While (Result=0) and (S1^<>#0) and (S2^<>#0) do begin
  309. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  310. Inc(S1);
  311. Inc(S2);
  312. end;
  313. if (Result=0) and (s1[0]<>s2[0]) then //length(s1)<>length(s2)
  314. if s1[0]=#0 then
  315. Result:=-1 //s1 shorter than s2
  316. else
  317. Result:=1; //s1 longer than s2
  318. end;
  319. function GenericAnsiStrLComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  320. Var I : PtrUInt;
  321. begin
  322. Result:=0;
  323. If MaxLen=0 then exit;
  324. If S1=Nil then
  325. begin
  326. If S2=Nil Then Exit;
  327. result:=-1;
  328. exit;
  329. end;
  330. If S2=Nil then
  331. begin
  332. Result:=1;
  333. exit;
  334. end;
  335. I:=0;
  336. Repeat
  337. Result:=Ord(S1[0])-Ord(S2[0]); //!! Must be replaced by ansi characters !!
  338. Inc(S1);
  339. Inc(S2);
  340. Inc(I);
  341. Until (Result<>0) or (I=MaxLen)
  342. end;
  343. function GenericAnsiStrLIComp(S1, S2: PChar; MaxLen: PtrUInt): PtrInt;
  344. Var I : PtrUInt;
  345. begin
  346. Result:=0;
  347. If MaxLen=0 then exit;
  348. If S1=Nil then
  349. begin
  350. If S2=Nil Then Exit;
  351. result:=-1;
  352. exit;
  353. end;
  354. If S2=Nil then
  355. begin
  356. Result:=1;
  357. exit;
  358. end;
  359. I:=0;
  360. Repeat
  361. Result:=Ord(LowerCaseTable[Ord(S1[0])])-Ord(LowerCaseTable[Ord(S2[0])]); //!! Must be replaced by ansi characters !!
  362. Inc(S1);
  363. Inc(S2);
  364. Inc(I);
  365. Until (Result<>0) or (I=MaxLen)
  366. end;
  367. function GenericAnsiStrLower(Str: PChar): PChar;
  368. begin
  369. result := Str;
  370. if Str <> Nil then begin
  371. while Str^ <> #0 do begin
  372. Str^ := LowerCaseTable[byte(Str^)];
  373. Str := Str + 1;
  374. end;
  375. end;
  376. end;
  377. function GenericAnsiStrUpper(Str: PChar): PChar;
  378. begin
  379. result := Str;
  380. if Str <> Nil then begin
  381. while Str^ <> #0 do begin
  382. Str^ := UpperCaseTable[byte(Str^)];
  383. Str := Str + 1;
  384. end ;
  385. end ;
  386. end ;
  387. {$endif FPC_NOGENERICANSIROUTINES}
  388. function AnsiSameText(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  389. begin
  390. AnsiSameText:=AnsiCompareText(S1,S2)=0;
  391. end;
  392. function AnsiSameStr(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  393. begin
  394. AnsiSameStr:=AnsiCompareStr(S1,S2)=0;
  395. end;
  396. function AnsiLastChar(const S: string): PChar;
  397. begin
  398. //!! No multibyte yet, so we return the last one.
  399. result:=StrEnd(Pchar(pointer(S))); // strend checks for nil
  400. Dec(Result);
  401. end ;
  402. function AnsiStrLastChar(Str: PChar): PChar;
  403. begin
  404. //!! No multibyte yet, so we return the last one.
  405. result:=StrEnd(Str);
  406. Dec(Result);
  407. end ;
  408. function AnsiUpperCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  409. begin
  410. result:=widestringmanager.UpperAnsiStringProc(s);
  411. end;
  412. function AnsiLowerCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  413. begin
  414. result:=widestringmanager.LowerAnsiStringProc(s);
  415. end;
  416. function AnsiCompareStr(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  417. begin
  418. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  419. result:=CAPSIZEINT(widestringmanager.CompareStrAnsiStringProc(s1,s2));
  420. end;
  421. function AnsiCompareText(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  422. begin
  423. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  424. result:=CAPSIZEINT(widestringmanager.CompareTextAnsiStringProc(s1,s2));
  425. end;
  426. function AnsiStrComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  427. begin
  428. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  429. result:=CAPSIZEINT(widestringmanager.StrCompAnsiStringProc(s1,s2));
  430. end;
  431. function AnsiStrIComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  432. begin
  433. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  434. result:=CAPSIZEINT(widestringmanager.StrICompAnsiStringProc(s1,s2));
  435. end;
  436. function AnsiStrLComp(S1, S2: PChar; MaxLen: SizeUInt): Integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  437. begin
  438. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  439. result:=CAPSIZEINT(widestringmanager.StrLCompAnsiStringProc(s1,s2,maxlen));
  440. end;
  441. function AnsiStrLIComp(S1, S2: PChar; MaxLen: SizeUint): Integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  442. begin
  443. // CAPSIZEINT is no-op if Sizeof(Sizeint)<=SizeOF(Integer)
  444. result:=CAPSIZEINT(widestringmanager.StrLICompAnsiStringProc(s1,s2,maxlen));
  445. end;
  446. function AnsiStrLower(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  447. begin
  448. result:=widestringmanager.StrLowerAnsiStringProc(Str);
  449. end;
  450. function AnsiStrUpper(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  451. begin
  452. result:=widestringmanager.StrUpperAnsiStringProc(Str);
  453. end;
  454. {==============================================================================}
  455. { End of Ansi functions }
  456. {==============================================================================}
  457. { Trim returns a copy of S with blanks characters on the left and right stripped off }
  458. Const WhiteSpace = [#0..' '];
  459. function Trim(const S: string): string;
  460. var Ofs, Len: integer;
  461. begin
  462. len := Length(S);
  463. while (Len>0) and (S[Len] in WhiteSpace) do
  464. dec(Len);
  465. Ofs := 1;
  466. while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
  467. Inc(Ofs);
  468. result := Copy(S, Ofs, 1 + Len - Ofs);
  469. end ;
  470. { TrimLeft returns a copy of S with all blank characters on the left stripped off }
  471. function TrimLeft(const S: string): string;
  472. var i,l:integer;
  473. begin
  474. l := length(s);
  475. i := 1;
  476. while (i<=l) and (s[i] in whitespace) do
  477. inc(i);
  478. Result := copy(s, i, l);
  479. end ;
  480. { TrimRight returns a copy of S with all blank characters on the right stripped off }
  481. function TrimRight(const S: string): string;
  482. var l:integer;
  483. begin
  484. l := length(s);
  485. while (l>0) and (s[l] in whitespace) do
  486. dec(l);
  487. result := copy(s,1,l);
  488. end ;
  489. { QuotedStr returns S quoted left and right and every single quote in S
  490. replaced by two quotes }
  491. function QuotedStr(const S: string): string;
  492. begin
  493. result := AnsiQuotedStr(s, '''');
  494. end ;
  495. { AnsiQuotedStr returns S quoted left and right by Quote,
  496. and every single occurance of Quote replaced by two }
  497. function AnsiQuotedStr(const S: string; Quote: char): string;
  498. var i, j, count: integer;
  499. begin
  500. result := '' + Quote;
  501. count := length(s);
  502. i := 0;
  503. j := 0;
  504. while i < count do begin
  505. i := i + 1;
  506. if S[i] = Quote then begin
  507. result := result + copy(S, 1 + j, i - j) + Quote;
  508. j := i;
  509. end ;
  510. end ;
  511. if i <> j then
  512. result := result + copy(S, 1 + j, i - j);
  513. result := result + Quote;
  514. end ;
  515. { AnsiExtractQuotedStr returns a copy of Src with quote characters
  516. deleted to the left and right and double occurances
  517. of Quote replaced by a single Quote }
  518. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  519. var
  520. P,Q,R: PChar;
  521. begin
  522. P := Src;
  523. Q := StrEnd(P);
  524. result:='';
  525. if P=Q then exit;
  526. if P^<>quote then exit(strpas(P));
  527. inc(p);
  528. setlength(result,(Q-P)+1);
  529. R:=@Result[1];
  530. while P <> Q do
  531. begin
  532. R^:=P^;
  533. inc(R);
  534. if (P^ = Quote) then
  535. begin
  536. P := P + 1;
  537. if (p^ <> Quote) then
  538. begin
  539. dec(R);
  540. break;
  541. end;
  542. end;
  543. P := P + 1;
  544. end ;
  545. src:=p;
  546. SetLength(result, (R-pchar(@Result[1])));
  547. end ;
  548. { AdjustLineBreaks returns S with all CR characters not followed by LF
  549. replaced with CR/LF }
  550. // under Linux all CR characters or CR/LF combinations should be replaced with LF
  551. function AdjustLineBreaks(const S: string): string;
  552. begin
  553. Result:=AdjustLineBreaks(S,DefaultTextLineBreakStyle);
  554. end;
  555. function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
  556. var
  557. Source,Dest: PChar;
  558. DestLen: Integer;
  559. I,J,L: Longint;
  560. begin
  561. Source:=Pointer(S);
  562. L:=Length(S);
  563. DestLen:=L;
  564. I:=1;
  565. while (I<=L) do
  566. begin
  567. case S[i] of
  568. #10: if (Style=tlbsCRLF) then
  569. Inc(DestLen);
  570. #13: if (Style=tlbsCRLF) then
  571. if (I<L) and (S[i+1]=#10) then
  572. Inc(I)
  573. else
  574. Inc(DestLen)
  575. else if (I<L) and (S[I+1]=#10) then
  576. Dec(DestLen);
  577. end;
  578. Inc(I);
  579. end;
  580. if (DestLen=L) then
  581. Result:=S
  582. else
  583. begin
  584. SetLength(Result, DestLen);
  585. FillChar(Result[1],DestLen,0);
  586. Dest := Pointer(Result);
  587. J:=0;
  588. I:=0;
  589. While I<L do
  590. case Source[I] of
  591. #10: begin
  592. if Style=tlbsCRLF then
  593. begin
  594. Dest[j]:=#13;
  595. Inc(J);
  596. end;
  597. Dest[J] := #10;
  598. Inc(J);
  599. Inc(I);
  600. end;
  601. #13: begin
  602. if Style=tlbsCRLF then
  603. begin
  604. Dest[j] := #13;
  605. Inc(J);
  606. end;
  607. Dest[j]:=#10;
  608. Inc(J);
  609. Inc(I);
  610. if Source[I]=#10 then
  611. Inc(I);
  612. end;
  613. else
  614. Dest[j]:=Source[i];
  615. Inc(J);
  616. Inc(I);
  617. end;
  618. end;
  619. end;
  620. { IsValidIdent returns true if the first character of Ident is in:
  621. 'A' to 'Z', 'a' to 'z' or '_' and the following characters are
  622. on of: 'A' to 'Z', 'a' to 'z', '0'..'9' or '_' }
  623. function IsValidIdent(const Ident: string; AllowDots: Boolean = False; StrictDots: Boolean = False): Boolean;
  624. const
  625. Alpha = ['A'..'Z', 'a'..'z', '_'];
  626. AlphaNum = Alpha + ['0'..'9'];
  627. Dot = '.';
  628. var
  629. First: Boolean;
  630. I, Len: Integer;
  631. begin
  632. Len := Length(Ident);
  633. if Len < 1 then
  634. Exit(False);
  635. First := True;
  636. for I := 1 to Len do
  637. begin
  638. if First then
  639. begin
  640. Result := Ident[I] in Alpha;
  641. First := False;
  642. end
  643. else if AllowDots and (Ident[I] = Dot) then
  644. begin
  645. if StrictDots then
  646. begin
  647. Result := I < Len;
  648. First := True;
  649. end;
  650. end
  651. else
  652. Result := Ident[I] in AlphaNum;
  653. if not Result then
  654. Break;
  655. end;
  656. end;
  657. { IntToStr returns a string representing the value of Value }
  658. function IntToStr(Value: Longint): string;
  659. begin
  660. System.Str(Value, result);
  661. end ;
  662. function IntToStr(Value: int64): string;
  663. begin
  664. System.Str(Value, result);
  665. end ;
  666. function IntToStr(Value: QWord): string;
  667. begin
  668. System.Str(Value, result);
  669. end ;
  670. { IntToHex returns a string representing the hexadecimal value of Value }
  671. const
  672. HexDigits: array[0..15] of char = '0123456789ABCDEF';
  673. function IntToHex(Value: Longint; Digits: integer): string;
  674. var i: integer;
  675. begin
  676. If Digits=0 then
  677. Digits:=1;
  678. SetLength(result, digits);
  679. for i := 0 to digits - 1 do
  680. begin
  681. result[digits - i] := HexDigits[value and 15];
  682. value := value shr 4;
  683. end ;
  684. while value <> 0 do begin
  685. result := HexDigits[value and 15] + result;
  686. value := value shr 4;
  687. end;
  688. end ;
  689. function IntToHex(Value: int64; Digits: integer): string;
  690. var i: integer;
  691. begin
  692. If Digits=0 then
  693. Digits:=1;
  694. SetLength(result, digits);
  695. for i := 0 to digits - 1 do
  696. begin
  697. result[digits - i] := HexDigits[value and 15];
  698. value := value shr 4;
  699. end ;
  700. while value <> 0 do begin
  701. result := HexDigits[value and 15] + result;
  702. value := value shr 4;
  703. end;
  704. end ;
  705. function IntToHex(Value: QWord; Digits: integer): string;
  706. begin
  707. result:=IntToHex(Int64(Value),Digits);
  708. end;
  709. function TryStrToInt(const s: string; out i : Longint) : boolean;
  710. var Error : word;
  711. begin
  712. Val(s, i, Error);
  713. TryStrToInt:=Error=0
  714. end;
  715. { StrToInt converts the string S to an integer value,
  716. if S does not represent a valid integer value EConvertError is raised }
  717. function StrToInt(const S: string): Longint;
  718. var Error: word;
  719. begin
  720. Val(S, result, Error);
  721. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  722. end ;
  723. function StrToInt64(const S: string): int64;
  724. var Error: word;
  725. begin
  726. Val(S, result, Error);
  727. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  728. end;
  729. function TryStrToInt64(const s: string; Out i : int64) : boolean;
  730. var Error : word;
  731. begin
  732. Val(s, i, Error);
  733. TryStrToInt64:=Error=0
  734. end;
  735. function StrToQWord(const s: string): QWord;
  736. var Error: word;
  737. begin
  738. Val(S, result, Error);
  739. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  740. end;
  741. function StrToDWord(const s: string): DWord;
  742. var Error: word;
  743. begin
  744. Val(S, result, Error);
  745. if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
  746. end;
  747. function TryStrToDWord(const s: string; Out D: DWord): boolean;
  748. var Error : word;
  749. begin
  750. Val(s, D, Error);
  751. TryStrToDWord:=Error=0
  752. end;
  753. function TryStrToQWord(const s: string; Out Q: QWord): boolean;
  754. var Error : word;
  755. begin
  756. Val(s, Q, Error);
  757. TryStrToQWord:=Error=0
  758. end;
  759. { StrToIntDef converts the string S to an integer value,
  760. Default is returned in case S does not represent a valid integer value }
  761. function StrToIntDef(const S: string; Default: Longint): Longint;
  762. var Error: word;
  763. begin
  764. Val(S, result, Error);
  765. if Error <> 0 then result := Default;
  766. end ;
  767. { StrToDWordDef converts the string S to an DWord value,
  768. Default is returned in case S does not represent a valid DWord value }
  769. function StrToDWordDef(const S: string; Default: DWord): DWord;
  770. var Error: word;
  771. begin
  772. Val(S, result, Error);
  773. if Error <> 0 then result := Default;
  774. end;
  775. { StrToInt64Def converts the string S to an int64 value,
  776. Default is returned in case S does not represent a valid int64 value }
  777. function StrToInt64Def(const S: string; Default: int64): int64;
  778. var Error: word;
  779. begin
  780. Val(S, result, Error);
  781. if Error <> 0 then result := Default;
  782. end ;
  783. { StrToQWordDef converts the string S to an QWord value,
  784. Default is returned in case S does not represent a valid QWord value }
  785. function StrToQWordDef(const S: string; Default: QWord): QWord;
  786. var Error: word;
  787. begin
  788. Val(S, result, Error);
  789. if Error <> 0 then result := Default;
  790. end;
  791. { LoadStr returns the string resource Ident. }
  792. function LoadStr(Ident: integer): string;
  793. begin
  794. result:='';
  795. end ;
  796. { FmtLoadStr returns the string resource Ident and formats it accordingly }
  797. function FmtLoadStr(Ident: integer; const Args: array of const): string;
  798. begin
  799. result:='';
  800. end;
  801. Const
  802. feInvalidFormat = 1;
  803. feMissingArgument = 2;
  804. feInvalidArgIndex = 3;
  805. {$ifdef fmtdebug}
  806. Procedure Log (Const S: String);
  807. begin
  808. Writeln (S);
  809. end;
  810. {$endif}
  811. Procedure DoFormatError (ErrCode : Longint;const fmt:ansistring);
  812. Var
  813. S : String;
  814. begin
  815. //!! must be changed to contain format string...
  816. S:=fmt;
  817. Case ErrCode of
  818. feInvalidFormat : raise EConvertError.Createfmt(SInvalidFormat,[s]);
  819. feMissingArgument : raise EConvertError.Createfmt(SArgumentMissing,[s]);
  820. feInvalidArgIndex : raise EConvertError.Createfmt(SInvalidArgIndex,[s]);
  821. end;
  822. end;
  823. { we've no templates, but with includes we can simulate this :) }
  824. {$macro on}
  825. {$define INFORMAT}
  826. {$define TFormatString:=ansistring}
  827. {$define TFormatChar:=char}
  828. Function Format (Const Fmt : AnsiString; const Args : Array of const; const FormatSettings: TFormatSettings) : AnsiString;
  829. {$i sysformt.inc}
  830. {$undef TFormatString}
  831. {$undef TFormatChar}
  832. {$undef INFORMAT}
  833. {$macro off}
  834. Function Format (Const Fmt : AnsiString; const Args : Array of const) : AnsiString;
  835. begin
  836. Result:=Format(Fmt,Args,DefaultFormatSettings);
  837. end;
  838. Function FormatBuf (Var Buffer; BufLen : Cardinal; Const Fmt; fmtLen : Cardinal; Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
  839. Var S,F : String;
  840. begin
  841. Setlength(F,fmtlen);
  842. if fmtlen > 0 then
  843. Move(fmt,F[1],fmtlen);
  844. S:=Format (F,Args,FormatSettings);
  845. If Cardinal(Length(S))<Buflen then
  846. Result:=Length(S)
  847. else
  848. Result:=Buflen;
  849. Move(S[1],Buffer,Result);
  850. end;
  851. Function FormatBuf (Var Buffer; BufLen : Cardinal;
  852. Const Fmt; fmtLen : Cardinal;
  853. Const Args : Array of const) : Cardinal;
  854. begin
  855. Result:=FormatBuf(Buffer,BufLen,Fmt,FmtLen,Args,DefaultFormatSettings);
  856. end;
  857. Procedure FmtStr(Var Res: string; const Fmt : string; Const args: Array of const; Const FormatSettings: TFormatSettings);
  858. begin
  859. Res:=Format(fmt,Args,FormatSettings);
  860. end;
  861. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  862. begin
  863. FmtStr(Res,Fmt,Args,DefaultFormatSettings);
  864. end;
  865. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  866. begin
  867. Result:=StrFmt(Buffer,Fmt,Args,DefaultFormatSettings);
  868. end;
  869. Function StrFmt(Buffer,Fmt : PChar; Const Args: Array of const; Const FormatSettings: TFormatSettings): PChar;
  870. begin
  871. Buffer[FormatBuf(Buffer^,Maxint,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
  872. Result:=Buffer;
  873. end;
  874. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  875. begin
  876. Result:=StrLFmt(Buffer,MaxLen,Fmt,Args,DefaultFormatSettings);
  877. end;
  878. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const; Const FormatSettings: TFormatSettings) : Pchar;
  879. begin
  880. Buffer[FormatBuf(Buffer^,MaxLen,Fmt^,strlen(fmt),args,FormatSettings)]:=#0;
  881. Result:=Buffer;
  882. end;
  883. {$ifndef FPUNONE}
  884. Function StrToFloat(Const S: String): Extended;
  885. begin
  886. Result:=StrToFloat(S,DefaultFormatSettings);
  887. end;
  888. Function StrToFloat(Const S : String; Const FormatSettings: TFormatSettings) : Extended;
  889. Begin // texttofloat handles NIL properly
  890. If Not TextToFloat(Pchar(pointer(S)),Result,FormatSettings) then
  891. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  892. End;
  893. function StrToFloatDef(const S: string; const Default: Extended): Extended;
  894. begin
  895. Result:=StrToFloatDef(S,Default,DefaultFormatSettings);
  896. end;
  897. Function StrToFloatDef(Const S: String; Const Default: Extended; Const FormatSettings: TFormatSettings): Extended;
  898. begin
  899. if not TextToFloat(PChar(pointer(S)),Result,fvExtended,FormatSettings) then
  900. Result:=Default;
  901. end;
  902. Function TextToFloat(Buffer: PChar; Out Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  903. Var
  904. E,P : Integer;
  905. S : String;
  906. Begin
  907. S:=StrPas(Buffer);
  908. //ThousandSeparator not allowed as by Delphi specs
  909. if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
  910. (Pos(FormatSettings.ThousandSeparator, S) <> 0) then
  911. begin
  912. Result := False;
  913. Exit;
  914. end;
  915. if (FormatSettings.DecimalSeparator <> '.') and
  916. (Pos('.', S) <>0) then
  917. begin
  918. Result := False;
  919. Exit;
  920. end;
  921. P:=Pos(FormatSettings.DecimalSeparator,S);
  922. If (P<>0) Then
  923. S[P] := '.';
  924. Val(trim(S),Value,E);
  925. Result:=(E=0);
  926. End;
  927. Function TextToFloat(Buffer: PChar; Out Value: Extended): Boolean;
  928. begin
  929. Result:=TextToFloat(Buffer,Value,DefaultFormatSettings);
  930. end;
  931. Function TextToFloat(Buffer: PChar; Out Value; ValueType: TFloatValue): Boolean;
  932. begin
  933. Result:=TextToFloat(Buffer,Value,ValueType,DefaultFormatSettings);
  934. end;
  935. Function TextToFloat(Buffer: PChar; Out Value; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): Boolean;
  936. Var
  937. E,P : Integer;
  938. S : String;
  939. Begin
  940. S:=StrPas(Buffer);
  941. //ThousandSeparator not allowed as by Delphi specs
  942. if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
  943. (Pos(FormatSettings.ThousandSeparator, S) <> 0) then
  944. begin
  945. Result := False;
  946. Exit;
  947. end;
  948. if (FormatSettings.DecimalSeparator <> '.') and
  949. (Pos('.', S) <>0) then
  950. begin
  951. Result := False;
  952. Exit;
  953. end;
  954. P:=Pos(FormatSettings.DecimalSeparator,S);
  955. If (P<>0) Then
  956. S[P] := '.';
  957. try
  958. case ValueType of
  959. fvCurrency:
  960. Val(S,Currency(Value),E);
  961. fvExtended:
  962. Val(S,Extended(Value),E);
  963. fvDouble:
  964. Val(S,Double(Value),E);
  965. fvSingle:
  966. Val(S,Single(Value),E);
  967. fvComp:
  968. Val(S,Comp(Value),E);
  969. fvReal:
  970. Val(S,Real(Value),E);
  971. end;
  972. { on x87, a floating point exception may be pending in case of an invalid
  973. input value -> trigger it now }
  974. {$ifdef cpux86}
  975. asm
  976. fwait
  977. end;
  978. {$endif}
  979. except
  980. E:=1;
  981. end;
  982. Result:=(E=0);
  983. End;
  984. Function TryStrToFloat(Const S : String; Out Value: Single): Boolean;
  985. begin
  986. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  987. end;
  988. Function TryStrToFloat(Const S : String; Out Value: Single; Const FormatSettings: TFormatSettings): Boolean;
  989. Begin
  990. Result := TextToFloat(PChar(pointer(S)), Value, fvSingle,FormatSettings);
  991. End;
  992. Function TryStrToFloat(Const S : String; Out Value: Double): Boolean;
  993. begin
  994. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  995. end;
  996. Function TryStrToFloat(Const S : String; Out Value: Double; Const FormatSettings: TFormatSettings): Boolean;
  997. Begin
  998. Result := TextToFloat(PChar(pointer(S)), Value, fvDouble,FormatSettings);
  999. End;
  1000. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1001. Function TryStrToFloat(Const S : String; Out Value: Extended): Boolean;
  1002. begin
  1003. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  1004. end;
  1005. Function TryStrToFloat(Const S : String; Out Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  1006. Begin
  1007. Result := TextToFloat(PChar(pointer(S)), Value,FormatSettings);
  1008. End;
  1009. {$endif FPC_HAS_TYPE_EXTENDED}
  1010. const
  1011. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1012. maxdigits = 17;
  1013. {$else}
  1014. maxdigits = 15;
  1015. {$endif}
  1016. { deactive aligned function for 2.6 }
  1017. {$ifdef VER2_6}
  1018. {$macro on}
  1019. {$define aligned:= }
  1020. {$endif VER2_6}
  1021. Function FloatToStrFIntl(const Value; format: TFloatFormat; Precision, Digits: Integer; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): String;
  1022. Var
  1023. P, PE, Q, Exponent: Integer;
  1024. Negative: Boolean;
  1025. DS: Char;
  1026. function RemoveLeadingNegativeSign(var AValue: String): Boolean;
  1027. // removes negative sign in case when result is zero eg. -0.00
  1028. var
  1029. i: PtrInt;
  1030. TS: Char;
  1031. StartPos: PtrInt;
  1032. begin
  1033. Result := False;
  1034. if Format = ffCurrency then
  1035. StartPos := 1
  1036. else
  1037. StartPos := 2;
  1038. TS := FormatSettings.ThousandSeparator;
  1039. for i := StartPos to length(AValue) do
  1040. begin
  1041. Result := (AValue[i] in ['0', DS, 'E', '+', TS]);
  1042. if not Result then
  1043. break;
  1044. end;
  1045. if (Result) and (Format <> ffCurrency) then
  1046. Delete(AValue, 1, 1);
  1047. end;
  1048. Begin
  1049. DS:=FormatSettings.DecimalSeparator;
  1050. Case format Of
  1051. ffGeneral:
  1052. Begin
  1053. case ValueType of
  1054. fvCurrency:
  1055. If (Precision = -1) Or (Precision > 19) Then Precision := 19;
  1056. else
  1057. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  1058. end;
  1059. { First convert to scientific format, with correct precision }
  1060. case ValueType of
  1061. fvDouble:
  1062. Str(Double(Extended(Aligned(Value))):precision+7, Result);
  1063. fvSingle:
  1064. Str(Single(Extended(Aligned(Value))):precision+6, Result);
  1065. fvCurrency:
  1066. Str(Currency(Aligned(Value)):precision+6, Result);
  1067. else
  1068. Str(Extended(Aligned(Value)):precision+8, Result);
  1069. end;
  1070. { Delete leading spaces }
  1071. while Result[1] = ' ' do
  1072. System.Delete(Result, 1, 1);
  1073. P := Pos('.', Result);
  1074. if P<>0 then
  1075. Result[P] := DS
  1076. else
  1077. Exit; { NAN or other special case }
  1078. { Consider removing exponent }
  1079. PE:=Pos('E',Result);
  1080. if PE > 0 then begin
  1081. { Read exponent }
  1082. Q := PE+2;
  1083. Exponent := 0;
  1084. while (Q <= Length(Result)) do begin
  1085. Exponent := Exponent*10 + Ord(Result[Q])-Ord('0');
  1086. Inc(Q);
  1087. end;
  1088. if Result[PE+1] = '-' then
  1089. Exponent := -Exponent;
  1090. if (P+Exponent < PE) and (Exponent > -6) then begin
  1091. { OK to remove exponent }
  1092. SetLength(Result,PE-1); { Trim exponent }
  1093. if Exponent >= 0 then begin
  1094. { Shift point to right }
  1095. for Q := 0 to Exponent-1 do begin
  1096. Result[P] := Result[P+1];
  1097. Inc(P);
  1098. end;
  1099. Result[P] := DS;
  1100. P := 1;
  1101. if Result[P] = '-' then
  1102. Inc(P);
  1103. while (Result[P] = '0') and (P < Length(Result)) and (Result[P+1] <> DS) do
  1104. { Trim leading zeros; conversion above should not give any, but occasionally does
  1105. because of rounding }
  1106. System.Delete(Result,P,1);
  1107. end else begin
  1108. { Add zeros at start }
  1109. Insert(Copy('00000',1,-Exponent),Result,P-1);
  1110. Result[P-Exponent] := Result[P-Exponent-1]; { Copy leading digit }
  1111. Result[P] := DS;
  1112. if Exponent <> -1 then
  1113. Result[P-Exponent-1] := '0';
  1114. end;
  1115. { Remove trailing zeros }
  1116. Q := Length(Result);
  1117. while (Q > 0) and (Result[Q] = '0') do
  1118. Dec(Q);
  1119. if Result[Q] = DS then
  1120. Dec(Q); { Remove trailing decimal point }
  1121. if (Q = 0) or ((Q=1) and (Result[1] = '-')) then
  1122. Result := '0'
  1123. else
  1124. SetLength(Result,Q);
  1125. end else begin
  1126. { Need exponent, but remove superfluous characters }
  1127. { Delete trailing zeros }
  1128. while Result[PE-1] = '0' do begin
  1129. System.Delete(Result,PE-1,1);
  1130. Dec(PE);
  1131. end;
  1132. { If number ends in decimal point, remove it }
  1133. if Result[PE-1] = DS then begin
  1134. System.Delete(Result,PE-1,1);
  1135. Dec(PE);
  1136. end;
  1137. { delete superfluous + in exponent }
  1138. if Result[PE+1]='+' then
  1139. System.Delete(Result,PE+1,1)
  1140. else
  1141. Inc(PE);
  1142. while Result[PE+1] = '0' do
  1143. { Delete leading zeros in exponent }
  1144. System.Delete(Result,PE+1,1)
  1145. end;
  1146. end;
  1147. End;
  1148. ffExponent:
  1149. Begin
  1150. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  1151. case ValueType of
  1152. fvDouble:
  1153. Str(Double(Extended(Aligned(Value))):Precision+7, Result);
  1154. fvSingle:
  1155. Str(Single(Extended(Aligned(Value))):Precision+6, Result);
  1156. fvCurrency:
  1157. Str(Currency(Aligned(Value)):Precision+6, Result);
  1158. else
  1159. Str(Extended(Aligned(Value)):Precision+8, Result);
  1160. end;
  1161. { Delete leading spaces }
  1162. while Result[1] = ' ' do
  1163. System.Delete(Result, 1, 1);
  1164. if (Result[1]='-') and
  1165. { not Nan etc.? }
  1166. (Result[3]='.') then
  1167. Result[3] := DS
  1168. else if Result[2]='.' then
  1169. Result[2] := DS;
  1170. P:=Pos('E',Result);
  1171. if P <> 0 then
  1172. begin
  1173. Inc(P, 2);
  1174. if Digits > 4 then
  1175. Digits:=4;
  1176. Digits:=Length(Result) - P - Digits + 1;
  1177. if Digits < 0 then
  1178. insert(copy('0000',1,-Digits),Result,P)
  1179. else
  1180. while (Digits > 0) and (Result[P] = '0') do
  1181. begin
  1182. System.Delete(Result, P, 1);
  1183. if P > Length(Result) then
  1184. begin
  1185. System.Delete(Result, P - 2, 2);
  1186. break;
  1187. end;
  1188. Dec(Digits);
  1189. end;
  1190. end;
  1191. End;
  1192. ffFixed:
  1193. Begin
  1194. If Digits = -1 Then Digits := 2
  1195. Else If Digits > 18 Then Digits := 18;
  1196. case ValueType of
  1197. fvDouble:
  1198. Str(Double(Extended(Aligned(Value))):0:Digits, Result);
  1199. fvSingle:
  1200. Str(Single(Extended(Aligned(Value))):0:Digits, Result);
  1201. fvCurrency:
  1202. Str(Currency(Aligned(Value)):0:Digits, Result);
  1203. else
  1204. Str(Extended(Aligned(Value)):0:Digits, Result);
  1205. end;
  1206. If Result[1] = ' ' Then
  1207. System.Delete(Result, 1, 1);
  1208. P := Pos('.', Result);
  1209. If P <> 0 Then Result[P] := DS;
  1210. End;
  1211. ffNumber:
  1212. Begin
  1213. If Digits = -1 Then Digits := 2
  1214. Else If Digits > maxdigits Then Digits := maxdigits;
  1215. case ValueType of
  1216. fvDouble:
  1217. Str(Double(Extended(Aligned(Value))):0:Digits, Result);
  1218. fvSingle:
  1219. Str(Single(Extended(Aligned(Value))):0:Digits, Result);
  1220. fvCurrency:
  1221. Str(Currency(Aligned(Value)):0:Digits, Result);
  1222. else
  1223. Str(Extended(Aligned(Value)):0:Digits, Result);
  1224. end;
  1225. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  1226. P := Pos('.', Result);
  1227. If P <> 0 Then
  1228. Result[P] := DS
  1229. else
  1230. P := Length(Result)+1;
  1231. Dec(P, 3);
  1232. While (P > 1) Do
  1233. Begin
  1234. If (Result[P - 1] <> '-') And (FormatSettings.ThousandSeparator <> #0) Then
  1235. Insert(FormatSettings.ThousandSeparator, Result, P);
  1236. Dec(P, 3);
  1237. End;
  1238. End;
  1239. ffCurrency:
  1240. Begin
  1241. If Digits = -1 Then Digits := FormatSettings.CurrencyDecimals
  1242. Else If Digits > 18 Then Digits := 18;
  1243. case ValueType of
  1244. fvDouble:
  1245. Str(Double(Extended(Aligned(Value))):0:Digits, Result);
  1246. fvSingle:
  1247. Str(Single(Extended(Aligned(Value))):0:Digits, Result);
  1248. fvCurrency:
  1249. Str(Currency(Aligned(Value)):0:Digits, Result);
  1250. else
  1251. Str(Extended(Aligned(Value)):0:Digits, Result);
  1252. end;
  1253. Negative:=Result[1] = '-';
  1254. if Negative then
  1255. System.Delete(Result, 1, 1);
  1256. P := Pos('.', Result);
  1257. If P <> 0 Then Result[P] := DS else P := Length(Result)+1;
  1258. Dec(P, 3);
  1259. While (P > 1) Do
  1260. Begin
  1261. If FormatSettings.ThousandSeparator<>#0 Then
  1262. Insert(FormatSettings.ThousandSeparator, Result, P);
  1263. Dec(P, 3);
  1264. End;
  1265. if (length(Result) > 1) and Negative then
  1266. Negative := not RemoveLeadingNegativeSign(Result);
  1267. If Not Negative Then
  1268. Begin
  1269. Case FormatSettings.CurrencyFormat Of
  1270. 0: Result := FormatSettings.CurrencyString + Result;
  1271. 1: Result := Result + FormatSettings.CurrencyString;
  1272. 2: Result := FormatSettings.CurrencyString + ' ' + Result;
  1273. 3: Result := Result + ' ' + FormatSettings.CurrencyString;
  1274. End
  1275. End
  1276. Else
  1277. Begin
  1278. Case FormatSettings.NegCurrFormat Of
  1279. 0: Result := '(' + FormatSettings.CurrencyString + Result + ')';
  1280. 1: Result := '-' + FormatSettings.CurrencyString + Result;
  1281. 2: Result := FormatSettings.CurrencyString + '-' + Result;
  1282. 3: Result := FormatSettings.CurrencyString + Result + '-';
  1283. 4: Result := '(' + Result + FormatSettings.CurrencyString + ')';
  1284. 5: Result := '-' + Result + FormatSettings.CurrencyString;
  1285. 6: Result := Result + '-' + FormatSettings.CurrencyString;
  1286. 7: Result := Result + FormatSettings.CurrencyString + '-';
  1287. 8: Result := '-' + Result + ' ' + FormatSettings.CurrencyString;
  1288. 9: Result := '-' + FormatSettings.CurrencyString + ' ' + Result;
  1289. 10: Result := Result + ' ' + FormatSettings.CurrencyString + '-';
  1290. 11: Result := FormatSettings.CurrencyString + ' ' + Result + '-';
  1291. 12: Result := FormatSettings.CurrencyString + ' ' + '-' + Result;
  1292. 13: Result := Result + '-' + ' ' + FormatSettings.CurrencyString;
  1293. 14: Result := '(' + FormatSettings.CurrencyString + ' ' + Result + ')';
  1294. 15: Result := '(' + Result + ' ' + FormatSettings.CurrencyString + ')';
  1295. End;
  1296. End;
  1297. End;
  1298. End;
  1299. if not (format in [ffCurrency]) and (length(Result) > 1) and (Result[1] = '-') then
  1300. RemoveLeadingNegativeSign(Result);
  1301. End;
  1302. {$macro off}
  1303. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1304. Function FloatToStr(Value: Extended; Const FormatSettings: TFormatSettings): String;
  1305. Begin
  1306. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvExtended,FormatSettings);
  1307. End;
  1308. Function FloatToStr(Value: Extended): String;
  1309. begin
  1310. Result:=FloatToStr(Value,DefaultFormatSettings);
  1311. end;
  1312. {$endif FPC_HAS_TYPE_EXTENDED}
  1313. Function FloatToStr(Value: Currency; Const FormatSettings: TFormatSettings): String;
  1314. Begin
  1315. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvCurrency,FormatSettings);
  1316. End;
  1317. Function FloatToStr(Value: Currency): String;
  1318. begin
  1319. Result:=FloatToStr(Value,DefaultFormatSettings);
  1320. end;
  1321. Function FloatToStr(Value: Double; Const FormatSettings: TFormatSettings): String;
  1322. var
  1323. e: Extended;
  1324. Begin
  1325. e := Value;
  1326. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvDouble,FormatSettings);
  1327. End;
  1328. Function FloatToStr(Value: Double): String;
  1329. begin
  1330. Result:=FloatToStr(Value,DefaultFormatSettings);
  1331. end;
  1332. Function FloatToStr(Value: Single; Const FormatSettings: TFormatSettings): String;
  1333. var
  1334. e: Extended;
  1335. Begin
  1336. e := Value;
  1337. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvSingle,FormatSettings);
  1338. End;
  1339. Function FloatToStr(Value: Single): String;
  1340. begin
  1341. Result:=FloatToStr(Value,DefaultFormatSettings);
  1342. end;
  1343. Function FloatToStr(Value: Comp; Const FormatSettings: TFormatSettings): String;
  1344. var
  1345. e: Extended;
  1346. Begin
  1347. e := Value;
  1348. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1349. End;
  1350. Function FloatToStr(Value: Comp): String;
  1351. begin
  1352. Result:=FloatToStr(Value,DefaultFormatSettings);
  1353. end;
  1354. {$ifndef FPC_COMP_IS_INT64}
  1355. Function FloatToStr(Value: Int64): String;
  1356. begin
  1357. Result:=FloatToStr(Value,DefaultFormatSettings);
  1358. end;
  1359. Function FloatToStr(Value: Int64; Const FormatSettings: TFormatSettings): String;
  1360. var
  1361. e: Extended;
  1362. Begin
  1363. e := Comp(Value);
  1364. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1365. End;
  1366. {$endif FPC_COMP_IS_INT64}
  1367. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): Longint;
  1368. Var
  1369. Tmp: String[40];
  1370. Begin
  1371. Tmp := FloatToStrF(Value, format, Precision, Digits,FormatSettings);
  1372. Result := Length(Tmp);
  1373. Move(Tmp[1], Buffer[0], Result);
  1374. End;
  1375. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  1376. begin
  1377. Result:=FloatToText(Buffer,Value,Format,Precision,Digits,DefaultFormatSettings);
  1378. end;
  1379. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1380. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1381. begin
  1382. Result := FloatToStrFIntl(value,format,precision,digits,fvExtended,FormatSettings);
  1383. end;
  1384. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  1385. begin
  1386. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1387. end;
  1388. {$endif}
  1389. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1390. begin
  1391. Result := FloatToStrFIntl(value,format,precision,digits,fvCurrency,FormatSettings);
  1392. end;
  1393. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer): String;
  1394. begin
  1395. Result:=FloatToStrF(Value,format,Precision,Digits,DefaultFormatSettings);
  1396. end;
  1397. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1398. var
  1399. e: Extended;
  1400. begin
  1401. e := Value;
  1402. result := FloatToStrFIntl(e,format,precision,digits,fvDouble,FormatSettings);
  1403. end;
  1404. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer): String;
  1405. begin
  1406. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1407. end;
  1408. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1409. var
  1410. e: Extended;
  1411. begin
  1412. e:=Value;
  1413. result := FloatToStrFIntl(e,format,precision,digits,fvSingle,FormatSettings);
  1414. end;
  1415. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer): String;
  1416. begin
  1417. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1418. end;
  1419. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1420. var
  1421. e: Extended;
  1422. begin
  1423. e := Value;
  1424. Result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1425. end;
  1426. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer): String;
  1427. begin
  1428. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1429. end;
  1430. {$ifndef FPC_COMP_IS_INT64}
  1431. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1432. var
  1433. e: Extended;
  1434. begin
  1435. e := Comp(Value);
  1436. result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1437. end;
  1438. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer): String;
  1439. begin
  1440. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1441. end;
  1442. {$endif FPC_COMP_IS_INT64}
  1443. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer; Const FormatSettings: TFormatSettings): string;
  1444. begin
  1445. result:=FloatToStrF(Value,Format,19,Digits,FormatSettings);
  1446. end;
  1447. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
  1448. begin
  1449. Result:=CurrToStrF(Value,Format,Digits,DefaultFormatSettings);
  1450. end;
  1451. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  1452. begin
  1453. If (Value<MinDateTime) or (Value>MaxDateTime) then
  1454. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  1455. Result:=Value;
  1456. end;
  1457. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  1458. begin
  1459. Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
  1460. if Result then
  1461. AResult := Value;
  1462. end;
  1463. function FloatToCurr(const Value: Extended): Currency;
  1464. begin
  1465. if not TryFloatToCurr(Value, Result) then
  1466. Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
  1467. end;
  1468. Function CurrToStr(Value: Currency): string;
  1469. begin
  1470. Result:=FloatToStrF(Value,ffGeneral,-1,0);
  1471. end;
  1472. Function CurrToStr(Value: Currency; Const FormatSettings: TFormatSettings): string;
  1473. begin
  1474. Result:=FloatToStrF(Value,ffGeneral,-1,0,FormatSettings);
  1475. end;
  1476. function StrToCurr(const S: string): Currency;
  1477. begin
  1478. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1479. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1480. end;
  1481. function StrToCurr(const S: string; Const FormatSettings: TFormatSettings): Currency;
  1482. begin
  1483. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency,FormatSettings) then
  1484. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1485. end;
  1486. Function TryStrToCurr(Const S : String; Out Value: Currency): Boolean;
  1487. Begin
  1488. Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency);
  1489. End;
  1490. function TryStrToCurr(const S: string;Out Value : Currency; Const FormatSettings: TFormatSettings): Boolean;
  1491. Begin
  1492. Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency,FormatSettings);
  1493. End;
  1494. function StrToCurrDef(const S: string; Default : Currency): Currency;
  1495. begin
  1496. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1497. Result:=Default;
  1498. end;
  1499. function StrToCurrDef(const S: string; Default : Currency; Const FormatSettings: TFormatSettings): Currency;
  1500. begin
  1501. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency,FormatSettings) then
  1502. Result:=Default;
  1503. end;
  1504. {$endif FPUNONE}
  1505. function AnsiDequotedStr(const S: string; AQuote: Char): string;
  1506. var p : pchar;
  1507. begin
  1508. p:=pchar(pointer(s)); // work around CONST. Ansiextract is safe for nil
  1509. result:=AnsiExtractquotedStr(p,AQuote);
  1510. end;
  1511. function StrToBool(const S: string): Boolean;
  1512. begin
  1513. if not(TryStrToBool(S,Result)) then
  1514. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1515. end;
  1516. procedure CheckBoolStrs;
  1517. begin
  1518. If Length(TrueBoolStrs)=0 then
  1519. begin
  1520. SetLength(TrueBoolStrs,1);
  1521. TrueBoolStrs[0]:='True';
  1522. end;
  1523. If Length(FalseBoolStrs)=0 then
  1524. begin
  1525. SetLength(FalseBoolStrs,1);
  1526. FalseBoolStrs[0]:='False';
  1527. end;
  1528. end;
  1529. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
  1530. begin
  1531. if UseBoolStrs Then
  1532. begin
  1533. CheckBoolStrs;
  1534. if B then
  1535. Result:=TrueBoolStrs[0]
  1536. else
  1537. Result:=FalseBoolStrs[0];
  1538. end
  1539. else
  1540. If B then
  1541. Result:='-1'
  1542. else
  1543. Result:='0';
  1544. end;
  1545. // from textmode IDE util funcs.
  1546. function BoolToStr(B: boolean; const TrueS, FalseS: string): string;
  1547. begin
  1548. if B then Result:=TrueS else BoolToStr:=FalseS;
  1549. end;
  1550. function StrToBoolDef(const S: string; Default: Boolean): Boolean;
  1551. begin
  1552. if not(TryStrToBool(S,Result)) then
  1553. Result:=Default;
  1554. end;
  1555. function TryStrToBool(const S: string; out Value: Boolean): Boolean;
  1556. Var
  1557. Temp : String;
  1558. I : Longint;
  1559. {$ifdef FPUNONE}
  1560. D : Longint;
  1561. {$else}
  1562. D : Double;
  1563. {$endif}
  1564. Code: word;
  1565. begin
  1566. Temp:=upcase(S);
  1567. Val(temp,D,code);
  1568. Result:=true;
  1569. If Code=0 then
  1570. {$ifdef FPUNONE}
  1571. Value:=(D<>0)
  1572. {$else}
  1573. Value:=(D<>0.0)
  1574. {$endif}
  1575. else
  1576. begin
  1577. CheckBoolStrs;
  1578. for I:=low(TrueBoolStrs) to High(TrueBoolStrs) do
  1579. if Temp=upcase(TrueBoolStrs[I]) then
  1580. begin
  1581. Value:=true;
  1582. exit;
  1583. end;
  1584. for I:=low(FalseBoolStrs) to High(FalseBoolStrs) do
  1585. if Temp=upcase(FalseBoolStrs[I]) then
  1586. begin
  1587. Value:=false;
  1588. exit;
  1589. end;
  1590. Result:=false;
  1591. end;
  1592. end;
  1593. {$ifndef FPUNONE}
  1594. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  1595. begin
  1596. Result:=FloatToTextFmt(Buffer,Value,Format,DefaultFormatSettings);
  1597. end;
  1598. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar;FormatSettings : TFormatSettings): Integer;
  1599. Var
  1600. Digits: String[40]; { String Of Digits }
  1601. Exponent: String[8]; { Exponent strin }
  1602. FmtStart, FmtStop: PChar; { Start And End Of relevant part }
  1603. { Of format String }
  1604. ExpFmt, ExpSize: Integer; { Type And Length Of }
  1605. { exponential format chosen }
  1606. Placehold: Array[1..4] Of Integer; { Number Of placeholders In All }
  1607. { four Sections }
  1608. thousand: Boolean; { thousand separators? }
  1609. UnexpectedDigits: Integer; { Number Of unexpected Digits that }
  1610. { have To be inserted before the }
  1611. { First placeholder. }
  1612. DigitExponent: Integer; { Exponent Of First digit In }
  1613. { Digits Array. }
  1614. { Find end of format section starting at P. False, if empty }
  1615. Function GetSectionEnd(Var P: PChar): Boolean;
  1616. Var
  1617. C: Char;
  1618. SQ, DQ: Boolean;
  1619. Begin
  1620. Result := False;
  1621. SQ := False;
  1622. DQ := False;
  1623. C := P[0];
  1624. While (C<>#0) And ((C<>';') Or SQ Or DQ) Do
  1625. Begin
  1626. Result := True;
  1627. Case C Of
  1628. #34: If Not SQ Then DQ := Not DQ;
  1629. #39: If Not DQ Then SQ := Not SQ;
  1630. End;
  1631. Inc(P);
  1632. C := P[0];
  1633. End;
  1634. End;
  1635. { Find start and end of format section to apply. If section doesn't exist,
  1636. use section 1. If section 2 is used, the sign of value is ignored. }
  1637. Procedure GetSectionRange(section: Integer);
  1638. Var
  1639. Sec: Array[1..3] Of PChar;
  1640. SecOk: Array[1..3] Of Boolean;
  1641. Begin
  1642. Sec[1] := format;
  1643. SecOk[1] := GetSectionEnd(Sec[1]);
  1644. If section > 1 Then
  1645. Begin
  1646. Sec[2] := Sec[1];
  1647. If Sec[2][0] <> #0 Then
  1648. Inc(Sec[2]);
  1649. SecOk[2] := GetSectionEnd(Sec[2]);
  1650. If section > 2 Then
  1651. Begin
  1652. Sec[3] := Sec[2];
  1653. If Sec[3][0] <> #0 Then
  1654. Inc(Sec[3]);
  1655. SecOk[3] := GetSectionEnd(Sec[3]);
  1656. End;
  1657. End;
  1658. If Not SecOk[1] Then
  1659. FmtStart := Nil
  1660. Else
  1661. Begin
  1662. If Not SecOk[section] Then
  1663. section := 1
  1664. Else If section = 2 Then
  1665. Value := -Value; { Remove sign }
  1666. If section = 1 Then FmtStart := format Else
  1667. Begin
  1668. FmtStart := Sec[section - 1];
  1669. Inc(FmtStart);
  1670. End;
  1671. FmtStop := Sec[section];
  1672. End;
  1673. End;
  1674. { Find format section ranging from FmtStart to FmtStop. }
  1675. Procedure GetFormatOptions;
  1676. Var
  1677. Fmt: PChar;
  1678. SQ, DQ: Boolean;
  1679. area: Integer;
  1680. Begin
  1681. SQ := False;
  1682. DQ := False;
  1683. Fmt := FmtStart;
  1684. ExpFmt := 0;
  1685. area := 1;
  1686. thousand := False;
  1687. Placehold[1] := 0;
  1688. Placehold[2] := 0;
  1689. Placehold[3] := 0;
  1690. Placehold[4] := 0;
  1691. While Fmt < FmtStop Do
  1692. Begin
  1693. Case Fmt[0] Of
  1694. #34:
  1695. Begin
  1696. If Not SQ Then
  1697. DQ := Not DQ;
  1698. Inc(Fmt);
  1699. End;
  1700. #39:
  1701. Begin
  1702. If Not DQ Then
  1703. SQ := Not SQ;
  1704. Inc(Fmt);
  1705. End;
  1706. Else
  1707. { if not in quotes, then interpret}
  1708. If Not (SQ Or DQ) Then
  1709. Begin
  1710. Case Fmt[0] Of
  1711. '0':
  1712. Begin
  1713. Case area Of
  1714. 1:
  1715. area := 2;
  1716. 4:
  1717. Begin
  1718. area := 3;
  1719. Inc(Placehold[3], Placehold[4]);
  1720. Placehold[4] := 0;
  1721. End;
  1722. End;
  1723. Inc(Placehold[area]);
  1724. Inc(Fmt);
  1725. End;
  1726. '#':
  1727. Begin
  1728. If area=3 Then
  1729. area:=4;
  1730. Inc(Placehold[area]);
  1731. Inc(Fmt);
  1732. End;
  1733. '.':
  1734. Begin
  1735. If area<3 Then
  1736. area:=3;
  1737. Inc(Fmt);
  1738. End;
  1739. ',':
  1740. Begin
  1741. thousand := DefaultFormatSettings.ThousandSeparator<>#0;
  1742. Inc(Fmt);
  1743. End;
  1744. 'e', 'E':
  1745. If ExpFmt = 0 Then
  1746. Begin
  1747. If (Fmt[0]='E') Then
  1748. ExpFmt:=1
  1749. Else
  1750. ExpFmt := 3;
  1751. Inc(Fmt);
  1752. If (Fmt<FmtStop) Then
  1753. Begin
  1754. Case Fmt[0] Of
  1755. '+':
  1756. Begin
  1757. End;
  1758. '-':
  1759. Inc(ExpFmt);
  1760. Else
  1761. ExpFmt := 0;
  1762. End;
  1763. If ExpFmt <> 0 Then
  1764. Begin
  1765. Inc(Fmt);
  1766. ExpSize := 0;
  1767. While (Fmt<FmtStop) And
  1768. (ExpSize<4) And
  1769. (Fmt[0] In ['0'..'9']) Do
  1770. Begin
  1771. Inc(ExpSize);
  1772. Inc(Fmt);
  1773. End;
  1774. End;
  1775. End
  1776. Else
  1777. { just e/E without subsequent +/- -> not exponential format,
  1778. but we have to simply print e/E literally }
  1779. ExpFmt:=0;
  1780. End
  1781. Else
  1782. Inc(Fmt);
  1783. Else { Case }
  1784. Inc(Fmt);
  1785. End; { Case }
  1786. End { Begin }
  1787. Else
  1788. Inc(Fmt);
  1789. End; { Case }
  1790. End; { While .. Begin }
  1791. End;
  1792. Procedure FloatToStr;
  1793. Var
  1794. I, J, Exp, Width, Decimals, DecimalPoint, len: Integer;
  1795. Begin
  1796. If ExpFmt = 0 Then
  1797. Begin
  1798. { Fixpoint }
  1799. Decimals:=Placehold[3]+Placehold[4];
  1800. Width:=Placehold[1]+Placehold[2]+Decimals;
  1801. If (Decimals=0) Then
  1802. Str(Value:Width:0,Digits)
  1803. Else if Value>=0 then
  1804. Str(Value:Width+1:Decimals,Digits)
  1805. else
  1806. Str(Value:Width+2:Decimals,Digits);
  1807. len:=Length(Digits);
  1808. { Find the decimal point }
  1809. If (Decimals=0) Then
  1810. DecimalPoint:=len+1
  1811. Else
  1812. DecimalPoint:=len-Decimals;
  1813. { If value is very small, and no decimal places
  1814. are desired, remove the leading 0. }
  1815. If (Abs(Value) < 1) And (Placehold[2] = 0) Then
  1816. Begin
  1817. If (Placehold[1]=0) Then
  1818. Delete(Digits,DecimalPoint-1,1)
  1819. Else
  1820. Digits[DecimalPoint-1]:=' ';
  1821. End;
  1822. { Convert optional zeroes to spaces. }
  1823. I:=len;
  1824. J:=DecimalPoint+Placehold[3];
  1825. While (I>J) And (Digits[I]='0') Do
  1826. Begin
  1827. Digits[I] := ' ';
  1828. Dec(I);
  1829. End;
  1830. { If integer value and no obligatory decimal
  1831. places, remove decimal point. }
  1832. If (DecimalPoint < len) And (Digits[DecimalPoint + 1] = ' ') Then
  1833. Digits[DecimalPoint] := ' ';
  1834. { Convert spaces left from obligatory decimal point to zeroes.
  1835. MVC : If - sign is encountered, replace it too, and put at position 1}
  1836. I:=DecimalPoint-Placehold[2];
  1837. J:=0;
  1838. While (I<DecimalPoint) And (Digits[I] in [' ','-']) Do
  1839. Begin
  1840. If Digits[i]='-' then
  1841. J:=I;
  1842. Digits[I] := '0';
  1843. Inc(I);
  1844. End;
  1845. If (J<>0) then
  1846. Digits[1]:='-';
  1847. If (Digits[1]='-') then
  1848. Begin
  1849. I:=1;
  1850. While (I<=length(Digits)) And (Not (Digits[I] in ['1'..'9'])) Do
  1851. Inc(I);
  1852. If (I>length(Digits)) then
  1853. Begin
  1854. Digits:=Copy(Digits, 2, Length(Digits));
  1855. Dec(DecimalPoint);
  1856. End;
  1857. End;
  1858. Exp := 0;
  1859. End
  1860. Else
  1861. Begin
  1862. { Scientific: exactly <Width> Digits With <Precision> Decimals
  1863. And adjusted Exponent. }
  1864. If Placehold[1]+Placehold[2]=0 Then
  1865. Placehold[1]:=1;
  1866. Decimals := Placehold[3] + Placehold[4];
  1867. Width:=Placehold[1]+Placehold[2]+Decimals;
  1868. { depending on the maximally supported precision, the exponent field }
  1869. { is longer/shorter }
  1870. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1871. Str(Value:Width+8,Digits);
  1872. {$else FPC_HAS_TYPE_EXTENDED}
  1873. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1874. Str(Value:Width+7,Digits);
  1875. {$else FPC_HAS_TYPE_DOUBLE}
  1876. Str(Value:Width+6,Digits);
  1877. {$endif FPC_HAS_TYPE_DOUBLE}
  1878. {$endif FPC_HAS_TYPE_EXTENDED}
  1879. { Find and cut out exponent. Always the
  1880. last 6 characters in the string.
  1881. -> 0000E+0000
  1882. *** No, not always the last 6 characters, this depends on
  1883. the maximally supported precision (JM)}
  1884. I:=Pos('E',Digits);
  1885. Val(Copy(Digits,I+1,255),Exp,J);
  1886. Exp:=Exp+1-(Placehold[1]+Placehold[2]);
  1887. Delete(Digits, I, 255);
  1888. { Str() always returns at least one digit after the decimal point.
  1889. If we don't want it, we have to remove it. }
  1890. If (Decimals=0) And (Placehold[1]+Placehold[2]<= 1) Then
  1891. Begin
  1892. If (Digits[4]>='5') Then
  1893. Begin
  1894. Inc(Digits[2]);
  1895. If (Digits[2]>'9') Then
  1896. Begin
  1897. Digits[2] := '1';
  1898. Inc(Exp);
  1899. End;
  1900. End;
  1901. Delete(Digits, 3, 2);
  1902. DecimalPoint := Length(Digits) + 1;
  1903. End
  1904. Else
  1905. Begin
  1906. { Move decimal point at the desired position }
  1907. Delete(Digits, 3, 1);
  1908. DecimalPoint:=2+Placehold[1]+Placehold[2];
  1909. If (Decimals<>0) Then
  1910. Insert('.',Digits,DecimalPoint);
  1911. End;
  1912. { Convert optional zeroes to spaces. }
  1913. I := Length(Digits);
  1914. J := DecimalPoint + Placehold[3];
  1915. While (I > J) And (Digits[I] = '0') Do
  1916. Begin
  1917. Digits[I] := ' ';
  1918. Dec(I);
  1919. End;
  1920. { If integer number and no obligatory decimal paces, remove decimal point }
  1921. If (DecimalPoint<Length(Digits)) And
  1922. (Digits[DecimalPoint+1]=' ') Then
  1923. Digits[DecimalPoint]:=' ';
  1924. If (Digits[1]=' ') Then
  1925. Begin
  1926. Delete(Digits, 1, 1);
  1927. Dec(DecimalPoint);
  1928. End;
  1929. { Calculate exponent string }
  1930. Str(Abs(Exp), Exponent);
  1931. While Length(Exponent)<ExpSize Do
  1932. Insert('0',Exponent,1);
  1933. If Exp >= 0 Then
  1934. Begin
  1935. If (ExpFmt In [1,3]) Then
  1936. Insert('+', Exponent, 1);
  1937. End
  1938. Else
  1939. Insert('-',Exponent,1);
  1940. If (ExpFmt<3) Then
  1941. Insert('E',Exponent,1)
  1942. Else
  1943. Insert('e',Exponent,1);
  1944. End;
  1945. DigitExponent:=DecimalPoint-2;
  1946. I:=1;
  1947. While (I<=Length(Digits)) and (Digits[i] in [' ','-']) do
  1948. begin
  1949. Dec(DigitExponent);
  1950. Inc(i);
  1951. end;
  1952. UnexpectedDigits:=DecimalPoint-1-(Placehold[1]+Placehold[2]);
  1953. End;
  1954. Function PutResult: LongInt;
  1955. Var
  1956. SQ, DQ: Boolean;
  1957. Fmt, Buf: PChar;
  1958. Dig, N: Integer;
  1959. Begin
  1960. SQ := False;
  1961. DQ := False;
  1962. Fmt := FmtStart;
  1963. Buf := Buffer;
  1964. Dig := 1;
  1965. While (Fmt<FmtStop) Do
  1966. Begin
  1967. // WriteLn('Treating : "',Fmt[0],'"');
  1968. Case Fmt[0] Of
  1969. #34:
  1970. Begin
  1971. If Not SQ Then
  1972. DQ := Not DQ;
  1973. Inc(Fmt);
  1974. End;
  1975. #39:
  1976. Begin
  1977. If Not DQ Then
  1978. SQ := Not SQ;
  1979. Inc(Fmt);
  1980. End;
  1981. Else
  1982. If Not (SQ Or DQ) Then
  1983. Begin
  1984. Case Fmt[0] Of
  1985. '0', '#', '.':
  1986. Begin
  1987. If (Dig=1) And (UnexpectedDigits>0) Then
  1988. Begin
  1989. { Everything unexpected is written before the first digit }
  1990. For N := 1 To UnexpectedDigits Do
  1991. Begin
  1992. if (Digits[N]<>' ') Then
  1993. begin
  1994. Buf[0] := Digits[N];
  1995. Inc(Buf);
  1996. end;
  1997. If thousand And (Not (Digits[N] in [' ','-'])) Then
  1998. Begin
  1999. If (DigitExponent Mod 3 = 0) And (DigitExponent>0) Then
  2000. Begin
  2001. Buf[0] := FormatSettings.ThousandSeparator;
  2002. Inc(Buf);
  2003. End;
  2004. Dec(DigitExponent);
  2005. End;
  2006. End;
  2007. Inc(Dig, UnexpectedDigits);
  2008. End;
  2009. If (Digits[Dig]<>' ') Then
  2010. Begin
  2011. If (Digits[Dig]='.') Then
  2012. Buf[0] := FormatSettings.DecimalSeparator
  2013. Else
  2014. Buf[0] := Digits[Dig];
  2015. Inc(Buf);
  2016. If thousand And (DigitExponent Mod 3 = 0) And (DigitExponent > 0) and (Digits[Dig]<>'-') Then
  2017. Begin
  2018. Buf[0] := FormatSettings.ThousandSeparator;
  2019. Inc(Buf);
  2020. End;
  2021. End;
  2022. if Digits[Dig]<>'-' then
  2023. Dec(DigitExponent);
  2024. Inc(Dig);
  2025. Inc(Fmt);
  2026. End;
  2027. 'e', 'E':
  2028. Begin
  2029. If ExpFmt <> 0 Then
  2030. Begin
  2031. Inc(Fmt);
  2032. If Fmt < FmtStop Then
  2033. Begin
  2034. If Fmt[0] In ['+', '-'] Then
  2035. Begin
  2036. Inc(Fmt, ExpSize);
  2037. For N:=1 To Length(Exponent) Do
  2038. Buf[N-1] := Exponent[N];
  2039. Inc(Buf,Length(Exponent));
  2040. ExpFmt:=0;
  2041. End;
  2042. Inc(Fmt);
  2043. End;
  2044. End
  2045. Else
  2046. Begin
  2047. { No legal exponential format.
  2048. Simply write the 'E' to the result. }
  2049. Buf[0] := Fmt[0];
  2050. Inc(Buf);
  2051. Inc(Fmt);
  2052. End;
  2053. End;
  2054. Else { Case }
  2055. { Usual character }
  2056. If (Fmt[0]<>',') Then
  2057. Begin
  2058. Buf[0] := Fmt[0];
  2059. Inc(Buf);
  2060. End;
  2061. Inc(Fmt);
  2062. End; { Case }
  2063. End
  2064. Else { IF }
  2065. Begin
  2066. { Character inside single or double quotes }
  2067. Buf[0] := Fmt[0];
  2068. Inc(Buf);
  2069. Inc(Fmt);
  2070. End;
  2071. End; { Case }
  2072. End; { While .. Begin }
  2073. Result:=PtrUInt(Buf)-PtrUInt(Buffer);
  2074. End;
  2075. Begin
  2076. If (Value>0) Then
  2077. GetSectionRange(1)
  2078. Else If (Value<0) Then
  2079. GetSectionRange(2)
  2080. Else
  2081. GetSectionRange(3);
  2082. If FmtStart = Nil Then
  2083. Begin
  2084. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings);
  2085. End
  2086. Else
  2087. Begin
  2088. GetFormatOptions;
  2089. If (ExpFmt = 0) And (Abs(Value) >= 1E18) Then
  2090. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings)
  2091. Else
  2092. Begin
  2093. FloatToStr;
  2094. Result := PutResult;
  2095. End;
  2096. End;
  2097. End;
  2098. Procedure FloatToDecimal(Out Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals : integer);
  2099. var
  2100. Buffer: String[254]; //Though str func returns only 25 chars, this might change in the future
  2101. InfNan: string[3];
  2102. Error, N, L, Start, C: Integer;
  2103. GotNonZeroBeforeDot, BeforeDot : boolean;
  2104. begin
  2105. case ValueType of
  2106. fvExtended:
  2107. Str(Extended(Value):25, Buffer);
  2108. fvDouble,
  2109. fvReal:
  2110. Str(Double(Value):23, Buffer);
  2111. fvSingle:
  2112. Str(Single(Value):16, Buffer);
  2113. fvCurrency:
  2114. Str(Currency(Value):25, Buffer);
  2115. fvComp:
  2116. Str(Currency(Value):23, Buffer);
  2117. end;
  2118. N := 1;
  2119. L := Byte(Buffer[0]);
  2120. while Buffer[N]=' ' do
  2121. Inc(N);
  2122. Result.Negative := (Buffer[N] = '-');
  2123. if Result.Negative then
  2124. Inc(N)
  2125. else if (Buffer[N] = '+') then
  2126. inc(N);
  2127. { special cases for Inf and Nan }
  2128. if (L>=N+2) then
  2129. begin
  2130. InfNan:=copy(Buffer,N,3);
  2131. if (InfNan='Inf') then
  2132. begin
  2133. Result.Digits[0]:=#0;
  2134. Result.Exponent:=32767;
  2135. exit
  2136. end;
  2137. if (InfNan='Nan') then
  2138. begin
  2139. Result.Digits[0]:=#0;
  2140. Result.Exponent:=-32768;
  2141. exit
  2142. end;
  2143. end;
  2144. Start := N; //Start of digits
  2145. Result.Exponent := 0; BeforeDot := true;
  2146. GotNonZeroBeforeDot := false;
  2147. while (L>=N) and (Buffer[N]<>'E') do
  2148. begin
  2149. if Buffer[N]='.' then
  2150. BeforeDot := false
  2151. else
  2152. begin
  2153. if BeforeDot then
  2154. begin // Currently this is always 1 char
  2155. Inc(Result.Exponent);
  2156. Result.Digits[N-Start] := Buffer[N];
  2157. if Buffer[N] <> '0' then
  2158. GotNonZeroBeforeDot := true;
  2159. end
  2160. else
  2161. Result.Digits[N-Start-1] := Buffer[N]
  2162. end;
  2163. Inc(N);
  2164. end;
  2165. Inc(N); // Pass through 'E'
  2166. if N<=L then
  2167. begin
  2168. Val(Copy(Buffer, N, L-N+1), C, Error); // Get exponent after 'E'
  2169. Inc(Result.Exponent, C);
  2170. end;
  2171. // Calculate number of digits we have from str
  2172. if BeforeDot then
  2173. N := N - Start - 1
  2174. else
  2175. N := N - Start - 2;
  2176. L := SizeOf(Result.Digits);
  2177. if N<L then
  2178. FillChar(Result.Digits[N], L-N, '0'); //Zero remaining space
  2179. if Decimals + Result.Exponent < Precision Then //After this it is the same as in FloatToDecimal
  2180. N := Decimals + Result.Exponent
  2181. Else
  2182. N := Precision;
  2183. if N >= L Then
  2184. N := L-1;
  2185. if N = 0 Then
  2186. begin
  2187. if Result.Digits[0] >= '5' Then
  2188. begin
  2189. Result.Digits[0] := '1';
  2190. Result.Digits[1] := #0;
  2191. Inc(Result.Exponent);
  2192. end
  2193. Else
  2194. Result.Digits[0] := #0;
  2195. end //N=0
  2196. Else if N > 0 Then
  2197. begin
  2198. if Result.Digits[N] >= '5' Then
  2199. begin
  2200. Repeat
  2201. Result.Digits[N] := #0;
  2202. Dec(N);
  2203. Inc(Result.Digits[N]);
  2204. Until (N = 0) Or (Result.Digits[N] < ':');
  2205. If Result.Digits[0] = ':' Then
  2206. begin
  2207. Result.Digits[0] := '1';
  2208. Inc(Result.Exponent);
  2209. end;
  2210. end
  2211. Else
  2212. begin
  2213. Result.Digits[N] := '0';
  2214. While (N > -1) And (Result.Digits[N] = '0') Do
  2215. begin
  2216. Result.Digits[N] := #0;
  2217. Dec(N);
  2218. end;
  2219. end;
  2220. end //N>0
  2221. Else
  2222. Result.Digits[0] := #0;
  2223. if (Result.Digits[0] = #0) and
  2224. not GotNonZeroBeforeDot then
  2225. begin
  2226. Result.Exponent := 0;
  2227. Result.Negative := False;
  2228. end;
  2229. end;
  2230. Procedure FloatToDecimal(Out Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  2231. begin
  2232. FloatToDecimal(Result,Value,fvExtended,Precision,Decimals);
  2233. end;
  2234. Function FormatFloat(Const Format : String; Value : Extended; Const FormatSettings: TFormatSettings) : String;
  2235. Var
  2236. buf : Array[0..1024] of char;
  2237. Begin // not changed to pchar(pointer(). Possibly not safe
  2238. Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format),FormatSettings)]:=#0;
  2239. Result:=StrPas(@Buf[0]);
  2240. End;
  2241. Function FormatFloat(Const format: String; Value: Extended): String;
  2242. begin
  2243. Result:=FormatFloat(Format,Value,DefaultFormatSettings);
  2244. end;
  2245. Function FormatCurr(const Format: string; Value: Currency; Const FormatSettings: TFormatSettings): string;
  2246. begin
  2247. Result := FormatFloat(Format, Value,FormatSettings);
  2248. end;
  2249. function FormatCurr(const Format: string; Value: Currency): string;
  2250. begin
  2251. Result:=FormatCurr(Format,Value,DefaultFormatSettings);
  2252. end;
  2253. {$endif}
  2254. {==============================================================================}
  2255. { extra functions }
  2256. {==============================================================================}
  2257. { LeftStr returns Count left-most characters from S }
  2258. function LeftStr(const S: string; Count: integer): string;
  2259. begin
  2260. result := Copy(S, 1, Count);
  2261. end ;
  2262. { RightStr returns Count right-most characters from S }
  2263. function RightStr(const S: string; Count: integer): string;
  2264. begin
  2265. If Count>Length(S) then
  2266. Count:=Length(S);
  2267. result := Copy(S, 1 + Length(S) - Count, Count);
  2268. end;
  2269. { BCDToInt converts the BCD value Value to an integer }
  2270. function BCDToInt(Value: integer): integer;
  2271. var i, j, digit: integer;
  2272. begin
  2273. result := 0;
  2274. j := 1;
  2275. for i := 0 to SizeOf(Value) shl 1 - 1 do begin
  2276. digit := Value and 15;
  2277. if digit > $9 then
  2278. begin
  2279. if i = 0 then
  2280. begin
  2281. if digit in [$B, $D] then j := -1
  2282. end
  2283. else raise EConvertError.createfmt(SInvalidBCD,[Value]);
  2284. end
  2285. else
  2286. begin
  2287. result := result + j * digit;
  2288. j := j * 10;
  2289. end ;
  2290. Value := Value shr 4;
  2291. end ;
  2292. end ;
  2293. Function LastDelimiter(const Delimiters, S: string): SizeInt;
  2294. var
  2295. chs: TSysCharSet;
  2296. I: SizeInt;
  2297. begin
  2298. chs := [];
  2299. for I := 1 to Length(Delimiters) do
  2300. Include(chs, Delimiters[I]);
  2301. Result:=Length(S);
  2302. While (Result>0) and not (S[Result] in chs) do
  2303. Dec(Result);
  2304. end;
  2305. {$macro on}
  2306. {$define INSTRINGREPLACE}
  2307. {$define SRString:=String}
  2308. {$define SRUpperCase:=AnsiUppercase}
  2309. {$define SRPCHAR:=PChar}
  2310. {$define SRCHAR:=Char}
  2311. Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  2312. {$i syssr.inc}
  2313. {$undef INSTRINGREPLACE}
  2314. {$undef SRString}
  2315. {$undef SRUpperCase}
  2316. {$undef SRPCHAR}
  2317. {$undef SRCHAR}
  2318. Function IsDelimiter(const Delimiters, S: string; Index: SizeInt): Boolean;
  2319. begin
  2320. Result:=False;
  2321. If (Index>0) and (Index<=Length(S)) then
  2322. Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
  2323. end;
  2324. Function ByteToCharLen(const S: string; MaxLen: SizeInt): SizeInt;
  2325. begin
  2326. Result:=Length(S);
  2327. If Result>MaxLen then
  2328. Result:=MaxLen;
  2329. end;
  2330. Function ByteToCharIndex(const S: string; Index: SizeInt): SizeInt;
  2331. begin
  2332. Result:=Index;
  2333. end;
  2334. Function CharToByteLen(const S: string; MaxLen: SizeInt): SizeInt;
  2335. begin
  2336. Result:=Length(S);
  2337. If Result>MaxLen then
  2338. Result:=MaxLen;
  2339. end;
  2340. Function CharToByteIndex(const S: string; Index: SizeInt): SizeInt;
  2341. begin
  2342. Result:=Index;
  2343. end;
  2344. Function ByteType(const S: string; Index: SizeUInt): TMbcsByteType;
  2345. begin
  2346. Result:=mbSingleByte;
  2347. end;
  2348. Function StrByteType(Str: PChar; Index: SizeUInt): TMbcsByteType;
  2349. begin
  2350. Result:=mbSingleByte;
  2351. end;
  2352. Function StrCharLength(const Str: PChar): SizeInt;
  2353. begin
  2354. result:=widestringmanager.CharLengthPCharProc(Str);
  2355. end;
  2356. function StrNextChar(const Str: PChar): PChar;
  2357. begin
  2358. result:=Str+StrCharLength(Str);
  2359. end;
  2360. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  2361. Var
  2362. I,L : Integer;
  2363. S,T : String;
  2364. begin
  2365. Result:=False;
  2366. S:=Switch;
  2367. If IgnoreCase then
  2368. S:=UpperCase(S);
  2369. I:=ParamCount;
  2370. While (Not Result) and (I>0) do
  2371. begin
  2372. L:=Length(Paramstr(I));
  2373. If (L>0) and (ParamStr(I)[1] in Chars) then
  2374. begin
  2375. T:=Copy(ParamStr(I),2,L-1);
  2376. If IgnoreCase then
  2377. T:=UpperCase(T);
  2378. Result:=S=T;
  2379. end;
  2380. Dec(i);
  2381. end;
  2382. end;
  2383. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  2384. begin
  2385. Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
  2386. end;
  2387. Function FindCmdLineSwitch(const Switch: string): Boolean;
  2388. begin
  2389. Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
  2390. end;
  2391. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  2392. const
  2393. Quotes = ['''', '"'];
  2394. Var
  2395. L : String;
  2396. C,LQ,BC : Char;
  2397. P,BLen,Len : Integer;
  2398. HB,IBC : Boolean;
  2399. begin
  2400. Result:='';
  2401. L:=Line;
  2402. Blen:=Length(BreakStr);
  2403. If (BLen>0) then
  2404. BC:=BreakStr[1]
  2405. else
  2406. BC:=#0;
  2407. Len:=Length(L);
  2408. While (Len>0) do
  2409. begin
  2410. P:=1;
  2411. LQ:=#0;
  2412. HB:=False;
  2413. IBC:=False;
  2414. While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
  2415. begin
  2416. C:=L[P];
  2417. If (C=LQ) then
  2418. LQ:=#0
  2419. else If (C in Quotes) then
  2420. LQ:=C;
  2421. If (LQ<>#0) then
  2422. Inc(P)
  2423. else
  2424. begin
  2425. HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
  2426. If HB then
  2427. Inc(P,Blen)
  2428. else
  2429. begin
  2430. If (P>=MaxCol) then
  2431. IBC:=C in BreakChars;
  2432. Inc(P);
  2433. end;
  2434. end;
  2435. // Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
  2436. end;
  2437. Result:=Result+Copy(L,1,P-1);
  2438. Delete(L,1,P-1);
  2439. Len:=Length(L);
  2440. If (Len>0) and Not HB then
  2441. Result:=Result+BreakStr;
  2442. end;
  2443. end;
  2444. function WrapText(const Line: string; MaxCol: Integer): string;
  2445. begin
  2446. Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
  2447. end;
  2448. {$ifndef FPC_NOGENERICANSIROUTINES}
  2449. {
  2450. Case Translation Tables
  2451. Can be used in internationalization support.
  2452. Although these tables can be obtained through system calls
  2453. cd it is better to not use those, since most implementation are not 100%
  2454. WARNING:
  2455. before modifying a translation table make sure that the current codepage
  2456. of the OS corresponds to the one you make changes to
  2457. }
  2458. const
  2459. {$if defined(MSDOS) or defined(GO32V2) or defined(WATCOM) }
  2460. { upper case translation table for character set 850 }
  2461. CP850UCT: array[128..255] of char =
  2462. (#128,#154,#144,#182,#142,#182,#143,#128,#210,#211,#212,#216,#215,#222,#142,#143,
  2463. #144,#146,#146,#226,#153,#227,#234,#235,'Y',#153,#154,#157,#156,#157,#158,#159,
  2464. #181,#214,#224,#233,#165,#165,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2465. #176,#177,#178,#179,#180,#181,#182,#183,#184,#185,#186,#187,#188,#189,#190,#191,
  2466. #192,#193,#194,#195,#196,#197,#199,#199,#200,#201,#202,#203,#204,#205,#206,#207,
  2467. #208,#209,#210,#211,#212,#213,#214,#215,#216,#217,#218,#219,#220,#221,#222,#223,
  2468. #224,#225,#226,#227,#229,#229,#230,#237,#232,#233,#234,#235,#237,#237,#238,#239,
  2469. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2470. { lower case translation table for character set 850 }
  2471. CP850LCT: array[128..255] of char =
  2472. (#135,#129,#130,#131,#132,#133,#134,#135,#136,#137,#138,#139,#140,#141,#132,#134,
  2473. #130,#145,#145,#147,#148,#149,#150,#151,#152,#148,#129,#155,#156,#155,#158,#159,
  2474. #160,#161,#162,#163,#164,#164,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2475. #176,#177,#178,#179,#180,#160,#131,#133,#184,#185,#186,#187,#188,#189,#190,#191,
  2476. #192,#193,#194,#195,#196,#197,#198,#198,#200,#201,#202,#203,#204,#205,#206,#207,
  2477. #208,#209,#136,#137,#138,#213,#161,#140,#139,#217,#218,#219,#220,#221,#141,#223,
  2478. #162,#225,#147,#149,#228,#228,#230,#237,#232,#163,#150,#151,#236,#236,#238,#239,
  2479. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2480. {$endif}
  2481. { upper case translation table for character set ISO 8859/1 Latin 1 }
  2482. CPISO88591UCT: array[192..255] of char =
  2483. ( #192, #193, #194, #195, #196, #197, #198, #199,
  2484. #200, #201, #202, #203, #204, #205, #206, #207,
  2485. #208, #209, #210, #211, #212, #213, #214, #215,
  2486. #216, #217, #218, #219, #220, #221, #222, #223,
  2487. #192, #193, #194, #195, #196, #197, #198, #199,
  2488. #200, #201, #202, #203, #204, #205, #206, #207,
  2489. #208, #209, #210, #211, #212, #213, #214, #247,
  2490. #216, #217, #218, #219, #220, #221, #222, #89 );
  2491. { lower case translation table for character set ISO 8859/1 Latin 1 }
  2492. CPISO88591LCT: array[192..255] of char =
  2493. ( #224, #225, #226, #227, #228, #229, #230, #231,
  2494. #232, #233, #234, #235, #236, #237, #238, #239,
  2495. #240, #241, #242, #243, #244, #245, #246, #215,
  2496. #248, #249, #250, #251, #252, #253, #254, #223,
  2497. #224, #225, #226, #227, #228, #229, #230, #231,
  2498. #232, #233, #234, #235, #236, #237, #238, #239,
  2499. #240, #241, #242, #243, #244, #245, #246, #247,
  2500. #248, #249, #250, #251, #252, #253, #254, #255 );
  2501. {$endif FPC_NOGENERICANSIROUTINES}
  2502. function sscanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  2503. var
  2504. i,j,n,m : SizeInt;
  2505. s1 : string;
  2506. function GetInt(unsigned : boolean=false) : Integer;
  2507. begin
  2508. s1 := '';
  2509. while (Length(s) > n) and (s[n] = ' ') do
  2510. inc(n);
  2511. { read sign }
  2512. if (Length(s)>= n) and (s[n] in ['+', '-']) then
  2513. begin
  2514. { don't accept - when reading unsigned }
  2515. if unsigned and (s[n]='-') then
  2516. begin
  2517. result:=length(s1);
  2518. exit;
  2519. end
  2520. else
  2521. begin
  2522. s1:=s1+s[n];
  2523. inc(n);
  2524. end;
  2525. end;
  2526. { read numbers }
  2527. while (Length(s) >= n) and
  2528. (s[n] in ['0'..'9']) do
  2529. begin
  2530. s1 := s1+s[n];
  2531. inc(n);
  2532. end;
  2533. Result := Length(s1);
  2534. end;
  2535. function GetFloat : Integer;
  2536. begin
  2537. s1 := '';
  2538. while (Length(s) > n) and (s[n] = ' ') do
  2539. inc(n);
  2540. while (Length(s) >= n) and
  2541. (s[n] in ['0'..'9', '+', '-', FormatSettings.DecimalSeparator, 'e', 'E']) do
  2542. begin
  2543. s1 := s1+s[n];
  2544. inc(n);
  2545. end;
  2546. Result := Length(s1);
  2547. end;
  2548. function GetString : Integer;
  2549. begin
  2550. s1 := '';
  2551. while (Length(s) > n) and (s[n] = ' ') do
  2552. inc(n);
  2553. while (Length(s) >= n) and (s[n] <> ' ')do
  2554. begin
  2555. s1 := s1+s[n];
  2556. inc(n);
  2557. end;
  2558. Result := Length(s1);
  2559. end;
  2560. function ScanStr(c : Char) : Boolean;
  2561. begin
  2562. while (Length(s) > n) and (s[n] <> c) do
  2563. inc(n);
  2564. inc(n);
  2565. If (n <= Length(s)) then
  2566. Result := True
  2567. else
  2568. Result := False;
  2569. end;
  2570. function GetFmt : Integer;
  2571. begin
  2572. Result := -1;
  2573. while true do
  2574. begin
  2575. while (Length(fmt) > m) and (fmt[m] = ' ') do
  2576. inc(m);
  2577. if (m >= Length(fmt)) then
  2578. break;
  2579. if (fmt[m] = '%') then
  2580. begin
  2581. inc(m);
  2582. case fmt[m] of
  2583. 'd':
  2584. Result:=vtInteger;
  2585. {$ifndef FPUNONE}
  2586. 'f':
  2587. Result:=vtExtended;
  2588. {$endif}
  2589. 's':
  2590. Result:=vtString;
  2591. 'c':
  2592. Result:=vtChar;
  2593. else
  2594. raise EFormatError.CreateFmt(SInvalidFormat,[fmt]);
  2595. end;
  2596. inc(m);
  2597. break;
  2598. end;
  2599. if not(ScanStr(fmt[m])) then
  2600. break;
  2601. inc(m);
  2602. end;
  2603. end;
  2604. begin
  2605. n := 1;
  2606. m := 1;
  2607. Result := 0;
  2608. for i:=0 to High(Pointers) do
  2609. begin
  2610. j := GetFmt;
  2611. case j of
  2612. vtInteger :
  2613. begin
  2614. if GetInt>0 then
  2615. begin
  2616. pLongint(Pointers[i])^:=StrToInt(s1);
  2617. inc(Result);
  2618. end
  2619. else
  2620. break;
  2621. end;
  2622. vtchar :
  2623. begin
  2624. if Length(s)>n then
  2625. begin
  2626. pchar(Pointers[i])^:=s[n];
  2627. inc(n);
  2628. inc(Result);
  2629. end
  2630. else
  2631. break;
  2632. end;
  2633. {$ifndef FPUNONE}
  2634. vtExtended :
  2635. begin
  2636. if GetFloat>0 then
  2637. begin
  2638. pextended(Pointers[i])^:=StrToFloat(s1);
  2639. inc(Result);
  2640. end
  2641. else
  2642. break;
  2643. end;
  2644. {$endif}
  2645. vtString :
  2646. begin
  2647. if GetString > 0 then
  2648. begin
  2649. pansistring(Pointers[i])^:=s1;
  2650. inc(Result);
  2651. end
  2652. else
  2653. break;
  2654. end;
  2655. else
  2656. break;
  2657. end;
  2658. end;
  2659. end;
  2660. {$macro on}
  2661. // Ansi version declaration
  2662. {$UNDEF SBUNICODE}
  2663. {$define SBChar:=AnsiChar}
  2664. {$define SBString:=AnsiString}
  2665. {$define TSBCharArray:=Array of SBChar}
  2666. {$define PSBChar:=PAnsiChar}
  2667. {$define SBRAWString:=RawByteString}
  2668. {$define TStringBuilder:=TAnsiStringBuilder}
  2669. {$i syssb.inc}
  2670. {$undef SBChar}
  2671. {$undef SBString}
  2672. {$undef TSBCharArray}
  2673. {$undef PSBChar}
  2674. {$undef SBRAWString}
  2675. {$undef TStringBuilder}
  2676. // Unicode version declaration
  2677. {$define SBUNICODE}
  2678. {$define SBChar:=WideChar}
  2679. {$define SBString:=UnicodeString}
  2680. {$define TSBCharArray:=Array of SBChar}
  2681. {$define PSBChar:=PWideChar}
  2682. {$define SBRAWString:=UnicodeString}
  2683. {$define TStringBuilder:=TUnicodeStringBuilder}
  2684. {$i syssb.inc}
  2685. {$undef SBChar}
  2686. {$undef SBString}
  2687. {$undef TSBCharArray}
  2688. {$undef PSBChar}
  2689. {$undef SBRAWString}
  2690. {$undef TStringBuilder}
  2691. {$undef SBUNICODE}