strutils.pp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. {
  2. Delphi/Kylix compatibility unit: String handling routines.
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2005 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. {$inline on}
  14. unit strutils;
  15. interface
  16. uses
  17. SysUtils{, Types};
  18. { ---------------------------------------------------------------------
  19. Case insensitive search/replace
  20. ---------------------------------------------------------------------}
  21. Function AnsiResemblesText(const AText, AOther: string): Boolean;
  22. Function AnsiContainsText(const AText, ASubText: string): Boolean;
  23. Function AnsiStartsText(const ASubText, AText: string): Boolean;inline;
  24. Function AnsiEndsText(const ASubText, AText: string): Boolean;inline;
  25. Function AnsiReplaceText(const AText, AFromText, AToText: string): string;inline;
  26. Function AnsiMatchText(const AText: string; const AValues: array of string): Boolean;inline;
  27. Function AnsiIndexText(const AText: string; const AValues: array of string): Integer;
  28. { ---------------------------------------------------------------------
  29. Case sensitive search/replace
  30. ---------------------------------------------------------------------}
  31. Function AnsiContainsStr(const AText, ASubText: string): Boolean;inline;
  32. Function AnsiStartsStr(const ASubText, AText: string): Boolean;inline;
  33. Function AnsiEndsStr(const ASubText, AText: string): Boolean;inline;
  34. Function AnsiReplaceStr(const AText, AFromText, AToText: string): string;inline;
  35. Function AnsiMatchStr(const AText: string; const AValues: array of string): Boolean;inline;
  36. Function AnsiIndexStr(const AText: string; const AValues: array of string): Integer;
  37. { ---------------------------------------------------------------------
  38. Playthingies
  39. ---------------------------------------------------------------------}
  40. Function DupeString(const AText: string; ACount: Integer): string;
  41. Function ReverseString(const AText: string): string;
  42. Function AnsiReverseString(const AText: AnsiString): AnsiString;inline;
  43. Function StuffString(const AText: string; AStart, ALength: Cardinal; const ASubText: string): string;
  44. Function RandomFrom(const AValues: array of string): string; overload;
  45. Function IfThen(AValue: Boolean; const ATrue: string; AFalse: string): string;inline;
  46. Function IfThen(AValue: Boolean; const ATrue: string): string;inline; // ; AFalse: string = ''
  47. { ---------------------------------------------------------------------
  48. VB emulations.
  49. ---------------------------------------------------------------------}
  50. Function LeftStr(const AText: AnsiString; const ACount: Integer): AnsiString;inline;
  51. Function RightStr(const AText: AnsiString; const ACount: Integer): AnsiString;
  52. Function MidStr(const AText: AnsiString; const AStart, ACount: Integer): AnsiString;inline;
  53. Function RightBStr(const AText: AnsiString; const AByteCount: Integer): AnsiString;inline;
  54. Function MidBStr(const AText: AnsiString; const AByteStart, AByteCount: Integer): AnsiString;inline;
  55. Function AnsiLeftStr(const AText: AnsiString; const ACount: Integer): AnsiString;inline;
  56. Function AnsiRightStr(const AText: AnsiString; const ACount: Integer): AnsiString;inline;
  57. Function AnsiMidStr(const AText: AnsiString; const AStart, ACount: Integer): AnsiString;inline;
  58. Function LeftBStr(const AText: AnsiString; const AByteCount: Integer): AnsiString;inline;
  59. Function LeftStr(const AText: WideString; const ACount: Integer): WideString;inline;
  60. Function RightStr(const AText: WideString; const ACount: Integer): WideString;
  61. Function MidStr(const AText: WideString; const AStart, ACount: Integer): WideString;inline;
  62. { ---------------------------------------------------------------------
  63. Extended search and replace
  64. ---------------------------------------------------------------------}
  65. const
  66. { Default word delimiters are any character except the core alphanumerics. }
  67. WordDelimiters: set of Char = [#0..#255] - ['a'..'z','A'..'Z','1'..'9','0'];
  68. type
  69. TStringSearchOption = (soDown, soMatchCase, soWholeWord);
  70. TStringSearchOptions = set of TStringSearchOption;
  71. TStringSeachOption = TStringSearchOption;
  72. Function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer; SearchString: String; Options: TStringSearchOptions): PChar;
  73. Function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer; SearchString: String): PChar;inline; // ; Options: TStringSearchOptions = [soDown]
  74. Function PosEx(const SubStr, S: string; Offset: Cardinal): Integer;
  75. Function PosEx(const SubStr, S: string): Integer;inline; // Offset: Cardinal = 1
  76. Function PosEx(c:char; const S: string; Offset: Cardinal): Integer;
  77. { ---------------------------------------------------------------------
  78. Soundex Functions.
  79. ---------------------------------------------------------------------}
  80. type
  81. TSoundexLength = 1..MaxInt;
  82. Function Soundex(const AText: string; ALength: TSoundexLength): string;
  83. Function Soundex(const AText: string): string;inline; // ; ALength: TSoundexLength = 4
  84. type
  85. TSoundexIntLength = 1..8;
  86. Function SoundexInt(const AText: string; ALength: TSoundexIntLength): Integer;
  87. Function SoundexInt(const AText: string): Integer;inline; //; ALength: TSoundexIntLength = 4
  88. Function DecodeSoundexInt(AValue: Integer): string;
  89. Function SoundexWord(const AText: string): Word;
  90. Function DecodeSoundexWord(AValue: Word): string;
  91. Function SoundexSimilar(const AText, AOther: string; ALength: TSoundexLength): Boolean;inline;
  92. Function SoundexSimilar(const AText, AOther: string): Boolean;inline; //; ALength: TSoundexLength = 4
  93. Function SoundexCompare(const AText, AOther: string; ALength: TSoundexLength): Integer;inline;
  94. Function SoundexCompare(const AText, AOther: string): Integer;inline; //; ALength: TSoundexLength = 4
  95. Function SoundexProc(const AText, AOther: string): Boolean;
  96. type
  97. TCompareTextProc = Function(const AText, AOther: string): Boolean;
  98. Const
  99. AnsiResemblesProc: TCompareTextProc = @SoundexProc;
  100. { ---------------------------------------------------------------------
  101. Other functions, based on RxStrUtils.
  102. ---------------------------------------------------------------------}
  103. function IsEmptyStr(const S: string; const EmptyChars: TSysCharSet): Boolean;
  104. function DelSpace(const S: string): string;
  105. function DelChars(const S: string; Chr: Char): string;
  106. function DelSpace1(const S: string): string;
  107. function Tab2Space(const S: string; Numb: Byte): string;
  108. function NPos(const C: string; S: string; N: Integer): Integer;
  109. Function RPosEX(C:char;const S : AnsiString;offs:cardinal):Integer; overload;
  110. Function RPosex (Const Substr : AnsiString; Const Source : AnsiString;offs:cardinal) : Integer; overload;
  111. Function RPos(c:char;const S : AnsiString):Integer; overload;
  112. Function RPos (Const Substr : AnsiString; Const Source : AnsiString) : Integer; overload;
  113. function AddChar(C: Char; const S: string; N: Integer): string;
  114. function AddCharR(C: Char; const S: string; N: Integer): string;
  115. function PadLeft(const S: string; N: Integer): string;inline;
  116. function PadRight(const S: string; N: Integer): string;inline;
  117. function PadCenter(const S: string; Len: Integer): string;
  118. function Copy2Symb(const S: string; Symb: Char): string;
  119. function Copy2SymbDel(var S: string; Symb: Char): string;
  120. function Copy2Space(const S: string): string;inline;
  121. function Copy2SpaceDel(var S: string): string;inline;
  122. function AnsiProperCase(const S: string; const WordDelims: TSysCharSet): string;
  123. function WordCount(const S: string; const WordDelims: TSysCharSet): Integer;
  124. function WordPosition(const N: Integer; const S: string; const WordDelims: TSysCharSet): Integer;
  125. function ExtractWord(N: Integer; const S: string; const WordDelims: TSysCharSet): string;inline;
  126. function ExtractWordPos(N: Integer; const S: string; const WordDelims: TSysCharSet; var Pos: Integer): string;
  127. function ExtractDelimited(N: Integer; const S: string; const Delims: TSysCharSet): string;
  128. function ExtractSubstr(const S: string; var Pos: Integer; const Delims: TSysCharSet): string;
  129. function IsWordPresent(const W, S: string; const WordDelims: TSysCharSet): Boolean;
  130. function FindPart(const HelpWilds, InputStr: string): Integer;
  131. function IsWild(InputStr, Wilds: string; IgnoreCase: Boolean): Boolean;
  132. function XorString(const Key, Src: ShortString): ShortString;
  133. function XorEncode(const Key, Source: string): string;
  134. function XorDecode(const Key, Source: string): string;
  135. function GetCmdLineArg(const Switch: string; SwitchChars: TSysCharSet): string;
  136. function Numb2USA(const S: string): string;
  137. function Hex2Dec(const S: string): Longint;
  138. function Dec2Numb(N: Longint; Len, Base: Byte): string;
  139. function Numb2Dec(S: string; Base: Byte): Longint;
  140. function IntToBin(Value: Longint; Digits, Spaces: Integer): string;
  141. function IntToRoman(Value: Longint): string;
  142. function RomanToInt(const S: string): Longint;
  143. procedure BinToHex(BinValue, HexValue: PChar; BinBufSize: Integer);
  144. function HexToBin(HexValue, BinValue: PChar; BinBufSize: Integer): Integer;
  145. const
  146. DigitChars = ['0'..'9'];
  147. Brackets = ['(',')','[',']','{','}'];
  148. StdWordDelims = [#0..' ',',','.',';','/','\',':','''','"','`'] + Brackets;
  149. StdSwitchChars = ['-','/'];
  150. function PosSet (const c:TSysCharSet;const s : ansistring ):Integer;
  151. function PosSet (const c:string;const s : ansistring ):Integer;
  152. function PosSetEx (const c:TSysCharSet;const s : ansistring;count:Integer ):Integer;
  153. function PosSetEx (const c:string;const s : ansistring;count:Integer ):Integer;
  154. implementation
  155. { ---------------------------------------------------------------------
  156. Auxiliary functions
  157. ---------------------------------------------------------------------}
  158. Procedure NotYetImplemented (FN : String);
  159. begin
  160. Raise Exception.CreateFmt('Function "%s" (strutils) is not yet implemented',[FN]);
  161. end;
  162. { ---------------------------------------------------------------------
  163. Case insensitive search/replace
  164. ---------------------------------------------------------------------}
  165. Function AnsiResemblesText(const AText, AOther: string): Boolean;
  166. begin
  167. if Assigned(AnsiResemblesProc) then
  168. Result:=AnsiResemblesProc(AText,AOther)
  169. else
  170. Result:=False;
  171. end;
  172. Function AnsiContainsText(const AText, ASubText: string): Boolean;
  173. begin
  174. AnsiContainsText:=AnsiPos(AnsiUppercase(ASubText),AnsiUppercase(AText))>0;
  175. end;
  176. Function AnsiStartsText(const ASubText, AText: string): Boolean;inline;
  177. begin
  178. Result:=AnsiCompareText(Copy(AText,1,Length(AsubText)),ASubText)=0;
  179. end;
  180. Function AnsiEndsText(const ASubText, AText: string): Boolean;inline;
  181. begin
  182. result:=AnsiCompareText(Copy(AText,Length(AText)-Length(ASubText)+1,Length(ASubText)),asubtext)=0;
  183. end;
  184. Function AnsiReplaceText(const AText, AFromText, AToText: string): string;inline;
  185. begin
  186. Result := StringReplace(AText,AFromText,AToText,[rfReplaceAll,rfIgnoreCase]);
  187. end;
  188. Function AnsiMatchText(const AText: string; const AValues: array of string): Boolean;inline;
  189. begin
  190. Result:=(AnsiIndexText(AText,AValues)<>-1)
  191. end;
  192. Function AnsiIndexText(const AText: string; const AValues: array of string): Integer;
  193. var i : longint;
  194. begin
  195. result:=-1;
  196. if high(AValues)=-1 Then
  197. Exit;
  198. for i:=low(AValues) to High(Avalues) do
  199. if CompareText(avalues[i],atext)=0 Then
  200. exit(i); // make sure it is the first val.
  201. end;
  202. { ---------------------------------------------------------------------
  203. Case sensitive search/replace
  204. ---------------------------------------------------------------------}
  205. Function AnsiContainsStr(const AText, ASubText: string): Boolean;inline;
  206. begin
  207. Result := AnsiPos(ASubText,AText)>0;
  208. end;
  209. Function AnsiStartsStr(const ASubText, AText: string): Boolean;inline;
  210. begin
  211. Result := AnsiPos(ASubText,AText)=1;
  212. end;
  213. Function AnsiEndsStr(const ASubText, AText: string): Boolean;inline;
  214. begin
  215. Result := AnsiCompareStr(Copy(AText,length(AText)-length(ASubText)+1,length(ASubText)),ASubText)=0;
  216. end;
  217. Function AnsiReplaceStr(const AText, AFromText, AToText: string): string;inline;
  218. begin
  219. Result := StringReplace(AText,AFromText,AToText,[rfReplaceAll]);
  220. end;
  221. Function AnsiMatchStr(const AText: string; const AValues: array of string): Boolean;inline;
  222. begin
  223. Result:=AnsiIndexStr(AText,Avalues)<>-1;
  224. end;
  225. Function AnsiIndexStr(const AText: string; const AValues: array of string): Integer;
  226. var
  227. i : longint;
  228. begin
  229. result:=-1;
  230. if high(AValues)=-1 Then
  231. Exit;
  232. for i:=low(AValues) to High(Avalues) do
  233. if (avalues[i]=AText) Then
  234. exit(i); // make sure it is the first val.
  235. end;
  236. { ---------------------------------------------------------------------
  237. Playthingies
  238. ---------------------------------------------------------------------}
  239. Function DupeString(const AText: string; ACount: Integer): string;
  240. var i,l : integer;
  241. begin
  242. result:='';
  243. if aCount>=0 then
  244. begin
  245. l:=length(atext);
  246. SetLength(result,aCount*l);
  247. for i:=0 to ACount-1 do
  248. move(atext[1],Result[l*i+1],l);
  249. end;
  250. end;
  251. Function ReverseString(const AText: string): string;
  252. var
  253. i,j:longint;
  254. begin
  255. setlength(result,length(atext));
  256. i:=1; j:=length(atext);
  257. while (i<=j) do
  258. begin
  259. result[i]:=atext[j-i+1];
  260. inc(i);
  261. end;
  262. end;
  263. Function AnsiReverseString(const AText: AnsiString): AnsiString;inline;
  264. begin
  265. Result:=ReverseString(AText);
  266. end;
  267. Function StuffString(const AText: string; AStart, ALength: Cardinal; const ASubText: string): string;
  268. var i,j : SizeUInt;
  269. begin
  270. j:=length(ASubText);
  271. i:=length(AText);
  272. SetLength(Result,i+j-ALength);
  273. move (AText[1],result[1],AStart-1);
  274. move (ASubText[1],result[AStart],j);
  275. move (AText[AStart+ALength], Result[AStart+j],i+1-AStart-ALength);
  276. end;
  277. Function RandomFrom(const AValues: array of string): string; overload;
  278. begin
  279. if high(AValues)=-1 then exit('');
  280. result:=Avalues[random(High(AValues)+1)];
  281. end;
  282. Function IfThen(AValue: Boolean; const ATrue: string; AFalse: string): string;inline;
  283. begin
  284. if avalue then
  285. result:=atrue
  286. else
  287. result:=afalse;
  288. end;
  289. Function IfThen(AValue: Boolean; const ATrue: string): string;inline; // ; AFalse: string = ''
  290. begin
  291. if avalue then
  292. result:=atrue
  293. else
  294. result:='';
  295. end;
  296. { ---------------------------------------------------------------------
  297. VB emulations.
  298. ---------------------------------------------------------------------}
  299. Function LeftStr(const AText: AnsiString; const ACount: Integer): AnsiString;inline;
  300. begin
  301. Result:=Copy(AText,1,ACount);
  302. end;
  303. Function RightStr(const AText: AnsiString; const ACount: Integer): AnsiString;
  304. var j,l:integer;
  305. begin
  306. l:=length(atext);
  307. j:=ACount;
  308. if j>l then j:=l;
  309. Result:=Copy(AText,l-j+1,j);
  310. end;
  311. Function MidStr(const AText: AnsiString; const AStart, ACount: Integer): AnsiString;inline;
  312. begin
  313. if (ACount=0) or (AStart>length(atext)) then
  314. exit('');
  315. Result:=Copy(AText,AStart,ACount);
  316. end;
  317. Function LeftBStr(const AText: AnsiString; const AByteCount: Integer): AnsiString;inline;
  318. begin
  319. Result:=LeftStr(AText,AByteCount);
  320. end;
  321. Function RightBStr(const AText: AnsiString; const AByteCount: Integer): AnsiString;inline;
  322. begin
  323. Result:=RightStr(Atext,AByteCount);
  324. end;
  325. Function MidBStr(const AText: AnsiString; const AByteStart, AByteCount: Integer): AnsiString;inline;
  326. begin
  327. Result:=MidStr(AText,AByteStart,AByteCount);
  328. end;
  329. Function AnsiLeftStr(const AText: AnsiString; const ACount: Integer): AnsiString;inline;
  330. begin
  331. Result := copy(AText,1,ACount);
  332. end;
  333. Function AnsiRightStr(const AText: AnsiString; const ACount: Integer): AnsiString;inline;
  334. begin
  335. Result := copy(AText,length(AText)-ACount+1,ACount);
  336. end;
  337. Function AnsiMidStr(const AText: AnsiString; const AStart, ACount: Integer): AnsiString;inline;
  338. begin
  339. Result:=Copy(AText,AStart,ACount);
  340. end;
  341. Function LeftStr(const AText: WideString; const ACount: Integer): WideString;inline;
  342. begin
  343. Result:=Copy(AText,1,ACount);
  344. end;
  345. Function RightStr(const AText: WideString; const ACount: Integer): WideString;
  346. var
  347. j,l:integer;
  348. begin
  349. l:=length(atext);
  350. j:=ACount;
  351. if j>l then j:=l;
  352. Result:=Copy(AText,l-j+1,j);
  353. end;
  354. Function MidStr(const AText: WideString; const AStart, ACount: Integer): WideString;inline;
  355. begin
  356. Result:=Copy(AText,AStart,ACount);
  357. end;
  358. { ---------------------------------------------------------------------
  359. Extended search and replace
  360. ---------------------------------------------------------------------}
  361. Function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer; SearchString: String; Options: TStringSearchOptions): PChar;
  362. var
  363. Len,I,SLen: Integer;
  364. C: Char;
  365. Found : Boolean;
  366. Direction: Shortint;
  367. CharMap: array[Char] of Char;
  368. Function GotoNextWord(var P : PChar): Boolean;
  369. begin
  370. if (Direction=1) then
  371. begin
  372. // Skip characters
  373. While (Len>0) and not (P^ in WordDelimiters) do
  374. begin
  375. Inc(P);
  376. Dec(Len);
  377. end;
  378. // skip delimiters
  379. While (Len>0) and (P^ in WordDelimiters) do
  380. begin
  381. Inc(P);
  382. Dec(Len);
  383. end;
  384. Result:=Len>0;
  385. end
  386. else
  387. begin
  388. // Skip Delimiters
  389. While (Len>0) and (P^ in WordDelimiters) do
  390. begin
  391. Dec(P);
  392. Dec(Len);
  393. end;
  394. // skip characters
  395. While (Len>0) and not (P^ in WordDelimiters) do
  396. begin
  397. Dec(P);
  398. Dec(Len);
  399. end;
  400. Result:=Len>0;
  401. // We're on the first delimiter. Pos back on char.
  402. Inc(P);
  403. Inc(Len);
  404. end;
  405. end;
  406. begin
  407. Result:=nil;
  408. Slen:=Length(SearchString);
  409. if (BufLen<=0) or (Slen=0) then
  410. Exit;
  411. if soDown in Options then
  412. begin
  413. Direction:=1;
  414. Inc(SelStart,SelLength);
  415. Len:=BufLen-SelStart-SLen+1;
  416. if (Len<=0) then
  417. Exit;
  418. end
  419. else
  420. begin
  421. Direction:=-1;
  422. Dec(SelStart,Length(SearchString));
  423. Len:=SelStart+1;
  424. end;
  425. if (SelStart<0) or (SelStart>BufLen) then
  426. Exit;
  427. Result:=@Buf[SelStart];
  428. for C:=Low(Char) to High(Char) do
  429. if (soMatchCase in Options) then
  430. CharMap[C]:=C
  431. else
  432. CharMap[C]:=Upcase(C);
  433. if Not (soMatchCase in Options) then
  434. SearchString:=UpCase(SearchString);
  435. Found:=False;
  436. while (Result<>Nil) and (Not Found) do
  437. begin
  438. if ((soWholeWord in Options) and
  439. (Result<>@Buf[SelStart]) and
  440. not GotoNextWord(Result)) then
  441. Result:=Nil
  442. else
  443. begin
  444. // try to match whole searchstring
  445. I:=0;
  446. while (I<Slen) and (CharMap[Result[I]]=SearchString[I+1]) do
  447. Inc(I);
  448. // Whole searchstring matched ?
  449. if (I=SLen) then
  450. Found:=(Len=0) or
  451. (not (soWholeWord in Options)) or
  452. (Result[SLen] in WordDelimiters);
  453. if not Found then
  454. begin
  455. Inc(Result,Direction);
  456. Dec(Len);
  457. If (Len=0) then
  458. Result:=Nil;
  459. end;
  460. end;
  461. end;
  462. end;
  463. Function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer; SearchString: String): PChar;inline; // ; Options: TStringSearchOptions = [soDown]
  464. begin
  465. Result:=SearchBuf(Buf,BufLen,SelStart,SelLength,SearchString,[soDown]);
  466. end;
  467. Function PosEx(const SubStr, S: string; Offset: Cardinal): Integer;
  468. var i : pchar;
  469. begin
  470. if (offset<1) or (offset>SizeUInt(length(s))) then exit(0);
  471. i:=strpos(@s[offset],@substr[1]);
  472. if i=nil then
  473. PosEx:=0
  474. else
  475. PosEx:=succ(i-pchar(s));
  476. end;
  477. Function PosEx(const SubStr, S: string): Integer;inline; // Offset: Cardinal = 1
  478. begin
  479. posex:=posex(substr,s,1);
  480. end;
  481. Function PosEx(c:char; const S: string; Offset: Cardinal): Integer;
  482. var l : longint;
  483. begin
  484. if (offset<1) or (offset>SizeUInt(length(s))) then exit(0);
  485. l:=length(s);
  486. {$ifndef useindexbyte}
  487. while (SizeInt(offset)<=l) and (s[offset]<>c) do inc(offset);
  488. if SizeInt(offset)>l then
  489. posex:=0
  490. else
  491. posex:=offset;
  492. {$else}
  493. posex:=offset+indexbyte(s[offset],l-offset+1);
  494. if posex=(offset-1) then
  495. posex:=0;
  496. {$endif}
  497. end;
  498. { ---------------------------------------------------------------------
  499. Soundex Functions.
  500. ---------------------------------------------------------------------}
  501. Const
  502. SScore : array[1..255] of Char =
  503. ('0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0', // 1..32
  504. '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0', // 33..64
  505. '0','1','2','3','0','1','2','i','0','2','2','4','5','5','0','1','2','6','2','3','0','1','i','2','i','2', // 64..90
  506. '0','0','0','0','0','0', // 91..95
  507. '0','1','2','3','0','1','2','i','0','2','2','4','5','5','0','1','2','6','2','3','0','1','i','2','i','2', // 96..122
  508. '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0', // 123..154
  509. '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0', // 155..186
  510. '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0', // 187..218
  511. '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0', // 219..250
  512. '0','0','0','0','0'); // 251..255
  513. Function Soundex(const AText: string; ALength: TSoundexLength): string;
  514. Var
  515. S,PS : Char;
  516. I,L : integer;
  517. begin
  518. Result:='';
  519. PS:=#0;
  520. If Length(AText)>0 then
  521. begin
  522. Result:=Upcase(AText[1]);
  523. I:=2;
  524. L:=Length(AText);
  525. While (I<=L) and (Length(Result)<ALength) do
  526. begin
  527. S:=SScore[Ord(AText[i])];
  528. If Not (S in ['0','i',PS]) then
  529. Result:=Result+S;
  530. If (S<>'i') then
  531. PS:=S;
  532. Inc(I);
  533. end;
  534. end;
  535. L:=Length(Result);
  536. If (L<ALength) then
  537. Result:=Result+StringOfChar('0',Alength-L);
  538. end;
  539. Function Soundex(const AText: string): string;inline; // ; ALength: TSoundexLength = 4
  540. begin
  541. Result:=Soundex(AText,4);
  542. end;
  543. Const
  544. Ord0 = Ord('0');
  545. OrdA = Ord('A');
  546. Function SoundexInt(const AText: string; ALength: TSoundexIntLength): Integer;
  547. var
  548. SE: string;
  549. I: Integer;
  550. begin
  551. Result:=-1;
  552. SE:=Soundex(AText,ALength);
  553. If Length(SE)>0 then
  554. begin
  555. Result:=Ord(SE[1])-OrdA;
  556. if ALength > 1 then
  557. begin
  558. Result:=Result*26+(Ord(SE[2])-Ord0);
  559. for I:=3 to ALength do
  560. Result:=(Ord(SE[I])-Ord0)+Result*7;
  561. end;
  562. Result:=ALength+Result*9;
  563. end;
  564. end;
  565. Function SoundexInt(const AText: string): Integer;inline; //; ALength: TSoundexIntLength = 4
  566. begin
  567. Result:=SoundexInt(AText,4);
  568. end;
  569. Function DecodeSoundexInt(AValue: Integer): string;
  570. var
  571. I, Len: Integer;
  572. begin
  573. Result := '';
  574. Len := AValue mod 9;
  575. AValue := AValue div 9;
  576. for I:=Len downto 3 do
  577. begin
  578. Result:=Chr(Ord0+(AValue mod 7))+Result;
  579. AValue:=AValue div 7;
  580. end;
  581. if Len>2 then
  582. Result:=IntToStr(AValue mod 26)+Result;
  583. AValue:=AValue div 26;
  584. Result:=Chr(OrdA+AValue)+Result;
  585. end;
  586. Function SoundexWord(const AText: string): Word;
  587. Var
  588. S : String;
  589. begin
  590. S:=SoundEx(Atext,4);
  591. Result:=Ord(S[1])-OrdA;
  592. Result:=Result*26+StrToInt(S[2]);
  593. Result:=Result*7+StrToInt(S[3]);
  594. Result:=Result*7+StrToInt(S[4]);
  595. end;
  596. Function DecodeSoundexWord(AValue: Word): string;
  597. begin
  598. Result := Chr(Ord0+ (AValue mod 7));
  599. AValue := AValue div 7;
  600. Result := Chr(Ord0+ (AValue mod 7)) + Result;
  601. AValue := AValue div 7;
  602. Result := IntToStr(AValue mod 26) + Result;
  603. AValue := AValue div 26;
  604. Result := Chr(OrdA+AValue) + Result;
  605. end;
  606. Function SoundexSimilar(const AText, AOther: string; ALength: TSoundexLength): Boolean;inline;
  607. begin
  608. Result:=Soundex(AText,ALength)=Soundex(AOther,ALength);
  609. end;
  610. Function SoundexSimilar(const AText, AOther: string): Boolean;inline; //; ALength: TSoundexLength = 4
  611. begin
  612. Result:=SoundexSimilar(AText,AOther,4);
  613. end;
  614. Function SoundexCompare(const AText, AOther: string; ALength: TSoundexLength): Integer;inline;
  615. begin
  616. Result:=AnsiCompareStr(Soundex(AText,ALength),Soundex(AOther,ALength));
  617. end;
  618. Function SoundexCompare(const AText, AOther: string): Integer;inline; //; ALength: TSoundexLength = 4
  619. begin
  620. Result:=SoundexCompare(AText,AOther,4);
  621. end;
  622. Function SoundexProc(const AText, AOther: string): Boolean;
  623. begin
  624. Result:=SoundexSimilar(AText,AOther);
  625. end;
  626. { ---------------------------------------------------------------------
  627. RxStrUtils-like functions.
  628. ---------------------------------------------------------------------}
  629. function IsEmptyStr(const S: string; const EmptyChars: TSysCharSet): Boolean;
  630. var
  631. i,l: Integer;
  632. begin
  633. l:=Length(S);
  634. i:=1;
  635. Result:=True;
  636. while Result and (i<=l) do
  637. begin
  638. Result:=Not (S[i] in EmptyChars);
  639. Inc(i);
  640. end;
  641. end;
  642. function DelSpace(const S: String): string;
  643. begin
  644. Result:=DelChars(S,' ');
  645. end;
  646. function DelChars(const S: string; Chr: Char): string;
  647. var
  648. I,J: Integer;
  649. begin
  650. Result:=S;
  651. I:=Length(Result);
  652. While I>0 do
  653. begin
  654. if Result[I]=Chr then
  655. begin
  656. J:=I-1;
  657. While (J>0) and (Result[J]=Chr) do
  658. Dec(j);
  659. Delete(Result,J+1,I-J);
  660. I:=J+1;
  661. end;
  662. dec(I);
  663. end;
  664. end;
  665. function DelSpace1(const S: string): string;
  666. var
  667. i: Integer;
  668. begin
  669. Result:=S;
  670. for i:=Length(Result) downto 2 do
  671. if (Result[i]=' ') and (Result[I-1]=' ') then
  672. Delete(Result,I,1);
  673. end;
  674. function Tab2Space(const S: string; Numb: Byte): string;
  675. var
  676. I: Integer;
  677. begin
  678. I:=1;
  679. Result:=S;
  680. while I <= Length(Result) do
  681. if Result[I]<>Chr(9) then
  682. inc(I)
  683. else
  684. begin
  685. Result[I]:=' ';
  686. If (Numb>1) then
  687. Insert(StringOfChar('0',Numb-1),Result,I);
  688. Inc(I,Numb);
  689. end;
  690. end;
  691. function NPos(const C: string; S: string; N: Integer): Integer;
  692. var
  693. i,p,k: Integer;
  694. begin
  695. Result:=0;
  696. if N<1 then
  697. Exit;
  698. k:=0;
  699. i:=1;
  700. Repeat
  701. p:=pos(C,S);
  702. Inc(k,p);
  703. if p>0 then
  704. delete(S,1,p);
  705. Inc(i);
  706. Until (i>n) or (p=0);
  707. If (P>0) then
  708. Result:=K;
  709. end;
  710. function AddChar(C: Char; const S: string; N: Integer): string;
  711. Var
  712. l : Integer;
  713. begin
  714. Result:=S;
  715. l:=Length(Result);
  716. if l<N then
  717. Result:=StringOfChar(C,N-l)+Result;
  718. end;
  719. function AddCharR(C: Char; const S: string; N: Integer): string;
  720. Var
  721. l : Integer;
  722. begin
  723. Result:=S;
  724. l:=Length(Result);
  725. if l<N then
  726. Result:=Result+StringOfChar(C,N-l);
  727. end;
  728. function PadRight(const S: string; N: Integer): string;inline;
  729. begin
  730. Result:=AddCharR(' ',S,N);
  731. end;
  732. function PadLeft(const S: string; N: Integer): string;inline;
  733. begin
  734. Result:=AddChar(' ',S,N);
  735. end;
  736. function Copy2Symb(const S: string; Symb: Char): string;
  737. var
  738. p: Integer;
  739. begin
  740. p:=Pos(Symb,S);
  741. if p=0 then
  742. p:=Length(S)+1;
  743. Result:=Copy(S,1,p-1);
  744. end;
  745. function Copy2SymbDel(var S: string; Symb: Char): string;
  746. begin
  747. Result:=Copy2Symb(S,Symb);
  748. S:=TrimRight(Copy(S,Length(Result)+1,Length(S)));
  749. end;
  750. function Copy2Space(const S: string): string;inline;
  751. begin
  752. Result:=Copy2Symb(S,' ');
  753. end;
  754. function Copy2SpaceDel(var S: string): string;inline;
  755. begin
  756. Result:=Copy2SymbDel(S,' ');
  757. end;
  758. function AnsiProperCase(const S: string; const WordDelims: TSysCharSet): string;
  759. var
  760. // l : Integer;
  761. P,PE : PChar;
  762. begin
  763. Result:=AnsiLowerCase(S);
  764. P:=PChar(Result);
  765. PE:=P+Length(Result);
  766. while (P<PE) do
  767. begin
  768. while (P<PE) and (P^ in WordDelims) do
  769. inc(P);
  770. if (P<PE) then
  771. P^:=UpCase(P^);
  772. while (P<PE) and not (P^ in WordDelims) do
  773. inc(P);
  774. end;
  775. end;
  776. function WordCount(const S: string; const WordDelims: TSysCharSet): Integer;
  777. var
  778. P,PE : PChar;
  779. begin
  780. Result:=0;
  781. P:=Pchar(S);
  782. PE:=P+Length(S);
  783. while (P<PE) do
  784. begin
  785. while (P<PE) and (P^ in WordDelims) do
  786. Inc(P);
  787. if (P<PE) then
  788. inc(Result);
  789. while (P<PE) and not (P^ in WordDelims) do
  790. inc(P);
  791. end;
  792. end;
  793. function WordPosition(const N: Integer; const S: string; const WordDelims: TSysCharSet): Integer;
  794. var
  795. PS,P,PE : PChar;
  796. Count: Integer;
  797. begin
  798. Result:=0;
  799. Count:=0;
  800. PS:=PChar(S);
  801. PE:=PS+Length(S);
  802. P:=PS;
  803. while (P<PE) and (Count<>N) do
  804. begin
  805. while (P<PE) and (P^ in WordDelims) do
  806. inc(P);
  807. if (P<PE) then
  808. inc(Count);
  809. if (Count<>N) then
  810. while (P<PE) and not (P^ in WordDelims) do
  811. inc(P)
  812. else
  813. Result:=(P-PS)+1;
  814. end;
  815. end;
  816. function ExtractWord(N: Integer; const S: string; const WordDelims: TSysCharSet): string;inline;
  817. var
  818. i: Integer;
  819. begin
  820. Result:=ExtractWordPos(N,S,WordDelims,i);
  821. end;
  822. function ExtractWordPos(N: Integer; const S: string; const WordDelims: TSysCharSet; var Pos: Integer): string;
  823. var
  824. i,j,l: Integer;
  825. begin
  826. j:=0;
  827. i:=WordPosition(N, S, WordDelims);
  828. Pos:=i;
  829. if (i<>0) then
  830. begin
  831. j:=i;
  832. l:=Length(S);
  833. while (j<=L) and not (S[j] in WordDelims) do
  834. inc(j);
  835. end;
  836. SetLength(Result,j-i);
  837. If ((j-i)>0) then
  838. Move(S[i],Result[1],j-i);
  839. end;
  840. function ExtractDelimited(N: Integer; const S: string; const Delims: TSysCharSet): string;
  841. var
  842. w,i,l,len: Integer;
  843. begin
  844. w:=0;
  845. i:=1;
  846. l:=0;
  847. len:=Length(S);
  848. SetLength(Result, 0);
  849. while (i<=len) and (w<>N) do
  850. begin
  851. if s[i] in Delims then
  852. inc(w)
  853. else
  854. begin
  855. if (N-1)=w then
  856. begin
  857. inc(l);
  858. SetLength(Result,l);
  859. Result[L]:=S[i];
  860. end;
  861. end;
  862. inc(i);
  863. end;
  864. end;
  865. function ExtractSubstr(const S: string; var Pos: Integer; const Delims: TSysCharSet): string;
  866. var
  867. i,l: Integer;
  868. begin
  869. i:=Pos;
  870. l:=Length(S);
  871. while (i<=l) and not (S[i] in Delims) do
  872. inc(i);
  873. Result:=Copy(S,Pos,i-Pos);
  874. if (i<=l) and (S[i] in Delims) then
  875. inc(i);
  876. Pos:=i;
  877. end;
  878. function isWordPresent(const W, S: string; const WordDelims: TSysCharSet): Boolean;
  879. var
  880. i,Count : Integer;
  881. begin
  882. Result:=False;
  883. Count:=WordCount(S, WordDelims);
  884. I:=1;
  885. While (Not Result) and (I<=Count) do
  886. begin
  887. Result:=ExtractWord(i,S,WordDelims)=W;
  888. Inc(i);
  889. end;
  890. end;
  891. function Numb2USA(const S: string): string;
  892. var
  893. i, NA: Integer;
  894. begin
  895. i:=Length(S);
  896. Result:=S;
  897. NA:=0;
  898. while (i > 0) do begin
  899. if ((Length(Result) - i + 1 - NA) mod 3 = 0) and (i <> 1) then
  900. begin
  901. insert(',', Result, i);
  902. inc(NA);
  903. end;
  904. Dec(i);
  905. end;
  906. end;
  907. function PadCenter(const S: string; Len: Integer): string;
  908. begin
  909. if Length(S)<Len then
  910. begin
  911. Result:=StringOfChar(' ',(Len div 2) -(Length(S) div 2))+S;
  912. Result:=Result+StringOfChar(' ',Len-Length(Result));
  913. end
  914. else
  915. Result:=S;
  916. end;
  917. function Hex2Dec(const S: string): Longint;
  918. var
  919. HexStr: string;
  920. begin
  921. if Pos('$',S)=0 then
  922. HexStr:='$'+ S
  923. else
  924. HexStr:=S;
  925. Result:=StrTointDef(HexStr,0);
  926. end;
  927. function Dec2Numb(N: Longint; Len, Base: Byte): string;
  928. var
  929. C: Integer;
  930. Number: Longint;
  931. begin
  932. if N=0 then
  933. Result:='0'
  934. else
  935. begin
  936. Number:=N;
  937. Result:='';
  938. while Number>0 do
  939. begin
  940. C:=Number mod Base;
  941. if C>9 then
  942. C:=C+55
  943. else
  944. C:=C+48;
  945. Result:=Chr(C)+Result;
  946. Number:=Number div Base;
  947. end;
  948. end;
  949. if (Result<>'') then
  950. Result:=AddChar('0',Result,Len);
  951. end;
  952. function Numb2Dec(S: string; Base: Byte): Longint;
  953. var
  954. i, P: Longint;
  955. begin
  956. i:=Length(S);
  957. Result:=0;
  958. S:=UpperCase(S);
  959. P:=1;
  960. while (i>=1) do
  961. begin
  962. if (S[i]>'@') then
  963. Result:=Result+(Ord(S[i])-55)*P
  964. else
  965. Result:=Result+(Ord(S[i])-48)*P;
  966. Dec(i);
  967. P:=P*Base;
  968. end;
  969. end;
  970. function RomanToint(const S: string): Longint;
  971. const
  972. RomanChars = ['C','D','i','L','M','V','X'];
  973. RomanValues : array['C'..'X'] of Word
  974. = (100,500,0,0,0,0,1,0,0,50,1000,0,0,0,0,0,0,0,0,5,0,10);
  975. var
  976. index, Next: Char;
  977. i,l: Integer;
  978. Negative: Boolean;
  979. begin
  980. Result:=0;
  981. i:=0;
  982. Negative:=(Length(S)>0) and (S[1]='-');
  983. if Negative then
  984. inc(i);
  985. l:=Length(S);
  986. while (i<l) do
  987. begin
  988. inc(i);
  989. index:=UpCase(S[i]);
  990. if index in RomanChars then
  991. begin
  992. if Succ(i)<=l then
  993. Next:=UpCase(S[i+1])
  994. else
  995. Next:=#0;
  996. if (Next in RomanChars) and (RomanValues[index]<RomanValues[Next]) then
  997. begin
  998. inc(Result, RomanValues[Next]);
  999. Dec(Result, RomanValues[index]);
  1000. inc(i);
  1001. end
  1002. else
  1003. inc(Result, RomanValues[index]);
  1004. end
  1005. else
  1006. begin
  1007. Result:=0;
  1008. Exit;
  1009. end;
  1010. end;
  1011. if Negative then
  1012. Result:=-Result;
  1013. end;
  1014. function intToRoman(Value: Longint): string;
  1015. const
  1016. Arabics : Array[1..13] of Integer
  1017. = (1,4,5,9,10,40,50,90,100,400,500,900,1000);
  1018. Romans : Array[1..13] of String
  1019. = ('I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M');
  1020. var
  1021. i: Integer;
  1022. begin
  1023. Result:='';
  1024. for i:=13 downto 1 do
  1025. while (Value >= Arabics[i]) do
  1026. begin
  1027. Value:=Value-Arabics[i];
  1028. Result:=Result+Romans[i];
  1029. end;
  1030. end;
  1031. function intToBin(Value: Longint; Digits, Spaces: Integer): string;
  1032. begin
  1033. Result:='';
  1034. if (Digits>32) then
  1035. Digits:=32;
  1036. while (Digits>0) do
  1037. begin
  1038. if (Digits mod Spaces)=0 then
  1039. Result:=Result+' ';
  1040. Dec(Digits);
  1041. Result:=Result+intToStr((Value shr Digits) and 1);
  1042. end;
  1043. end;
  1044. function FindPart(const HelpWilds, inputStr: string): Integer;
  1045. var
  1046. i, J: Integer;
  1047. Diff: Integer;
  1048. begin
  1049. Result:=0;
  1050. i:=Pos('?',HelpWilds);
  1051. if (i=0) then
  1052. Result:=Pos(HelpWilds, inputStr)
  1053. else
  1054. begin
  1055. Diff:=Length(inputStr) - Length(HelpWilds);
  1056. for i:=0 to Diff do
  1057. begin
  1058. for J:=1 to Length(HelpWilds) do
  1059. if (inputStr[i + J] = HelpWilds[J]) or (HelpWilds[J] = '?') then
  1060. begin
  1061. if (J=Length(HelpWilds)) then
  1062. begin
  1063. Result:=i+1;
  1064. Exit;
  1065. end;
  1066. end
  1067. else
  1068. Break;
  1069. end;
  1070. end;
  1071. end;
  1072. function isWild(inputStr, Wilds: string; ignoreCase: Boolean): Boolean;
  1073. function SearchNext(var Wilds: string): Integer;
  1074. begin
  1075. Result:=Pos('*', Wilds);
  1076. if Result>0 then
  1077. Wilds:=Copy(Wilds,1,Result - 1);
  1078. end;
  1079. var
  1080. CWild, CinputWord: Integer; { counter for positions }
  1081. i, LenHelpWilds: Integer;
  1082. MaxinputWord, MaxWilds: Integer; { Length of inputStr and Wilds }
  1083. HelpWilds: string;
  1084. begin
  1085. if Wilds = inputStr then begin
  1086. Result:=True;
  1087. Exit;
  1088. end;
  1089. repeat { delete '**', because '**' = '*' }
  1090. i:=Pos('**', Wilds);
  1091. if i > 0 then
  1092. Wilds:=Copy(Wilds, 1, i - 1) + '*' + Copy(Wilds, i + 2, Maxint);
  1093. until i = 0;
  1094. if Wilds = '*' then begin { for fast end, if Wilds only '*' }
  1095. Result:=True;
  1096. Exit;
  1097. end;
  1098. MaxinputWord:=Length(inputStr);
  1099. MaxWilds:=Length(Wilds);
  1100. if ignoreCase then begin { upcase all letters }
  1101. inputStr:=AnsiUpperCase(inputStr);
  1102. Wilds:=AnsiUpperCase(Wilds);
  1103. end;
  1104. if (MaxWilds = 0) or (MaxinputWord = 0) then begin
  1105. Result:=False;
  1106. Exit;
  1107. end;
  1108. CinputWord:=1;
  1109. CWild:=1;
  1110. Result:=True;
  1111. repeat
  1112. if inputStr[CinputWord] = Wilds[CWild] then begin { equal letters }
  1113. { goto next letter }
  1114. inc(CWild);
  1115. inc(CinputWord);
  1116. Continue;
  1117. end;
  1118. if Wilds[CWild] = '?' then begin { equal to '?' }
  1119. { goto next letter }
  1120. inc(CWild);
  1121. inc(CinputWord);
  1122. Continue;
  1123. end;
  1124. if Wilds[CWild] = '*' then begin { handling of '*' }
  1125. HelpWilds:=Copy(Wilds, CWild + 1, MaxWilds);
  1126. i:=SearchNext(HelpWilds);
  1127. LenHelpWilds:=Length(HelpWilds);
  1128. if i = 0 then begin
  1129. { no '*' in the rest, compare the ends }
  1130. if HelpWilds = '' then Exit; { '*' is the last letter }
  1131. { check the rest for equal Length and no '?' }
  1132. for i:=0 to LenHelpWilds - 1 do begin
  1133. if (HelpWilds[LenHelpWilds - i] <> inputStr[MaxinputWord - i]) and
  1134. (HelpWilds[LenHelpWilds - i]<> '?') then
  1135. begin
  1136. Result:=False;
  1137. Exit;
  1138. end;
  1139. end;
  1140. Exit;
  1141. end;
  1142. { handle all to the next '*' }
  1143. inc(CWild, 1 + LenHelpWilds);
  1144. i:=FindPart(HelpWilds, Copy(inputStr, CinputWord, Maxint));
  1145. if i= 0 then begin
  1146. Result:=False;
  1147. Exit;
  1148. end;
  1149. CinputWord:=i + LenHelpWilds;
  1150. Continue;
  1151. end;
  1152. Result:=False;
  1153. Exit;
  1154. until (CinputWord > MaxinputWord) or (CWild > MaxWilds);
  1155. { no completed evaluation }
  1156. if CinputWord <= MaxinputWord then Result:=False;
  1157. if (CWild <= MaxWilds) and (Wilds[MaxWilds] <> '*') then Result:=False;
  1158. end;
  1159. function XorString(const Key, Src: ShortString): ShortString;
  1160. var
  1161. i: Integer;
  1162. begin
  1163. Result:=Src;
  1164. if Length(Key) > 0 then
  1165. for i:=1 to Length(Src) do
  1166. Result[i]:=Chr(Byte(Key[1 + ((i - 1) mod Length(Key))]) xor Ord(Src[i]));
  1167. end;
  1168. function XorEncode(const Key, Source: string): string;
  1169. var
  1170. i: Integer;
  1171. C: Byte;
  1172. begin
  1173. Result:='';
  1174. for i:=1 to Length(Source) do
  1175. begin
  1176. if Length(Key) > 0 then
  1177. C:=Byte(Key[1 + ((i - 1) mod Length(Key))]) xor Byte(Source[i])
  1178. else
  1179. C:=Byte(Source[i]);
  1180. Result:=Result+AnsiLowerCase(intToHex(C, 2));
  1181. end;
  1182. end;
  1183. function XorDecode(const Key, Source: string): string;
  1184. var
  1185. i: Integer;
  1186. C: Char;
  1187. begin
  1188. Result:='';
  1189. for i:=0 to Length(Source) div 2 - 1 do
  1190. begin
  1191. C:=Chr(StrTointDef('$' + Copy(Source, (i * 2) + 1, 2), Ord(' ')));
  1192. if Length(Key) > 0 then
  1193. C:=Chr(Byte(Key[1 + (i mod Length(Key))]) xor Byte(C));
  1194. Result:=Result + C;
  1195. end;
  1196. end;
  1197. function GetCmdLineArg(const Switch: string; SwitchChars: TSysCharSet): string;
  1198. var
  1199. i: Integer;
  1200. S: string;
  1201. begin
  1202. i:=1;
  1203. Result:='';
  1204. while (Result='') and (i<=ParamCount) do
  1205. begin
  1206. S:=ParamStr(i);
  1207. if (SwitchChars=[]) or ((S[1] in SwitchChars) and (Length(S) > 1)) and
  1208. (AnsiCompareText(Copy(S,2,Length(S)-1),Switch)=0) then
  1209. begin
  1210. inc(i);
  1211. if i<=ParamCount then
  1212. Result:=ParamStr(i);
  1213. end;
  1214. inc(i);
  1215. end;
  1216. end;
  1217. Function RPosEX(C:char;const S : AnsiString;offs:cardinal):Integer; overload;
  1218. var I : SizeUInt;
  1219. p,p2: pChar;
  1220. Begin
  1221. I:=Length(S);
  1222. If (I<>0) and (offs<=i) Then
  1223. begin
  1224. p:=@s[offs];
  1225. p2:=@s[1];
  1226. while (p2<=p) and (p^<>c) do dec(p);
  1227. RPosEx:=(p-p2)+1;
  1228. end
  1229. else
  1230. RPosEX:=0;
  1231. End;
  1232. Function RPos(c:char;const S : AnsiString):Integer; overload;
  1233. var I : Integer;
  1234. p,p2: pChar;
  1235. Begin
  1236. I:=Length(S);
  1237. If I<>0 Then
  1238. begin
  1239. p:=@s[i];
  1240. p2:=@s[1];
  1241. while (p2<=p) and (p^<>c) do dec(p);
  1242. i:=p-p2+1;
  1243. end;
  1244. RPos:=i;
  1245. End;
  1246. Function RPos (Const Substr : AnsiString; Const Source : AnsiString) : Integer; overload;
  1247. var
  1248. MaxLen,llen : Integer;
  1249. c : char;
  1250. pc,pc2 : pchar;
  1251. begin
  1252. rPos:=0;
  1253. llen:=Length(SubStr);
  1254. maxlen:=length(source);
  1255. if (llen>0) and (maxlen>0) and ( llen<=maxlen) then
  1256. begin
  1257. // i:=maxlen;
  1258. pc:=@source[maxlen];
  1259. pc2:=@source[llen-1];
  1260. c:=substr[llen];
  1261. while pc>=pc2 do
  1262. begin
  1263. if (c=pc^) and
  1264. (CompareChar(Substr[1],pchar(pc-llen+1)^,Length(SubStr))=0) then
  1265. begin
  1266. rPos:=pchar(pc-llen+1)-pchar(@source[1])+1;
  1267. exit;
  1268. end;
  1269. dec(pc);
  1270. end;
  1271. end;
  1272. end;
  1273. Function RPosex (Const Substr : AnsiString; Const Source : AnsiString;offs:cardinal) : Integer; overload;
  1274. var
  1275. MaxLen,llen : Integer;
  1276. c : char;
  1277. pc,pc2 : pchar;
  1278. begin
  1279. rPosex:=0;
  1280. llen:=Length(SubStr);
  1281. maxlen:=length(source);
  1282. if SizeInt(offs)<maxlen then maxlen:=offs;
  1283. if (llen>0) and (maxlen>0) and ( llen<=maxlen) then
  1284. begin
  1285. // i:=maxlen;
  1286. pc:=@source[maxlen];
  1287. pc2:=@source[llen-1];
  1288. c:=substr[llen];
  1289. while pc>=pc2 do
  1290. begin
  1291. if (c=pc^) and
  1292. (CompareChar(Substr[1],pchar(pc-llen+1)^,Length(SubStr))=0) then
  1293. begin
  1294. rPosex:=pchar(pc-llen+1)-pchar(@source[1])+1;
  1295. exit;
  1296. end;
  1297. dec(pc);
  1298. end;
  1299. end;
  1300. end;
  1301. // def from delphi.about.com:
  1302. procedure BinToHex(BinValue, HexValue: PChar; BinBufSize: Integer);
  1303. Const
  1304. HexDigits='0123456789ABCDEF';
  1305. var
  1306. i : longint;
  1307. begin
  1308. for i:=0 to binbufsize-1 do
  1309. begin
  1310. HexValue[0]:=hexdigits[1+((ord(binvalue^) shr 4))];
  1311. HexValue[1]:=hexdigits[1+((ord(binvalue^) and 15))];
  1312. inc(hexvalue,2);
  1313. inc(binvalue);
  1314. end;
  1315. end;
  1316. function HexToBin(HexValue, BinValue: PChar; BinBufSize: Integer): Integer;
  1317. // more complex, have to accept more than bintohex
  1318. // A..F    1000001
  1319. // a..f    1100001
  1320. // 0..9     110000
  1321. var i,j,h,l : integer;
  1322. begin
  1323. i:=binbufsize;
  1324. while (i>0) do
  1325. begin
  1326. if hexvalue^ IN ['A'..'F','a'..'f'] then
  1327. h:=((ord(hexvalue^)+9) and 15)
  1328. else if hexvalue^ IN ['0'..'9'] then
  1329. h:=((ord(hexvalue^)) and 15)
  1330. else
  1331. break;
  1332. inc(hexvalue);
  1333. if hexvalue^ IN ['A'..'F','a'..'f'] then
  1334. l:=(ord(hexvalue^)+9) and 15
  1335. else if hexvalue^ IN ['0'..'9'] then
  1336. l:=(ord(hexvalue^)) and 15
  1337. else
  1338. break;
  1339. j := l + (h shl 4);
  1340. inc(hexvalue);
  1341. binvalue^:=chr(j);
  1342. inc(binvalue);
  1343. dec(i);
  1344. end;
  1345. result:=binbufsize-i;
  1346. end;
  1347. function possetex (const c:TSysCharSet;const s : ansistring;count:Integer ):Integer;
  1348. var i,j:Integer;
  1349. begin
  1350. if pchar(s)=nil then
  1351. j:=0
  1352. else
  1353. begin
  1354. i:=length(s);
  1355. j:=count;
  1356. if j>i then
  1357. begin
  1358. result:=0;
  1359. exit;
  1360. end;
  1361. while (j<=i) and (not (s[j] in c)) do inc(j);
  1362. if (j>i) then
  1363. j:=0; // not found.
  1364. end;
  1365. result:=j;
  1366. end;
  1367. function posset (const c:TSysCharSet;const s : ansistring ):Integer;
  1368. begin
  1369. result:=possetex(c,s,1);
  1370. end;
  1371. function possetex (const c:string;const s : ansistring;count:Integer ):Integer;
  1372. var cset : TSysCharSet;
  1373. i : integer;
  1374. begin
  1375. cset:=[];
  1376. if length(c)>0 then
  1377. for i:=1 to length(c) do
  1378. include(cset,c[i]);
  1379. result:=possetex(cset,s,count);
  1380. end;
  1381. function posset (const c:string;const s : ansistring ):Integer;
  1382. var cset : TSysCharSet;
  1383. i : integer;
  1384. begin
  1385. cset:=[];
  1386. if length(c)>0 then
  1387. for i:=1 to length(c) do
  1388. include(cset,c[i]);
  1389. result:=possetex(cset,s,1);
  1390. end;
  1391. end.