sysstr.inc 79 KB

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