sysstr.inc 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106
  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. try
  938. case ValueType of
  939. fvCurrency:
  940. {$ifdef FPC_HAS_STR_CURRENCY}
  941. Val(S,Currency(Value),E);
  942. {$else FPC_HAS_STR_CURRENCY}
  943. begin
  944. // needed for platforms where Currency = Int64
  945. Val(S,TempValue,E);
  946. Currency(Value) := TempValue;
  947. end;
  948. {$endif FPC_HAS_STR_CURRENCY}
  949. fvExtended:
  950. Val(S,Extended(Value),E);
  951. fvDouble:
  952. Val(S,Double(Value),E);
  953. fvSingle:
  954. Val(S,Single(Value),E);
  955. fvComp:
  956. Val(S,Comp(Value),E);
  957. fvReal:
  958. Val(S,Real(Value),E);
  959. end;
  960. { on x87, a floating point exception may be pending in case of an invalid
  961. input value -> trigger it now }
  962. {$ifdef cpux86}
  963. asm
  964. fwait
  965. end;
  966. {$endif}
  967. except
  968. E:=1;
  969. end;
  970. Result:=(E=0);
  971. End;
  972. Function TryStrToFloat(Const S : String; Out Value: Single): Boolean;
  973. begin
  974. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  975. end;
  976. Function TryStrToFloat(Const S : String; Out Value: Single; Const FormatSettings: TFormatSettings): Boolean;
  977. Begin
  978. Result := TextToFloat(PChar(pointer(S)), Value, fvSingle,FormatSettings);
  979. End;
  980. Function TryStrToFloat(Const S : String; Out Value: Double): Boolean;
  981. begin
  982. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  983. end;
  984. Function TryStrToFloat(Const S : String; Out Value: Double; Const FormatSettings: TFormatSettings): Boolean;
  985. Begin
  986. Result := TextToFloat(PChar(pointer(S)), Value, fvDouble,FormatSettings);
  987. End;
  988. {$ifdef FPC_HAS_TYPE_EXTENDED}
  989. Function TryStrToFloat(Const S : String; Out Value: Extended): Boolean;
  990. begin
  991. Result:=TryStrToFloat(S,Value,DefaultFormatSettings);
  992. end;
  993. Function TryStrToFloat(Const S : String; Out Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  994. Begin
  995. Result := TextToFloat(PChar(pointer(S)), Value,FormatSettings);
  996. End;
  997. {$endif FPC_HAS_TYPE_EXTENDED}
  998. const
  999. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1000. maxdigits = 17;
  1001. {$else}
  1002. maxdigits = 15;
  1003. {$endif}
  1004. { deactive aligned function for 2.6 }
  1005. {$ifdef VER2_6}
  1006. {$macro on}
  1007. {$define aligned:= }
  1008. {$endif VER2_6}
  1009. Function FloatToStrFIntl(const Value; format: TFloatFormat; Precision, Digits: Integer; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): String;
  1010. Var
  1011. P, PE, Q, Exponent: Integer;
  1012. Negative: Boolean;
  1013. DS: Char;
  1014. function RemoveLeadingNegativeSign(var AValue: String): Boolean;
  1015. // removes negative sign in case when result is zero eg. -0.00
  1016. var
  1017. i: PtrInt;
  1018. TS: Char;
  1019. StartPos: PtrInt;
  1020. begin
  1021. Result := False;
  1022. if Format = ffCurrency then
  1023. StartPos := 1
  1024. else
  1025. StartPos := 2;
  1026. TS := FormatSettings.ThousandSeparator;
  1027. for i := StartPos to length(AValue) do
  1028. begin
  1029. Result := (AValue[i] in ['0', DS, 'E', '+', TS]);
  1030. if not Result then
  1031. break;
  1032. end;
  1033. if (Result) and (Format <> ffCurrency) then
  1034. Delete(AValue, 1, 1);
  1035. end;
  1036. Begin
  1037. DS:=FormatSettings.DecimalSeparator;
  1038. Case format Of
  1039. ffGeneral:
  1040. Begin
  1041. case ValueType of
  1042. fvCurrency:
  1043. If (Precision = -1) Or (Precision > 19) Then Precision := 19;
  1044. else
  1045. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  1046. end;
  1047. { First convert to scientific format, with correct precision }
  1048. case ValueType of
  1049. fvDouble:
  1050. Str(Double(Extended(Aligned(Value))):precision+7, Result);
  1051. fvSingle:
  1052. Str(Single(Extended(Aligned(Value))):precision+6, Result);
  1053. fvCurrency:
  1054. {$ifdef FPC_HAS_STR_CURRENCY}
  1055. Str(Currency(Aligned(Value)):precision+6, Result);
  1056. {$else}
  1057. Str(Extended(Currency(Aligned(Value))):precision+8, Result);
  1058. {$endif FPC_HAS_STR_CURRENCY}
  1059. else
  1060. Str(Extended(Aligned(Value)):precision+8, Result);
  1061. end;
  1062. { Delete leading spaces }
  1063. while Result[1] = ' ' do
  1064. System.Delete(Result, 1, 1);
  1065. P := Pos('.', Result);
  1066. if P<>0 then
  1067. Result[P] := DS
  1068. else
  1069. Exit; { NAN or other special case }
  1070. { Consider removing exponent }
  1071. PE:=Pos('E',Result);
  1072. if PE > 0 then begin
  1073. { Read exponent }
  1074. Q := PE+2;
  1075. Exponent := 0;
  1076. while (Q <= Length(Result)) do begin
  1077. Exponent := Exponent*10 + Ord(Result[Q])-Ord('0');
  1078. Inc(Q);
  1079. end;
  1080. if Result[PE+1] = '-' then
  1081. Exponent := -Exponent;
  1082. if (P+Exponent < PE) and (Exponent > -6) then begin
  1083. { OK to remove exponent }
  1084. SetLength(Result,PE-1); { Trim exponent }
  1085. if Exponent >= 0 then begin
  1086. { Shift point to right }
  1087. for Q := 0 to Exponent-1 do begin
  1088. Result[P] := Result[P+1];
  1089. Inc(P);
  1090. end;
  1091. Result[P] := DS;
  1092. P := 1;
  1093. if Result[P] = '-' then
  1094. Inc(P);
  1095. while (Result[P] = '0') and (P < Length(Result)) and (Result[P+1] <> DS) do
  1096. { Trim leading zeros; conversion above should not give any, but occasionally does
  1097. because of rounding }
  1098. System.Delete(Result,P,1);
  1099. end else begin
  1100. { Add zeros at start }
  1101. Insert(Copy('00000',1,-Exponent),Result,P-1);
  1102. Result[P-Exponent] := Result[P-Exponent-1]; { Copy leading digit }
  1103. Result[P] := DS;
  1104. if Exponent <> -1 then
  1105. Result[P-Exponent-1] := '0';
  1106. end;
  1107. { Remove trailing zeros }
  1108. Q := Length(Result);
  1109. while (Q > 0) and (Result[Q] = '0') do
  1110. Dec(Q);
  1111. if Result[Q] = DS then
  1112. Dec(Q); { Remove trailing decimal point }
  1113. if (Q = 0) or ((Q=1) and (Result[1] = '-')) then
  1114. Result := '0'
  1115. else
  1116. SetLength(Result,Q);
  1117. end else begin
  1118. { Need exponent, but remove superfluous characters }
  1119. { Delete trailing zeros }
  1120. while Result[PE-1] = '0' do begin
  1121. System.Delete(Result,PE-1,1);
  1122. Dec(PE);
  1123. end;
  1124. { If number ends in decimal point, remove it }
  1125. if Result[PE-1] = DS then begin
  1126. System.Delete(Result,PE-1,1);
  1127. Dec(PE);
  1128. end;
  1129. { delete superfluous + in exponent }
  1130. if Result[PE+1]='+' then
  1131. System.Delete(Result,PE+1,1)
  1132. else
  1133. Inc(PE);
  1134. while Result[PE+1] = '0' do
  1135. { Delete leading zeros in exponent }
  1136. System.Delete(Result,PE+1,1)
  1137. end;
  1138. end;
  1139. End;
  1140. ffExponent:
  1141. Begin
  1142. If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
  1143. case ValueType of
  1144. fvDouble:
  1145. Str(Double(Extended(Aligned(Value))):Precision+7, Result);
  1146. fvSingle:
  1147. Str(Single(Extended(Aligned(Value))):Precision+6, Result);
  1148. fvCurrency:
  1149. {$ifdef FPC_HAS_STR_CURRENCY}
  1150. Str(Currency(Aligned(Value)):Precision+6, Result);
  1151. {$else}
  1152. Str(Extended(Currency(Aligned(Value))):Precision+8, Result);
  1153. {$endif FPC_HAS_STR_CURRENCY}
  1154. else
  1155. Str(Extended(Aligned(Value)):Precision+8, Result);
  1156. end;
  1157. { Delete leading spaces }
  1158. while Result[1] = ' ' do
  1159. System.Delete(Result, 1, 1);
  1160. if (Result[1]='-') and
  1161. { not Nan etc.? }
  1162. (Result[3]='.') then
  1163. Result[3] := DS
  1164. else if Result[2]='.' then
  1165. Result[2] := DS;
  1166. P:=Pos('E',Result);
  1167. if P <> 0 then
  1168. begin
  1169. Inc(P, 2);
  1170. if Digits > 4 then
  1171. Digits:=4;
  1172. Digits:=Length(Result) - P - Digits + 1;
  1173. if Digits < 0 then
  1174. insert(copy('0000',1,-Digits),Result,P)
  1175. else
  1176. while (Digits > 0) and (Result[P] = '0') do
  1177. begin
  1178. System.Delete(Result, P, 1);
  1179. if P > Length(Result) then
  1180. begin
  1181. System.Delete(Result, P - 2, 2);
  1182. break;
  1183. end;
  1184. Dec(Digits);
  1185. end;
  1186. end;
  1187. End;
  1188. ffFixed:
  1189. Begin
  1190. If Digits = -1 Then Digits := 2
  1191. Else If Digits > 18 Then Digits := 18;
  1192. case ValueType of
  1193. fvDouble:
  1194. Str(Double(Extended(Aligned(Value))):0:Digits, Result);
  1195. fvSingle:
  1196. Str(Single(Extended(Aligned(Value))):0:Digits, Result);
  1197. fvCurrency:
  1198. {$ifdef FPC_HAS_STR_CURRENCY}
  1199. Str(Currency(Aligned(Value)):0:Digits, Result);
  1200. {$else}
  1201. Str(Extended(Currency(Aligned(Value))):0:Digits, Result);
  1202. {$endif FPC_HAS_STR_CURRENCY}
  1203. else
  1204. Str(Extended(Aligned(Value)):0:Digits, Result);
  1205. end;
  1206. If Result[1] = ' ' Then
  1207. System.Delete(Result, 1, 1);
  1208. P := Pos('.', Result);
  1209. If P <> 0 Then Result[P] := DS;
  1210. End;
  1211. ffNumber:
  1212. Begin
  1213. If Digits = -1 Then Digits := 2
  1214. Else If Digits > maxdigits Then Digits := maxdigits;
  1215. case ValueType of
  1216. fvDouble:
  1217. Str(Double(Extended(Aligned(Value))):0:Digits, Result);
  1218. fvSingle:
  1219. Str(Single(Extended(Aligned(Value))):0:Digits, Result);
  1220. fvCurrency:
  1221. {$ifdef FPC_HAS_STR_CURRENCY}
  1222. Str(Currency(Aligned(Value)):0:Digits, Result);
  1223. {$else}
  1224. Str(Extended(Currency(Aligned(Value))):0:Digits, Result);
  1225. {$endif FPC_HAS_STR_CURRENCY}
  1226. else
  1227. Str(Extended(Aligned(Value)):0:Digits, Result);
  1228. end;
  1229. If Result[1] = ' ' Then System.Delete(Result, 1, 1);
  1230. P := Pos('.', Result);
  1231. If P <> 0 Then
  1232. Result[P] := DS
  1233. else
  1234. P := Length(Result)+1;
  1235. Dec(P, 3);
  1236. While (P > 1) Do
  1237. Begin
  1238. If (Result[P - 1] <> '-') And (FormatSettings.ThousandSeparator <> #0) Then
  1239. Insert(FormatSettings.ThousandSeparator, Result, P);
  1240. Dec(P, 3);
  1241. End;
  1242. End;
  1243. ffCurrency:
  1244. Begin
  1245. If Digits = -1 Then Digits := FormatSettings.CurrencyDecimals
  1246. Else If Digits > 18 Then Digits := 18;
  1247. case ValueType of
  1248. fvDouble:
  1249. Str(Double(Extended(Aligned(Value))):0:Digits, Result);
  1250. fvSingle:
  1251. Str(Single(Extended(Aligned(Value))):0:Digits, Result);
  1252. fvCurrency:
  1253. {$ifdef FPC_HAS_STR_CURRENCY}
  1254. Str(Currency(Aligned(Value)):0:Digits, Result);
  1255. {$else}
  1256. Str(Extended(Currency(Aligned(Value))):0:Digits, Result);
  1257. {$endif FPC_HAS_STR_CURRENCY}
  1258. else
  1259. Str(Extended(Aligned(Value)):0:Digits, Result);
  1260. end;
  1261. Negative:=Result[1] = '-';
  1262. if Negative then
  1263. System.Delete(Result, 1, 1);
  1264. P := Pos('.', Result);
  1265. If P <> 0 Then Result[P] := DS else P := Length(Result)+1;
  1266. Dec(P, 3);
  1267. While (P > 1) Do
  1268. Begin
  1269. If FormatSettings.ThousandSeparator<>#0 Then
  1270. Insert(FormatSettings.ThousandSeparator, Result, P);
  1271. Dec(P, 3);
  1272. End;
  1273. if (length(Result) > 1) and Negative then
  1274. Negative := not RemoveLeadingNegativeSign(Result);
  1275. If Not Negative Then
  1276. Begin
  1277. Case FormatSettings.CurrencyFormat Of
  1278. 0: Result := FormatSettings.CurrencyString + Result;
  1279. 1: Result := Result + FormatSettings.CurrencyString;
  1280. 2: Result := FormatSettings.CurrencyString + ' ' + Result;
  1281. 3: Result := Result + ' ' + FormatSettings.CurrencyString;
  1282. End
  1283. End
  1284. Else
  1285. Begin
  1286. Case FormatSettings.NegCurrFormat Of
  1287. 0: Result := '(' + FormatSettings.CurrencyString + Result + ')';
  1288. 1: Result := '-' + FormatSettings.CurrencyString + Result;
  1289. 2: Result := FormatSettings.CurrencyString + '-' + Result;
  1290. 3: Result := FormatSettings.CurrencyString + Result + '-';
  1291. 4: Result := '(' + Result + FormatSettings.CurrencyString + ')';
  1292. 5: Result := '-' + Result + FormatSettings.CurrencyString;
  1293. 6: Result := Result + '-' + FormatSettings.CurrencyString;
  1294. 7: Result := Result + FormatSettings.CurrencyString + '-';
  1295. 8: Result := '-' + Result + ' ' + FormatSettings.CurrencyString;
  1296. 9: Result := '-' + FormatSettings.CurrencyString + ' ' + Result;
  1297. 10: Result := Result + ' ' + FormatSettings.CurrencyString + '-';
  1298. 11: Result := FormatSettings.CurrencyString + ' ' + Result + '-';
  1299. 12: Result := FormatSettings.CurrencyString + ' ' + '-' + Result;
  1300. 13: Result := Result + '-' + ' ' + FormatSettings.CurrencyString;
  1301. 14: Result := '(' + FormatSettings.CurrencyString + ' ' + Result + ')';
  1302. 15: Result := '(' + Result + ' ' + FormatSettings.CurrencyString + ')';
  1303. End;
  1304. End;
  1305. End;
  1306. End;
  1307. if not (format in [ffCurrency]) and (length(Result) > 1) and (Result[1] = '-') then
  1308. RemoveLeadingNegativeSign(Result);
  1309. End;
  1310. {$macro off}
  1311. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1312. Function FloatToStr(Value: Extended; Const FormatSettings: TFormatSettings): String;
  1313. Begin
  1314. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvExtended,FormatSettings);
  1315. End;
  1316. Function FloatToStr(Value: Extended): String;
  1317. begin
  1318. Result:=FloatToStr(Value,DefaultFormatSettings);
  1319. end;
  1320. {$endif FPC_HAS_TYPE_EXTENDED}
  1321. Function FloatToStr(Value: Currency; Const FormatSettings: TFormatSettings): String;
  1322. Begin
  1323. Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvCurrency,FormatSettings);
  1324. End;
  1325. Function FloatToStr(Value: Currency): String;
  1326. begin
  1327. Result:=FloatToStr(Value,DefaultFormatSettings);
  1328. end;
  1329. Function FloatToStr(Value: Double; Const FormatSettings: TFormatSettings): String;
  1330. var
  1331. e: Extended;
  1332. Begin
  1333. e := Value;
  1334. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvDouble,FormatSettings);
  1335. End;
  1336. Function FloatToStr(Value: Double): String;
  1337. begin
  1338. Result:=FloatToStr(Value,DefaultFormatSettings);
  1339. end;
  1340. Function FloatToStr(Value: Single; Const FormatSettings: TFormatSettings): String;
  1341. var
  1342. e: Extended;
  1343. Begin
  1344. e := Value;
  1345. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvSingle,FormatSettings);
  1346. End;
  1347. Function FloatToStr(Value: Single): String;
  1348. begin
  1349. Result:=FloatToStr(Value,DefaultFormatSettings);
  1350. end;
  1351. Function FloatToStr(Value: Comp; Const FormatSettings: TFormatSettings): String;
  1352. var
  1353. e: Extended;
  1354. Begin
  1355. e := Value;
  1356. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1357. End;
  1358. Function FloatToStr(Value: Comp): String;
  1359. begin
  1360. Result:=FloatToStr(Value,DefaultFormatSettings);
  1361. end;
  1362. {$ifndef FPC_COMP_IS_INT64}
  1363. Function FloatToStr(Value: Int64): String;
  1364. begin
  1365. Result:=FloatToStr(Value,DefaultFormatSettings);
  1366. end;
  1367. Function FloatToStr(Value: Int64; Const FormatSettings: TFormatSettings): String;
  1368. var
  1369. e: Extended;
  1370. Begin
  1371. e := Comp(Value);
  1372. Result := FloatToStrFIntl(e, ffGeneral, 15, 0, fvComp,FormatSettings);
  1373. End;
  1374. {$endif FPC_COMP_IS_INT64}
  1375. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): Longint;
  1376. Var
  1377. Tmp: String[40];
  1378. Begin
  1379. Tmp := FloatToStrF(Value, format, Precision, Digits,FormatSettings);
  1380. Result := Length(Tmp);
  1381. Move(Tmp[1], Buffer[0], Result);
  1382. End;
  1383. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  1384. begin
  1385. Result:=FloatToText(Buffer,Value,Format,Precision,Digits,DefaultFormatSettings);
  1386. end;
  1387. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1388. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1389. begin
  1390. Result := FloatToStrFIntl(value,format,precision,digits,fvExtended,FormatSettings);
  1391. end;
  1392. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  1393. begin
  1394. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1395. end;
  1396. {$endif}
  1397. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1398. begin
  1399. Result := FloatToStrFIntl(value,format,precision,digits,fvCurrency,FormatSettings);
  1400. end;
  1401. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer): String;
  1402. begin
  1403. Result:=FloatToStrF(Value,format,Precision,Digits,DefaultFormatSettings);
  1404. end;
  1405. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1406. var
  1407. e: Extended;
  1408. begin
  1409. e := Value;
  1410. result := FloatToStrFIntl(e,format,precision,digits,fvDouble,FormatSettings);
  1411. end;
  1412. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer): String;
  1413. begin
  1414. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1415. end;
  1416. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1417. var
  1418. e: Extended;
  1419. begin
  1420. e:=Value;
  1421. result := FloatToStrFIntl(e,format,precision,digits,fvSingle,FormatSettings);
  1422. end;
  1423. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer): String;
  1424. begin
  1425. Result:= FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1426. end;
  1427. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1428. var
  1429. e: Extended;
  1430. begin
  1431. e := Value;
  1432. Result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1433. end;
  1434. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer): String;
  1435. begin
  1436. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1437. end;
  1438. {$ifndef FPC_COMP_IS_INT64}
  1439. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  1440. var
  1441. e: Extended;
  1442. begin
  1443. e := Comp(Value);
  1444. result := FloatToStrFIntl(e,format,precision,digits,fvComp,FormatSettings);
  1445. end;
  1446. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer): String;
  1447. begin
  1448. Result:=FloatToStrF(Value,Format,Precision,Digits,DefaultFormatSettings);
  1449. end;
  1450. {$endif FPC_COMP_IS_INT64}
  1451. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer; Const FormatSettings: TFormatSettings): string;
  1452. begin
  1453. result:=FloatToStrF(Value,Format,19,Digits,FormatSettings);
  1454. end;
  1455. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
  1456. begin
  1457. Result:=CurrToStrF(Value,Format,Digits,DefaultFormatSettings);
  1458. end;
  1459. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  1460. begin
  1461. If (Value<MinDateTime) or (Value>MaxDateTime) then
  1462. Raise EConvertError.CreateFmt (SInvalidDateTime,[Value]);
  1463. Result:=Value;
  1464. end;
  1465. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  1466. begin
  1467. Result:=(Value>=MinCurrency) and (Value<=MaxCurrency);
  1468. if Result then
  1469. AResult := Value;
  1470. end;
  1471. function FloatToCurr(const Value: Extended): Currency;
  1472. begin
  1473. if not TryFloatToCurr(Value, Result) then
  1474. Raise EConvertError.CreateFmt(SInvalidCurrency, [FloatToStr(Value)]);
  1475. end;
  1476. Function CurrToStr(Value: Currency): string;
  1477. begin
  1478. Result:=FloatToStrF(Value,ffGeneral,-1,0);
  1479. end;
  1480. Function CurrToStr(Value: Currency; Const FormatSettings: TFormatSettings): string;
  1481. begin
  1482. Result:=FloatToStrF(Value,ffGeneral,-1,0,FormatSettings);
  1483. end;
  1484. function StrToCurr(const S: string): Currency;
  1485. begin
  1486. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1487. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1488. end;
  1489. function StrToCurr(const S: string; Const FormatSettings: TFormatSettings): Currency;
  1490. begin
  1491. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency,FormatSettings) then
  1492. Raise EConvertError.createfmt(SInValidFLoat,[S]);
  1493. end;
  1494. Function TryStrToCurr(Const S : String; Out Value: Currency): Boolean;
  1495. Begin
  1496. Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency);
  1497. End;
  1498. function TryStrToCurr(const S: string;Out Value : Currency; Const FormatSettings: TFormatSettings): Boolean;
  1499. Begin
  1500. Result := TextToFloat(PChar(pointer(S)), Value, fvCurrency,FormatSettings);
  1501. End;
  1502. function StrToCurrDef(const S: string; Default : Currency): Currency;
  1503. begin
  1504. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency) then
  1505. Result:=Default;
  1506. end;
  1507. function StrToCurrDef(const S: string; Default : Currency; Const FormatSettings: TFormatSettings): Currency;
  1508. begin
  1509. if not TextToFloat(PChar(pointer(S)), Result, fvCurrency,FormatSettings) then
  1510. Result:=Default;
  1511. end;
  1512. {$endif FPUNONE}
  1513. function AnsiDequotedStr(const S: string; AQuote: Char): string;
  1514. var p : pchar;
  1515. begin
  1516. p:=pchar(pointer(s)); // work around CONST. Ansiextract is safe for nil
  1517. result:=AnsiExtractquotedStr(p,AQuote);
  1518. if result='' Then
  1519. result:=s;
  1520. end;
  1521. function StrToBool(const S: string): Boolean;
  1522. begin
  1523. if not(TryStrToBool(S,Result)) then
  1524. Raise EConvertError.CreateFmt(SInvalidBoolean,[S]);
  1525. end;
  1526. procedure CheckBoolStrs;
  1527. begin
  1528. If Length(TrueBoolStrs)=0 then
  1529. begin
  1530. SetLength(TrueBoolStrs,1);
  1531. TrueBoolStrs[0]:='True';
  1532. end;
  1533. If Length(FalseBoolStrs)=0 then
  1534. begin
  1535. SetLength(FalseBoolStrs,1);
  1536. FalseBoolStrs[0]:='False';
  1537. end;
  1538. end;
  1539. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
  1540. begin
  1541. if UseBoolStrs Then
  1542. begin
  1543. CheckBoolStrs;
  1544. if B then
  1545. Result:=TrueBoolStrs[0]
  1546. else
  1547. Result:=FalseBoolStrs[0];
  1548. end
  1549. else
  1550. If B then
  1551. Result:='-1'
  1552. else
  1553. Result:='0';
  1554. end;
  1555. // from textmode IDE util funcs.
  1556. function BoolToStr(B: boolean; const TrueS, FalseS: string): string;
  1557. begin
  1558. if B then Result:=TrueS else BoolToStr:=FalseS;
  1559. end;
  1560. function StrToBoolDef(const S: string; Default: Boolean): Boolean;
  1561. begin
  1562. if not(TryStrToBool(S,Result)) then
  1563. Result:=Default;
  1564. end;
  1565. function TryStrToBool(const S: string; out Value: Boolean): Boolean;
  1566. Var
  1567. Temp : String;
  1568. I : Longint;
  1569. {$ifdef FPUNONE}
  1570. D : Longint;
  1571. {$else}
  1572. D : Double;
  1573. {$endif}
  1574. Code: word;
  1575. begin
  1576. Temp:=upcase(S);
  1577. Val(temp,D,code);
  1578. Result:=true;
  1579. If Code=0 then
  1580. {$ifdef FPUNONE}
  1581. Value:=(D<>0)
  1582. {$else}
  1583. Value:=(D<>0.0)
  1584. {$endif}
  1585. else
  1586. begin
  1587. CheckBoolStrs;
  1588. for I:=low(TrueBoolStrs) to High(TrueBoolStrs) do
  1589. if Temp=upcase(TrueBoolStrs[I]) then
  1590. begin
  1591. Value:=true;
  1592. exit;
  1593. end;
  1594. for I:=low(FalseBoolStrs) to High(FalseBoolStrs) do
  1595. if Temp=upcase(FalseBoolStrs[I]) then
  1596. begin
  1597. Value:=false;
  1598. exit;
  1599. end;
  1600. Result:=false;
  1601. end;
  1602. end;
  1603. {$ifndef FPUNONE}
  1604. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  1605. begin
  1606. Result:=FloatToTextFmt(Buffer,Value,Format,DefaultFormatSettings);
  1607. end;
  1608. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar;FormatSettings : TFormatSettings): Integer;
  1609. Var
  1610. Digits: String[40]; { String Of Digits }
  1611. Exponent: String[8]; { Exponent strin }
  1612. FmtStart, FmtStop: PChar; { Start And End Of relevant part }
  1613. { Of format String }
  1614. ExpFmt, ExpSize: Integer; { Type And Length Of }
  1615. { exponential format chosen }
  1616. Placehold: Array[1..4] Of Integer; { Number Of placeholders In All }
  1617. { four Sections }
  1618. thousand: Boolean; { thousand separators? }
  1619. UnexpectedDigits: Integer; { Number Of unexpected Digits that }
  1620. { have To be inserted before the }
  1621. { First placeholder. }
  1622. UnexpectedDigitsStart: Integer; { Location in Digits where first unexpected Digit is located }
  1623. DigitExponent: Integer; { Exponent Of First digit In }
  1624. { Digits Array. }
  1625. { Find end of format section starting at P. False, if empty }
  1626. Function GetSectionEnd(Var P: PChar): Boolean;
  1627. Var
  1628. C: Char;
  1629. SQ, DQ: Boolean;
  1630. Begin
  1631. Result := False;
  1632. SQ := False;
  1633. DQ := False;
  1634. C := P[0];
  1635. While (C<>#0) And ((C<>';') Or SQ Or DQ) Do
  1636. Begin
  1637. Result := True;
  1638. Case C Of
  1639. #34: If Not SQ Then DQ := Not DQ;
  1640. #39: If Not DQ Then SQ := Not SQ;
  1641. End;
  1642. Inc(P);
  1643. C := P[0];
  1644. End;
  1645. End;
  1646. { Find start and end of format section to apply. If section doesn't exist,
  1647. use section 1. If section 2 is used, the sign of value is ignored. }
  1648. Procedure GetSectionRange(section: Integer);
  1649. Var
  1650. Sec: Array[1..3] Of PChar;
  1651. SecOk: Array[1..3] Of Boolean;
  1652. Begin
  1653. Sec[1] := format;
  1654. SecOk[1] := GetSectionEnd(Sec[1]);
  1655. If section > 1 Then
  1656. Begin
  1657. Sec[2] := Sec[1];
  1658. If Sec[2][0] <> #0 Then
  1659. Inc(Sec[2]);
  1660. SecOk[2] := GetSectionEnd(Sec[2]);
  1661. If section > 2 Then
  1662. Begin
  1663. Sec[3] := Sec[2];
  1664. If Sec[3][0] <> #0 Then
  1665. Inc(Sec[3]);
  1666. SecOk[3] := GetSectionEnd(Sec[3]);
  1667. End;
  1668. End;
  1669. If Not SecOk[1] Then
  1670. FmtStart := Nil
  1671. Else
  1672. Begin
  1673. If Not SecOk[section] Then
  1674. section := 1
  1675. Else If section = 2 Then
  1676. Value := -Value; { Remove sign }
  1677. If section = 1 Then FmtStart := format Else
  1678. Begin
  1679. FmtStart := Sec[section - 1];
  1680. Inc(FmtStart);
  1681. End;
  1682. FmtStop := Sec[section];
  1683. End;
  1684. End;
  1685. { Find format section ranging from FmtStart to FmtStop. }
  1686. Procedure GetFormatOptions;
  1687. Var
  1688. Fmt: PChar;
  1689. SQ, DQ: Boolean;
  1690. area: Integer;
  1691. Begin
  1692. SQ := False;
  1693. DQ := False;
  1694. Fmt := FmtStart;
  1695. ExpFmt := 0;
  1696. area := 1;
  1697. thousand := False;
  1698. Placehold[1] := 0;
  1699. Placehold[2] := 0;
  1700. Placehold[3] := 0;
  1701. Placehold[4] := 0;
  1702. While Fmt < FmtStop Do
  1703. Begin
  1704. Case Fmt[0] Of
  1705. #34:
  1706. Begin
  1707. If Not SQ Then
  1708. DQ := Not DQ;
  1709. Inc(Fmt);
  1710. End;
  1711. #39:
  1712. Begin
  1713. If Not DQ Then
  1714. SQ := Not SQ;
  1715. Inc(Fmt);
  1716. End;
  1717. Else
  1718. { if not in quotes, then interpret}
  1719. If Not (SQ Or DQ) Then
  1720. Begin
  1721. Case Fmt[0] Of
  1722. '0':
  1723. Begin
  1724. Case area Of
  1725. 1:
  1726. area := 2;
  1727. 4:
  1728. Begin
  1729. area := 3;
  1730. Inc(Placehold[3], Placehold[4]);
  1731. Placehold[4] := 0;
  1732. End;
  1733. End;
  1734. Inc(Placehold[area]);
  1735. Inc(Fmt);
  1736. End;
  1737. '#':
  1738. Begin
  1739. If area=3 Then
  1740. area:=4;
  1741. Inc(Placehold[area]);
  1742. Inc(Fmt);
  1743. End;
  1744. '.':
  1745. Begin
  1746. If area<3 Then
  1747. area:=3;
  1748. Inc(Fmt);
  1749. End;
  1750. ',':
  1751. Begin
  1752. thousand := DefaultFormatSettings.ThousandSeparator<>#0;
  1753. Inc(Fmt);
  1754. End;
  1755. 'e', 'E':
  1756. If ExpFmt = 0 Then
  1757. Begin
  1758. If (Fmt[0]='E') Then
  1759. ExpFmt:=1
  1760. Else
  1761. ExpFmt := 3;
  1762. Inc(Fmt);
  1763. If (Fmt<FmtStop) Then
  1764. Begin
  1765. Case Fmt[0] Of
  1766. '+':
  1767. Begin
  1768. End;
  1769. '-':
  1770. Inc(ExpFmt);
  1771. Else
  1772. ExpFmt := 0;
  1773. End;
  1774. If ExpFmt <> 0 Then
  1775. Begin
  1776. Inc(Fmt);
  1777. ExpSize := 0;
  1778. While (Fmt<FmtStop) And
  1779. (ExpSize<4) And
  1780. (Fmt[0] In ['0'..'9']) Do
  1781. Begin
  1782. Inc(ExpSize);
  1783. Inc(Fmt);
  1784. End;
  1785. End;
  1786. End
  1787. Else
  1788. { just e/E without subsequent +/- -> not exponential format,
  1789. but we have to simply print e/E literally }
  1790. ExpFmt:=0;
  1791. End
  1792. Else
  1793. Inc(Fmt);
  1794. Else { Case }
  1795. Inc(Fmt);
  1796. End; { Case }
  1797. End { Begin }
  1798. Else
  1799. Inc(Fmt);
  1800. End; { Case }
  1801. End; { While .. Begin }
  1802. End;
  1803. Procedure FloatToStr;
  1804. Var
  1805. I, J, Exp, Width, Decimals, DecimalPoint, len: Integer;
  1806. Begin
  1807. If ExpFmt = 0 Then
  1808. Begin
  1809. { Fixpoint }
  1810. Decimals:=Placehold[3]+Placehold[4];
  1811. Width:=Placehold[1]+Placehold[2]+Decimals;
  1812. If (Decimals=0) Then
  1813. Str(Value:Width:0,Digits)
  1814. Else if Value>=0 then
  1815. Str(Value:Width+1:Decimals,Digits)
  1816. else
  1817. Str(Value:Width+2:Decimals,Digits);
  1818. len:=Length(Digits);
  1819. { Find the decimal point }
  1820. If (Decimals=0) Then
  1821. DecimalPoint:=len+1
  1822. Else
  1823. DecimalPoint:=len-Decimals;
  1824. { If value is very small, and no decimal places
  1825. are desired, remove the leading 0. }
  1826. If (Abs(Value) < 1) And (Placehold[2] = 0) Then
  1827. Begin
  1828. If (Placehold[1]=0) Then
  1829. Delete(Digits,DecimalPoint-1,1)
  1830. Else
  1831. Digits[DecimalPoint-1]:=' ';
  1832. End;
  1833. { Convert optional zeroes to spaces. }
  1834. I:=len;
  1835. J:=DecimalPoint+Placehold[3];
  1836. While (I>J) And (Digits[I]='0') Do
  1837. Begin
  1838. Digits[I] := ' ';
  1839. Dec(I);
  1840. End;
  1841. { If integer value and no obligatory decimal
  1842. places, remove decimal point. }
  1843. If (DecimalPoint < len) And (Digits[DecimalPoint + 1] = ' ') Then
  1844. Digits[DecimalPoint] := ' ';
  1845. { Convert spaces left from obligatory decimal point to zeroes.
  1846. MVC : If - sign is encountered, replace it too, and put at position 1}
  1847. I:=DecimalPoint-Placehold[2];
  1848. J:=0;
  1849. While (I<DecimalPoint) And (Digits[I] in [' ','-']) Do
  1850. Begin
  1851. If Digits[i]='-' then
  1852. J:=I;
  1853. Digits[I] := '0';
  1854. Inc(I);
  1855. End;
  1856. If (J<>0) then
  1857. Digits[1]:='-';
  1858. Exp := 0;
  1859. End
  1860. Else
  1861. Begin
  1862. { Scientific: exactly <Width> Digits With <Precision> Decimals
  1863. And adjusted Exponent. }
  1864. If Placehold[1]+Placehold[2]=0 Then
  1865. Placehold[1]:=1;
  1866. Decimals := Placehold[3] + Placehold[4];
  1867. Width:=Placehold[1]+Placehold[2]+Decimals;
  1868. { depending on the maximally supported precision, the exponent field }
  1869. { is longer/shorter }
  1870. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1871. Str(Value:Width+8,Digits);
  1872. {$else FPC_HAS_TYPE_EXTENDED}
  1873. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1874. Str(Value:Width+7,Digits);
  1875. {$else FPC_HAS_TYPE_DOUBLE}
  1876. Str(Value:Width+6,Digits);
  1877. {$endif FPC_HAS_TYPE_DOUBLE}
  1878. {$endif FPC_HAS_TYPE_EXTENDED}
  1879. { Find and cut out exponent. Always the
  1880. last 6 characters in the string.
  1881. -> 0000E+0000
  1882. *** No, not always the last 6 characters, this depends on
  1883. the maximally supported precision (JM)}
  1884. I:=Pos('E',Digits);
  1885. Val(Copy(Digits,I+1,255),Exp,J);
  1886. Exp:=Exp+1-(Placehold[1]+Placehold[2]);
  1887. Delete(Digits, I, 255);
  1888. { Str() always returns at least one digit after the decimal point.
  1889. If we don't want it, we have to remove it. }
  1890. If (Decimals=0) And (Placehold[1]+Placehold[2]<= 1) Then
  1891. Begin
  1892. If (Digits[4]>='5') Then
  1893. Begin
  1894. Inc(Digits[2]);
  1895. If (Digits[2]>'9') Then
  1896. Begin
  1897. Digits[2] := '1';
  1898. Inc(Exp);
  1899. End;
  1900. End;
  1901. Delete(Digits, 3, 2);
  1902. DecimalPoint := Length(Digits) + 1;
  1903. End
  1904. Else
  1905. Begin
  1906. { Move decimal point at the desired position }
  1907. Delete(Digits, 3, 1);
  1908. DecimalPoint:=2+Placehold[1]+Placehold[2];
  1909. If (Decimals<>0) Then
  1910. Insert('.',Digits,DecimalPoint);
  1911. End;
  1912. { Convert optional zeroes to spaces. }
  1913. I := Length(Digits);
  1914. J := DecimalPoint + Placehold[3];
  1915. While (I > J) And (Digits[I] = '0') Do
  1916. Begin
  1917. Digits[I] := ' ';
  1918. Dec(I);
  1919. End;
  1920. { If integer number and no obligatory decimal paces, remove decimal point }
  1921. If (DecimalPoint<Length(Digits)) And
  1922. (Digits[DecimalPoint+1]=' ') Then
  1923. Digits[DecimalPoint]:=' ';
  1924. If (Digits[1]=' ') Then
  1925. Begin
  1926. Delete(Digits, 1, 1);
  1927. Dec(DecimalPoint);
  1928. End;
  1929. { Calculate exponent string }
  1930. Str(Abs(Exp), Exponent);
  1931. While Length(Exponent)<ExpSize Do
  1932. Insert('0',Exponent,1);
  1933. If Exp >= 0 Then
  1934. Begin
  1935. If (ExpFmt In [1,3]) Then
  1936. Insert('+', Exponent, 1);
  1937. End
  1938. Else
  1939. Insert('-',Exponent,1);
  1940. If (ExpFmt<3) Then
  1941. Insert('E',Exponent,1)
  1942. Else
  1943. Insert('e',Exponent,1);
  1944. End;
  1945. DigitExponent:=DecimalPoint-2;
  1946. I:=1;
  1947. While (I<=Length(Digits)) and (Digits[i] in [' ','-']) do
  1948. begin
  1949. Dec(DigitExponent);
  1950. Inc(i);
  1951. end;
  1952. UnexpectedDigits:=DecimalPoint-1-(Placehold[1]+Placehold[2]);
  1953. End;
  1954. Function PutResult: LongInt;
  1955. Var
  1956. SQ, DQ: Boolean;
  1957. Fmt, Buf: PChar;
  1958. Dig, N: Integer;
  1959. Begin
  1960. SQ := False;
  1961. DQ := False;
  1962. Fmt := FmtStart;
  1963. Buf := Buffer;
  1964. Dig := 1;
  1965. While (Fmt<FmtStop) Do
  1966. Begin
  1967. // WriteLn('Treating : "',Fmt[0],'"');
  1968. Case Fmt[0] Of
  1969. #34:
  1970. Begin
  1971. If Not SQ Then
  1972. DQ := Not DQ;
  1973. Inc(Fmt);
  1974. End;
  1975. #39:
  1976. Begin
  1977. If Not DQ Then
  1978. SQ := Not SQ;
  1979. Inc(Fmt);
  1980. End;
  1981. Else
  1982. If Not (SQ Or DQ) Then
  1983. Begin
  1984. Case Fmt[0] Of
  1985. '0', '#', '.':
  1986. Begin
  1987. If (Dig=1) And (UnexpectedDigits>0) Then
  1988. Begin
  1989. { Everything unexpected is written before the first digit }
  1990. For N := 1 To UnexpectedDigits Do
  1991. Begin
  1992. if (Digits[N]<>' ') Then
  1993. begin
  1994. Buf[0] := Digits[N];
  1995. Inc(Buf);
  1996. end;
  1997. If thousand And (Not (Digits[N] in [' ','-'])) Then
  1998. Begin
  1999. If (DigitExponent Mod 3 = 0) And (DigitExponent>0) Then
  2000. Begin
  2001. Buf[0] := FormatSettings.ThousandSeparator;
  2002. Inc(Buf);
  2003. End;
  2004. Dec(DigitExponent);
  2005. End;
  2006. End;
  2007. Inc(Dig, UnexpectedDigits);
  2008. End;
  2009. If (Digits[Dig]<>' ') Then
  2010. Begin
  2011. If (Digits[Dig]='.') Then
  2012. Buf[0] := FormatSettings.DecimalSeparator
  2013. Else
  2014. Buf[0] := Digits[Dig];
  2015. Inc(Buf);
  2016. If thousand And (DigitExponent Mod 3 = 0) And (DigitExponent > 0) and (Digits[Dig]<>'-') Then
  2017. Begin
  2018. Buf[0] := FormatSettings.ThousandSeparator;
  2019. Inc(Buf);
  2020. End;
  2021. End;
  2022. if Digits[Dig]<>'-' then
  2023. Dec(DigitExponent);
  2024. Inc(Dig);
  2025. Inc(Fmt);
  2026. End;
  2027. 'e', 'E':
  2028. Begin
  2029. If ExpFmt <> 0 Then
  2030. Begin
  2031. Inc(Fmt);
  2032. If Fmt < FmtStop Then
  2033. Begin
  2034. If Fmt[0] In ['+', '-'] Then
  2035. Begin
  2036. Inc(Fmt, ExpSize);
  2037. For N:=1 To Length(Exponent) Do
  2038. Buf[N-1] := Exponent[N];
  2039. Inc(Buf,Length(Exponent));
  2040. ExpFmt:=0;
  2041. End;
  2042. Inc(Fmt);
  2043. End;
  2044. End
  2045. Else
  2046. Begin
  2047. { No legal exponential format.
  2048. Simply write the 'E' to the result. }
  2049. Buf[0] := Fmt[0];
  2050. Inc(Buf);
  2051. Inc(Fmt);
  2052. End;
  2053. End;
  2054. Else { Case }
  2055. { Usual character }
  2056. If (Fmt[0]<>',') Then
  2057. Begin
  2058. Buf[0] := Fmt[0];
  2059. Inc(Buf);
  2060. End;
  2061. Inc(Fmt);
  2062. End; { Case }
  2063. End
  2064. Else { IF }
  2065. Begin
  2066. { Character inside single or double quotes }
  2067. Buf[0] := Fmt[0];
  2068. Inc(Buf);
  2069. Inc(Fmt);
  2070. End;
  2071. End; { Case }
  2072. End; { While .. Begin }
  2073. Result:=PtrUInt(Buf)-PtrUInt(Buffer);
  2074. End;
  2075. Begin
  2076. If (Value>0) Then
  2077. GetSectionRange(1)
  2078. Else If (Value<0) Then
  2079. GetSectionRange(2)
  2080. Else
  2081. GetSectionRange(3);
  2082. If FmtStart = Nil Then
  2083. Begin
  2084. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings);
  2085. End
  2086. Else
  2087. Begin
  2088. GetFormatOptions;
  2089. If (ExpFmt = 0) And (Abs(Value) >= 1E18) Then
  2090. Result := FloatToText(Buffer, Value, ffGeneral, 15, 4, FormatSettings)
  2091. Else
  2092. Begin
  2093. FloatToStr;
  2094. Result := PutResult;
  2095. End;
  2096. End;
  2097. End;
  2098. Procedure FloatToDecimal(Out Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals : integer);
  2099. var
  2100. Buffer: String[254]; //Though str func returns only 25 chars, this might change in the future
  2101. InfNan: string[3];
  2102. Error, N, L, Start, C: Integer;
  2103. GotNonZeroBeforeDot, BeforeDot : boolean;
  2104. begin
  2105. case ValueType of
  2106. fvExtended:
  2107. Str(Extended(Value):25, Buffer);
  2108. fvDouble,
  2109. fvReal:
  2110. Str(Double(Value):23, Buffer);
  2111. fvSingle:
  2112. Str(Single(Value):16, Buffer);
  2113. fvCurrency:
  2114. Str(Currency(Value):25, Buffer);
  2115. fvComp:
  2116. Str(Currency(Value):23, Buffer);
  2117. end;
  2118. N := 1;
  2119. L := Byte(Buffer[0]);
  2120. while Buffer[N]=' ' do
  2121. Inc(N);
  2122. Result.Negative := (Buffer[N] = '-');
  2123. if Result.Negative then
  2124. Inc(N)
  2125. else if (Buffer[N] = '+') then
  2126. inc(N);
  2127. { special cases for Inf and Nan }
  2128. if (L>=N+2) then
  2129. begin
  2130. InfNan:=copy(Buffer,N,3);
  2131. if (InfNan='Inf') then
  2132. begin
  2133. Result.Digits[0]:=#0;
  2134. Result.Exponent:=32767;
  2135. exit
  2136. end;
  2137. if (InfNan='Nan') then
  2138. begin
  2139. Result.Digits[0]:=#0;
  2140. Result.Exponent:=-32768;
  2141. exit
  2142. end;
  2143. end;
  2144. Start := N; //Start of digits
  2145. Result.Exponent := 0; BeforeDot := true;
  2146. GotNonZeroBeforeDot := false;
  2147. while (L>=N) and (Buffer[N]<>'E') do
  2148. begin
  2149. if Buffer[N]='.' then
  2150. BeforeDot := false
  2151. else
  2152. begin
  2153. if BeforeDot then
  2154. begin // Currently this is always 1 char
  2155. Inc(Result.Exponent);
  2156. Result.Digits[N-Start] := Buffer[N];
  2157. if Buffer[N] <> '0' then
  2158. GotNonZeroBeforeDot := true;
  2159. end
  2160. else
  2161. Result.Digits[N-Start-1] := Buffer[N]
  2162. end;
  2163. Inc(N);
  2164. end;
  2165. Inc(N); // Pass through 'E'
  2166. if N<=L then
  2167. begin
  2168. Val(Copy(Buffer, N, L-N+1), C, Error); // Get exponent after 'E'
  2169. Inc(Result.Exponent, C);
  2170. end;
  2171. // Calculate number of digits we have from str
  2172. if BeforeDot then
  2173. N := N - Start - 1
  2174. else
  2175. N := N - Start - 2;
  2176. L := SizeOf(Result.Digits);
  2177. if N<L then
  2178. FillChar(Result.Digits[N], L-N, '0'); //Zero remaining space
  2179. if Decimals + Result.Exponent < Precision Then //After this it is the same as in FloatToDecimal
  2180. N := Decimals + Result.Exponent
  2181. Else
  2182. N := Precision;
  2183. if N >= L Then
  2184. N := L-1;
  2185. if N = 0 Then
  2186. begin
  2187. if Result.Digits[0] >= '5' Then
  2188. begin
  2189. Result.Digits[0] := '1';
  2190. Result.Digits[1] := #0;
  2191. Inc(Result.Exponent);
  2192. end
  2193. Else
  2194. Result.Digits[0] := #0;
  2195. end //N=0
  2196. Else if N > 0 Then
  2197. begin
  2198. if Result.Digits[N] >= '5' Then
  2199. begin
  2200. Repeat
  2201. Result.Digits[N] := #0;
  2202. Dec(N);
  2203. Inc(Result.Digits[N]);
  2204. Until (N = 0) Or (Result.Digits[N] < ':');
  2205. If Result.Digits[0] = ':' Then
  2206. begin
  2207. Result.Digits[0] := '1';
  2208. Inc(Result.Exponent);
  2209. end;
  2210. end
  2211. Else
  2212. begin
  2213. Result.Digits[N] := '0';
  2214. While (N > -1) And (Result.Digits[N] = '0') Do
  2215. begin
  2216. Result.Digits[N] := #0;
  2217. Dec(N);
  2218. end;
  2219. end;
  2220. end //N>0
  2221. Else
  2222. Result.Digits[0] := #0;
  2223. if (Result.Digits[0] = #0) and
  2224. not GotNonZeroBeforeDot then
  2225. begin
  2226. Result.Exponent := 0;
  2227. Result.Negative := False;
  2228. end;
  2229. end;
  2230. Procedure FloatToDecimal(Out Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  2231. begin
  2232. FloatToDecimal(Result,Value,fvExtended,Precision,Decimals);
  2233. end;
  2234. Function FormatFloat(Const Format : String; Value : Extended; Const FormatSettings: TFormatSettings) : String;
  2235. Var
  2236. buf : Array[0..1024] of char;
  2237. Begin // not changed to pchar(pointer(). Possibly not safe
  2238. Buf[FloatToTextFmt(@Buf[0],Value,Pchar(Format),FormatSettings)]:=#0;
  2239. Result:=StrPas(@Buf[0]);
  2240. End;
  2241. Function FormatFloat(Const format: String; Value: Extended): String;
  2242. begin
  2243. Result:=FormatFloat(Format,Value,DefaultFormatSettings);
  2244. end;
  2245. Function FormatCurr(const Format: string; Value: Currency; Const FormatSettings: TFormatSettings): string;
  2246. begin
  2247. Result := FormatFloat(Format, Value,FormatSettings);
  2248. end;
  2249. function FormatCurr(const Format: string; Value: Currency): string;
  2250. begin
  2251. Result:=FormatCurr(Format,Value,DefaultFormatSettings);
  2252. end;
  2253. {$endif}
  2254. {==============================================================================}
  2255. { extra functions }
  2256. {==============================================================================}
  2257. { LeftStr returns Count left-most characters from S }
  2258. function LeftStr(const S: string; Count: integer): string;
  2259. begin
  2260. result := Copy(S, 1, Count);
  2261. end ;
  2262. { RightStr returns Count right-most characters from S }
  2263. function RightStr(const S: string; Count: integer): string;
  2264. begin
  2265. If Count>Length(S) then
  2266. Count:=Length(S);
  2267. result := Copy(S, 1 + Length(S) - Count, Count);
  2268. end;
  2269. { BCDToInt converts the BCD value Value to an integer }
  2270. function BCDToInt(Value: integer): integer;
  2271. var i, j, digit: integer;
  2272. begin
  2273. result := 0;
  2274. j := 1;
  2275. for i := 0 to SizeOf(Value) shl 1 - 1 do begin
  2276. digit := Value and 15;
  2277. if digit > $9 then
  2278. begin
  2279. if i = 0 then
  2280. begin
  2281. if digit in [$B, $D] then j := -1
  2282. end
  2283. else raise EConvertError.createfmt(SInvalidBCD,[Value]);
  2284. end
  2285. else
  2286. begin
  2287. result := result + j * digit;
  2288. j := j * 10;
  2289. end ;
  2290. Value := Value shr 4;
  2291. end ;
  2292. end ;
  2293. Function LastDelimiter(const Delimiters, S: string): Integer;
  2294. var
  2295. chs: TSysCharSet;
  2296. I: LongInt;
  2297. begin
  2298. chs := [];
  2299. for I := 1 to Length(Delimiters) do
  2300. Include(chs, Delimiters[I]);
  2301. Result:=Length(S);
  2302. While (Result>0) and not (S[Result] in chs) do
  2303. Dec(Result);
  2304. end;
  2305. {$macro on}
  2306. {$define INSTRINGREPLACE}
  2307. {$define SRString:=String}
  2308. {$define SRUpperCase:=AnsiUppercase}
  2309. {$define SRPCHAR:=PChar}
  2310. {$define SRCHAR:=Char}
  2311. Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  2312. {$i syssr.inc}
  2313. {$undef INSTRINGREPLACE}
  2314. {$undef SRString}
  2315. {$undef SRUpperCase}
  2316. {$undef SRPCHAR}
  2317. {$undef SRCHAR}
  2318. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  2319. begin
  2320. Result:=False;
  2321. If (Index>0) and (Index<=Length(S)) then
  2322. Result:=Pos(S[Index],Delimiters)<>0; // Note we don't do MBCS yet
  2323. end;
  2324. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  2325. begin
  2326. Result:=Length(S);
  2327. If Result>MaxLen then
  2328. Result:=MaxLen;
  2329. end;
  2330. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  2331. begin
  2332. Result:=Index;
  2333. end;
  2334. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  2335. begin
  2336. Result:=Length(S);
  2337. If Result>MaxLen then
  2338. Result:=MaxLen;
  2339. end;
  2340. Function CharToByteIndex(const S: string; Index: Integer): Integer;
  2341. begin
  2342. Result:=Index;
  2343. end;
  2344. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  2345. begin
  2346. Result:=mbSingleByte;
  2347. end;
  2348. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  2349. begin
  2350. Result:=mbSingleByte;
  2351. end;
  2352. Function StrCharLength(const Str: PChar): Integer;
  2353. begin
  2354. result:=widestringmanager.CharLengthPCharProc(Str);
  2355. end;
  2356. function StrNextChar(const Str: PChar): PChar;
  2357. begin
  2358. result:=Str+StrCharLength(Str);
  2359. end;
  2360. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  2361. Var
  2362. I,L : Integer;
  2363. S,T : String;
  2364. begin
  2365. Result:=False;
  2366. S:=Switch;
  2367. If IgnoreCase then
  2368. S:=UpperCase(S);
  2369. I:=ParamCount;
  2370. While (Not Result) and (I>0) do
  2371. begin
  2372. L:=Length(Paramstr(I));
  2373. If (L>0) and (ParamStr(I)[1] in Chars) then
  2374. begin
  2375. T:=Copy(ParamStr(I),2,L-1);
  2376. If IgnoreCase then
  2377. T:=UpperCase(T);
  2378. Result:=S=T;
  2379. end;
  2380. Dec(i);
  2381. end;
  2382. end;
  2383. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  2384. begin
  2385. Result:=FindCmdLineSwitch(Switch,SwitchChars,IgnoreCase);
  2386. end;
  2387. Function FindCmdLineSwitch(const Switch: string): Boolean;
  2388. begin
  2389. Result:=FindCmdLineSwitch(Switch,SwitchChars,False);
  2390. end;
  2391. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  2392. const
  2393. Quotes = ['''', '"'];
  2394. Var
  2395. L : String;
  2396. C,LQ,BC : Char;
  2397. P,BLen,Len : Integer;
  2398. HB,IBC : Boolean;
  2399. begin
  2400. Result:='';
  2401. L:=Line;
  2402. Blen:=Length(BreakStr);
  2403. If (BLen>0) then
  2404. BC:=BreakStr[1]
  2405. else
  2406. BC:=#0;
  2407. Len:=Length(L);
  2408. While (Len>0) do
  2409. begin
  2410. P:=1;
  2411. LQ:=#0;
  2412. HB:=False;
  2413. IBC:=False;
  2414. While ((P<=Len) and ((P<=MaxCol) or not IBC)) and ((LQ<>#0) or Not HB) do
  2415. begin
  2416. C:=L[P];
  2417. If (C=LQ) then
  2418. LQ:=#0
  2419. else If (C in Quotes) then
  2420. LQ:=C;
  2421. If (LQ<>#0) then
  2422. Inc(P)
  2423. else
  2424. begin
  2425. HB:=((C=BC) and (BreakStr=Copy(L,P,BLen)));
  2426. If HB then
  2427. Inc(P,Blen)
  2428. else
  2429. begin
  2430. If (P>MaxCol) then
  2431. IBC:=C in BreakChars;
  2432. Inc(P);
  2433. end;
  2434. end;
  2435. // Writeln('"',C,'" : IBC : ',IBC,' HB : ',HB,' LQ : ',LQ,' P>MaxCol : ',P>MaxCol);
  2436. end;
  2437. Result:=Result+Copy(L,1,P-1);
  2438. If Not HB then
  2439. Result:=Result+BreakStr;
  2440. Delete(L,1,P-1);
  2441. Len:=Length(L);
  2442. end;
  2443. end;
  2444. function WrapText(const Line: string; MaxCol: Integer): string;
  2445. begin
  2446. Result:=WrapText(Line,sLineBreak, [' ', '-', #9], MaxCol);
  2447. end;
  2448. {$ifndef FPC_NOGENERICANSIROUTINES}
  2449. {
  2450. Case Translation Tables
  2451. Can be used in internationalization support.
  2452. Although these tables can be obtained through system calls
  2453. cd it is better to not use those, since most implementation are not 100%
  2454. WARNING:
  2455. before modifying a translation table make sure that the current codepage
  2456. of the OS corresponds to the one you make changes to
  2457. }
  2458. const
  2459. { upper case translation table for character set 850 }
  2460. CP850UCT: array[128..255] of char =
  2461. (#128,#154,#144,#182,#142,#182,#143,#128,#210,#211,#212,#216,#215,#222,#142,#143,
  2462. #144,#146,#146,#226,#153,#227,#234,#235,'Y',#153,#154,#157,#156,#157,#158,#159,
  2463. #181,#214,#224,#233,#165,#165,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2464. #176,#177,#178,#179,#180,#181,#182,#183,#184,#185,#186,#187,#188,#189,#190,#191,
  2465. #192,#193,#194,#195,#196,#197,#199,#199,#200,#201,#202,#203,#204,#205,#206,#207,
  2466. #208,#209,#210,#211,#212,#213,#214,#215,#216,#217,#218,#219,#220,#221,#222,#223,
  2467. #224,#225,#226,#227,#229,#229,#230,#237,#232,#233,#234,#235,#237,#237,#238,#239,
  2468. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2469. { lower case translation table for character set 850 }
  2470. CP850LCT: array[128..255] of char =
  2471. (#135,#129,#130,#131,#132,#133,#134,#135,#136,#137,#138,#139,#140,#141,#132,#134,
  2472. #130,#145,#145,#147,#148,#149,#150,#151,#152,#148,#129,#155,#156,#155,#158,#159,
  2473. #160,#161,#162,#163,#164,#164,#166,#167,#168,#169,#170,#171,#172,#173,#174,#175,
  2474. #176,#177,#178,#179,#180,#160,#131,#133,#184,#185,#186,#187,#188,#189,#190,#191,
  2475. #192,#193,#194,#195,#196,#197,#198,#198,#200,#201,#202,#203,#204,#205,#206,#207,
  2476. #208,#209,#136,#137,#138,#213,#161,#140,#139,#217,#218,#219,#220,#221,#141,#223,
  2477. #162,#225,#147,#149,#228,#228,#230,#237,#232,#163,#150,#151,#236,#236,#238,#239,
  2478. #240,#241,#242,#243,#244,#245,#246,#247,#248,#249,#250,#251,#252,#253,#254,#255);
  2479. { upper case translation table for character set ISO 8859/1 Latin 1 }
  2480. CPISO88591UCT: array[192..255] of char =
  2481. ( #192, #193, #194, #195, #196, #197, #198, #199,
  2482. #200, #201, #202, #203, #204, #205, #206, #207,
  2483. #208, #209, #210, #211, #212, #213, #214, #215,
  2484. #216, #217, #218, #219, #220, #221, #222, #223,
  2485. #192, #193, #194, #195, #196, #197, #198, #199,
  2486. #200, #201, #202, #203, #204, #205, #206, #207,
  2487. #208, #209, #210, #211, #212, #213, #214, #247,
  2488. #216, #217, #218, #219, #220, #221, #222, #89 );
  2489. { lower case translation table for character set ISO 8859/1 Latin 1 }
  2490. CPISO88591LCT: array[192..255] of char =
  2491. ( #224, #225, #226, #227, #228, #229, #230, #231,
  2492. #232, #233, #234, #235, #236, #237, #238, #239,
  2493. #240, #241, #242, #243, #244, #245, #246, #215,
  2494. #248, #249, #250, #251, #252, #253, #254, #223,
  2495. #224, #225, #226, #227, #228, #229, #230, #231,
  2496. #232, #233, #234, #235, #236, #237, #238, #239,
  2497. #240, #241, #242, #243, #244, #245, #246, #247,
  2498. #248, #249, #250, #251, #252, #253, #254, #255 );
  2499. {$endif FPC_NOGENERICANSIROUTINES}
  2500. function sscanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  2501. var
  2502. i,j,n,m : SizeInt;
  2503. s1 : string;
  2504. function GetInt(unsigned : boolean=false) : Integer;
  2505. begin
  2506. s1 := '';
  2507. while (Length(s) > n) and (s[n] = ' ') do
  2508. inc(n);
  2509. { read sign }
  2510. if (Length(s)>= n) and (s[n] in ['+', '-']) then
  2511. begin
  2512. { don't accept - when reading unsigned }
  2513. if unsigned and (s[n]='-') then
  2514. begin
  2515. result:=length(s1);
  2516. exit;
  2517. end
  2518. else
  2519. begin
  2520. s1:=s1+s[n];
  2521. inc(n);
  2522. end;
  2523. end;
  2524. { read numbers }
  2525. while (Length(s) >= n) and
  2526. (s[n] in ['0'..'9']) do
  2527. begin
  2528. s1 := s1+s[n];
  2529. inc(n);
  2530. end;
  2531. Result := Length(s1);
  2532. end;
  2533. function GetFloat : Integer;
  2534. begin
  2535. s1 := '';
  2536. while (Length(s) > n) and (s[n] = ' ') do
  2537. inc(n);
  2538. while (Length(s) >= n) and
  2539. (s[n] in ['0'..'9', '+', '-', FormatSettings.DecimalSeparator, 'e', 'E']) do
  2540. begin
  2541. s1 := s1+s[n];
  2542. inc(n);
  2543. end;
  2544. Result := Length(s1);
  2545. end;
  2546. function GetString : Integer;
  2547. begin
  2548. s1 := '';
  2549. while (Length(s) > n) and (s[n] = ' ') do
  2550. inc(n);
  2551. while (Length(s) >= n) and (s[n] <> ' ')do
  2552. begin
  2553. s1 := s1+s[n];
  2554. inc(n);
  2555. end;
  2556. Result := Length(s1);
  2557. end;
  2558. function ScanStr(c : Char) : Boolean;
  2559. begin
  2560. while (Length(s) > n) and (s[n] <> c) do
  2561. inc(n);
  2562. inc(n);
  2563. If (n <= Length(s)) then
  2564. Result := True
  2565. else
  2566. Result := False;
  2567. end;
  2568. function GetFmt : Integer;
  2569. begin
  2570. Result := -1;
  2571. while true do
  2572. begin
  2573. while (Length(fmt) > m) and (fmt[m] = ' ') do
  2574. inc(m);
  2575. if (m >= Length(fmt)) then
  2576. break;
  2577. if (fmt[m] = '%') then
  2578. begin
  2579. inc(m);
  2580. case fmt[m] of
  2581. 'd':
  2582. Result:=vtInteger;
  2583. {$ifndef FPUNONE}
  2584. 'f':
  2585. Result:=vtExtended;
  2586. {$endif}
  2587. 's':
  2588. Result:=vtString;
  2589. 'c':
  2590. Result:=vtChar;
  2591. else
  2592. raise EFormatError.CreateFmt(SInvalidFormat,[fmt]);
  2593. end;
  2594. inc(m);
  2595. break;
  2596. end;
  2597. if not(ScanStr(fmt[m])) then
  2598. break;
  2599. inc(m);
  2600. end;
  2601. end;
  2602. begin
  2603. n := 1;
  2604. m := 1;
  2605. Result := 0;
  2606. for i:=0 to High(Pointers) do
  2607. begin
  2608. j := GetFmt;
  2609. case j of
  2610. vtInteger :
  2611. begin
  2612. if GetInt>0 then
  2613. begin
  2614. pLongint(Pointers[i])^:=StrToInt(s1);
  2615. inc(Result);
  2616. end
  2617. else
  2618. break;
  2619. end;
  2620. vtchar :
  2621. begin
  2622. if Length(s)>n then
  2623. begin
  2624. pchar(Pointers[i])^:=s[n];
  2625. inc(n);
  2626. inc(Result);
  2627. end
  2628. else
  2629. break;
  2630. end;
  2631. {$ifndef FPUNONE}
  2632. vtExtended :
  2633. begin
  2634. if GetFloat>0 then
  2635. begin
  2636. pextended(Pointers[i])^:=StrToFloat(s1);
  2637. inc(Result);
  2638. end
  2639. else
  2640. break;
  2641. end;
  2642. {$endif}
  2643. vtString :
  2644. begin
  2645. if GetString > 0 then
  2646. begin
  2647. pansistring(Pointers[i])^:=s1;
  2648. inc(Result);
  2649. end
  2650. else
  2651. break;
  2652. end;
  2653. else
  2654. break;
  2655. end;
  2656. end;
  2657. end;