utbytesof.pp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. unit utbytesof;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. SysUtils, Classes;
  6. Implementation
  7. uses punit,utrtl;
  8. function CheckBytes(const B: TBytes): Boolean;
  9. const
  10. Etalon: array[0..3] of Byte = (84, 101, 115, 116);
  11. var
  12. I: Integer;
  13. begin
  14. Result := Length(B) <= Length(Etalon);
  15. if Result then
  16. for I := Low(B) to High(B) do
  17. Result := Result and (B[I] = Etalon[I]);
  18. end;
  19. function CheckWideBytes(const B: TBytes): Boolean;
  20. const
  21. Etalon: array[0..7] of Byte = (
  22. {$ifdef FPC_BIG_ENDIAN}
  23. 00, 84, 00, 101, 00, 115, 00, 116
  24. {$else}
  25. 84, 00, 101, 00, 115, 00, 116, 00
  26. {$endif}
  27. );
  28. var
  29. I: Integer;
  30. begin
  31. Result := Length(B) <= Length(Etalon);
  32. if Result then
  33. for I := Low(B) to High(B) do
  34. Result := Result and (B[I] = Etalon[I]);
  35. end;
  36. Function CheckBytesOf : AnsiString;
  37. var
  38. S: AnsiString;
  39. U: UnicodeString;
  40. B: TBytes;
  41. begin
  42. Result:='';
  43. S := 'Test';
  44. U := S;
  45. B := BytesOf(S);
  46. if not CheckBytes(B) then
  47. Exit('Error at 1');
  48. if StringOf(B) <> U then
  49. Exit('Error at 2');
  50. B := BytesOf(S[1]);
  51. if not CheckBytes(B) then
  52. Exit('Error at 3');
  53. B := BytesOf(U);
  54. if not CheckBytes(B) then
  55. Exit('Error at 4');
  56. B := BytesOf(U[1]);
  57. if not CheckBytes(B) then
  58. Exit('Error at 5');
  59. B := WideBytesOf(U);
  60. if not CheckWideBytes(B) then
  61. Exit('Error at 6');
  62. if WideStringOf(B) <> U then
  63. Exit('Error at 7');
  64. end;
  65. begin
  66. SysUtilsTest('BytesOf',@CheckBytesOf);
  67. end.