ClpStringUtils.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. { *********************************************************************************** }
  2. { * CryptoLib Library * }
  3. { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
  4. { * Github Repository <https://github.com/Xor-el> * }
  5. { * Distributed under the MIT software license, see the accompanying file LICENSE * }
  6. { * or visit http://www.opensource.org/licenses/mit-license.php. * }
  7. { * Acknowledgements: * }
  8. { * * }
  9. { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
  10. { * development of this library * }
  11. { * ******************************************************************************* * }
  12. (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
  13. unit ClpStringUtils;
  14. {$I CryptoLib.inc}
  15. interface
  16. uses
  17. SysUtils,
  18. StrUtils,
  19. ClpBits,
  20. ClpCryptoLibTypes;
  21. type
  22. TStringUtils = class sealed(TObject)
  23. public
  24. class function GetStringHashCode(const Input: string): Int32; static;
  25. class function SplitString(const Input: string; Delimiter: Char)
  26. : TCryptoLibStringArray; static;
  27. class function BeginsWith(const Input, SubString: string;
  28. IgnoreCase: Boolean; Offset: Int32 = 1): Boolean; static;
  29. class function EndsWith(const Input, SubString: String; IgnoreCase: Boolean)
  30. : Boolean; static;
  31. class function LastIndexOf(const Input, SubString: string;
  32. IgnoreCase: Boolean): Int32; overload; static; inline;
  33. class function LastIndexOf(const Input, SubString: string;
  34. StartIndex, Count: Int32; IgnoreCase: Boolean): Int32; overload; static;
  35. class function StringToCharArray(const S: String)
  36. : TCryptoLibCharArray; static;
  37. end;
  38. implementation
  39. { TStringUtils }
  40. class function TStringUtils.EndsWith(const Input, SubString: String;
  41. IgnoreCase: Boolean): Boolean;
  42. var
  43. SubStringLength: Int32;
  44. TempString: String;
  45. begin
  46. SubStringLength := System.Length(SubString);
  47. Result := SubStringLength > 0;
  48. if Result then
  49. begin
  50. TempString := System.Copy(Input, System.Length(Input) - SubStringLength + 1,
  51. SubStringLength);
  52. Result := System.Length(TempString) = SubStringLength;
  53. if Result then
  54. begin
  55. if IgnoreCase then
  56. begin
  57. Result := CompareText(TempString, SubString) = 0
  58. end
  59. else
  60. begin
  61. Result := TempString = SubString;
  62. end;
  63. end;
  64. end;
  65. end;
  66. class function TStringUtils.GetStringHashCode(const Input: string): Int32;
  67. var
  68. LowPoint, HighPoint: Int32;
  69. LResult: UInt32;
  70. begin
  71. LResult := 0;
  72. {$IFDEF DELPHIXE3_UP}
  73. LowPoint := System.Low(Input);
  74. HighPoint := System.High(Input);
  75. {$ELSE}
  76. LowPoint := 1;
  77. HighPoint := System.Length(Input);
  78. {$ENDIF DELPHIXE3_UP}
  79. while LowPoint <= HighPoint do
  80. begin
  81. LResult := TBits.RotateLeft32(LResult, 5);
  82. LResult := LResult xor UInt32(Input[LowPoint]);
  83. System.Inc(LowPoint);
  84. end;
  85. Result := Int32(LResult);
  86. end;
  87. class function TStringUtils.LastIndexOf(const Input, SubString: string;
  88. StartIndex, Count: Int32; IgnoreCase: Boolean): Int32;
  89. var
  90. I, L, LS, M: Int32;
  91. S: String;
  92. P: PChar;
  93. begin
  94. Result := -1;
  95. LS := System.Length(Input);
  96. L := System.Length(SubString);
  97. if (L = 0) or (L > LS) then
  98. begin
  99. Exit;
  100. end;
  101. P := PChar(SubString);
  102. S := Input;
  103. I := StartIndex + 1; // 1 based
  104. if (I > LS) then
  105. begin
  106. I := LS;
  107. end;
  108. I := I - L + 1;
  109. M := StartIndex - Count + 1; // 1 based
  110. if M < 1 then
  111. begin
  112. M := 1;
  113. end;
  114. while (Result = -1) and (I >= M) do
  115. begin
  116. if IgnoreCase then
  117. begin
  118. if (StrLiComp(PChar(@S[I]), P, L) = 0) then
  119. begin
  120. Result := I - 1;
  121. end;
  122. end
  123. else
  124. begin
  125. if (StrLComp(PChar(@S[I]), P, L) = 0) then
  126. begin
  127. Result := I - 1;
  128. end;
  129. end;
  130. Dec(I);
  131. end;
  132. end;
  133. class function TStringUtils.LastIndexOf(const Input, SubString: string;
  134. IgnoreCase: Boolean): Int32;
  135. begin
  136. Result := LastIndexOf(Input, SubString, System.Length(Input) - 1,
  137. System.Length(Input), IgnoreCase);
  138. end;
  139. class function TStringUtils.SplitString(const Input: string; Delimiter: Char)
  140. : TCryptoLibStringArray;
  141. var
  142. PosStart, PosDel, SplitPoints, I, LowPoint, HighPoint, Len: Int32;
  143. begin
  144. Result := Nil;
  145. if Input <> '' then
  146. begin
  147. { Determine the length of the resulting array }
  148. {$IFDEF DELPHIXE3_UP}
  149. LowPoint := System.Low(Input);
  150. HighPoint := System.High(Input);
  151. {$ELSE}
  152. LowPoint := 1;
  153. HighPoint := System.Length(Input);
  154. {$ENDIF DELPHIXE3_UP}
  155. SplitPoints := 0;
  156. for I := LowPoint to HighPoint do
  157. begin
  158. if (Delimiter = Input[I]) then
  159. System.Inc(SplitPoints);
  160. end;
  161. System.SetLength(Result, SplitPoints + 1);
  162. { Split the string and fill the resulting array }
  163. I := 0;
  164. Len := System.Length(Delimiter);
  165. PosStart := 1;
  166. PosDel := System.Pos(Delimiter, Input);
  167. while PosDel > 0 do
  168. begin
  169. Result[I] := System.Copy(Input, PosStart, PosDel - PosStart);
  170. PosStart := PosDel + Len;
  171. PosDel := PosEx(Delimiter, Input, PosStart);
  172. System.Inc(I);
  173. end;
  174. Result[I] := System.Copy(Input, PosStart, System.Length(Input));
  175. end;
  176. end;
  177. class function TStringUtils.StringToCharArray(const S: String)
  178. : TCryptoLibCharArray;
  179. begin
  180. Result := Nil;
  181. if System.Length(S) > 0 then
  182. begin
  183. System.SetLength(Result, System.Length(S) + 1);
  184. StrPLCopy(PChar(Result), S, System.Length(Result));
  185. System.SetLength(Result, System.Length(S)); // to remove the null terminator
  186. end;
  187. end;
  188. class function TStringUtils.BeginsWith(const Input, SubString: string;
  189. IgnoreCase: Boolean; Offset: Int32): Boolean;
  190. var
  191. L: Int32;
  192. PtrInput, PtrSubString: PChar;
  193. begin
  194. L := System.Length(SubString);
  195. Result := L > 0;
  196. PtrInput := PChar(Input);
  197. System.Inc(PtrInput, Offset - 1);
  198. PtrSubString := PChar(SubString);
  199. if Result then
  200. begin
  201. if IgnoreCase then
  202. begin
  203. Result := StrLiComp(PtrSubString, PtrInput, L) = 0
  204. end
  205. else
  206. begin
  207. Result := StrLComp(PtrSubString, PtrInput, L) = 0
  208. end;
  209. end;
  210. end;
  211. end.