sysstr.inc 75 KB

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