sysstr.inc 78 KB

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