sysstr.inc 79 KB

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