sysstr.inc 78 KB

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