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