Преглед на файлове

Libraries: added buffer comparison function

Herman Schoenfeld преди 7 години
родител
ревизия
98762bece8
променени са 1 файла, в които са добавени 24 реда и са изтрити 0 реда
  1. 24 0
      src/libraries/sphere10/UCommon.pas

+ 24 - 0
src/libraries/sphere10/UCommon.pas

@@ -43,6 +43,7 @@ function Buffer2Hex(const ABuffer: TBytes) : AnsiString;
 
 { Binary-safe StrComp replacement. StrComp will return 0 for when str1 and str2 both start with NUL character. }
 function BinStrComp(const Str1, Str2 : AnsiString): Integer;
+function BufferCompare(const ABytes1, ABytes2: TBytes): integer;
 
 { Ternary operator equivalent of predicate ? (true-val) : (false-value) }
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Cardinal): Cardinal; overload;
@@ -396,6 +397,29 @@ begin
    end;
 End;
 
+function BufferCompare(const ABytes1, ABytes2: TBytes): integer;
+var ABytes1Len, ABytes2Len, i : Integer;
+begin
+   ABytes1Len := Length(ABytes1);
+   ABytes2Len := Length(ABytes2);
+   if (ABytes1Len < ABytes2Len) then
+     Result := -1
+   else if (ABytes1Len > ABytes2Len) then
+     Result := 1
+   else begin
+     Result := 0;
+     for i:= Low(ABytes1) to High(ABytes1) do begin
+       if ABytes1[i] < ABytes2[i] then begin
+         Result := -1;
+         break;
+       end else if ABytes1[i] > ABytes2[i] then begin
+         Result := 1;
+         break;
+       end
+     end;
+   end;
+end;
+
 {%endregion}
 
 {$IFDEF FPC}