sysstr.inc 78 KB

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