strutils.pp 44 KB

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