JclAnsiStrings.pas 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275
  1. {**************************************************************************************************}
  2. { }
  3. { Project JEDI Code Library (JCL) }
  4. { }
  5. { The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
  6. { you may not use this file except in compliance with the License. You may obtain a copy of the }
  7. { License at http://www.mozilla.org/MPL/ }
  8. { }
  9. { Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
  10. { ANY KIND, either express or implied. See the License for the specific language governing rights }
  11. { and limitations under the License. }
  12. { }
  13. { The Original Code is JclStrings.pas. }
  14. { }
  15. { The Initial Developer of the Original Code is Marcel van Brakel. }
  16. { Portions created by Marcel van Brakel are Copyright (C) Marcel van Brakel. All rights reserved. }
  17. { }
  18. { Contributor(s): }
  19. { Alexander Radchenko }
  20. { Andreas Hausladen (ahuser) }
  21. { Anthony Steele }
  22. { Azret Botash }
  23. { Barry Kelly }
  24. { Huanlin Tsai }
  25. { Jack N.A. Bakker }
  26. { Jean-Fabien Connault (cycocrew) }
  27. { John C Molyneux }
  28. { Leonard Wennekers }
  29. { Martin Kimmings }
  30. { Martin Kubecka }
  31. { Massimo Maria Ghisalberti }
  32. { Matthias Thoma (mthoma) }
  33. { Michael Winter }
  34. { Nick Hodges }
  35. { Olivier Sannier (obones) }
  36. { Patrick Kolla }
  37. { Pelle F. S. Liljendal }
  38. { Petr Vones (pvones) }
  39. { Robert Lee }
  40. { Robert Marquardt (marquardt) }
  41. { Robert Rossmair (rrossmair) }
  42. { Andreas Schmidt }
  43. { }
  44. {**************************************************************************************************}
  45. { }
  46. { Various character and string routines (searching, testing and transforming) }
  47. { }
  48. {**************************************************************************************************}
  49. { }
  50. { Last modified: $Date:: $ }
  51. { Revision: $Rev:: $ }
  52. { Author: $Author:: $ }
  53. { }
  54. {**************************************************************************************************}
  55. unit JclAnsiStrings;
  56. {$I jcl.inc}
  57. interface
  58. uses
  59. {$IFDEF UNITVERSIONING}
  60. JclUnitVersioning,
  61. {$ENDIF UNITVERSIONING}
  62. {$IFDEF HAS_UNITSCOPE}
  63. {$IFDEF MSWINDOWS}
  64. Winapi.Windows,
  65. {$ENDIF MSWINDOWS}
  66. System.Classes, System.SysUtils,
  67. {$IFDEF HAS_UNIT_ANSISTRINGS}
  68. System.AnsiStrings,
  69. {$ENDIF HAS_UNIT_ANSISTRINGS}
  70. {$ELSE ~HAS_UNITSCOPE}
  71. {$IFDEF MSWINDOWS}
  72. Windows,
  73. {$ENDIF MSWINDOWS}
  74. Classes, SysUtils,
  75. {$IFDEF HAS_UNIT_ANSISTRINGS}
  76. AnsiStrings,
  77. {$ENDIF HAS_UNIT_ANSISTRINGS}
  78. {$ENDIF ~HAS_UNITSCOPE}
  79. JclBase;
  80. // Ansi types
  81. type
  82. {$IFDEF SUPPORTS_UNICODE}
  83. TJclAnsiStringList = class;
  84. // Codegear should be the one providing this class, in the AnsiStrings unit.
  85. // It has been requested in QC 65630 but this was closed as "won't do".
  86. // So we are providing here a very light implementation that is designed
  87. // to provide the basics, and in no way be a "copy/paste" of what is in the RTL.
  88. TJclAnsiStrings = class(TPersistent)
  89. private
  90. FDelimiter: AnsiChar;
  91. FNameValueSeparator: AnsiChar;
  92. FStrictDelimiter: Boolean;
  93. FQuoteChar: AnsiChar;
  94. FUpdateCount: Integer;
  95. function GetText: AnsiString;
  96. procedure SetText(const Value: AnsiString);
  97. function GetCommaText: AnsiString; {$IFDEF SUPPORTS_INLINE}inline;{$ENDIF}
  98. procedure SetCommaText(const Value: AnsiString); {$IFDEF SUPPORTS_INLINE}inline;{$ENDIF}
  99. function GetDelimitedText: AnsiString; overload; {$IFDEF SUPPORTS_INLINE}inline;{$ENDIF}
  100. function GetDelimitedText(const ADelimiter: AnsiString; AQuoteChar: AnsiChar): AnsiString; overload;
  101. procedure SetDelimitedText(const Value: AnsiString); overload; {$IFDEF SUPPORTS_INLINE}inline;{$ENDIF}
  102. procedure SetDelimitedText(const Value, ADelimiter: AnsiString; AQuoteChar: AnsiChar); overload;
  103. function ExtractName(const S: AnsiString): AnsiString;
  104. function GetName(Index: Integer): AnsiString;
  105. function GetValue(const Name: AnsiString): AnsiString;
  106. procedure SetValue(const Name, Value: AnsiString);
  107. function GetValueFromIndex(Index: Integer): AnsiString;
  108. procedure SetValueFromIndex(Index: Integer; const Value: AnsiString);
  109. protected
  110. procedure AssignTo(Dest: TPersistent); override;
  111. procedure Error(const Msg: string; Data: Integer); overload;
  112. procedure Error(Msg: PResStringRec; Data: Integer); overload;
  113. function GetString(Index: Integer): AnsiString; virtual; abstract;
  114. procedure SetString(Index: Integer; const Value: AnsiString); virtual; abstract;
  115. function GetObject(Index: Integer): TObject; virtual; abstract;
  116. procedure SetObject(Index: Integer; AObject: TObject); virtual; abstract;
  117. function GetCapacity: Integer; virtual;
  118. procedure SetCapacity(const Value: Integer); virtual;
  119. function GetCount: Integer; virtual; abstract;
  120. function CompareStrings(const S1, S2: AnsiString): Integer; virtual;
  121. procedure SetUpdateState(Updating: Boolean); virtual;
  122. property UpdateCount: Integer read FUpdateCount;
  123. public
  124. constructor Create;
  125. procedure Assign(Source: TPersistent); override;
  126. function Add(const S: AnsiString): Integer; virtual;
  127. function AddObject(const S: AnsiString; AObject: TObject): Integer; virtual; abstract;
  128. procedure AddStrings(Strings: TJclAnsiStrings); virtual;
  129. procedure Insert(Index: Integer; const S: AnsiString); virtual;
  130. procedure InsertObject(Index: Integer; const S: AnsiString; AObject: TObject); virtual; abstract;
  131. procedure Delete(Index: Integer); virtual; abstract;
  132. procedure Clear; virtual; abstract;
  133. procedure LoadFromFile(const FileName: TFileName); virtual;
  134. procedure LoadFromStream(Stream: TStream); virtual;
  135. procedure SaveToFile(const FileName: TFileName); virtual;
  136. procedure SaveToStream(Stream: TStream); virtual;
  137. procedure BeginUpdate;
  138. procedure EndUpdate;
  139. function IndexOf(const S: AnsiString): Integer; virtual;
  140. function IndexOfName(const Name: AnsiString): Integer; virtual;
  141. function IndexOfObject(AObject: TObject): Integer; virtual;
  142. procedure Exchange(Index1, Index2: Integer); virtual;
  143. property Delimiter: AnsiChar read FDelimiter write FDelimiter;
  144. property DelimitedText: AnsiString read GetDelimitedText write SetDelimitedText;
  145. property CommaText: AnsiString read GetCommaText write SetCommaText;
  146. property StrictDelimiter: Boolean read FStrictDelimiter write FStrictDelimiter;
  147. property QuoteChar: AnsiChar read FQuoteChar write FQuoteChar;
  148. property Strings[Index: Integer]: AnsiString read GetString write SetString; default;
  149. property Objects[Index: Integer]: TObject read GetObject write SetObject;
  150. property Text: AnsiString read GetText write SetText;
  151. property Count: Integer read GetCount;
  152. property Capacity: Integer read GetCapacity write SetCapacity;
  153. property Names[Index: Integer]: AnsiString read GetName;
  154. property Values[const Name: AnsiString]: AnsiString read GetValue write SetValue;
  155. property ValueFromIndex[Index: Integer]: AnsiString read GetValueFromIndex write SetValueFromIndex;
  156. property NameValueSeparator: AnsiChar read FNameValueSeparator write FNameValueSeparator;
  157. end;
  158. TJclAnsiStringListSortCompare = function(List: TJclAnsiStringList; Index1, Index2: Integer): Integer;
  159. TJclAnsiStringObjectHolder = record
  160. Str: AnsiString;
  161. Obj: TObject;
  162. end;
  163. TJclAnsiStringList = class(TJclAnsiStrings)
  164. private
  165. FStrings: array of TJclAnsiStringObjectHolder;
  166. FCount: Integer;
  167. FDuplicates: TDuplicates;
  168. FSorted: Boolean;
  169. FCaseSensitive: Boolean;
  170. FOnChange: TNotifyEvent;
  171. FOnChanging: TNotifyEvent;
  172. procedure Grow;
  173. procedure QuickSort(L, R: Integer; SCompare: TJclAnsiStringListSortCompare);
  174. procedure SetSorted(Value: Boolean);
  175. protected
  176. procedure AssignTo(Dest: TPersistent); override;
  177. function GetString(Index: Integer): AnsiString; override;
  178. procedure SetString(Index: Integer; const Value: AnsiString); override;
  179. function GetObject(Index: Integer): TObject; override;
  180. procedure SetObject(Index: Integer; AObject: TObject); override;
  181. function GetCapacity: Integer; override;
  182. procedure SetCapacity(const Value: Integer); override;
  183. function GetCount: Integer; override;
  184. function CompareStrings(const S1, S2: AnsiString): Integer; override;
  185. procedure SetUpdateState(Updating: Boolean); override;
  186. procedure Changed; virtual;
  187. procedure Changing; virtual;
  188. public
  189. constructor Create;
  190. destructor Destroy; override;
  191. function AddObject(const S: AnsiString; AObject: TObject): Integer; override;
  192. procedure Assign(Source: TPersistent); override;
  193. procedure InsertObject(Index: Integer; const S: AnsiString; AObject: TObject); override;
  194. procedure Delete(Index: Integer); override;
  195. function Find(const S: AnsiString; var Index: Integer): Boolean; virtual;
  196. procedure CustomSort(Compare: TJclAnsiStringListSortCompare); virtual;
  197. procedure Sort; virtual;
  198. procedure Clear; override;
  199. property CaseSensitive: Boolean read FCaseSensitive write FCaseSensitive;
  200. property Duplicates: TDuplicates read FDuplicates write FDuplicates;
  201. property Sorted: Boolean read FSorted write SetSorted;
  202. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  203. property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  204. end;
  205. {$ELSE ~SUPPORTS_UNICODE}
  206. TJclAnsiStrings = Classes.TStrings;
  207. TJclAnsiStringList = Classes.TStringList;
  208. {$ENDIF ~SUPPORTS_UNICODE}
  209. TAnsiStrings = TJclAnsiStrings;
  210. TAnsiStringList = TJclAnsiStringList;
  211. // Exceptions
  212. type
  213. EJclAnsiStringError = class(EJclError);
  214. EJclAnsiStringListError = class(EJclAnsiStringError);
  215. // Character constants and sets
  216. const
  217. // Misc. often used character definitions
  218. AnsiNull = AnsiChar(#0);
  219. AnsiSoh = AnsiChar(#1);
  220. AnsiStx = AnsiChar(#2);
  221. AnsiEtx = AnsiChar(#3);
  222. AnsiEot = AnsiChar(#4);
  223. AnsiEnq = AnsiChar(#5);
  224. AnsiAck = AnsiChar(#6);
  225. AnsiBell = AnsiChar(#7);
  226. AnsiBackspace = AnsiChar(#8);
  227. AnsiTab = AnsiChar(#9);
  228. AnsiLineFeed = AnsiChar(#10);
  229. AnsiVerticalTab = AnsiChar(#11);
  230. AnsiFormFeed = AnsiChar(#12);
  231. AnsiCarriageReturn = AnsiChar(#13);
  232. AnsiCrLf = AnsiString(#13#10);
  233. AnsiSo = AnsiChar(#14);
  234. AnsiSi = AnsiChar(#15);
  235. AnsiDle = AnsiChar(#16);
  236. AnsiDc1 = AnsiChar(#17);
  237. AnsiDc2 = AnsiChar(#18);
  238. AnsiDc3 = AnsiChar(#19);
  239. AnsiDc4 = AnsiChar(#20);
  240. AnsiNak = AnsiChar(#21);
  241. AnsiSyn = AnsiChar(#22);
  242. AnsiEtb = AnsiChar(#23);
  243. AnsiCan = AnsiChar(#24);
  244. AnsiEm = AnsiChar(#25);
  245. AnsiEndOfFile = AnsiChar(#26);
  246. AnsiEscape = AnsiChar(#27);
  247. AnsiFs = AnsiChar(#28);
  248. AnsiGs = AnsiChar(#29);
  249. AnsiRs = AnsiChar(#30);
  250. AnsiUs = AnsiChar(#31);
  251. AnsiSpace = AnsiChar(' ');
  252. AnsiComma = AnsiChar(',');
  253. AnsiBackslash = AnsiChar('\');
  254. AnsiForwardSlash = AnsiChar('/');
  255. AnsiDoubleQuote = AnsiChar('"');
  256. AnsiSingleQuote = AnsiChar('''');
  257. {$IFDEF MSWINDOWS}
  258. AnsiLineBreak = AnsiCrLf;
  259. {$ENDIF MSWINDOWS}
  260. {$IFDEF UNIX}
  261. AnsiLineBreak = AnsiLineFeed;
  262. {$ENDIF UNIX}
  263. AnsiSignMinus = AnsiChar('-');
  264. AnsiSignPlus = AnsiChar('+');
  265. // Misc. character sets
  266. AnsiWhiteSpace = [AnsiTab, AnsiLineFeed, AnsiVerticalTab,
  267. AnsiFormFeed, AnsiCarriageReturn, AnsiSpace];
  268. AnsiSigns = [AnsiSignMinus, AnsiSignPlus];
  269. AnsiUppercaseLetters = ['A'..'Z'];
  270. AnsiLowercaseLetters = ['a'..'z'];
  271. AnsiLetters = ['A'..'Z', 'a'..'z'];
  272. AnsiDecDigits = ['0'..'9'];
  273. AnsiOctDigits = ['0'..'7'];
  274. AnsiHexDigits = ['0'..'9', 'A'..'F', 'a'..'f'];
  275. AnsiValidIdentifierLetters = ['0'..'9', 'A'..'Z', 'a'..'z', '_'];
  276. const
  277. // CharType return values
  278. C1_UPPER = $0001; // Uppercase
  279. C1_LOWER = $0002; // Lowercase
  280. C1_DIGIT = $0004; // Decimal digits
  281. C1_SPACE = $0008; // Space characters
  282. C1_PUNCT = $0010; // Punctuation
  283. C1_CNTRL = $0020; // Control characters
  284. C1_BLANK = $0040; // Blank characters
  285. C1_XDIGIT = $0080; // Hexadecimal digits
  286. C1_ALPHA = $0100; // Any linguistic character: alphabetic, syllabary, or ideographic
  287. {$IFDEF MSWINDOWS}
  288. {$IFDEF SUPPORTS_EXTSYM}
  289. {$EXTERNALSYM C1_UPPER}
  290. {$EXTERNALSYM C1_LOWER}
  291. {$EXTERNALSYM C1_DIGIT}
  292. {$EXTERNALSYM C1_SPACE}
  293. {$EXTERNALSYM C1_PUNCT}
  294. {$EXTERNALSYM C1_CNTRL}
  295. {$EXTERNALSYM C1_BLANK}
  296. {$EXTERNALSYM C1_XDIGIT}
  297. {$EXTERNALSYM C1_ALPHA}
  298. {$ENDIF SUPPORTS_EXTSYM}
  299. {$ENDIF MSWINDOWS}
  300. // String Test Routines
  301. function StrIsAlpha(const S: AnsiString): Boolean;
  302. function StrIsAlphaNum(const S: AnsiString): Boolean;
  303. function StrIsAlphaNumUnderscore(const S: AnsiString): Boolean;
  304. function StrContainsChars(const S: AnsiString; Chars: TSysCharSet; CheckAll: Boolean): Boolean;
  305. function StrConsistsOfNumberChars(const S: AnsiString): Boolean;
  306. function StrIsDigit(const S: AnsiString): Boolean;
  307. function StrIsSubset(const S: AnsiString; const ValidChars: TSysCharSet): Boolean;
  308. function StrSame(const S1, S2: AnsiString): Boolean;
  309. // String Transformation Routines
  310. function StrCenter(const S: AnsiString; L: SizeInt; C: AnsiChar = ' '): AnsiString;
  311. function StrCharPosLower(const S: AnsiString; CharPos: SizeInt): AnsiString;
  312. function StrCharPosUpper(const S: AnsiString; CharPos: SizeInt): AnsiString;
  313. function StrDoubleQuote(const S: AnsiString): AnsiString;
  314. function StrEnsureNoPrefix(const Prefix, Text: AnsiString): AnsiString;
  315. function StrEnsureNoSuffix(const Suffix, Text: AnsiString): AnsiString;
  316. function StrEnsurePrefix(const Prefix, Text: AnsiString): AnsiString;
  317. function StrEnsureSuffix(const Suffix, Text: AnsiString): AnsiString;
  318. function StrEscapedToString(const S: AnsiString): AnsiString;
  319. function StrLower(const S: AnsiString): AnsiString;
  320. procedure StrLowerInPlace(var S: AnsiString);
  321. procedure StrLowerBuff(S: PAnsiChar);
  322. procedure StrMove(var Dest: AnsiString; const Source: AnsiString; const ToIndex,
  323. FromIndex, Count: SizeInt);
  324. function StrPadLeft(const S: AnsiString; Len: SizeInt; C: AnsiChar = AnsiSpace): AnsiString;
  325. function StrPadRight(const S: AnsiString; Len: SizeInt; C: AnsiChar = AnsiSpace): AnsiString;
  326. function StrProper(const S: AnsiString): AnsiString;
  327. procedure StrProperBuff(S: PAnsiChar);
  328. function StrQuote(const S: AnsiString; C: AnsiChar): AnsiString;
  329. function StrRemoveChars(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  330. function StrKeepChars(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  331. procedure StrReplace(var S: AnsiString; const Search, Replace: AnsiString; Flags: TReplaceFlags = []);
  332. function StrReplaceChar(const S: AnsiString; const Source, Replace: AnsiChar): AnsiString;
  333. function StrReplaceChars(const S: AnsiString; const Chars: TSysCharSet; Replace: AnsiChar): AnsiString;
  334. function StrReplaceButChars(const S: AnsiString; const Chars: TSysCharSet; Replace: AnsiChar): AnsiString;
  335. function StrRepeat(const S: AnsiString; Count: SizeInt): AnsiString;
  336. function StrRepeatLength(const S: AnsiString; const L: SizeInt): AnsiString;
  337. function StrReverse(const S: AnsiString): AnsiString;
  338. procedure StrReverseInPlace(var S: AnsiString);
  339. function StrSingleQuote(const S: AnsiString): AnsiString;
  340. procedure StrSkipChars(var S: PAnsiChar; const Chars: TSysCharSet); overload;
  341. procedure StrSkipChars(const S: AnsiString; var Index: SizeInt; const Chars: TSysCharSet); overload;
  342. function StrSmartCase(const S: AnsiString; Delimiters: TSysCharSet): AnsiString;
  343. function StrStringToEscaped(const S: AnsiString): AnsiString;
  344. function StrStripNonNumberChars(const S: AnsiString): AnsiString;
  345. function StrToHex(const Source: AnsiString): AnsiString;
  346. function StrTrimCharLeft(const S: AnsiString; C: AnsiChar): AnsiString;
  347. function StrTrimCharsLeft(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  348. function StrTrimCharRight(const S: AnsiString; C: AnsiChar): AnsiString;
  349. function StrTrimCharsRight(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  350. function StrTrimQuotes(const S: AnsiString): AnsiString; overload;
  351. function StrTrimQuotes(const S: AnsiString; QuoteChar: AnsiChar): AnsiString; overload;
  352. function StrUpper(const S: AnsiString): AnsiString;
  353. procedure StrUpperInPlace(var S: AnsiString);
  354. procedure StrUpperBuff(S: PAnsiChar);
  355. {$IFDEF MSWINDOWS}
  356. function StrOemToAnsi(const S: AnsiString): AnsiString;
  357. function StrAnsiToOem(const S: AnsiString): AnsiString;
  358. {$ENDIF MSWINDOWS}
  359. // String Management
  360. procedure StrAddRef(var S: AnsiString);
  361. procedure StrDecRef(var S: AnsiString);
  362. function StrLength(const S: AnsiString): Longint;
  363. function StrRefCount(const S: AnsiString): Longint;
  364. procedure StrResetLength(var S: AnsiString);
  365. // String Search and Replace Routines
  366. function StrCharCount(const S: AnsiString; C: AnsiChar): SizeInt;
  367. function StrCharsCount(const S: AnsiString; Chars: TSysCharSet): SizeInt;
  368. function StrStrCount(const S, SubS: AnsiString): SizeInt;
  369. function StrCompare(const S1, S2: AnsiString; CaseSensitive: Boolean = False): SizeInt;
  370. function StrCompareRangeEx(const S1, S2: AnsiString; Index, Count: SizeInt; CaseSensitive: Boolean = False): SizeInt;
  371. function StrCompareRange(const S1, S2: AnsiString; Index, Count: SizeInt; CaseSensitive: Boolean = True): SizeInt;
  372. function StrRepeatChar(C: AnsiChar; Count: SizeInt): AnsiString;
  373. function StrFind(const Substr, S: AnsiString; const Index: SizeInt = 1): SizeInt;
  374. function StrHasPrefix(const S: AnsiString; const Prefixes: array of AnsiString): Boolean;
  375. function StrHasSuffix(const S: AnsiString; const Suffixes: array of AnsiString): Boolean;
  376. function StrIHasPrefix(const S: AnsiString; const Prefixes: array of AnsiString): Boolean;
  377. function StrIHasSuffix(const S: AnsiString; const Suffixes: array of AnsiString): Boolean;
  378. function StrIndex(const S: AnsiString; const List: array of AnsiString; CaseSensitive: Boolean = False): SizeInt;
  379. function StrILastPos(const SubStr, S: AnsiString): SizeInt;
  380. function StrIPos(const SubStr, S: AnsiString): SizeInt;
  381. function StrIPrefixIndex(const S: AnsiString; const Prefixes: array of AnsiString): SizeInt;
  382. function StrIsOneOf(const S: AnsiString; const List: array of AnsiString): Boolean;
  383. function StrISuffixIndex(const S: AnsiString; const Suffixes: array of AnsiString): SizeInt;
  384. function StrLastPos(const SubStr, S: AnsiString): SizeInt;
  385. function StrMatch(const Substr, S: AnsiString; Index: SizeInt = 1): SizeInt;
  386. function StrMatches(const Substr, S: AnsiString; const Index: SizeInt = 1): Boolean;
  387. function StrNIPos(const S, SubStr: AnsiString; N: SizeInt): SizeInt;
  388. function StrNPos(const S, SubStr: AnsiString; N: SizeInt): SizeInt;
  389. function StrPrefixIndex(const S: AnsiString; const Prefixes: array of AnsiString): SizeInt;
  390. function StrSearch(const Substr, S: AnsiString; const Index: SizeInt = 1): SizeInt;
  391. function StrSuffixIndex(const S: AnsiString; const Suffixes: array of AnsiString): SizeInt;
  392. // String Extraction
  393. // String Extraction
  394. // Returns the String before SubStr
  395. function StrAfter(const SubStr, S: AnsiString): AnsiString;
  396. /// Returns the AnsiString after SubStr
  397. function StrBefore(const SubStr, S: AnsiString): AnsiString;
  398. /// Splits a AnsiString at SubStr, returns true when SubStr is found, Left contains the
  399. /// AnsiString before the SubStr and Rigth the AnsiString behind SubStr
  400. function StrSplit(const SubStr, S: AnsiString;var Left, Right : AnsiString): boolean;
  401. /// Returns the AnsiString between Start and Stop
  402. function StrBetween(const S: AnsiString; const Start, Stop: AnsiChar): AnsiString;
  403. /// Returns the left N characters of the AnsiString
  404. function StrChopRight(const S: AnsiString; N: SizeInt): AnsiString;
  405. /// Returns the left Count characters of the AnsiString
  406. function StrLeft(const S: AnsiString; Count: SizeInt): AnsiString;
  407. /// Returns the AnsiString starting from position Start for the Count Characters
  408. function StrMid(const S: AnsiString; Start, Count: SizeInt): AnsiString;
  409. /// Returns the AnsiString starting from position N to the end
  410. function StrRestOf(const S: AnsiString; N: SizeInt): AnsiString;
  411. /// Returns the right Count characters of the AnsiString
  412. function StrRight(const S: AnsiString; Count: SizeInt): AnsiString;
  413. // Character Test Routines
  414. function CharEqualNoCase(const C1, C2: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  415. function CharIsAlpha(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  416. function CharIsAlphaNum(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  417. function CharIsBlank(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  418. function CharIsControl(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  419. function CharIsDelete(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  420. function CharIsDigit(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  421. function CharIsFracDigit(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  422. function CharIsHexDigit(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  423. function CharIsLower(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  424. function CharIsNumberChar(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  425. function CharIsNumber(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  426. function CharIsPrintable(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  427. function CharIsPunctuation(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  428. function CharIsReturn(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  429. function CharIsSpace(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  430. function CharIsUpper(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  431. function CharIsValidIdentifierLetter(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  432. function CharIsWhiteSpace(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  433. function CharIsWildcard(const C: AnsiChar): Boolean; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
  434. function CharType(const C: AnsiChar): Word;
  435. // Character Transformation Routines
  436. function CharHex(const C: AnsiChar): Byte;
  437. function CharLower(const C: AnsiChar): AnsiChar;
  438. function CharUpper(const C: AnsiChar): AnsiChar;
  439. function CharToggleCase(const C: AnsiChar): AnsiChar;
  440. // Character Search and Replace
  441. function CharPos(const S: AnsiString; const C: AnsiChar; const Index: SizeInt = 1): SizeInt;
  442. function CharLastPos(const S: AnsiString; const C: AnsiChar; const Index: SizeInt = 1): SizeInt;
  443. function CharIPos(const S: AnsiString; C: AnsiChar; const Index: SizeInt = 1): SizeInt;
  444. function CharReplace(var S: AnsiString; const Search, Replace: AnsiChar): SizeInt;
  445. // PCharVector
  446. type
  447. PAnsiCharVector = ^PAnsiChar;
  448. function StringsToPCharVector(var Dest: PAnsiCharVector; const Source: TJclAnsiStrings): PAnsiCharVector;
  449. function PCharVectorCount(Source: PAnsiCharVector): SizeInt;
  450. procedure PCharVectorToStrings(const Dest: TJclAnsiStrings; Source: PAnsiCharVector);
  451. procedure FreePCharVector(var Dest: PAnsiCharVector);
  452. // MultiSz Routines
  453. type
  454. PAnsiMultiSz = PAnsiChar;
  455. function StringsToMultiSz(var Dest: PAnsiMultiSz; const Source: TJclAnsiStrings): PAnsiMultiSz;
  456. procedure MultiSzToStrings(const Dest: TJclAnsiStrings; const Source: PAnsiMultiSz);
  457. function MultiSzLength(const Source: PAnsiMultiSz): SizeInt;
  458. procedure AllocateMultiSz(var Dest: PAnsiMultiSz; Len: SizeInt);
  459. procedure FreeMultiSz(var Dest: PAnsiMultiSz);
  460. function MultiSzDup(const Source: PAnsiMultiSz): PAnsiMultiSz;
  461. // TJclAnsiStrings Manipulation
  462. procedure StrIToStrings(S, Sep: AnsiString; const List: TJclAnsiStrings; const AllowEmptyString: Boolean = True);
  463. procedure StrToStrings(S, Sep: AnsiString; const List: TJclAnsiStrings; const AllowEmptyString: Boolean = True);
  464. function StringsToStr(const List: TJclAnsiStrings; const Sep: AnsiString; const AllowEmptyString: Boolean = True): AnsiString;
  465. procedure TrimStrings(const List: TJclAnsiStrings; DeleteIfEmpty: Boolean = True);
  466. procedure TrimStringsRight(const List: TJclAnsiStrings; DeleteIfEmpty: Boolean = True);
  467. procedure TrimStringsLeft(const List: TJclAnsiStrings; DeleteIfEmpty: Boolean = True);
  468. function AddStringToStrings(const S: AnsiString; Strings: TJclAnsiStrings; const Unique: Boolean): Boolean;
  469. // Miscellaneous
  470. // (OF) moved to JclSysUtils
  471. //function BooleanToStr(B: Boolean): AnsiString;
  472. function FileToString(const FileName: TFileName): AnsiString;
  473. procedure StringToFile(const FileName: TFileName; const Contents: AnsiString; Append: Boolean = False);
  474. function StrToken(var S: AnsiString; Separator: AnsiChar): AnsiString;
  475. procedure StrTokens(const S: AnsiString; const List: TJclAnsiStrings);
  476. procedure StrTokenToStrings(S: AnsiString; Separator: AnsiChar; const List: TJclAnsiStrings);
  477. function StrWord(const S: AnsiString; var Index: SizeInt; out Word: AnsiString): Boolean; overload;
  478. function StrWord(var S: PAnsiChar; out Word: AnsiString): Boolean; overload;
  479. function StrIdent(const S: AnsiString; var Index: SizeInt; out Ident: AnsiString): Boolean; overload;
  480. function StrIdent(var S: PAnsiChar; out Ident: AnsiString): Boolean; overload;
  481. function StrToFloatSafe(const S: AnsiString): Float;
  482. function StrToIntSafe(const S: AnsiString): Integer;
  483. {$IFNDEF WINSCP}
  484. procedure StrNormIndex(const StrLen: SizeInt; var Index: SizeInt; var Count: SizeInt); overload;
  485. {$ENDIF}
  486. function ArrayOf(List: TJclAnsiStrings): TDynStringArray; overload;
  487. function AnsiCompareNaturalStr(const S1, S2: AnsiString): SizeInt;
  488. function AnsiCompareNaturalText(const S1, S2: AnsiString): SizeInt;
  489. // Explicit ANSI version of former/deprecated SysUtils PAnsiChar functions
  490. {$IFNDEF DEPRECATED_SYSUTILS_ANSISTRINGS}
  491. {$IFDEF SUPPORTS_INLINE}
  492. {$DEFINE ANSI_INLINE} // inline if the functions are in SysUtils but don't force the user to include System.AnsiStrings
  493. {$ENDIF SUPPORTS_INLINE}
  494. {$ENDIF ~DEPRECATED_SYSUTILS_ANSISTRINGS}
  495. function StrNewA(const Str: PAnsiChar): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  496. procedure StrDisposeA(Str: PAnsiChar); {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  497. function StrLenA(S: PAnsiChar): Integer; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  498. function StrEndA(const Str: PAnsiChar): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  499. function StrPosA(const Str1, Str2: PAnsiChar): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  500. function StrPasA(const Str: PAnsiChar): AnsiString; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  501. function StrCopyA(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  502. function StrLCopyA(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  503. function StrPCopyA(Dest: PAnsiChar; const Source: AnsiString): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  504. function StrPLCopyA(Dest: PAnsiChar; const Source: AnsiString; MaxLen: Cardinal): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  505. function StrECopyA(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  506. function StrCatA(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  507. function StrLCatA(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  508. function StrCompA(const Str1, Str2: PAnsiChar): Integer; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  509. function StrLCompA(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  510. function StrICompA(const Str1, Str2: PAnsiChar): Integer; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  511. function StrLICompA(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  512. function StrFmtA(Buffer, Format: PAnsiChar; const Args: array of const): PAnsiChar;
  513. function AnsiStrPosA(const Str1, Str2: PAnsiChar): PAnsiChar; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  514. function AnsiStrLICompA(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  515. function AnsiStrLCompA(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer; {$IFDEF ANSI_INLINE}inline;{$ENDIF}
  516. // internal structures published to make function inlining working
  517. const
  518. AnsiCharCount = Ord(High(AnsiChar)) + 1; // # of chars in one set
  519. AnsiLoOffset = AnsiCharCount * 0; // offset to lower case chars
  520. AnsiUpOffset = AnsiCharCount * 1; // offset to upper case chars
  521. AnsiReOffset = AnsiCharCount * 2; // offset to reverse case chars
  522. AnsiCaseMapSize = AnsiCharCount * 3; // # of chars is a table
  523. var
  524. AnsiCaseMap: array [0..AnsiCaseMapSize - 1] of AnsiChar; // case mappings
  525. AnsiCaseMapReady: Boolean = False; // true if case map exists
  526. AnsiCharTypes: array [AnsiChar] of Word;
  527. {$IFDEF UNITVERSIONING}
  528. const
  529. UnitVersioning: TUnitVersionInfo = (
  530. RCSfile: '$URL$';
  531. Revision: '$Revision$';
  532. Date: '$Date$';
  533. LogPath: 'JCL\source\common';
  534. Extra: '';
  535. Data: nil
  536. );
  537. {$ENDIF UNITVERSIONING}
  538. implementation
  539. uses
  540. {$IFDEF HAS_UNIT_LIBC}
  541. Libc,
  542. {$ENDIF HAS_UNIT_LIBC}
  543. {$IFDEF SUPPORTS_UNICODE}
  544. {$IFDEF HAS_UNIT_RTLCONSTS}
  545. {$IFDEF HAS_UNITSCOPE}
  546. System.RTLConsts,
  547. {$ELSE ~HAS_UNITSCOPE}
  548. RtlConsts,
  549. {$ENDIF}
  550. {$ENDIF HAS_UNIT_RTLCONSTS}
  551. {$ENDIF SUPPORTS_UNICODE}
  552. {$IFNDEF WINSCP}JclLogic,{$ENDIF WINSCP} JclResources, JclStreams, JclSynch, JclSysUtils;
  553. //=== Internal ===============================================================
  554. type
  555. TAnsiStrRec = packed record
  556. RefCount: Integer;
  557. Length: Integer;
  558. end;
  559. PAnsiStrRec = ^TAnsiStrRec;
  560. const
  561. AnsiStrRecSize = SizeOf(TAnsiStrRec); // size of the AnsiString header rec
  562. procedure LoadCharTypes;
  563. var
  564. CurrChar: AnsiChar;
  565. CurrType: Word;
  566. begin
  567. for CurrChar := Low(AnsiChar) to High(AnsiChar) do
  568. begin
  569. {$IFDEF MSWINDOWS}
  570. CurrType := 0;
  571. GetStringTypeExA(LOCALE_USER_DEFAULT, CT_CTYPE1, @CurrChar, SizeOf(AnsiChar), CurrType);
  572. {$DEFINE CHAR_TYPES_INITIALIZED}
  573. {$ENDIF MSWINDOWS}
  574. {$IFDEF LINUX}
  575. CurrType := 0;
  576. if isupper(Byte(CurrChar)) <> 0 then
  577. CurrType := CurrType or C1_UPPER;
  578. if islower(Byte(CurrChar)) <> 0 then
  579. CurrType := CurrType or C1_LOWER;
  580. if isdigit(Byte(CurrChar)) <> 0 then
  581. CurrType := CurrType or C1_DIGIT;
  582. if isspace(Byte(CurrChar)) <> 0 then
  583. CurrType := CurrType or C1_SPACE;
  584. if ispunct(Byte(CurrChar)) <> 0 then
  585. CurrType := CurrType or C1_PUNCT;
  586. if iscntrl(Byte(CurrChar)) <> 0 then
  587. CurrType := CurrType or C1_CNTRL;
  588. if isblank(Byte(CurrChar)) <> 0 then
  589. CurrType := CurrType or C1_BLANK;
  590. if isxdigit(Byte(CurrChar)) <> 0 then
  591. CurrType := CurrType or C1_XDIGIT;
  592. if isalpha(Byte(CurrChar)) <> 0 then
  593. CurrType := CurrType or C1_ALPHA;
  594. {$DEFINE CHAR_TYPES_INITIALIZED}
  595. {$ENDIF LINUX}
  596. AnsiCharTypes[CurrChar] := CurrType;
  597. {$IFNDEF CHAR_TYPES_INITIALIZED}
  598. Implement case map initialization here
  599. {$ENDIF ~CHAR_TYPES_INITIALIZED}
  600. end;
  601. end;
  602. procedure LoadCaseMap;
  603. var
  604. CurrChar, UpCaseChar, LoCaseChar, ReCaseChar: AnsiChar;
  605. begin
  606. if not AnsiCaseMapReady then
  607. begin
  608. for CurrChar := Low(AnsiChar) to High(AnsiChar) do
  609. begin
  610. {$IFDEF MSWINDOWS}
  611. LoCaseChar := CurrChar;
  612. UpCaseChar := CurrChar;
  613. {$IFDEF HAS_UNITSCOPE}Winapi.{$ENDIF}Windows.CharLowerBuffA(@LoCaseChar, 1);
  614. {$IFDEF HAS_UNITSCOPE}Winapi.{$ENDIF}Windows.CharUpperBuffA(@UpCaseChar, 1);
  615. {$DEFINE CASE_MAP_INITIALIZED}
  616. {$ENDIF MSWINDOWS}
  617. {$IFDEF LINUX}
  618. LoCaseChar := AnsiChar(tolower(Byte(CurrChar)));
  619. UpCaseChar := AnsiChar(toupper(Byte(CurrChar)));
  620. {$DEFINE CASE_MAP_INITIALIZED}
  621. {$ENDIF LINUX}
  622. {$IFNDEF CASE_MAP_INITIALIZED}
  623. Implement case map initialization here
  624. {$ENDIF ~CASE_MAP_INITIALIZED}
  625. if CharIsUpper(CurrChar) then
  626. ReCaseChar := LoCaseChar
  627. else
  628. if CharIsLower(CurrChar) then
  629. ReCaseChar := UpCaseChar
  630. else
  631. ReCaseChar := CurrChar;
  632. AnsiCaseMap[Ord(CurrChar) + AnsiLoOffset] := LoCaseChar;
  633. AnsiCaseMap[Ord(CurrChar) + AnsiUpOffset] := UpCaseChar;
  634. AnsiCaseMap[Ord(CurrChar) + AnsiReOffset] := ReCaseChar;
  635. end;
  636. AnsiCaseMapReady := True;
  637. end;
  638. end;
  639. // Uppercases or Lowercases a give AnsiString depending on the
  640. // passed offset. (UpOffset or LoOffset)
  641. procedure StrCase(var Str: AnsiString; const Offset: SizeInt);
  642. var
  643. P: PAnsiChar;
  644. I, L: SizeInt;
  645. begin
  646. if Str <> '' then
  647. begin
  648. UniqueString(Str);
  649. P := PAnsiChar(Str);
  650. L := Length(Str);
  651. for I := 1 to L do
  652. begin
  653. P^ := AnsiCaseMap[Offset + Ord(P^)];
  654. Inc(P);
  655. end;
  656. end;
  657. end;
  658. // Internal utility function
  659. // Uppercases or Lowercases a give null terminated string depending on the
  660. // passed offset. (UpOffset or LoOffset)
  661. procedure StrCaseBuff(S: PAnsiChar; const Offset: SizeInt);
  662. begin
  663. if (S <> nil) and (S^ <> #0) then
  664. begin
  665. repeat
  666. S^ := AnsiCaseMap[Offset + Ord(S^)];
  667. Inc(S);
  668. until S^ = #0;
  669. end;
  670. end;
  671. {$IFDEF SUPPORTS_UNICODE}
  672. //=== { TJclAnsiStrings } ====================================================
  673. constructor TJclAnsiStrings.Create;
  674. begin
  675. inherited Create;
  676. FDelimiter := ',';
  677. FNameValueSeparator := '=';
  678. FQuoteChar := '"';
  679. FStrictDelimiter := False;
  680. end;
  681. procedure TJclAnsiStrings.Assign(Source: TPersistent);
  682. var
  683. StringsSource: TStrings;
  684. I: Integer;
  685. begin
  686. if Source is TStrings then
  687. begin
  688. StringsSource := TStrings(Source);
  689. BeginUpdate;
  690. try
  691. Clear;
  692. FDelimiter := AnsiChar(StringsSource.Delimiter);
  693. FNameValueSeparator := AnsiChar(StringsSource.NameValueSeparator);
  694. for I := 0 to StringsSource.Count - 1 do
  695. AddObject(AnsiString(StringsSource.Strings[I]), StringsSource.Objects[I]);
  696. finally
  697. EndUpdate;
  698. end;
  699. end
  700. else
  701. inherited Assign(Source);
  702. end;
  703. procedure TJclAnsiStrings.AssignTo(Dest: TPersistent);
  704. var
  705. StringsDest: TStrings;
  706. AnsiStringsDest: TJclAnsiStrings;
  707. I: Integer;
  708. begin
  709. if Dest is TStrings then
  710. begin
  711. StringsDest := TStrings(Dest);
  712. StringsDest.BeginUpdate;
  713. try
  714. StringsDest.Clear;
  715. StringsDest.Delimiter := Char(Delimiter);
  716. StringsDest.NameValueSeparator := Char(NameValueSeparator);
  717. for I := 0 to Count - 1 do
  718. StringsDest.AddObject(string(Strings[I]), Objects[I]);
  719. finally
  720. StringsDest.EndUpdate;
  721. end;
  722. end
  723. else
  724. if Dest is TJclAnsiStrings then
  725. begin
  726. AnsiStringsDest := TJclAnsiStrings(Dest);
  727. AnsiStringsDest.BeginUpdate;
  728. try
  729. AnsiStringsDest.Clear;
  730. AnsiStringsDest.FNameValueSeparator := FNameValueSeparator;
  731. AnsiStringsDest.FDelimiter := FDelimiter;
  732. for I := 0 to Count - 1 do
  733. AnsiStringsDest.AddObject(Strings[I], Objects[I]);
  734. finally
  735. AnsiStringsDest.EndUpdate;
  736. end;
  737. end
  738. else
  739. inherited AssignTo(Dest);
  740. end;
  741. function TJclAnsiStrings.Add(const S: AnsiString): Integer;
  742. begin
  743. Result := AddObject(S, nil);
  744. end;
  745. procedure TJclAnsiStrings.AddStrings(Strings: TJclAnsiStrings);
  746. var
  747. I: Integer;
  748. begin
  749. for I := 0 to Strings.Count - 1 do
  750. Add(Strings.Strings[I]);
  751. end;
  752. procedure TJclAnsiStrings.Error(const Msg: string; Data: Integer);
  753. begin
  754. raise EJclAnsiStringListError.CreateFmt(Msg, [Data]);
  755. end;
  756. procedure TJclAnsiStrings.Error(Msg: PResStringRec; Data: Integer);
  757. begin
  758. Error(LoadResString(Msg), Data);
  759. end;
  760. function TJclAnsiStrings.CompareStrings(const S1, S2: AnsiString): Integer;
  761. begin
  762. Result := CompareStr(S1, S2);
  763. end;
  764. procedure TJclAnsiStrings.SetUpdateState(Updating: Boolean);
  765. begin
  766. end;
  767. function TJclAnsiStrings.IndexOf(const S: AnsiString): Integer;
  768. begin
  769. for Result := 0 to Count - 1 do
  770. if CompareStrings(Strings[Result], S) = 0 then
  771. Exit;
  772. Result := -1;
  773. end;
  774. function TJclAnsiStrings.IndexOfName(const Name: AnsiString): Integer;
  775. var
  776. P: Integer;
  777. S: AnsiString;
  778. begin
  779. for Result := 0 to Count - 1 do
  780. begin
  781. S := Strings[Result];
  782. P := AnsiPos(NameValueSeparator, S);
  783. if (P > 0) and (CompareStrings(Copy(S, 1, P - 1), Name) = 0) then
  784. Exit;
  785. end;
  786. Result := -1;
  787. end;
  788. function TJclAnsiStrings.IndexOfObject(AObject: TObject): Integer;
  789. begin
  790. for Result := 0 to Count - 1 do
  791. if Objects[Result] = AObject then
  792. Exit;
  793. Result := -1;
  794. end;
  795. procedure TJclAnsiStrings.Exchange(Index1, Index2: Integer);
  796. var
  797. TempString: AnsiString;
  798. TempObject: TObject;
  799. begin
  800. BeginUpdate;
  801. try
  802. TempString := Strings[Index1];
  803. TempObject := Objects[Index1];
  804. Strings[Index1] := Strings[Index2];
  805. Objects[Index1] := Objects[Index2];
  806. Strings[Index2] := TempString;
  807. Objects[Index2] := TempObject;
  808. finally
  809. EndUpdate;
  810. end;
  811. end;
  812. function TJclAnsiStrings.GetCommaText: AnsiString;
  813. begin
  814. Result := GetDelimitedText(AnsiComma, AnsiDoubleQuote);
  815. end;
  816. function TJclAnsiStrings.GetDelimitedText: AnsiString;
  817. begin
  818. Result := GetDelimitedText(Delimiter, QuoteChar);
  819. end;
  820. function TJclAnsiStrings.GetDelimitedText(const ADelimiter: AnsiString; AQuoteChar: AnsiChar): AnsiString;
  821. function Quoted(Item: AnsiString): AnsiString;
  822. begin
  823. if (not StrictDelimiter) and ((Pos(AnsiSpace, Item) > 0) or (Pos(FQuoteChar, Item) > 0)) then
  824. begin
  825. Result := AnsiQuotedStr(Item, AQuoteChar);
  826. end
  827. else
  828. Result := Item;
  829. end;
  830. var
  831. I: Integer;
  832. begin
  833. Result := '';
  834. for I := 0 to Count - 2 do
  835. Result := Result + Quoted(Strings[I]) + ADelimiter;
  836. if Count > 0 then
  837. Result := Result + Quoted(Strings[Count - 1]);
  838. end;
  839. procedure TJclAnsiStrings.Insert(Index: Integer; const S: AnsiString);
  840. begin
  841. InsertObject(Index, S, nil);
  842. end;
  843. procedure TJclAnsiStrings.SetCommaText(const Value: AnsiString);
  844. begin
  845. SetDelimitedText(Value, AnsiComma, AnsiDoubleQuote);
  846. end;
  847. procedure TJclAnsiStrings.SetDelimitedText(const Value: AnsiString);
  848. begin
  849. SetDelimitedText(Value, Delimiter, QuoteChar);
  850. end;
  851. procedure TJclAnsiStrings.SetDelimitedText(const Value, ADelimiter: AnsiString; AQuoteChar: AnsiChar);
  852. procedure InternalAdd(Item: AnsiString);
  853. begin
  854. Item := StrTrimQuotes(Item, AQuoteChar);
  855. StrReplace(Item, AQuoteChar + AQuoteChar, AQuoteChar, [rfReplaceAll]);
  856. Add(Item);
  857. end;
  858. var
  859. ValueLength, LastStart, Index, QuoteCharCount: Integer;
  860. ValueChar: AnsiChar;
  861. begin
  862. Clear;
  863. LastStart := 1;
  864. QuoteCharCount := 0;
  865. ValueLength := Length(Value);
  866. for Index := 1 to ValueLength do
  867. begin
  868. ValueChar := Value[Index];
  869. if ValueChar = AQuoteChar then
  870. Inc(QuoteCharCount);
  871. if ((ValueChar = ADelimiter) or ((ValueChar = ' ') and (not StrictDelimiter)))
  872. and ((not Odd(QuoteCharCount) or (QuoteCharCount = 0))) then
  873. begin
  874. if StrictDelimiter then
  875. Add(Copy(Value, LastStart, Index - LastStart))
  876. else
  877. InternalAdd(Copy(Value, LastStart, Index - LastStart));
  878. QuoteCharCount := 0;
  879. LastStart := Index + 1;
  880. end;
  881. if (Index = ValueLength) and (LastStart <= ValueLength) then
  882. begin
  883. if StrictDelimiter then
  884. Add(Copy(Value, LastStart, ValueLength - LastStart + 1))
  885. else
  886. InternalAdd(Copy(Value, LastStart, Index - LastStart + 1));
  887. end;
  888. end;
  889. end;
  890. function TJclAnsiStrings.GetText: AnsiString;
  891. var
  892. I: Integer;
  893. begin
  894. Result := '';
  895. for I := 0 to Count - 2 do
  896. Result := Result + Strings[I] + AnsiLineBreak;
  897. if Count > 0 then
  898. Result := Result + Strings[Count - 1] + AnsiLineBreak;
  899. end;
  900. procedure TJclAnsiStrings.SetText(const Value: AnsiString);
  901. var
  902. Index, Start, Len: Integer;
  903. S: AnsiString;
  904. begin
  905. Clear;
  906. Len := Length(Value);
  907. Index := 1;
  908. while Index <= Len do
  909. begin
  910. Start := Index;
  911. while (Index <= Len) and not CharIsReturn(Value[Index]) do
  912. Inc(Index);
  913. S := Copy(Value, Start, Index - Start);
  914. Add(S);
  915. if (Index <= Len) and (Value[Index] = AnsiCarriageReturn) then
  916. Inc(Index);
  917. if (Index <= Len) and (Value[Index] = AnsiLineFeed) then
  918. Inc(Index);
  919. end;
  920. end;
  921. function TJclAnsiStrings.GetCapacity: Integer;
  922. begin
  923. Result := Count; // Might be overridden in derived classes
  924. end;
  925. procedure TJclAnsiStrings.SetCapacity(const Value: Integer);
  926. begin
  927. // Nothing at this level
  928. end;
  929. procedure TJclAnsiStrings.BeginUpdate;
  930. begin
  931. if FUpdateCount = 0 then SetUpdateState(True);
  932. Inc(FUpdateCount);
  933. end;
  934. procedure TJclAnsiStrings.EndUpdate;
  935. begin
  936. Dec(FUpdateCount);
  937. if FUpdateCount = 0 then SetUpdateState(False);
  938. end;
  939. procedure TJclAnsiStrings.LoadFromFile(const FileName: TFileName);
  940. var
  941. Stream: TStream;
  942. begin
  943. Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  944. try
  945. LoadFromStream(Stream);
  946. finally
  947. Stream.Free;
  948. end;
  949. end;
  950. procedure TJclAnsiStrings.LoadFromStream(Stream: TStream);
  951. var
  952. Size: Integer;
  953. S: AnsiString;
  954. begin
  955. BeginUpdate;
  956. try
  957. Size := Stream.Size - Stream.Position;
  958. System.SetString(S, nil, Size);
  959. Stream.Read(PAnsiChar(S)^, Size);
  960. SetText(S);
  961. finally
  962. EndUpdate;
  963. end;
  964. end;
  965. procedure TJclAnsiStrings.SaveToFile(const FileName: TFileName);
  966. var
  967. Stream: TStream;
  968. begin
  969. Stream := TFileStream.Create(FileName, fmCreate);
  970. try
  971. SaveToStream(Stream);
  972. finally
  973. Stream.Free;
  974. end;
  975. end;
  976. procedure TJclAnsiStrings.SaveToStream(Stream: TStream);
  977. var
  978. S: AnsiString;
  979. begin
  980. S := GetText;
  981. Stream.WriteBuffer(PAnsiChar(S)^, Length(S));
  982. end;
  983. function TJclAnsiStrings.ExtractName(const S: AnsiString): AnsiString;
  984. var
  985. P: Integer;
  986. begin
  987. Result := S;
  988. P := AnsiPos(NameValueSeparator, Result);
  989. if P > 0 then
  990. SetLength(Result, P - 1)
  991. else
  992. SetLength(Result, 0);
  993. end;
  994. function TJclAnsiStrings.GetName(Index: Integer): AnsiString;
  995. begin
  996. Result := ExtractName(Strings[Index]);
  997. end;
  998. function TJclAnsiStrings.GetValue(const Name: AnsiString): AnsiString;
  999. var
  1000. I: Integer;
  1001. begin
  1002. I := IndexOfName(Name);
  1003. if I >= 0 then
  1004. Result := Copy(GetString(I), Length(Name) + 2, MaxInt)
  1005. else
  1006. Result := '';
  1007. end;
  1008. procedure TJclAnsiStrings.SetValue(const Name, Value: AnsiString);
  1009. var
  1010. I: Integer;
  1011. begin
  1012. I := IndexOfName(Name);
  1013. if Value <> '' then
  1014. begin
  1015. if I < 0 then
  1016. I := Add('');
  1017. SetString(I, Name + NameValueSeparator + Value);
  1018. end
  1019. else
  1020. begin
  1021. if I >= 0 then
  1022. Delete(I);
  1023. end;
  1024. end;
  1025. function TJclAnsiStrings.GetValueFromIndex(Index: Integer): AnsiString;
  1026. var
  1027. S: AnsiString;
  1028. P: Integer;
  1029. begin
  1030. if Index >= 0 then
  1031. begin
  1032. S := Strings[Index];
  1033. P := AnsiPos(NameValueSeparator, S);
  1034. if P > 0 then
  1035. Result := Copy(S, P + 1, Length(S) - P)
  1036. else
  1037. Result := '';
  1038. end
  1039. else
  1040. Result := '';
  1041. end;
  1042. procedure TJclAnsiStrings.SetValueFromIndex(Index: Integer; const Value: AnsiString);
  1043. begin
  1044. if Value <> '' then
  1045. begin
  1046. if Index < 0 then
  1047. Index := Add('');
  1048. SetString(Index, Names[Index] + NameValueSeparator + Value);
  1049. end
  1050. else
  1051. begin
  1052. if Index >= 0 then
  1053. Delete(Index);
  1054. end;
  1055. end;
  1056. //=== { TJclAnsiStringList } =================================================
  1057. constructor TJclAnsiStringList.Create;
  1058. begin
  1059. inherited Create;
  1060. FCaseSensitive := True;
  1061. end;
  1062. destructor TJclAnsiStringList.Destroy;
  1063. begin
  1064. FOnChange := nil;
  1065. FOnChanging := nil;
  1066. inherited Destroy;
  1067. end;
  1068. procedure TJclAnsiStringList.Assign(Source: TPersistent);
  1069. var
  1070. StringListSource: TStringList;
  1071. begin
  1072. if Source is TStringList then
  1073. begin
  1074. StringListSource := TStringList(Source);
  1075. FDuplicates := StringListSource.Duplicates;
  1076. FSorted := StringListSource.Sorted;
  1077. FCaseSensitive := StringListSource.CaseSensitive;
  1078. end;
  1079. inherited Assign(Source);
  1080. end;
  1081. procedure TJclAnsiStringList.AssignTo(Dest: TPersistent);
  1082. var
  1083. StringListDest: TStringList;
  1084. AnsiStringListDest: TJclAnsiStringList;
  1085. begin
  1086. if Dest is TStringList then
  1087. begin
  1088. StringListDest := TStringList(Dest);
  1089. StringListDest.Clear; // make following assignments a lot faster
  1090. StringListDest.Duplicates := FDuplicates;
  1091. StringListDest.Sorted := FSorted;
  1092. StringListDest.CaseSensitive := FCaseSensitive;
  1093. end
  1094. else
  1095. if Dest is TJclAnsiStringList then
  1096. begin
  1097. AnsiStringListDest := TJclAnsiStringList(Dest);
  1098. AnsiStringListDest.Clear;
  1099. AnsiStringListDest.FDuplicates := FDuplicates;
  1100. AnsiStringListDest.FSorted := FSorted;
  1101. AnsiStringListDest.FCaseSensitive := FCaseSensitive;
  1102. end;
  1103. inherited AssignTo(Dest);
  1104. end;
  1105. function TJclAnsiStringList.CompareStrings(const S1: AnsiString; const S2: AnsiString): Integer;
  1106. begin
  1107. if FCaseSensitive then
  1108. Result := CompareStr(S1, S2)
  1109. else
  1110. Result := CompareText(S1, S2);
  1111. end;
  1112. procedure TJclAnsiStringList.SetUpdateState(Updating: Boolean);
  1113. begin
  1114. if Updating then Changing else Changed;
  1115. end;
  1116. procedure TJclAnsiStringList.Changed;
  1117. begin
  1118. if (FUpdateCount = 0) and Assigned(FOnChange) then
  1119. FOnChange(Self);
  1120. end;
  1121. procedure TJclAnsiStringList.Changing;
  1122. begin
  1123. if (FUpdateCount = 0) and Assigned(FOnChanging) then
  1124. FOnChanging(Self);
  1125. end;
  1126. procedure TJclAnsiStringList.Grow;
  1127. var
  1128. Delta: Integer;
  1129. begin
  1130. if Capacity > 64 then
  1131. Delta := Capacity div 4
  1132. else if Capacity > 8 then
  1133. Delta := 16
  1134. else
  1135. Delta := 4;
  1136. SetCapacity(Capacity + Delta);
  1137. end;
  1138. function TJclAnsiStringList.GetString(Index: Integer): AnsiString;
  1139. begin
  1140. if (Index < 0) or (Index >= FCount) then
  1141. Error(@SListIndexError, Index);
  1142. Result := FStrings[Index].Str;
  1143. end;
  1144. procedure TJclAnsiStringList.SetString(Index: Integer; const Value: AnsiString);
  1145. begin
  1146. if Sorted then
  1147. Error(@SSortedListError, 0);
  1148. if (Index < 0) or (Index >= FCount) then
  1149. Error(@SListIndexError, Index);
  1150. FStrings[Index].Str := Value;
  1151. end;
  1152. function TJclAnsiStringList.GetObject(Index: Integer): TObject;
  1153. begin
  1154. if (Index < 0) or (Index >= FCount) then
  1155. Error(@SListIndexError, Index);
  1156. Result := FStrings[Index].Obj;
  1157. end;
  1158. procedure TJclAnsiStringList.SetObject(Index: Integer; AObject: TObject);
  1159. begin
  1160. if (Index < 0) or (Index >= FCount) then
  1161. Error(@SListIndexError, Index);
  1162. FStrings[Index].Obj := AObject;
  1163. end;
  1164. function TJclAnsiStringList.GetCapacity: Integer;
  1165. begin
  1166. Result := Length(FStrings);
  1167. end;
  1168. procedure TJclAnsiStringList.SetCapacity(const Value: Integer);
  1169. begin
  1170. if (Value < FCount) then
  1171. Error(@SListCapacityError, Value);
  1172. if Value <> Capacity then
  1173. SetLength(FStrings, Value);
  1174. end;
  1175. function TJclAnsiStringList.GetCount: Integer;
  1176. begin
  1177. Result := FCount;
  1178. end;
  1179. procedure TJclAnsiStringList.InsertObject(Index: Integer; const S: AnsiString; AObject: TObject);
  1180. var
  1181. I: Integer;
  1182. begin
  1183. if Count = Capacity then
  1184. Grow;
  1185. for I := Count - 1 downto Index do
  1186. FStrings[I + 1] := FStrings[I];
  1187. FStrings[Index].Str := S;
  1188. FStrings[Index].Obj := AObject;
  1189. Inc(FCount);
  1190. end;
  1191. function TJclAnsiStringList.AddObject(const S: AnsiString; AObject: TObject): Integer;
  1192. var
  1193. Found: Boolean;
  1194. begin
  1195. if not Sorted then
  1196. Result := Count
  1197. else
  1198. begin
  1199. Found := Find(S, Result);
  1200. case Duplicates of
  1201. dupAccept: ;
  1202. dupIgnore:
  1203. if Found then
  1204. Exit;
  1205. dupError:
  1206. if Found then
  1207. Error(@SDuplicateString, 0);
  1208. end;
  1209. end;
  1210. InsertObject(Result, S, AObject);
  1211. end;
  1212. procedure TJclAnsiStringList.Delete(Index: Integer);
  1213. var
  1214. I: Integer;
  1215. begin
  1216. if (Index < 0) or (Index >= FCount) then
  1217. Error(@SListIndexError, Index);
  1218. for I := Index to Count - 2 do
  1219. FStrings[I] := FStrings[I + 1];
  1220. FStrings[FCount - 1].Str := ''; // the last string is no longer useful
  1221. Dec(FCount);
  1222. end;
  1223. procedure TJclAnsiStringList.Clear;
  1224. var
  1225. I: Integer;
  1226. begin
  1227. FCount := 0;
  1228. for I := 0 to Length(FStrings) - 1 do
  1229. begin
  1230. FStrings[I].Str := '';
  1231. FStrings[I].Obj := nil;
  1232. end;
  1233. end;
  1234. function TJclAnsiStringList.Find(const S: AnsiString; var Index: Integer): Boolean;
  1235. var
  1236. L, H, I, C: Integer;
  1237. begin
  1238. Result := False;
  1239. L := 0;
  1240. H := FCount - 1;
  1241. while L <= H do
  1242. begin
  1243. I := (L + H) shr 1;
  1244. C := CompareStrings(FStrings[I].Str, S);
  1245. if C < 0 then
  1246. L := I + 1
  1247. else
  1248. begin
  1249. H := I - 1;
  1250. if C = 0 then
  1251. begin
  1252. Result := True;
  1253. if Duplicates <> dupAccept then
  1254. L := I;
  1255. end;
  1256. end;
  1257. end;
  1258. Index := L;
  1259. end;
  1260. function AnsiStringListCompareStrings(List: TJclAnsiStringList; Index1, Index2: Integer): Integer;
  1261. begin
  1262. Result := List.CompareStrings(List.FStrings[Index1].Str,
  1263. List.FStrings[Index2].Str);
  1264. end;
  1265. procedure TJclAnsiStringList.Sort;
  1266. begin
  1267. CustomSort(AnsiStringListCompareStrings);
  1268. end;
  1269. procedure TJclAnsiStringList.CustomSort(Compare: TJclAnsiStringListSortCompare);
  1270. begin
  1271. if not Sorted and (FCount > 1) then
  1272. QuickSort(0, FCount - 1, Compare);
  1273. end;
  1274. procedure TJclAnsiStringList.QuickSort(L, R: Integer; SCompare: TJclAnsiStringListSortCompare);
  1275. var
  1276. I, J, P: Integer;
  1277. begin
  1278. repeat
  1279. I := L;
  1280. J := R;
  1281. P := (L + R) shr 1;
  1282. repeat
  1283. while SCompare(Self, I, P) < 0 do
  1284. Inc(I);
  1285. while SCompare(Self, J, P) > 0 do
  1286. Dec(J);
  1287. if I <= J then
  1288. begin
  1289. if I <> J then
  1290. Exchange(I, J);
  1291. if P = I then
  1292. P := J
  1293. else
  1294. if P = J then
  1295. P := I;
  1296. Inc(I);
  1297. Dec(J);
  1298. end;
  1299. until I > J;
  1300. if L < J then
  1301. QuickSort(L, J, SCompare);
  1302. L := I;
  1303. until I >= R;
  1304. end;
  1305. procedure TJclAnsiStringList.SetSorted(Value: Boolean);
  1306. begin
  1307. if FSorted <> Value then
  1308. begin
  1309. if Value then
  1310. Sort;
  1311. FSorted := Value;
  1312. end;
  1313. end;
  1314. {$ENDIF SUPPORTS_UNICODE}
  1315. // String Test Routines
  1316. function StrIsAlpha(const S: AnsiString): Boolean;
  1317. var
  1318. I: SizeInt;
  1319. begin
  1320. Result := S <> '';
  1321. for I := 1 to Length(S) do
  1322. begin
  1323. if not CharIsAlpha(S[I]) then
  1324. begin
  1325. Result := False;
  1326. Exit;
  1327. end;
  1328. end;
  1329. end;
  1330. function StrIsAlphaNum(const S: AnsiString): Boolean;
  1331. var
  1332. I: SizeInt;
  1333. begin
  1334. Result := S <> '';
  1335. for I := 1 to Length(S) do
  1336. begin
  1337. if not CharIsAlphaNum(S[I]) then
  1338. begin
  1339. Result := False;
  1340. Exit;
  1341. end;
  1342. end;
  1343. end;
  1344. function StrConsistsofNumberChars(const S: AnsiString): Boolean;
  1345. var
  1346. I: SizeInt;
  1347. begin
  1348. Result := S <> '';
  1349. for I := 1 to Length(S) do
  1350. begin
  1351. if not CharIsNumberChar(S[I]) then
  1352. begin
  1353. Result := False;
  1354. Exit;
  1355. end;
  1356. end;
  1357. end;
  1358. function StrContainsChars(const S: AnsiString; Chars: TSysCharSet; CheckAll: Boolean): Boolean;
  1359. var
  1360. I: SizeInt;
  1361. C: AnsiChar;
  1362. begin
  1363. Result := Chars = [];
  1364. if not Result then
  1365. begin
  1366. if CheckAll then
  1367. begin
  1368. for I := 1 to Length(S) do
  1369. begin
  1370. C := S[I];
  1371. if C in Chars then
  1372. begin
  1373. Chars := Chars - [C];
  1374. if Chars = [] then
  1375. Break;
  1376. end;
  1377. end;
  1378. Result := (Chars = []);
  1379. end
  1380. else
  1381. begin
  1382. for I := 1 to Length(S) do
  1383. if S[I] in Chars then
  1384. begin
  1385. Result := True;
  1386. Break;
  1387. end;
  1388. end;
  1389. end;
  1390. end;
  1391. function StrIsAlphaNumUnderscore(const S: AnsiString): Boolean;
  1392. var
  1393. I: SizeInt;
  1394. C: AnsiChar;
  1395. begin
  1396. for i := 1 to Length(s) do
  1397. begin
  1398. C := S[I];
  1399. if not (CharIsAlphaNum(C) or (C = '_')) then
  1400. begin
  1401. Result := False;
  1402. Exit;
  1403. end;
  1404. end;
  1405. Result := True and (Length(S) > 0);
  1406. end;
  1407. function StrIsDigit(const S: AnsiString): Boolean;
  1408. var
  1409. I: SizeInt;
  1410. begin
  1411. Result := S <> '';
  1412. for I := 1 to Length(S) do
  1413. begin
  1414. if not CharIsDigit(S[I]) then
  1415. begin
  1416. Result := False;
  1417. Exit;
  1418. end;
  1419. end;
  1420. end;
  1421. function StrIsSubset(const S: AnsiString; const ValidChars: TSysCharSet): Boolean;
  1422. var
  1423. I: SizeInt;
  1424. begin
  1425. for I := 1 to Length(S) do
  1426. begin
  1427. if not (S[I] in ValidChars) then
  1428. begin
  1429. Result := False;
  1430. Exit;
  1431. end;
  1432. end;
  1433. Result := True and (Length(S) > 0);
  1434. end;
  1435. function StrSame(const S1, S2: AnsiString): Boolean;
  1436. begin
  1437. Result := StrCompare(S1, S2) = 0;
  1438. end;
  1439. //=== String Transformation Routines =========================================
  1440. function StrCenter(const S: AnsiString; L: SizeInt; C: AnsiChar = ' '): AnsiString;
  1441. begin
  1442. if Length(S) < L then
  1443. begin
  1444. Result := StringOfChar(C, (L - Length(S)) div 2) + S;
  1445. Result := Result + StringOfChar(C, L - Length(Result));
  1446. end
  1447. else
  1448. Result := S;
  1449. end;
  1450. function StrCharPosLower(const S: AnsiString; CharPos: SizeInt): AnsiString;
  1451. begin
  1452. Result := S;
  1453. if (CharPos > 0) and (CharPos <= Length(S)) then
  1454. Result[CharPos] := CharLower(Result[CharPos]);
  1455. end;
  1456. function StrCharPosUpper(const S: AnsiString; CharPos: SizeInt): AnsiString;
  1457. begin
  1458. Result := S;
  1459. if (CharPos > 0) and (CharPos <= Length(S)) then
  1460. Result[CharPos] := CharUpper(Result[CharPos]);
  1461. end;
  1462. function StrDoubleQuote(const S: AnsiString): AnsiString;
  1463. begin
  1464. Result := AnsiDoubleQuote + S + AnsiDoubleQuote;
  1465. end;
  1466. function StrEnsureNoPrefix(const Prefix, Text: AnsiString): AnsiString;
  1467. var
  1468. PrefixLen: SizeInt;
  1469. begin
  1470. PrefixLen := Length(Prefix);
  1471. if Copy(Text, 1, PrefixLen) = Prefix then
  1472. Result := Copy(Text, PrefixLen + 1, Length(Text))
  1473. else
  1474. Result := Text;
  1475. end;
  1476. function StrEnsureNoSuffix(const Suffix, Text: AnsiString): AnsiString;
  1477. var
  1478. SuffixLen: SizeInt;
  1479. StrLength: SizeInt;
  1480. begin
  1481. SuffixLen := Length(Suffix);
  1482. StrLength := Length(Text);
  1483. if Copy(Text, StrLength - SuffixLen + 1, SuffixLen) = Suffix then
  1484. Result := Copy(Text, 1, StrLength - SuffixLen)
  1485. else
  1486. Result := Text;
  1487. end;
  1488. function StrEnsurePrefix(const Prefix, Text: AnsiString): AnsiString;
  1489. var
  1490. PrefixLen: SizeInt;
  1491. begin
  1492. PrefixLen := Length(Prefix);
  1493. if Copy(Text, 1, PrefixLen) = Prefix then
  1494. Result := Text
  1495. else
  1496. Result := Prefix + Text;
  1497. end;
  1498. function StrEnsureSuffix(const Suffix, Text: AnsiString): AnsiString;
  1499. var
  1500. SuffixLen: SizeInt;
  1501. begin
  1502. SuffixLen := Length(Suffix);
  1503. if Copy(Text, Length(Text) - SuffixLen + 1, SuffixLen) = Suffix then
  1504. Result := Text
  1505. else
  1506. Result := Text + Suffix;
  1507. end;
  1508. function StrEscapedToString(const S: AnsiString): AnsiString;
  1509. procedure HandleHexEscapeSeq(const S: AnsiString; var I: SizeInt; Len: SizeInt; var Dest: AnsiString);
  1510. const
  1511. HexDigits = AnsiString('0123456789abcdefABCDEF');
  1512. var
  1513. StartI, Val, N: SizeInt;
  1514. begin
  1515. StartI := I;
  1516. N := Pos(S[I + 1], HexDigits) - 1;
  1517. if N < 0 then
  1518. // '\x' without hex digit following is not escape sequence
  1519. Dest := Dest + '\x'
  1520. else
  1521. begin
  1522. Inc(I); // Jump over x
  1523. if N >= 16 then
  1524. N := N - 6;
  1525. Val := N;
  1526. // Same for second digit
  1527. if I < Len then
  1528. begin
  1529. N := Pos(S[I + 1], HexDigits) - 1;
  1530. if N >= 0 then
  1531. begin
  1532. Inc(I); // Jump over first digit
  1533. if N >= 16 then
  1534. N := N - 6;
  1535. Val := Val * 16 + N;
  1536. end;
  1537. end;
  1538. if Val > Ord(High(AnsiChar)) then
  1539. raise EJclAnsiStringError.CreateResFmt(@RsNumericConstantTooLarge, [Val, StartI]);
  1540. Dest := Dest + AnsiChar(Val);
  1541. end;
  1542. end;
  1543. procedure HandleOctEscapeSeq(const S: AnsiString; var I: SizeInt; Len: SizeInt; var Dest: AnsiString);
  1544. const
  1545. OctDigits = AnsiString('01234567');
  1546. var
  1547. StartI, Val, N: SizeInt;
  1548. begin
  1549. StartI := I;
  1550. // first digit
  1551. Val := Pos(S[I], OctDigits) - 1;
  1552. if I < Len then
  1553. begin
  1554. N := Pos(S[I + 1], OctDigits) - 1;
  1555. if N >= 0 then
  1556. begin
  1557. Inc(I);
  1558. Val := Val * 8 + N;
  1559. end;
  1560. if I < Len then
  1561. begin
  1562. N := Pos(S[I + 1], OctDigits) - 1;
  1563. if N >= 0 then
  1564. begin
  1565. Inc(I);
  1566. Val := Val * 8 + N;
  1567. end;
  1568. end;
  1569. end;
  1570. if Val > Ord(High(AnsiChar)) then
  1571. raise EJclAnsiStringError.CreateResFmt(@RsNumericConstantTooLarge, [Val, StartI]);
  1572. Dest := Dest + AnsiChar(Val);
  1573. end;
  1574. var
  1575. I, Len: SizeInt;
  1576. begin
  1577. Result := '';
  1578. I := 1;
  1579. Len := Length(S);
  1580. while I <= Len do
  1581. begin
  1582. if not ((S[I] = '\') and (I < Len)) then
  1583. Result := Result + S[I]
  1584. else
  1585. begin
  1586. Inc(I); // Jump over escape character
  1587. case S[I] of
  1588. 'a':
  1589. Result := Result + AnsiBell;
  1590. 'b':
  1591. Result := Result + AnsiBackspace;
  1592. 'f':
  1593. Result := Result + AnsiFormFeed;
  1594. 'n':
  1595. Result := Result + AnsiLineFeed;
  1596. 'r':
  1597. Result := Result + AnsiCarriageReturn;
  1598. 't':
  1599. Result := Result + AnsiTab;
  1600. 'v':
  1601. Result := Result + AnsiVerticalTab;
  1602. '\':
  1603. Result := Result + '\';
  1604. '"':
  1605. Result := Result + '"';
  1606. '''':
  1607. Result := Result + ''''; // Optionally escaped
  1608. '?':
  1609. Result := Result + '?'; // Optionally escaped
  1610. 'x':
  1611. if I < Len then
  1612. // Start of hex escape sequence
  1613. HandleHexEscapeSeq(S, I, Len, Result)
  1614. else
  1615. // '\x' at end of AnsiString is not escape sequence
  1616. Result := Result + '\x';
  1617. '0'..'7':
  1618. // start of octal escape sequence
  1619. HandleOctEscapeSeq(S, I, Len, Result);
  1620. else
  1621. // no escape sequence
  1622. Result := Result + '\' + S[I];
  1623. end;
  1624. end;
  1625. Inc(I);
  1626. end;
  1627. end;
  1628. function StrLower(const S: AnsiString): AnsiString;
  1629. begin
  1630. Result := S;
  1631. StrLowerInPlace(Result);
  1632. end;
  1633. procedure StrLowerInPlace(var S: AnsiString);
  1634. begin
  1635. StrCase(S, AnsiLoOffset);
  1636. end;
  1637. procedure StrLowerBuff(S: PAnsiChar);
  1638. begin
  1639. StrCaseBuff(S, AnsiLoOffset);
  1640. end;
  1641. procedure StrMove(var Dest: AnsiString; const Source: AnsiString;
  1642. const ToIndex, FromIndex, Count: SizeInt);
  1643. begin
  1644. // Check strings
  1645. if (Source = '') or (Length(Dest) = 0) then
  1646. Exit;
  1647. // Check FromIndex
  1648. if (FromIndex <= 0) or (FromIndex > Length(Source)) or
  1649. (ToIndex <= 0) or (ToIndex > Length(Dest)) or
  1650. ((FromIndex + Count - 1) > Length(Source)) or ((ToIndex + Count - 1) > Length(Dest)) then
  1651. { TODO : Is failure without notice the proper thing to do here? }
  1652. Exit;
  1653. // Move
  1654. Move(Source[FromIndex], Dest[ToIndex], Count);
  1655. end;
  1656. function StrPadLeft(const S: AnsiString; Len: SizeInt; C: AnsiChar): AnsiString;
  1657. var
  1658. L: SizeInt;
  1659. begin
  1660. L := Length(S);
  1661. if L < Len then
  1662. Result := StringOfChar(C, Len - L) + S
  1663. else
  1664. Result := S;
  1665. end;
  1666. function StrPadRight(const S: AnsiString; Len: SizeInt; C: AnsiChar): AnsiString;
  1667. var
  1668. L: SizeInt;
  1669. begin
  1670. L := Length(S);
  1671. if L < Len then
  1672. Result := S + StringOfChar(C, Len - L)
  1673. else
  1674. Result := S;
  1675. end;
  1676. function StrProper(const S: AnsiString): AnsiString;
  1677. begin
  1678. Result := StrLower(S);
  1679. if Result <> '' then
  1680. Result[1] := UpCase(Result[1]);
  1681. end;
  1682. procedure StrProperBuff(S: PAnsiChar);
  1683. begin
  1684. if (S <> nil) and (S^ <> #0) then
  1685. begin
  1686. StrLowerBuff(S);
  1687. S^ := CharUpper(S^);
  1688. end;
  1689. end;
  1690. function StrQuote(const S: AnsiString; C: AnsiChar): AnsiString;
  1691. var
  1692. L: SizeInt;
  1693. begin
  1694. L := Length(S);
  1695. Result := S;
  1696. if L > 0 then
  1697. begin
  1698. if Result[1] <> C then
  1699. begin
  1700. Result := C + Result;
  1701. Inc(L);
  1702. end;
  1703. if Result[L] <> C then
  1704. Result := Result + C;
  1705. end;
  1706. end;
  1707. function StrRemoveChars(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  1708. var
  1709. Source, Dest: PAnsiChar;
  1710. Index, Len: SizeInt;
  1711. begin
  1712. Len := Length(S);
  1713. SetLength(Result, Len);
  1714. UniqueString(Result);
  1715. Source := PAnsiChar(S);
  1716. Dest := PAnsiChar(Result);
  1717. for Index := 0 to Len - 1 do
  1718. begin
  1719. if not (Source^ in Chars) then
  1720. begin
  1721. Dest^ := Source^;
  1722. Inc(Dest);
  1723. end;
  1724. Inc(Source);
  1725. end;
  1726. SetLength(Result, Dest - PAnsiChar(Result));
  1727. end;
  1728. function StrKeepChars(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  1729. var
  1730. Source, Dest: PAnsiChar;
  1731. Index, Len: SizeInt;
  1732. begin
  1733. Len := Length(S);
  1734. SetLength(Result, Len);
  1735. UniqueString(Result);
  1736. Source := PAnsiChar(S);
  1737. Dest := PAnsiChar(Result);
  1738. for Index := 0 to Len - 1 do
  1739. begin
  1740. if Source^ in Chars then
  1741. begin
  1742. Dest^ := Source^;
  1743. Inc(Dest);
  1744. end;
  1745. Inc(Source);
  1746. end;
  1747. SetLength(Result, Dest - PAnsiChar(Result));
  1748. end;
  1749. function StrRepeat(const S: AnsiString; Count: SizeInt): AnsiString;
  1750. var
  1751. L: SizeInt;
  1752. P: PAnsiChar;
  1753. begin
  1754. L := Length(S);
  1755. SetLength(Result, Count * L);
  1756. P := Pointer(Result);
  1757. if P <> nil then
  1758. begin
  1759. while Count > 0 do
  1760. begin
  1761. Move(Pointer(S)^, P^, L);
  1762. P := P + L;
  1763. Dec(Count);
  1764. end;
  1765. end;
  1766. end;
  1767. function StrRepeatLength(const S: AnsiString; const L: SizeInt): AnsiString;
  1768. var
  1769. Count: SizeInt;
  1770. LenS: SizeInt;
  1771. P: PAnsiChar;
  1772. begin
  1773. Result := '';
  1774. LenS := Length(S);
  1775. if (LenS > 0) and (S <> '') then
  1776. begin
  1777. Count := L div LenS;
  1778. if Count * LenS < L then
  1779. Inc(Count);
  1780. SetLength(Result, Count * LenS);
  1781. P := Pointer(Result);
  1782. while Count > 0 do
  1783. begin
  1784. Move(Pointer(S)^, P^, LenS);
  1785. P := P + LenS;
  1786. Dec(Count);
  1787. end;
  1788. if Length(S) > L then
  1789. SetLength(Result, L);
  1790. end;
  1791. end;
  1792. procedure StrReplace(var S: AnsiString; const Search, Replace: AnsiString; Flags: TReplaceFlags);
  1793. var
  1794. SearchStr: AnsiString;
  1795. ResultStr: AnsiString; { result string }
  1796. SourcePtr: PAnsiChar; { pointer into S of character under examination }
  1797. SourceMatchPtr: PAnsiChar; { pointers into S and Search when first character has }
  1798. SearchMatchPtr: PAnsiChar; { been matched and we're probing for a complete match }
  1799. ResultPtr: PAnsiChar; { pointer into Result of character being written }
  1800. ResultIndex: SizeInt;
  1801. SearchLength: SizeInt; { length of search string }
  1802. ReplaceLength: SizeInt; { length of replace string }
  1803. BufferLength: SizeInt; { length of temporary result buffer }
  1804. ResultLength: SizeInt; { length of result string }
  1805. C: AnsiChar; { first character of search string }
  1806. IgnoreCase: Boolean;
  1807. begin
  1808. if Search = '' then
  1809. begin
  1810. if S = '' then
  1811. begin
  1812. S := Replace;
  1813. Exit;
  1814. end
  1815. else
  1816. raise EJclAnsiStringError.CreateRes(@RsBlankSearchString);
  1817. end;
  1818. if S <> '' then
  1819. begin
  1820. IgnoreCase := rfIgnoreCase in Flags;
  1821. if IgnoreCase then
  1822. SearchStr := StrUpper(Search)
  1823. else
  1824. SearchStr := Search;
  1825. { avoid having to call Length() within the loop }
  1826. SearchLength := Length(Search);
  1827. ReplaceLength := Length(Replace);
  1828. ResultLength := Length(S);
  1829. BufferLength := ResultLength;
  1830. SetLength(ResultStr, BufferLength);
  1831. { get pointers to begin of source and result }
  1832. ResultPtr := PAnsiChar(ResultStr);
  1833. SourcePtr := PAnsiChar(S);
  1834. C := SearchStr[1];
  1835. { while we haven't reached the end of the string }
  1836. while True do
  1837. begin
  1838. { copy characters until we find the first character of the search string }
  1839. if IgnoreCase then
  1840. while (CharUpper(SourcePtr^) <> C) and (SourcePtr^ <> #0) do
  1841. begin
  1842. ResultPtr^ := SourcePtr^;
  1843. Inc(ResultPtr);
  1844. Inc(SourcePtr);
  1845. end
  1846. else
  1847. while (SourcePtr^ <> C) and (SourcePtr^ <> #0) do
  1848. begin
  1849. ResultPtr^ := SourcePtr^;
  1850. Inc(ResultPtr);
  1851. Inc(SourcePtr);
  1852. end;
  1853. { did we find that first character or did we hit the end of the string? }
  1854. if SourcePtr^ = #0 then
  1855. Break
  1856. else
  1857. begin
  1858. { continue comparing, +1 because first character was matched already }
  1859. SourceMatchPtr := SourcePtr + 1;
  1860. SearchMatchPtr := PAnsiChar(SearchStr) + 1;
  1861. if IgnoreCase then
  1862. while (CharUpper(SourceMatchPtr^) = SearchMatchPtr^) and (SearchMatchPtr^ <> #0) do
  1863. begin
  1864. Inc(SourceMatchPtr);
  1865. Inc(SearchMatchPtr);
  1866. end
  1867. else
  1868. while (SourceMatchPtr^ = SearchMatchPtr^) and (SearchMatchPtr^ <> #0) do
  1869. begin
  1870. Inc(SourceMatchPtr);
  1871. Inc(SearchMatchPtr);
  1872. end;
  1873. { did we find a complete match? }
  1874. if SearchMatchPtr^ = #0 then
  1875. begin
  1876. // keep track of result length
  1877. Inc(ResultLength, ReplaceLength - SearchLength);
  1878. if ReplaceLength > 0 then
  1879. begin
  1880. // increase buffer size if required
  1881. if ResultLength > BufferLength then
  1882. begin
  1883. BufferLength := ResultLength * 2;
  1884. ResultIndex := ResultPtr - PAnsiChar(ResultStr) + 1;
  1885. SetLength(ResultStr, BufferLength);
  1886. ResultPtr := @ResultStr[ResultIndex];
  1887. end;
  1888. { append replace to result and move past the search string in source }
  1889. Move((@Replace[1])^, ResultPtr^, ReplaceLength);
  1890. end;
  1891. Inc(SourcePtr, SearchLength);
  1892. Inc(ResultPtr, ReplaceLength);
  1893. { replace all instances or just one? }
  1894. if not (rfReplaceAll in Flags) then
  1895. begin
  1896. { just one, copy until end of source and break out of loop }
  1897. while SourcePtr^ <> #0 do
  1898. begin
  1899. ResultPtr^ := SourcePtr^;
  1900. Inc(ResultPtr);
  1901. Inc(SourcePtr);
  1902. end;
  1903. Break;
  1904. end;
  1905. end
  1906. else
  1907. begin
  1908. { copy current character and start over with the next }
  1909. ResultPtr^ := SourcePtr^;
  1910. Inc(ResultPtr);
  1911. Inc(SourcePtr);
  1912. end;
  1913. end;
  1914. end;
  1915. { set result length and copy result into S }
  1916. SetLength(ResultStr, ResultLength);
  1917. S := ResultStr;
  1918. end;
  1919. end;
  1920. function StrReplaceChar(const S: AnsiString; const Source, Replace: AnsiChar): AnsiString;
  1921. var
  1922. I: SizeInt;
  1923. begin
  1924. Result := S;
  1925. for I := 1 to Length(S) do
  1926. if Result[I] = Source then
  1927. Result[I] := Replace;
  1928. end;
  1929. function StrReplaceChars(const S: AnsiString; const Chars: TSysCharSet; Replace: AnsiChar): AnsiString;
  1930. var
  1931. I: SizeInt;
  1932. begin
  1933. Result := S;
  1934. for I := 1 to Length(S) do
  1935. if Result[I] in Chars then
  1936. Result[I] := Replace;
  1937. end;
  1938. function StrReplaceButChars(const S: AnsiString; const Chars: TSysCharSet;
  1939. Replace: AnsiChar): AnsiString;
  1940. var
  1941. I: SizeInt;
  1942. begin
  1943. Result := S;
  1944. for I := 1 to Length(S) do
  1945. if not (Result[I] in Chars) then
  1946. Result[I] := Replace;
  1947. end;
  1948. function StrReverse(const S: AnsiString): AnsiString;
  1949. begin
  1950. Result := S;
  1951. StrReverseInplace(Result);
  1952. end;
  1953. procedure StrReverseInPlace(var S: AnsiString);
  1954. var
  1955. P1, P2: PAnsiChar;
  1956. C: AnsiChar;
  1957. begin
  1958. UniqueString(S);
  1959. P1 := PAnsiChar(S);
  1960. P2 := P1 + SizeOf(AnsiChar) * (Length(S) - 1);
  1961. while P1 < P2 do
  1962. begin
  1963. C := P1^;
  1964. P1^ := P2^;
  1965. P2^ := C;
  1966. Inc(P1);
  1967. Dec(P2);
  1968. end;
  1969. end;
  1970. function StrSingleQuote(const S: AnsiString): AnsiString;
  1971. begin
  1972. Result := AnsiSingleQuote + S + AnsiSingleQuote;
  1973. end;
  1974. procedure StrSkipChars(var S: PAnsiChar; const Chars: TSysCharSet);
  1975. begin
  1976. while S^ in Chars do
  1977. Inc(S);
  1978. end;
  1979. procedure StrSkipChars(const S: AnsiString; var Index: SizeInt; const Chars: TSysCharSet);
  1980. begin
  1981. while S[Index] in Chars do
  1982. Inc(Index);
  1983. end;
  1984. function StrSmartCase(const S: AnsiString; Delimiters: TSysCharSet): AnsiString;
  1985. var
  1986. Source, Dest: PAnsiChar;
  1987. Index, Len: SizeInt;
  1988. begin
  1989. Result := '';
  1990. if Delimiters = [] then
  1991. Include(Delimiters, AnsiSpace);
  1992. if S <> '' then
  1993. begin
  1994. Result := S;
  1995. UniqueString(Result);
  1996. Len := Length(S);
  1997. Source := PAnsiChar(S);
  1998. Dest := PAnsiChar(Result);
  1999. Inc(Dest);
  2000. for Index := 2 to Len do
  2001. begin
  2002. if (Source^ in Delimiters) then
  2003. Dest^ := CharUpper(Dest^);
  2004. Inc(Dest);
  2005. Inc(Source);
  2006. end;
  2007. Result[1] := CharUpper(Result[1]);
  2008. end;
  2009. end;
  2010. function StrStringToEscaped(const S: AnsiString): AnsiString;
  2011. var
  2012. I: SizeInt;
  2013. begin
  2014. Result := '';
  2015. for I := 1 to Length(S) do
  2016. begin
  2017. case S[I] of
  2018. AnsiBackspace:
  2019. Result := Result + '\b';
  2020. AnsiBell:
  2021. Result := Result + '\a';
  2022. AnsiCarriageReturn:
  2023. Result := Result + '\r';
  2024. AnsiFormFeed:
  2025. Result := Result + '\f';
  2026. AnsiLineFeed:
  2027. Result := Result + '\n';
  2028. AnsiTab:
  2029. Result := Result + '\t';
  2030. AnsiVerticalTab:
  2031. Result := Result + '\v';
  2032. '\':
  2033. Result := Result + '\\';
  2034. '"':
  2035. Result := Result + '\"';
  2036. else
  2037. // Characters < ' ' are escaped with hex sequence
  2038. if S[I] < #32 then
  2039. Result := Result + AnsiString(Format('\x%.2x', [SizeInt(S[I])]))
  2040. else
  2041. Result := Result + S[I];
  2042. end;
  2043. end;
  2044. end;
  2045. function StrStripNonNumberChars(const S: AnsiString): AnsiString;
  2046. var
  2047. I: SizeInt;
  2048. C: AnsiChar;
  2049. begin
  2050. Result := '';
  2051. for I := 1 to Length(S) do
  2052. begin
  2053. C := S[I];
  2054. if CharIsNumberChar(C) then
  2055. Result := Result + C;
  2056. end;
  2057. end;
  2058. function StrToHex(const Source: AnsiString): AnsiString;
  2059. var
  2060. Index: SizeInt;
  2061. C, L, N: SizeInt;
  2062. BL, BH: Byte;
  2063. S: AnsiString;
  2064. begin
  2065. Result := '';
  2066. if Source <> '' then
  2067. begin
  2068. S := Source;
  2069. L := Length(S);
  2070. if Odd(L) then
  2071. begin
  2072. S := '0' + S;
  2073. Inc(L);
  2074. end;
  2075. Index := 1;
  2076. SetLength(Result, L div 2);
  2077. C := 1;
  2078. N := 1;
  2079. while C <= L do
  2080. begin
  2081. BH := CharHex(S[Index]);
  2082. Inc(Index);
  2083. BL := CharHex(S[Index]);
  2084. Inc(Index);
  2085. Inc(C, 2);
  2086. if (BH = $FF) or (BL = $FF) then
  2087. begin
  2088. Result := '';
  2089. Exit;
  2090. end;
  2091. Result[N] := AnsiChar((Cardinal(BH) shl 4) or Cardinal(BL));
  2092. Inc(N);
  2093. end;
  2094. end;
  2095. end;
  2096. function StrTrimCharLeft(const S: AnsiString; C: AnsiChar): AnsiString;
  2097. var
  2098. I, L: SizeInt;
  2099. begin
  2100. I := 1;
  2101. L := Length(S);
  2102. while (I <= L) and (S[I] = C) do
  2103. Inc(I);
  2104. Result := Copy(S, I, L - I + 1);
  2105. end;
  2106. function StrTrimCharsLeft(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  2107. var
  2108. I, L: SizeInt;
  2109. begin
  2110. I := 1;
  2111. L := Length(S);
  2112. while (I <= L) and (S[I] in Chars) do
  2113. Inc(I);
  2114. Result := Copy(S, I, L - I + 1);
  2115. end;
  2116. function StrTrimCharsRight(const S: AnsiString; const Chars: TSysCharSet): AnsiString;
  2117. var
  2118. I: SizeInt;
  2119. begin
  2120. I := Length(S);
  2121. while (I >= 1) and (S[I] in Chars) do
  2122. Dec(I);
  2123. Result := Copy(S, 1, I);
  2124. end;
  2125. function StrTrimCharRight(const S: AnsiString; C: AnsiChar): AnsiString;
  2126. var
  2127. I: SizeInt;
  2128. begin
  2129. I := Length(S);
  2130. while (I >= 1) and (S[I] = C) do
  2131. Dec(I);
  2132. Result := Copy(S, 1, I);
  2133. end;
  2134. function StrTrimQuotes(const S: AnsiString): AnsiString;
  2135. var
  2136. First, Last: AnsiChar;
  2137. L: SizeInt;
  2138. begin
  2139. L := Length(S);
  2140. if L > 1 then
  2141. begin
  2142. First := S[1];
  2143. Last := S[L];
  2144. if (First = Last) and ((First = AnsiSingleQuote) or (First = AnsiDoubleQuote)) then
  2145. Result := Copy(S, 2, L - 2)
  2146. else
  2147. Result := S;
  2148. end
  2149. else
  2150. Result := S;
  2151. end;
  2152. function StrTrimQuotes(const S: AnsiString; QuoteChar: AnsiChar): AnsiString;
  2153. var
  2154. First, Last: AnsiChar;
  2155. L: SizeInt;
  2156. begin
  2157. L := Length(S);
  2158. if L > 1 then
  2159. begin
  2160. First := S[1];
  2161. Last := S[L];
  2162. if (First = Last) and (First = QuoteChar) then
  2163. Result := Copy(S, 2, L - 2)
  2164. else
  2165. Result := S;
  2166. end
  2167. else
  2168. Result := S;
  2169. end;
  2170. function StrUpper(const S: AnsiString): AnsiString;
  2171. begin
  2172. Result := S;
  2173. StrUpperInPlace(Result);
  2174. end;
  2175. procedure StrUpperInPlace(var S: AnsiString);
  2176. begin
  2177. StrCase(S, AnsiUpOffset);
  2178. end;
  2179. procedure StrUpperBuff(S: PAnsiChar);
  2180. begin
  2181. StrCaseBuff(S, AnsiUpOffset);
  2182. end;
  2183. {$IFDEF MSWINDOWS}
  2184. function StrOemToAnsi(const S: AnsiString): AnsiString;
  2185. begin
  2186. SetLength(Result, Length(S));
  2187. OemToAnsiBuff(PAnsiChar(S), PAnsiChar(Result), Length(S));
  2188. end;
  2189. function StrAnsiToOem(const S: AnsiString): AnsiString;
  2190. begin
  2191. SetLength(Result, Length(S));
  2192. AnsiToOemBuff(PAnsiChar(S), PAnsiChar(Result), Length(S));
  2193. end;
  2194. {$ENDIF MSWINDOWS}
  2195. //=== String Management ======================================================
  2196. procedure StrAddRef(var S: AnsiString);
  2197. var
  2198. P: PAnsiStrRec;
  2199. begin
  2200. P := Pointer(S);
  2201. if P <> nil then
  2202. begin
  2203. Dec(P);
  2204. if P^.RefCount = -1 then
  2205. UniqueString(S)
  2206. else
  2207. LockedInc(P^.RefCount);
  2208. end;
  2209. end;
  2210. procedure StrDecRef(var S: AnsiString);
  2211. var
  2212. P: PAnsiStrRec;
  2213. begin
  2214. P := Pointer(S);
  2215. if P <> nil then
  2216. begin
  2217. Dec(P);
  2218. case P^.RefCount of
  2219. -1, 0:
  2220. { nothing } ;
  2221. 1:
  2222. begin
  2223. Finalize(S);
  2224. Pointer(S) := nil;
  2225. end;
  2226. else
  2227. LockedDec(P^.RefCount);
  2228. end;
  2229. end;
  2230. end;
  2231. function StrLength(const S: AnsiString): Longint;
  2232. var
  2233. P: PAnsiStrRec;
  2234. begin
  2235. Result := 0;
  2236. P := Pointer(S);
  2237. if P <> nil then
  2238. begin
  2239. Dec(P);
  2240. Result := P^.Length and (not $80000000 shr 1);
  2241. end;
  2242. end;
  2243. function StrRefCount(const S: AnsiString): Longint;
  2244. var
  2245. P: PAnsiStrRec;
  2246. begin
  2247. Result := 0;
  2248. P := Pointer(S);
  2249. if P <> nil then
  2250. begin
  2251. Dec(P);
  2252. Result := P^.RefCount;
  2253. end;
  2254. end;
  2255. procedure StrResetLength(var S: AnsiString);
  2256. var
  2257. I: SizeInt;
  2258. begin
  2259. for I := 0 to Length(S) - 1 do
  2260. if S[I + 1] = #0 then
  2261. begin
  2262. SetLength(S, I);
  2263. Exit;
  2264. end;
  2265. end;
  2266. //=== String Search and Replace Routines =====================================
  2267. function StrCharCount(const S: AnsiString; C: AnsiChar): SizeInt;
  2268. var
  2269. I: SizeInt;
  2270. begin
  2271. Result := 0;
  2272. for I := 1 to Length(S) do
  2273. if S[I] = C then
  2274. Inc(Result);
  2275. end;
  2276. function StrCharsCount(const S: AnsiString; Chars: TSysCharSet): SizeInt;
  2277. var
  2278. I: SizeInt;
  2279. begin
  2280. Result := 0;
  2281. for I := 1 to Length(S) do
  2282. if S[I] in Chars then
  2283. Inc(Result);
  2284. end;
  2285. function StrStrCount(const S, SubS: AnsiString): SizeInt;
  2286. var
  2287. I: SizeInt;
  2288. begin
  2289. Result := 0;
  2290. if (Length(SubS) > Length(S)) or (Length(SubS) = 0) or (Length(S) = 0) then
  2291. Exit;
  2292. if Length(SubS) = 1 then
  2293. begin
  2294. Result := StrCharCount(S, SubS[1]);
  2295. Exit;
  2296. end;
  2297. I := StrSearch(SubS, S, 1);
  2298. if I > 0 then
  2299. Inc(Result);
  2300. while (I > 0) and (Length(S) > I + Length(SubS)) do
  2301. begin
  2302. I := StrSearch(SubS, S, I + 1);
  2303. if I > 0 then
  2304. Inc(Result);
  2305. end;
  2306. end;
  2307. (*
  2308. { 1} Test(StrCompareRange('', '', 1, 5), 0);
  2309. { 2} Test(StrCompareRange('A', '', 1, 5), -1);
  2310. { 3} Test(StrCompareRange('AB', '', 1, 5), -1);
  2311. { 4} Test(StrCompareRange('ABC', '', 1, 5), -1);
  2312. { 5} Test(StrCompareRange('', 'A', 1, 5), -1);
  2313. { 6} Test(StrCompareRange('', 'AB', 1, 5), -1);
  2314. { 7} Test(StrCompareRange('', 'ABC', 1, 5), -1);
  2315. { 8} Test(StrCompareRange('A', 'a', 1, 5), -2);
  2316. { 9} Test(StrCompareRange('A', 'a', 1, 1), -32);
  2317. {10} Test(StrCompareRange('aA', 'aB', 1, 1), 0);
  2318. {11} Test(StrCompareRange('aA', 'aB', 1, 2), -1);
  2319. {12} Test(StrCompareRange('aB', 'aA', 1, 2), 1);
  2320. {13} Test(StrCompareRange('aA', 'aa', 1, 2), -32);
  2321. {14} Test(StrCompareRange('aa', 'aA', 1, 2), 32);
  2322. {15} Test(StrCompareRange('', '', 1, 0), 0);
  2323. {16} Test(StrCompareRange('A', 'A', 1, 0), -2);
  2324. {17} Test(StrCompareRange('Aa', 'A', 1, 0), -2);
  2325. {18} Test(StrCompareRange('Aa', 'Aa', 1, 2), 0);
  2326. {19} Test(StrCompareRange('Aa', 'A', 1, 2), 0);
  2327. {20} Test(StrCompareRange('Ba', 'A', 1, 2), 1);
  2328. *)
  2329. function StrCompareRangeEx(const S1, S2: AnsiString; Index, Count: SizeInt; CaseSensitive: Boolean): SizeInt;
  2330. var
  2331. Len1, Len2: SizeInt;
  2332. I: SizeInt;
  2333. C1, C2: AnsiChar;
  2334. begin
  2335. if Pointer(S1) = Pointer(S2) then
  2336. begin
  2337. if (Count <= 0) and (S1 <> '') then
  2338. Result := -2 // no work
  2339. else
  2340. Result := 0;
  2341. end
  2342. else
  2343. if (S1 = '') or (S2 = '') then
  2344. Result := -1 // null string
  2345. else
  2346. if Count <= 0 then
  2347. Result := -2 // no work
  2348. else
  2349. begin
  2350. Len1 := Length(S1);
  2351. Len2 := Length(S2);
  2352. if (Index - 1) + Count > Len1 then
  2353. Result := -2
  2354. else
  2355. begin
  2356. if (Index - 1) + Count > Len2 then // strange behaviour, but the assembler code does it
  2357. Count := Len2 - (Index - 1);
  2358. if CaseSensitive then
  2359. begin
  2360. for I := 0 to Count - 1 do
  2361. begin
  2362. C1 := S1[Index + I];
  2363. C2 := S2[Index + I];
  2364. if C1 <> C2 then
  2365. begin
  2366. Result := Ord(C1) - Ord(C2);
  2367. Exit;
  2368. end;
  2369. end;
  2370. end
  2371. else
  2372. begin
  2373. for I := 0 to Count - 1 do
  2374. begin
  2375. C1 := S1[Index + I];
  2376. C2 := S2[Index + I];
  2377. if C1 <> C2 then
  2378. begin
  2379. C1 := CharLower(C1);
  2380. C2 := CharLower(C2);
  2381. if C1 <> C2 then
  2382. begin
  2383. Result := Ord(C1) - Ord(C2);
  2384. Exit;
  2385. end;
  2386. end;
  2387. end;
  2388. end;
  2389. Result := 0;
  2390. end;
  2391. end;
  2392. end;
  2393. function StrCompare(const S1, S2: AnsiString; CaseSensitive: Boolean): SizeInt;
  2394. var
  2395. Len1, Len2: SizeInt;
  2396. begin
  2397. if Pointer(S1) = Pointer(S2) then
  2398. Result := 0
  2399. else
  2400. begin
  2401. Len1 := Length(S1);
  2402. Len2 := Length(S2);
  2403. Result := Len1 - Len2;
  2404. if Result = 0 then
  2405. Result := StrCompareRangeEx(S1, S2, 1, Len1, CaseSensitive);
  2406. end;
  2407. end;
  2408. function StrCompareRange(const S1, S2: AnsiString; Index, Count: SizeInt; CaseSensitive: Boolean): SizeInt;
  2409. begin
  2410. Result := StrCompareRangeEx(S1, S2, Index, Count, CaseSensitive);
  2411. end;
  2412. function StrRepeatChar(C: AnsiChar; Count: SizeInt): AnsiString;
  2413. begin
  2414. SetLength(Result, Count);
  2415. if Count > 0 then
  2416. FillChar(Result[1], Count, C);
  2417. end;
  2418. function StrFind(const Substr, S: AnsiString; const Index: SizeInt): SizeInt;
  2419. var
  2420. pos: SizeInt;
  2421. begin
  2422. if (SubStr <> '') and (S <> '') then
  2423. begin
  2424. pos := StrIPos(Substr, Copy(S, Index, Length(S) - Index + 1));
  2425. if pos = 0 then
  2426. Result := 0
  2427. else
  2428. Result := Index + Pos - 1;
  2429. end
  2430. else
  2431. Result := 0;
  2432. end;
  2433. function StrHasPrefix(const S: AnsiString; const Prefixes: array of AnsiString): Boolean;
  2434. begin
  2435. Result := StrPrefixIndex(S, Prefixes) > -1;
  2436. end;
  2437. function StrHasSuffix(const S: AnsiString; const Suffixes: array of AnsiString): Boolean;
  2438. begin
  2439. Result := StrSuffixIndex(S, Suffixes) > -1;
  2440. end;
  2441. function StrIHasPrefix(const S: AnsiString; const Prefixes: array of AnsiString): Boolean;
  2442. begin
  2443. Result := StrIPrefixIndex(S, Prefixes) > -1;
  2444. end;
  2445. function StrIHasSuffix(const S: AnsiString; const Suffixes: array of AnsiString): Boolean;
  2446. begin
  2447. Result := StrISuffixIndex(S, Suffixes) > -1;
  2448. end;
  2449. function StrIndex(const S: AnsiString; const List: array of AnsiString; CaseSensitive: Boolean): SizeInt;
  2450. var
  2451. I: SizeInt;
  2452. begin
  2453. Result := -1;
  2454. for I := Low(List) to High(List) do
  2455. begin
  2456. if StrCompare(S, List[I], CaseSensitive) = 0 then
  2457. begin
  2458. Result := I;
  2459. Break;
  2460. end;
  2461. end;
  2462. end;
  2463. function StrILastPos(const SubStr, S: AnsiString): SizeInt;
  2464. begin
  2465. Result := StrLastPos(StrUpper(SubStr), StrUpper(S));
  2466. end;
  2467. function StrIPos(const SubStr, S: AnsiString): SizeInt;
  2468. begin
  2469. Result := Pos(StrUpper(SubStr), StrUpper(S));
  2470. end;
  2471. function StrIPrefixIndex(const S: AnsiString; const Prefixes: array of AnsiString): SizeInt;
  2472. var
  2473. I: SizeInt;
  2474. Test: AnsiString;
  2475. begin
  2476. Result := -1;
  2477. for I := Low(Prefixes) to High(Prefixes) do
  2478. begin
  2479. Test := StrLeft(S, Length(Prefixes[I]));
  2480. if CompareText(Test, Prefixes[I]) = 0 then
  2481. begin
  2482. Result := I;
  2483. Break;
  2484. end;
  2485. end;
  2486. end;
  2487. function StrIsOneOf(const S: AnsiString; const List: array of AnsiString): Boolean;
  2488. begin
  2489. Result := StrIndex(S, List) > -1;
  2490. end;
  2491. function StrISuffixIndex(const S: AnsiString; const Suffixes: array of AnsiString): SizeInt;
  2492. var
  2493. I: SizeInt;
  2494. Test: AnsiString;
  2495. begin
  2496. Result := -1;
  2497. for I := Low(Suffixes) to High(Suffixes) do
  2498. begin
  2499. Test := StrRight(S, Length(Suffixes[I]));
  2500. if CompareText(Test, Suffixes[I]) = 0 then
  2501. begin
  2502. Result := I;
  2503. Break;
  2504. end;
  2505. end;
  2506. end;
  2507. function StrLastPos(const SubStr, S: AnsiString): SizeInt;
  2508. var
  2509. Last, Current: PAnsiChar;
  2510. begin
  2511. Result := 0;
  2512. Last := nil;
  2513. Current := PAnsiChar(S);
  2514. while (Current <> nil) and (Current^ <> #0) do
  2515. begin
  2516. Current := AnsiStrPosA(PAnsiChar(Current), PAnsiChar(SubStr));
  2517. if Current <> nil then
  2518. begin
  2519. Last := Current;
  2520. Inc(Current);
  2521. end;
  2522. end;
  2523. if Last <> nil then
  2524. Result := Abs(PAnsiChar(S) - Last) + 1;
  2525. end;
  2526. // IMPORTANT NOTE: The StrMatch function does currently not work with the Asterix (*)
  2527. // (*) acts like (?)
  2528. function StrMatch(const Substr, S: AnsiString; Index: SizeInt): SizeInt;
  2529. var
  2530. SI, SubI, SLen, SubLen: SizeInt;
  2531. SubC: AnsiChar;
  2532. begin
  2533. SLen := Length(S);
  2534. SubLen := Length(Substr);
  2535. Result := 0;
  2536. if (Index > SLen) or (SubLen = 0) then
  2537. Exit;
  2538. while Index <= SLen do
  2539. begin
  2540. SubI := 1;
  2541. SI := Index;
  2542. while (SI <= SLen) and (SubI <= SubLen) do
  2543. begin
  2544. SubC := Substr[SubI];
  2545. if (SubC = '*') or (SubC = '?') or (SubC = S[SI]) then
  2546. begin
  2547. Inc(SI);
  2548. Inc(SubI);
  2549. end
  2550. else
  2551. Break;
  2552. end;
  2553. if SubI > SubLen then
  2554. begin
  2555. Result := Index;
  2556. Break;
  2557. end;
  2558. Inc(Index);
  2559. end;
  2560. end;
  2561. // Derived from "Like" by Michael Winter
  2562. function StrMatches(const Substr, S: AnsiString; const Index: SizeInt): Boolean;
  2563. var
  2564. StringPtr: PAnsiChar;
  2565. PatternPtr: PAnsiChar;
  2566. StringRes: PAnsiChar;
  2567. PatternRes: PAnsiChar;
  2568. begin
  2569. if SubStr = '' then
  2570. raise EJclAnsiStringError.CreateRes(@RsBlankSearchString);
  2571. Result := SubStr = '*';
  2572. if Result or (S = '') then
  2573. Exit;
  2574. if (Index <= 0) or (Index > Length(S)) then
  2575. raise EJclAnsiStringError.CreateRes(@RsArgumentOutOfRange);
  2576. StringPtr := PAnsiChar(@S[Index]);
  2577. PatternPtr := PAnsiChar(SubStr);
  2578. StringRes := nil;
  2579. PatternRes := nil;
  2580. repeat
  2581. repeat
  2582. case PatternPtr^ of
  2583. #0:
  2584. begin
  2585. Result := StringPtr^ = #0;
  2586. if Result or (StringRes = nil) or (PatternRes = nil) then
  2587. Exit;
  2588. StringPtr := StringRes;
  2589. PatternPtr := PatternRes;
  2590. Break;
  2591. end;
  2592. '*':
  2593. begin
  2594. Inc(PatternPtr);
  2595. PatternRes := PatternPtr;
  2596. Break;
  2597. end;
  2598. '?':
  2599. begin
  2600. if StringPtr^ = #0 then
  2601. Exit;
  2602. Inc(StringPtr);
  2603. Inc(PatternPtr);
  2604. end;
  2605. else
  2606. begin
  2607. if StringPtr^ = #0 then
  2608. Exit;
  2609. if StringPtr^ <> PatternPtr^ then
  2610. begin
  2611. if (StringRes = nil) or (PatternRes = nil) then
  2612. Exit;
  2613. StringPtr := StringRes;
  2614. PatternPtr := PatternRes;
  2615. Break;
  2616. end
  2617. else
  2618. begin
  2619. Inc(StringPtr);
  2620. Inc(PatternPtr);
  2621. end;
  2622. end;
  2623. end;
  2624. until False;
  2625. repeat
  2626. case PatternPtr^ of
  2627. #0:
  2628. begin
  2629. Result := True;
  2630. Exit;
  2631. end;
  2632. '*':
  2633. begin
  2634. Inc(PatternPtr);
  2635. PatternRes := PatternPtr;
  2636. end;
  2637. '?':
  2638. begin
  2639. if StringPtr^ = #0 then
  2640. Exit;
  2641. Inc(StringPtr);
  2642. Inc(PatternPtr);
  2643. end;
  2644. else
  2645. begin
  2646. repeat
  2647. if StringPtr^ = #0 then
  2648. Exit;
  2649. if StringPtr^ = PatternPtr^ then
  2650. Break;
  2651. Inc(StringPtr);
  2652. until False;
  2653. Inc(StringPtr);
  2654. StringRes := StringPtr;
  2655. Inc(PatternPtr);
  2656. Break;
  2657. end;
  2658. end;
  2659. until False;
  2660. until False;
  2661. end;
  2662. function StrNPos(const S, SubStr: AnsiString; N: SizeInt): SizeInt;
  2663. var
  2664. I, P: SizeInt;
  2665. begin
  2666. if N < 1 then
  2667. begin
  2668. Result := 0;
  2669. Exit;
  2670. end;
  2671. Result := StrSearch(SubStr, S, 1);
  2672. I := 1;
  2673. while I < N do
  2674. begin
  2675. P := StrSearch(SubStr, S, Result + 1);
  2676. if P = 0 then
  2677. begin
  2678. Result := 0;
  2679. Break;
  2680. end
  2681. else
  2682. begin
  2683. Result := P;
  2684. Inc(I);
  2685. end;
  2686. end;
  2687. end;
  2688. function StrNIPos(const S, SubStr: AnsiString; N: SizeInt): SizeInt;
  2689. var
  2690. I, P: SizeInt;
  2691. begin
  2692. if N < 1 then
  2693. begin
  2694. Result := 0;
  2695. Exit;
  2696. end;
  2697. Result := StrFind(SubStr, S, 1);
  2698. I := 1;
  2699. while I < N do
  2700. begin
  2701. P := StrFind(SubStr, S, Result + 1);
  2702. if P = 0 then
  2703. begin
  2704. Result := 0;
  2705. Break;
  2706. end
  2707. else
  2708. begin
  2709. Result := P;
  2710. Inc(I);
  2711. end;
  2712. end;
  2713. end;
  2714. function StrPrefixIndex(const S: AnsiString; const Prefixes: array of AnsiString): SizeInt;
  2715. var
  2716. I: SizeInt;
  2717. Test: AnsiString;
  2718. begin
  2719. Result := -1;
  2720. for I := Low(Prefixes) to High(Prefixes) do
  2721. begin
  2722. Test := StrLeft(S, Length(Prefixes[I]));
  2723. if CompareStr(Test, Prefixes[I]) = 0 then
  2724. begin
  2725. Result := I;
  2726. Break;
  2727. end;
  2728. end;
  2729. end;
  2730. function StrSearch(const Substr, S: AnsiString; const Index: SizeInt): SizeInt;
  2731. var
  2732. SP, SPI, SubP: PAnsiChar;
  2733. SLen: SizeInt;
  2734. begin
  2735. SLen := Length(S);
  2736. if Index <= SLen then
  2737. begin
  2738. SP := PAnsiChar(S);
  2739. SubP := PAnsiChar(Substr);
  2740. SPI := SP;
  2741. Inc(SPI, Index);
  2742. Dec(SPI);
  2743. SPI := StrPosA(SPI, SubP);
  2744. if SPI <> nil then
  2745. Result := SPI - SP + 1
  2746. else
  2747. Result := 0;
  2748. end
  2749. else
  2750. Result := 0;
  2751. end;
  2752. function StrSuffixIndex(const S: AnsiString; const Suffixes: array of AnsiString): SizeInt;
  2753. var
  2754. I: SizeInt;
  2755. Test: AnsiString;
  2756. begin
  2757. Result := -1;
  2758. for I := Low(Suffixes) to High(Suffixes) do
  2759. begin
  2760. Test := StrRight(S, Length(Suffixes[I]));
  2761. if CompareStr(Test, Suffixes[I]) = 0 then
  2762. begin
  2763. Result := I;
  2764. Break;
  2765. end;
  2766. end;
  2767. end;
  2768. //=== String Extraction ======================================================
  2769. function StrAfter(const SubStr, S: AnsiString): AnsiString;
  2770. var
  2771. P: SizeInt;
  2772. begin
  2773. P := StrFind(SubStr, S, 1); // StrFind is case-insensitive pos
  2774. if P <= 0 then
  2775. Result := '' // substr not found -> nothing after it
  2776. else
  2777. Result := StrRestOf(S, P + Length(SubStr));
  2778. end;
  2779. function StrBefore(const SubStr, S: AnsiString): AnsiString;
  2780. var
  2781. P: SizeInt;
  2782. begin
  2783. P := StrFind(SubStr, S, 1);
  2784. if P <= 0 then
  2785. Result := S
  2786. else
  2787. Result := StrLeft(S, P - 1);
  2788. end;
  2789. function StrSplit(const SubStr, S: AnsiString;var Left, Right : AnsiString): boolean;
  2790. var
  2791. P: SizeInt;
  2792. begin
  2793. P := StrFind(SubStr, S, 1);
  2794. Result:= p > 0;
  2795. if Result then
  2796. begin
  2797. Left := StrLeft(S, P - 1);
  2798. Right := StrRestOf(S, P + Length(SubStr));
  2799. end
  2800. else
  2801. begin
  2802. Left := '';
  2803. Right := '';
  2804. end;
  2805. end;
  2806. function StrBetween(const S: AnsiString; const Start, Stop: AnsiChar): AnsiString;
  2807. var
  2808. PosStart, PosEnd: SizeInt;
  2809. L: SizeInt;
  2810. begin
  2811. PosStart := Pos(Start, S);
  2812. PosEnd := StrSearch(Stop, S, PosStart + 1); // PosEnd has to be after PosStart.
  2813. if (PosStart > 0) and (PosEnd > PosStart) then
  2814. begin
  2815. L := PosEnd - PosStart;
  2816. Result := Copy(S, PosStart + 1, L - 1);
  2817. end
  2818. else
  2819. Result := '';
  2820. end;
  2821. function StrChopRight(const S: AnsiString; N: SizeInt): AnsiString;
  2822. begin
  2823. Result := Copy(S, 1, Length(S) - N);
  2824. end;
  2825. function StrLeft(const S: AnsiString; Count: SizeInt): AnsiString;
  2826. begin
  2827. Result := Copy(S, 1, Count);
  2828. end;
  2829. function StrMid(const S: AnsiString; Start, Count: SizeInt): AnsiString;
  2830. begin
  2831. Result := Copy(S, Start, Count);
  2832. end;
  2833. function StrRestOf(const S: AnsiString; N: SizeInt): AnsiString;
  2834. begin
  2835. Result := Copy(S, N, (Length(S) - N + 1));
  2836. end;
  2837. function StrRight(const S: AnsiString; Count: SizeInt): AnsiString;
  2838. begin
  2839. Result := Copy(S, Length(S) - Count + 1, Count);
  2840. end;
  2841. //=== Character (do we have it ;) ============================================
  2842. function CharEqualNoCase(const C1, C2: AnsiChar): Boolean;
  2843. begin
  2844. // if they are not equal chars, may be same letter different case
  2845. Result := (C1 = C2) or
  2846. (CharIsAlpha(C1) and CharIsAlpha(C2) and (CharLower(C1) = CharLower(C2)));
  2847. end;
  2848. function CharIsAlpha(const C: AnsiChar): Boolean;
  2849. begin
  2850. Result := (AnsiCharTypes[C] and C1_ALPHA) <> 0;
  2851. end;
  2852. function CharIsAlphaNum(const C: AnsiChar): Boolean;
  2853. begin
  2854. Result := ((AnsiCharTypes[C] and C1_ALPHA) <> 0) or
  2855. ((AnsiCharTypes[C] and C1_DIGIT) <> 0);
  2856. end;
  2857. function CharIsBlank(const C: AnsiChar): Boolean;
  2858. begin
  2859. Result := ((AnsiCharTypes[C] and C1_BLANK) <> 0);
  2860. end;
  2861. function CharIsControl(const C: AnsiChar): Boolean;
  2862. begin
  2863. Result := (AnsiCharTypes[C] and C1_CNTRL) <> 0;
  2864. end;
  2865. function CharIsDelete(const C: AnsiChar): Boolean;
  2866. begin
  2867. Result := (C = #8);
  2868. end;
  2869. function CharIsDigit(const C: AnsiChar): Boolean;
  2870. begin
  2871. Result := (AnsiCharTypes[C] and C1_DIGIT) <> 0;
  2872. end;
  2873. function CharIsFracDigit(const C: AnsiChar): Boolean;
  2874. begin
  2875. Result := (C = '.') or ((AnsiCharTypes[C] and C1_DIGIT) <> 0);
  2876. end;
  2877. function CharIsHexDigit(const C: AnsiChar): Boolean;
  2878. begin
  2879. case C of
  2880. 'A'..'F',
  2881. 'a'..'f':
  2882. Result := True;
  2883. else
  2884. Result := ((AnsiCharTypes[C] and C1_DIGIT) <> 0);
  2885. end;
  2886. end;
  2887. function CharIsLower(const C: AnsiChar): Boolean;
  2888. begin
  2889. Result := (AnsiCharTypes[C] and C1_LOWER) <> 0;
  2890. end;
  2891. // JclSysUtils.TJclFormatSettings.GetDecimalSeparator is manually inlined in the 2 following functions
  2892. // this fixes compiler warnings about functions not being inlined
  2893. function CharIsNumberChar(const C: AnsiChar): Boolean;
  2894. begin
  2895. Result := ((AnsiCharTypes[C] and C1_DIGIT) <> 0) or (C = AnsiSignMinus) or (C = AnsiSignPlus) or
  2896. (Char(C) = {$IFDEF RTL220_UP}FormatSettings.DecimalSeparator{$ELSE}SysUtils.DecimalSeparator{$ENDIF});
  2897. end;
  2898. function CharIsNumber(const C: AnsiChar): Boolean;
  2899. begin
  2900. Result := ((AnsiCharTypes[C] and C1_DIGIT) <> 0) or
  2901. (Char(C) = {$IFDEF RTL220_UP}FormatSettings.DecimalSeparator{$ELSE}SysUtils.DecimalSeparator{$ENDIF});
  2902. end;
  2903. function CharIsPrintable(const C: AnsiChar): Boolean;
  2904. begin
  2905. Result := not CharIsControl(C);
  2906. end;
  2907. function CharIsPunctuation(const C: AnsiChar): Boolean;
  2908. begin
  2909. Result := ((AnsiCharTypes[C] and C1_PUNCT) <> 0);
  2910. end;
  2911. function CharIsReturn(const C: AnsiChar): Boolean;
  2912. begin
  2913. Result := (C = AnsiLineFeed) or (C = AnsiCarriageReturn);
  2914. end;
  2915. function CharIsSpace(const C: AnsiChar): Boolean;
  2916. begin
  2917. Result := (AnsiCharTypes[C] and C1_SPACE) <> 0;
  2918. end;
  2919. function CharIsUpper(const C: AnsiChar): Boolean;
  2920. begin
  2921. Result := (AnsiCharTypes[C] and C1_UPPER) <> 0;
  2922. end;
  2923. function CharIsValidIdentifierLetter(const C: AnsiChar): Boolean;
  2924. begin
  2925. case C of
  2926. '0'..'9', 'A'..'Z', 'a'..'z', '_':
  2927. Result := True;
  2928. else
  2929. Result := False;
  2930. end;
  2931. end;
  2932. function CharIsWhiteSpace(const C: AnsiChar): Boolean;
  2933. begin
  2934. Result := (C = AnsiTab) or (C = AnsiLineFeed) or (C = AnsiVerticalTab) or
  2935. (C = AnsiFormFeed) or (C = AnsiCarriageReturn) or (C =AnsiSpace) or
  2936. ((AnsiCharTypes[C] and C1_SPACE) <> 0);
  2937. end;
  2938. function CharIsWildcard(const C: AnsiChar): Boolean;
  2939. begin
  2940. case C of
  2941. '*', '?':
  2942. Result := True;
  2943. else
  2944. Result := False;
  2945. end;
  2946. end;
  2947. function CharType(const C: AnsiChar): Word;
  2948. begin
  2949. Result := AnsiCharTypes[C];
  2950. end;
  2951. //=== PCharVector ============================================================
  2952. function StringsToPCharVector(var Dest: PAnsiCharVector; const Source: TJclAnsiStrings): PAnsiCharVector;
  2953. var
  2954. I: SizeInt;
  2955. S: AnsiString;
  2956. List: array of PAnsiChar;
  2957. begin
  2958. Assert(Source <> nil);
  2959. Dest := AllocMem((Source.Count + SizeOf(AnsiChar)) * SizeOf(PAnsiChar));
  2960. SetLength(List, Source.Count + SizeOf(AnsiChar));
  2961. for I := 0 to Source.Count - 1 do
  2962. begin
  2963. S := Source[I];
  2964. {$IFDEF SUPPORTS_UNICODE}
  2965. List[I] := AnsiStrAlloc(Length(S) + SizeOf(AnsiChar));
  2966. {$ELSE ~SUPPORTS_UNICODE}
  2967. List[I] := StrAlloc(Length(S) + SizeOf(AnsiChar));
  2968. {$ENDIF ~SUPPORTS_UNICODE}
  2969. StrPCopyA(List[I], S);
  2970. end;
  2971. List[Source.Count] := nil;
  2972. Move(List[0], Dest^, (Source.Count + 1) * SizeOf(PAnsiChar));
  2973. Result := Dest;
  2974. end;
  2975. function PCharVectorCount(Source: PAnsiCharVector): SizeInt;
  2976. begin
  2977. Result := 0;
  2978. if Source <> nil then
  2979. while Source^ <> nil do
  2980. begin
  2981. Inc(Source);
  2982. Inc(Result);
  2983. end;
  2984. end;
  2985. procedure PCharVectorToStrings(const Dest: TJclAnsiStrings; Source: PAnsiCharVector);
  2986. var
  2987. I, Count: SizeInt;
  2988. List: array of PAnsiChar;
  2989. begin
  2990. Assert(Dest <> nil);
  2991. if Source <> nil then
  2992. begin
  2993. Count := PCharVectorCount(Source);
  2994. SetLength(List, Count);
  2995. Move(Source^, List[0], Count * SizeOf(PAnsiChar));
  2996. Dest.BeginUpdate;
  2997. try
  2998. Dest.Clear;
  2999. for I := 0 to Count - 1 do
  3000. Dest.Add(List[I]);
  3001. finally
  3002. Dest.EndUpdate;
  3003. end;
  3004. end;
  3005. end;
  3006. procedure FreePCharVector(var Dest: PAnsiCharVector);
  3007. var
  3008. I, Count: SizeInt;
  3009. List: array of PAnsiChar;
  3010. begin
  3011. if Dest <> nil then
  3012. begin
  3013. Count := PCharVectorCount(Dest);
  3014. SetLength(List, Count);
  3015. Move(Dest^, List[0], Count * SizeOf(PAnsiChar));
  3016. for I := 0 to Count - 1 do
  3017. StrDisposeA(List[I]);
  3018. FreeMem(Dest, (Count + 1) * SizeOf(PAnsiChar));
  3019. Dest := nil;
  3020. end;
  3021. end;
  3022. //=== Character Transformation Routines ======================================
  3023. function CharHex(const C: AnsiChar): Byte;
  3024. begin
  3025. case C of
  3026. '0'..'9':
  3027. Result := Ord(C) - Ord('0');
  3028. 'a'..'f':
  3029. Result := Ord(C) - Ord('a') + 10;
  3030. 'A'..'F':
  3031. Result := Ord(C) - Ord('A') + 10;
  3032. else
  3033. Result := $FF;
  3034. end;
  3035. end;
  3036. function CharLower(const C: AnsiChar): AnsiChar;
  3037. begin
  3038. Result := AnsiCaseMap[Ord(C) + AnsiLoOffset];
  3039. end;
  3040. function CharToggleCase(const C: AnsiChar): AnsiChar;
  3041. begin
  3042. Result := AnsiCaseMap[Ord(C) + AnsiReOffset];
  3043. end;
  3044. function CharUpper(const C: AnsiChar): AnsiChar;
  3045. begin
  3046. Result := AnsiCaseMap[Ord(C) + AnsiUpOffset];
  3047. end;
  3048. //=== Character Search and Replace ===========================================
  3049. function CharLastPos(const S: AnsiString; const C: AnsiChar; const Index: SizeInt): SizeInt;
  3050. begin
  3051. if (Index > 0) and (Index <= Length(S)) then
  3052. for Result := Length(S) downto Index do
  3053. if S[Result] = C then
  3054. Exit;
  3055. Result := 0;
  3056. end;
  3057. function CharPos(const S: AnsiString; const C: AnsiChar; const Index: SizeInt): SizeInt;
  3058. begin
  3059. if (Index > 0) and (Index <= Length(S)) then
  3060. for Result := Index to Length(S) do
  3061. if S[Result] = C then
  3062. Exit;
  3063. Result := 0;
  3064. end;
  3065. function CharIPos(const S: AnsiString; C: AnsiChar; const Index: SizeInt): SizeInt;
  3066. begin
  3067. if (Index > 0) and (Index <= Length(S)) then
  3068. begin
  3069. C := CharUpper(C);
  3070. for Result := Index to Length(S) do
  3071. if AnsiCaseMap[Ord(S[Result]) + AnsiUpOffset] = C then
  3072. Exit;
  3073. end;
  3074. Result := 0;
  3075. end;
  3076. function CharReplace(var S: AnsiString; const Search, Replace: AnsiChar): SizeInt;
  3077. var
  3078. P: PAnsiChar;
  3079. Index, Len: SizeInt;
  3080. begin
  3081. Result := 0;
  3082. if Search <> Replace then
  3083. begin
  3084. UniqueString(S);
  3085. Len := Length(S);
  3086. P := PAnsiChar(S);
  3087. for Index := 0 to Len - 1 do
  3088. begin
  3089. if P^ = Search then
  3090. begin
  3091. P^ := Replace;
  3092. Inc(Result);
  3093. end;
  3094. Inc(P);
  3095. end;
  3096. end;
  3097. end;
  3098. //=== MultiSz ================================================================
  3099. function StringsToMultiSz(var Dest: PAnsiMultiSz; const Source: TJclAnsiStrings): PAnsiMultiSz;
  3100. var
  3101. I, TotalLength: SizeInt;
  3102. P: PAnsiMultiSz;
  3103. begin
  3104. Assert(Source <> nil);
  3105. TotalLength := 1;
  3106. for I := 0 to Source.Count - 1 do
  3107. if Source[I] = '' then
  3108. raise EJclAnsiStringError.CreateRes(@RsInvalidEmptyStringItem)
  3109. else
  3110. Inc(TotalLength, StrLenA(PAnsiChar(AnsiString(Source[I]))) + 1);
  3111. AllocateMultiSz(Dest, TotalLength);
  3112. P := Dest;
  3113. for I := 0 to Source.Count - 1 do
  3114. begin
  3115. P := StrECopyA(P, PAnsiChar(AnsiString(Source[I])));
  3116. Inc(P);
  3117. end;
  3118. P^ := #0;
  3119. Result := Dest;
  3120. end;
  3121. procedure MultiSzToStrings(const Dest: TJclAnsiStrings; const Source: PAnsiMultiSz);
  3122. var
  3123. P: PAnsiMultiSz;
  3124. begin
  3125. Assert(Dest <> nil);
  3126. Dest.BeginUpdate;
  3127. try
  3128. Dest.Clear;
  3129. if Source <> nil then
  3130. begin
  3131. P := Source;
  3132. while P^ <> #0 do
  3133. begin
  3134. Dest.Add(P);
  3135. P := StrEndA(P);
  3136. Inc(P);
  3137. end;
  3138. end;
  3139. finally
  3140. Dest.EndUpdate;
  3141. end;
  3142. end;
  3143. function MultiSzLength(const Source: PAnsiMultiSz): SizeInt;
  3144. var
  3145. P: PAnsiMultiSz;
  3146. begin
  3147. Result := 0;
  3148. if Source <> nil then
  3149. begin
  3150. P := Source;
  3151. repeat
  3152. Inc(Result, StrLenA(P) + 1);
  3153. P := StrEndA(P);
  3154. Inc(P);
  3155. until P^ = #0;
  3156. Inc(Result);
  3157. end;
  3158. end;
  3159. procedure AllocateMultiSz(var Dest: PAnsiMultiSz; Len: SizeInt);
  3160. begin
  3161. if Len > 0 then
  3162. GetMem(Dest, Len * SizeOf(AnsiChar))
  3163. else
  3164. Dest := nil;
  3165. end;
  3166. procedure FreeMultiSz(var Dest: PAnsiMultiSz);
  3167. begin
  3168. if Dest <> nil then
  3169. FreeMem(Dest);
  3170. Dest := nil;
  3171. end;
  3172. function MultiSzDup(const Source: PAnsiMultiSz): PAnsiMultiSz;
  3173. var
  3174. Len: SizeInt;
  3175. begin
  3176. if Source <> nil then
  3177. begin
  3178. Len := MultiSzLength(Source);
  3179. Result := nil;
  3180. AllocateMultiSz(Result, Len);
  3181. Move(Source^, Result^, Len * SizeOf(AnsiChar));
  3182. end
  3183. else
  3184. Result := nil;
  3185. end;
  3186. //=== TJclAnsiStrings Manipulation ===============================================
  3187. procedure StrToStrings(S, Sep: AnsiString; const List: TJclAnsiStrings; const AllowEmptyString: Boolean = True);
  3188. var
  3189. I, L: SizeInt;
  3190. Left: AnsiString;
  3191. begin
  3192. Assert(List <> nil);
  3193. List.BeginUpdate;
  3194. try
  3195. List.Clear;
  3196. L := Length(Sep);
  3197. I := Pos(Sep, S);
  3198. while I > 0 do
  3199. begin
  3200. Left := StrLeft(S, I - 1);
  3201. if (Left <> '') or AllowEmptyString then
  3202. List.Add(Left);
  3203. Delete(S, 1, I + L - 1);
  3204. I := Pos(Sep, S);
  3205. end;
  3206. if (S <> '') or AllowEmptyString then
  3207. List.Add(S); // Ignore empty strings at the end (only if AllowEmptyString = False).
  3208. finally
  3209. List.EndUpdate;
  3210. end;
  3211. end;
  3212. procedure StrIToStrings(S, Sep: AnsiString; const List: TJclAnsiStrings; const AllowEmptyString: Boolean = True);
  3213. var
  3214. I, L: SizeInt;
  3215. LowerCaseStr: AnsiString;
  3216. Left: AnsiString;
  3217. begin
  3218. Assert(List <> nil);
  3219. LowerCaseStr := StrLower(S);
  3220. Sep := StrLower(Sep);
  3221. L := Length(Sep);
  3222. I := Pos(Sep, LowerCaseStr);
  3223. List.BeginUpdate;
  3224. try
  3225. List.Clear;
  3226. while I > 0 do
  3227. begin
  3228. Left := StrLeft(S, I - 1);
  3229. if (Left <> '') or AllowEmptyString then
  3230. List.Add(Left);
  3231. Delete(S, 1, I + L - 1);
  3232. Delete(LowerCaseStr, 1, I + L - 1);
  3233. I := Pos(Sep, LowerCaseStr);
  3234. end;
  3235. if (S <> '') or AllowEmptyString then
  3236. List.Add(S); // Ignore empty strings at the end (only if AllowEmptyString = False).
  3237. finally
  3238. List.EndUpdate;
  3239. end;
  3240. end;
  3241. function StringsToStr(const List: TJclAnsiStrings; const Sep: AnsiString;
  3242. const AllowEmptyString: Boolean): AnsiString;
  3243. var
  3244. I, L: SizeInt;
  3245. begin
  3246. Result := '';
  3247. for I := 0 to List.Count - 1 do
  3248. begin
  3249. if (List[I] <> '') or AllowEmptyString then
  3250. begin
  3251. // don't combine these into one addition, somehow it hurts performance
  3252. Result := Result + List[I];
  3253. Result := Result + Sep;
  3254. end;
  3255. end;
  3256. // remove terminating separator
  3257. if List.Count <> 0 then
  3258. begin
  3259. L := Length(Sep);
  3260. Delete(Result, Length(Result) - L + 1, L);
  3261. end;
  3262. end;
  3263. procedure TrimStrings(const List: TJclAnsiStrings; DeleteIfEmpty: Boolean);
  3264. var
  3265. I: SizeInt;
  3266. begin
  3267. Assert(List <> nil);
  3268. List.BeginUpdate;
  3269. try
  3270. for I := List.Count - 1 downto 0 do
  3271. begin
  3272. List[I] := Trim(List[I]);
  3273. if (List[I] = '') and DeleteIfEmpty then
  3274. List.Delete(I);
  3275. end;
  3276. finally
  3277. List.EndUpdate;
  3278. end;
  3279. end;
  3280. procedure TrimStringsRight(const List: TJclAnsiStrings; DeleteIfEmpty: Boolean);
  3281. var
  3282. I: SizeInt;
  3283. begin
  3284. Assert(List <> nil);
  3285. List.BeginUpdate;
  3286. try
  3287. for I := List.Count - 1 downto 0 do
  3288. begin
  3289. List[I] := TrimRight(List[I]);
  3290. if (List[I] = '') and DeleteIfEmpty then
  3291. List.Delete(I);
  3292. end;
  3293. finally
  3294. List.EndUpdate;
  3295. end;
  3296. end;
  3297. procedure TrimStringsLeft(const List: TJclAnsiStrings; DeleteIfEmpty: Boolean);
  3298. var
  3299. I: SizeInt;
  3300. begin
  3301. Assert(List <> nil);
  3302. List.BeginUpdate;
  3303. try
  3304. for I := List.Count - 1 downto 0 do
  3305. begin
  3306. List[I] := TrimLeft(List[I]);
  3307. if (List[I] = '') and DeleteIfEmpty then
  3308. List.Delete(I);
  3309. end;
  3310. finally
  3311. List.EndUpdate;
  3312. end;
  3313. end;
  3314. function AddStringToStrings(const S: AnsiString; Strings: TJclAnsiStrings; const Unique: Boolean): Boolean;
  3315. begin
  3316. Assert(Strings <> nil);
  3317. Result := Unique and (Strings.IndexOf(S) <> -1);
  3318. if not Result then
  3319. Result := Strings.Add(S) > -1;
  3320. end;
  3321. //=== Miscellaneous ==========================================================
  3322. function FileToString(const FileName: TFileName): AnsiString;
  3323. var
  3324. FS: TFileStream;
  3325. Len: SizeInt;
  3326. begin
  3327. FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  3328. try
  3329. Len := FS.Size;
  3330. SetLength(Result, Len);
  3331. if Len > 0 then
  3332. FS.ReadBuffer(Result[1], Len);
  3333. finally
  3334. FS.Free;
  3335. end;
  3336. end;
  3337. procedure StringToFile(const FileName: TFileName; const Contents: AnsiString; Append: Boolean);
  3338. var
  3339. FS: TFileStream;
  3340. Len: SizeInt;
  3341. begin
  3342. if Append and FileExists(FileName) then
  3343. FS := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyWrite)
  3344. else
  3345. FS := TFileStream.Create(FileName, fmCreate);
  3346. try
  3347. if Append then
  3348. FS.Seek(0, soEnd); // faster than .Position := .Size
  3349. Len := Length(Contents);
  3350. if Len > 0 then
  3351. FS.WriteBuffer(Contents[1], Len);
  3352. finally
  3353. FS.Free;
  3354. end;
  3355. end;
  3356. function StrToken(var S: AnsiString; Separator: AnsiChar): AnsiString;
  3357. var
  3358. I: SizeInt;
  3359. begin
  3360. I := Pos(Separator, S);
  3361. if I <> 0 then
  3362. begin
  3363. Result := Copy(S, 1, I - 1);
  3364. Delete(S, 1, I);
  3365. end
  3366. else
  3367. begin
  3368. Result := S;
  3369. S := '';
  3370. end;
  3371. end;
  3372. procedure StrTokens(const S: AnsiString; const List: TJclAnsiStrings);
  3373. var
  3374. Start: PAnsiChar;
  3375. Token: AnsiString;
  3376. Done: Boolean;
  3377. begin
  3378. Assert(List <> nil);
  3379. if List = nil then
  3380. Exit;
  3381. List.BeginUpdate;
  3382. try
  3383. List.Clear;
  3384. Start := Pointer(S);
  3385. repeat
  3386. Done := StrWord(Start, Token);
  3387. if Token <> '' then
  3388. List.Add(Token);
  3389. until Done;
  3390. finally
  3391. List.EndUpdate;
  3392. end;
  3393. end;
  3394. procedure StrTokenToStrings(S: AnsiString; Separator: AnsiChar; const List: TJclAnsiStrings);
  3395. var
  3396. Token: AnsiString;
  3397. begin
  3398. Assert(List <> nil);
  3399. if List = nil then
  3400. Exit;
  3401. List.BeginUpdate;
  3402. try
  3403. List.Clear;
  3404. while S <> '' do
  3405. begin
  3406. Token := StrToken(S, Separator);
  3407. List.Add(Token);
  3408. end;
  3409. finally
  3410. List.EndUpdate;
  3411. end;
  3412. end;
  3413. function StrWord(const S: AnsiString; var Index: SizeInt; out Word: AnsiString): Boolean;
  3414. var
  3415. Start: SizeInt;
  3416. C: AnsiChar;
  3417. begin
  3418. Word := '';
  3419. if (S = '') then
  3420. begin
  3421. Result := True;
  3422. Exit;
  3423. end;
  3424. Start := Index;
  3425. Result := False;
  3426. while True do
  3427. begin
  3428. C := S[Index];
  3429. case C of
  3430. #0:
  3431. begin
  3432. if Start <> 0 then
  3433. Word := Copy(S, Start, Index - Start);
  3434. Result := True;
  3435. Exit;
  3436. end;
  3437. AnsiSpace, AnsiLineFeed, AnsiCarriageReturn:
  3438. begin
  3439. if Start <> 0 then
  3440. begin
  3441. Word := Copy(S, Start, Index - Start);
  3442. Exit;
  3443. end
  3444. else
  3445. begin
  3446. while CharIsWhiteSpace(C) do
  3447. begin
  3448. Inc(Index);
  3449. C := S[Index];
  3450. end;
  3451. end;
  3452. end;
  3453. else
  3454. if Start = 0 then
  3455. Start := Index;
  3456. Inc(Index);
  3457. end;
  3458. end;
  3459. end;
  3460. function StrWord(var S: PAnsiChar; out Word: AnsiString): Boolean;
  3461. var
  3462. Start: PAnsiChar;
  3463. begin
  3464. Word := '';
  3465. if S = nil then
  3466. begin
  3467. Result := True;
  3468. Exit;
  3469. end;
  3470. Start := nil;
  3471. Result := False;
  3472. while True do
  3473. begin
  3474. case S^ of
  3475. #0:
  3476. begin
  3477. if Start <> nil then
  3478. SetString(Word, Start, S - Start);
  3479. Result := True;
  3480. Exit;
  3481. end;
  3482. AnsiSpace, AnsiLineFeed, AnsiCarriageReturn:
  3483. begin
  3484. if Start <> nil then
  3485. begin
  3486. SetString(Word, Start, S - Start);
  3487. Exit;
  3488. end
  3489. else
  3490. while CharIsWhiteSpace(S^) do
  3491. Inc(S);
  3492. end;
  3493. else
  3494. if Start = nil then
  3495. Start := S;
  3496. Inc(S);
  3497. end;
  3498. end;
  3499. end;
  3500. function StrIdent(const S: AnsiString; var Index: SizeInt; out Ident: AnsiString): Boolean;
  3501. var
  3502. Start: SizeInt;
  3503. C: AnsiChar;
  3504. begin
  3505. Ident := '';
  3506. if (S = '') then
  3507. begin
  3508. Result := True;
  3509. Exit;
  3510. end;
  3511. Start := Index;
  3512. Result := False;
  3513. while True do
  3514. begin
  3515. C := S[Index];
  3516. if CharIsValidIdentifierLetter(C) then
  3517. begin
  3518. if Start = 0 then
  3519. Start := Index;
  3520. end
  3521. else
  3522. if C = #0 then
  3523. begin
  3524. if Start <> 0 then
  3525. Ident := Copy(S, Start, Index - Start);
  3526. Result := True;
  3527. Exit;
  3528. end
  3529. else
  3530. begin
  3531. if Start <> 0 then
  3532. begin
  3533. Ident := Copy(S, Start, Index - Start);
  3534. Exit;
  3535. end;
  3536. end;
  3537. Inc(Index);
  3538. end;
  3539. end;
  3540. function StrIdent(var S: PAnsiChar; out Ident: AnsiString): Boolean;
  3541. var
  3542. Start: PAnsiChar;
  3543. C: AnsiChar;
  3544. begin
  3545. Ident := '';
  3546. if S = nil then
  3547. begin
  3548. Result := True;
  3549. Exit;
  3550. end;
  3551. Start := nil;
  3552. Result := False;
  3553. while True do
  3554. begin
  3555. C := S^;
  3556. if CharIsValidIdentifierLetter(C) then
  3557. begin
  3558. if Start = nil then
  3559. Start := S;
  3560. end
  3561. else
  3562. if C = #0 then
  3563. begin
  3564. if Start <> nil then
  3565. SetString(Ident, Start, S - Start);
  3566. Result := True;
  3567. Exit;
  3568. end
  3569. else
  3570. begin
  3571. if Start <> nil then
  3572. begin
  3573. SetString(Ident, Start, S - Start);
  3574. Exit;
  3575. end
  3576. end;
  3577. Inc(S);
  3578. end;
  3579. end;
  3580. function StrToFloatSafe(const S: AnsiString): Float;
  3581. var
  3582. Temp: AnsiString;
  3583. I, J, K: SizeInt;
  3584. SwapSeparators, IsNegative: Boolean;
  3585. DecSep: AnsiChar;
  3586. ThouSep: AnsiChar;
  3587. begin
  3588. DecSep := AnsiChar(JclFormatSettings.DecimalSeparator);
  3589. ThouSep := AnsiChar(JclFormatSettings.ThousandSeparator);
  3590. Temp := S;
  3591. SwapSeparators := False;
  3592. IsNegative := False;
  3593. J := 0;
  3594. for I := 1 to Length(Temp) do
  3595. begin
  3596. if Temp[I] = '-' then
  3597. IsNegative := not IsNegative
  3598. else
  3599. if not (Temp[I] in [' ', '(', '+']) then
  3600. begin
  3601. // if it appears prior to any digit, it has to be a decimal separator
  3602. SwapSeparators := Temp[I] = ThouSep;
  3603. J := I;
  3604. Break;
  3605. end;
  3606. end;
  3607. if not SwapSeparators then
  3608. begin
  3609. K := CharPos(Temp, DecSep);
  3610. SwapSeparators :=
  3611. // if it appears prior to any digit, it has to be a decimal separator
  3612. (K > J) and
  3613. // if it appears multiple times, it has to be a thousand separator
  3614. ((StrCharCount(Temp, DecSep) > 1) or
  3615. // we assume (consistent with Windows Platform SDK documentation),
  3616. // that thousand separators appear only to the left of the decimal
  3617. (K < CharPos(Temp, ThouSep)));
  3618. end;
  3619. if SwapSeparators then
  3620. begin
  3621. // assume a numerical string from a different locale,
  3622. // where DecimalSeparator and ThousandSeparator are exchanged
  3623. for I := 1 to Length(Temp) do
  3624. if Temp[I] = DecSep then
  3625. Temp[I] := ThouSep
  3626. else
  3627. if Temp[I] = ThouSep then
  3628. Temp[I] := DecSep;
  3629. end;
  3630. Temp := StrKeepChars(Temp, AnsiDecDigits + [DecSep]);
  3631. if Length(Temp) > 0 then
  3632. begin
  3633. if Temp[1] = DecSep then
  3634. Temp := '0' + Temp;
  3635. if Temp[Length(Temp)] = DecSep then
  3636. Temp := Temp + '0';
  3637. Result := StrToFloat(string(Temp));
  3638. if IsNegative then
  3639. Result := -Result;
  3640. end
  3641. else
  3642. Result := 0.0;
  3643. end;
  3644. function StrToIntSafe(const S: AnsiString): Integer;
  3645. begin
  3646. Result := Trunc(StrToFloatSafe(S));
  3647. end;
  3648. {$IFNDEF WINSCP}
  3649. procedure StrNormIndex(const StrLen: SizeInt; var Index: SizeInt; var Count: SizeInt); overload;
  3650. begin
  3651. Index := Max(1, Min(Index, StrLen + 1));
  3652. Count := Max(0, Min(Count, StrLen + 1 - Index));
  3653. end;
  3654. {$ENDIF WINSCP}
  3655. function ArrayOf(List: TJclAnsiStrings): TDynStringArray;
  3656. var
  3657. I: SizeInt;
  3658. begin
  3659. if List <> nil then
  3660. begin
  3661. SetLength(Result, List.Count);
  3662. for I := 0 to List.Count - 1 do
  3663. Result[I] := string(List[I]);
  3664. end
  3665. else
  3666. Result := nil;
  3667. end;
  3668. function AnsiCompareNatural(const S1, S2: AnsiString; CaseInsensitive: Boolean): SizeInt;
  3669. var
  3670. Cur1, Len1,
  3671. Cur2, Len2: SizeInt;
  3672. procedure NumberCompare;
  3673. var
  3674. IsReallyNumber: Boolean;
  3675. FirstDiffBreaks: Boolean;
  3676. Val1, Val2: SizeInt;
  3677. begin
  3678. Result := 0;
  3679. IsReallyNumber := False;
  3680. // count leading spaces in S1
  3681. while CharIsWhiteSpace(S1[Cur1]) do
  3682. begin
  3683. Dec(Result);
  3684. Inc(Cur1);
  3685. end;
  3686. // count leading spaces in S2 (canceling them out against the ones in S1)
  3687. while CharIsWhiteSpace(S2[Cur2]) do
  3688. begin
  3689. Inc(Result);
  3690. Inc(Cur2);
  3691. end;
  3692. // if spaces match, or both strings are actually followed by a numeric character, continue the checks
  3693. if (Result = 0) or (CharIsNumberChar(S1[Cur1])) and (CharIsNumberChar(S2[Cur2])) then
  3694. begin
  3695. // Check signed number
  3696. if (S1[Cur1] = '-') and (S2[Cur2] <> '-') then
  3697. Result := 1
  3698. else
  3699. if (S2[Cur2] = '-') and (S1[Cur1] <> '-') then
  3700. Result := -1
  3701. else
  3702. Result := 0;
  3703. if (S1[Cur1] = '-') or (S1[Cur1] = '+') then
  3704. Inc(Cur1);
  3705. if (S2[Cur2] = '-') or (S2[Cur2] = '+') then
  3706. Inc(Cur2);
  3707. FirstDiffBreaks := (S1[Cur1] = '0') or (S2[Cur2] = '0');
  3708. while CharIsDigit(S1[Cur1]) and CharIsDigit(S2[Cur2]) do
  3709. begin
  3710. IsReallyNumber := True;
  3711. Val1 := StrToInt(string(S1[Cur1]));
  3712. Val2 := StrToInt(string(S2[Cur2]));
  3713. if (Result = 0) and (Val1 < Val2) then
  3714. Result := -1
  3715. else
  3716. if (Result = 0) and (Val1 > Val2) then
  3717. Result := 1;
  3718. if FirstDiffBreaks and (Result <> 0) then
  3719. Break;
  3720. Inc(Cur1);
  3721. Inc(Cur2);
  3722. end;
  3723. if IsReallyNumber then
  3724. begin
  3725. if not FirstDiffBreaks then
  3726. begin
  3727. if CharIsDigit(S1[Cur1]) then
  3728. Result := 1
  3729. else
  3730. if CharIsDigit(S2[Cur2]) then
  3731. Result := -1;
  3732. end;
  3733. end;
  3734. end;
  3735. end;
  3736. begin
  3737. Cur1 := 1;
  3738. Len1 := Length(S1);
  3739. Cur2 := 1;
  3740. Len2 := Length(S2);
  3741. Result := 0;
  3742. while (Result = 0) do
  3743. begin
  3744. if (Cur1 = Len1) and (Cur2 = Len2) then
  3745. Break
  3746. else
  3747. if (S1[Cur1] = '-') and CharIsNumberChar(S2[Cur2]) and (S2[Cur2] <> '-') then
  3748. Result := -1
  3749. else
  3750. if (S2[Cur2] = '-') and CharIsNumberChar(S1[Cur1]) and (S1[Cur1] <> '-') then
  3751. Result := 1
  3752. else
  3753. if CharIsNumberChar(S1[Cur1]) and CharIsNumberChar(S2[Cur2]) then
  3754. NumberCompare
  3755. else
  3756. if (Cur1 = Len1) and (Cur2 < Len2) then
  3757. Result := -1
  3758. else
  3759. if (Cur1 < Len1) and (Cur2 = Len2) then
  3760. Result := 1
  3761. else
  3762. begin
  3763. Result := StrCompare(S1,S2);
  3764. if CaseInsensitive then
  3765. Result := AnsiStrLICompA(PAnsiChar(@S1[Cur1]), PAnsiChar(@S2[Cur2]), 1)
  3766. else
  3767. Result := AnsiStrLCompA(PAnsiChar(@S1[Cur1]), PAnsiChar(@S2[Cur2]), 1);
  3768. Inc(Cur1);
  3769. Inc(Cur2);
  3770. end;
  3771. end;
  3772. end;
  3773. function AnsiCompareNaturalStr(const S1, S2: AnsiString): SizeInt; overload;
  3774. begin
  3775. Result := AnsiCompareNatural(S1, S2, False);
  3776. end;
  3777. function AnsiCompareNaturalText(const S1, S2: AnsiString): SizeInt; overload;
  3778. begin
  3779. Result := AnsiCompareNatural(S1, S2, True);
  3780. end;
  3781. function StrNewA(const Str: PAnsiChar): PAnsiChar;
  3782. begin
  3783. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrNew(Str);
  3784. end;
  3785. procedure StrDisposeA(Str: PAnsiChar);
  3786. begin
  3787. {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrDispose(Str);
  3788. end;
  3789. function StrLenA(S: PAnsiChar): Integer;
  3790. begin
  3791. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrLen(S);
  3792. end;
  3793. function StrEndA(const Str: PAnsiChar): PAnsiChar;
  3794. begin
  3795. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrEnd(Str);
  3796. end;
  3797. function StrPosA(const Str1, Str2: PAnsiChar): PAnsiChar;
  3798. begin
  3799. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrPos(Str1, Str2);
  3800. end;
  3801. function StrPasA(const Str: PAnsiChar): AnsiString;
  3802. begin
  3803. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrPas(Str);
  3804. end;
  3805. function StrCopyA(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar;
  3806. begin
  3807. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrCopy(Dest, Source);
  3808. end;
  3809. function StrLCopyA(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar;
  3810. begin
  3811. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrLCopy(Dest, Source, MaxLen);
  3812. end;
  3813. function StrPCopyA(Dest: PAnsiChar; const Source: AnsiString): PAnsiChar;
  3814. begin
  3815. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrPCopy(Dest, Source);
  3816. end;
  3817. function StrPLCopyA(Dest: PAnsiChar; const Source: AnsiString; MaxLen: Cardinal): PAnsiChar;
  3818. begin
  3819. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrPLCopy(Dest, Source, MaxLen);
  3820. end;
  3821. function StrECopyA(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar;
  3822. begin
  3823. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrECopy(Dest, Source);
  3824. end;
  3825. function StrCatA(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar;
  3826. begin
  3827. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrCat(Dest, Source);
  3828. end;
  3829. function StrLCatA(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar;
  3830. begin
  3831. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrLCat(Dest, Source, MaxLen);
  3832. end;
  3833. function StrCompA(const Str1, Str2: PAnsiChar): Integer;
  3834. begin
  3835. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrComp(Str1, Str2);
  3836. end;
  3837. function StrLCompA(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
  3838. begin
  3839. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrLComp(Str1, Str2, MaxLen);
  3840. end;
  3841. function StrICompA(const Str1, Str2: PAnsiChar): Integer;
  3842. begin
  3843. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrIComp(Str1, Str2);
  3844. end;
  3845. function StrLICompA(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
  3846. begin
  3847. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrIComp(Str1, Str2);
  3848. end;
  3849. function StrFmtA(Buffer, Format: PAnsiChar; const Args: array of const): PAnsiChar;
  3850. begin
  3851. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}StrFmt(Buffer, Format, Args);
  3852. end;
  3853. function AnsiStrPosA(const Str1, Str2: PAnsiChar): PAnsiChar;
  3854. begin
  3855. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}AnsiStrPos(Str1, Str2);
  3856. end;
  3857. function AnsiStrLICompA(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
  3858. begin
  3859. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}AnsiStrLIComp(S1, S2, MaxLen);
  3860. end;
  3861. function AnsiStrLCompA(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
  3862. begin
  3863. Result := {$IFDEF DEPRECATED_SYSUTILS_ANSISTRINGS}System.AnsiStrings.{$ENDIF}AnsiStrLComp(S1, S2, MaxLen);
  3864. end;
  3865. initialization
  3866. LoadCharTypes; // this table first
  3867. LoadCaseMap; // or this function does not work
  3868. {$IFDEF UNITVERSIONING}
  3869. RegisterUnitVersion(HInstance, UnitVersioning);
  3870. finalization
  3871. UnregisterUnitVersion(HInstance);
  3872. {$ENDIF UNITVERSIONING}
  3873. end.