sysstr.inc 78 KB

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