ClpStringUtils.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 ..\Include\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. end;
  30. implementation
  31. { TStringUtils }
  32. class function TStringUtils.GetStringHashCode(const Input: string): Int32;
  33. var
  34. LowPoint, HighPoint: Int32;
  35. LResult: UInt32;
  36. begin
  37. LResult := 0;
  38. {$IFDEF DELPHIXE3_UP}
  39. LowPoint := System.Low(Input);
  40. HighPoint := System.High(Input);
  41. {$ELSE}
  42. LowPoint := 1;
  43. HighPoint := System.Length(Input);
  44. {$ENDIF DELPHIXE3_UP}
  45. while LowPoint <= HighPoint do
  46. begin
  47. LResult := TBits.RotateLeft32(LResult, 5);
  48. LResult := LResult xor UInt32(Input[LowPoint]);
  49. System.Inc(LowPoint);
  50. end;
  51. Result := Int32(LResult);
  52. end;
  53. class function TStringUtils.SplitString(const Input: string; Delimiter: Char)
  54. : TCryptoLibStringArray;
  55. var
  56. PosStart, PosDel, SplitPoints, I, LowPoint, HighPoint, Len: Int32;
  57. begin
  58. Result := Nil;
  59. if Input <> '' then
  60. begin
  61. { Determine the length of the resulting array }
  62. {$IFDEF DELPHIXE3_UP}
  63. LowPoint := System.Low(Input);
  64. HighPoint := System.High(Input);
  65. {$ELSE}
  66. LowPoint := 1;
  67. HighPoint := System.Length(Input);
  68. {$ENDIF DELPHIXE3_UP}
  69. SplitPoints := 0;
  70. for I := LowPoint to HighPoint do
  71. begin
  72. if (Delimiter = Input[I]) then
  73. System.Inc(SplitPoints);
  74. end;
  75. System.SetLength(Result, SplitPoints + 1);
  76. { Split the string and fill the resulting array }
  77. I := 0;
  78. Len := System.Length(Delimiter);
  79. PosStart := 1;
  80. PosDel := System.Pos(Delimiter, Input);
  81. while PosDel > 0 do
  82. begin
  83. Result[I] := System.Copy(Input, PosStart, PosDel - PosStart);
  84. PosStart := PosDel + Len;
  85. PosDel := PosEx(Delimiter, Input, PosStart);
  86. System.Inc(I);
  87. end;
  88. Result[I] := System.Copy(Input, PosStart, System.Length(Input));
  89. end;
  90. end;
  91. class function TStringUtils.BeginsWith(const Input, SubString: string;
  92. IgnoreCase: Boolean; Offset: Int32): Boolean;
  93. var
  94. L: Integer;
  95. PtrInput, PtrSubString: PChar;
  96. begin
  97. L := System.Length(SubString);
  98. Result := L > 0;
  99. PtrInput := PChar(Input);
  100. System.Inc(PtrInput, Offset - 1);
  101. PtrSubString := PChar(SubString);
  102. if Result then
  103. begin
  104. if IgnoreCase then
  105. begin
  106. Result := StrLiComp(PtrSubString, PtrInput, L) = 0
  107. end
  108. else
  109. begin
  110. Result := StrLComp(PtrSubString, PtrInput, L) = 0
  111. end;
  112. end;
  113. end;
  114. end.