UnsignedFunc.pas 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. unit UnsignedFunc;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Provides unsigned alternatives to Delphi functions that use signed types for parameters or return
  8. values where unsigned types would have been more appropriate
  9. }
  10. interface
  11. { FRAMEWORK_VCL is available as of Delphi 11.1, so define it manually here when we need it, to
  12. support Delphi 10.4 }
  13. {$IF Defined(COMPIL32PROJ) or Defined(SETUPPROJ)}
  14. {$DEFINE FRAMEWORK_VCL}
  15. {$IFEND}
  16. uses
  17. {$IFDEF FRAMEWORK_VCL} Windows, UITypes, Controls, Graphics, {$ENDIF} SysUtils;
  18. function ULength(const S: String): Cardinal; overload; inline;
  19. function ULength(const S: RawByteString): Cardinal; overload; inline;
  20. function ULength(const S: WideString): Cardinal; overload; inline;
  21. function ULength(const S: TBytes): Cardinal; overload; inline;
  22. procedure UMove(const Source; var Dest; Count: NativeUInt);
  23. procedure UFillChar(var Dest; Count: NativeUInt; const Value: Integer);
  24. {$IFDEF FRAMEWORK_VCL}
  25. function UColorToRGB(Color: TColor): TColorRef;
  26. function UDrawTextBiDiModeFlags(const Control: TControl; const Flags: UINT): UINT;
  27. {$ENDIF}
  28. implementation
  29. function ULength(const S: String): Cardinal;
  30. begin
  31. Result := Cardinal(Length(S));
  32. end;
  33. function ULength(const S: RawByteString): Cardinal;
  34. begin
  35. Result := Cardinal(Length(S));
  36. end;
  37. function ULength(const S: WideString): Cardinal;
  38. begin
  39. Result := Cardinal(Length(S));
  40. end;
  41. function ULength(const S: TBytes): Cardinal;
  42. begin
  43. Result := Cardinal(Length(S));
  44. end;
  45. procedure UMove(const Source; var Dest; Count: NativeUInt);
  46. begin
  47. var SourceBuf: PByte := @Source;
  48. var DestBuf: PByte := @Dest;
  49. while Count > 0 do begin
  50. var SignedCount := High(NativeInt);
  51. if Count < NativeUInt(SignedCount) then
  52. SignedCount := NativeInt(Count);
  53. Move(SourceBuf^, DestBuf^, SignedCount);
  54. Dec(Count, SignedCount);
  55. Inc(SourceBuf, SignedCount);
  56. Inc(DestBuf, SignedCount);
  57. end;
  58. end;
  59. procedure UFillChar(var Dest; Count: NativeUInt; const Value: Integer);
  60. begin
  61. var DestBuf: PByte := @Dest;
  62. while Count > 0 do begin
  63. var SignedCount := High(NativeInt);
  64. if Count < NativeUInt(SignedCount) then
  65. SignedCount := NativeInt(Count);
  66. FillChar(DestBuf^, SignedCount, Value);
  67. Dec(Count, SignedCount);
  68. Inc(DestBuf, SignedCount);
  69. end;
  70. end;
  71. {$IFDEF FRAMEWORK_VCL}
  72. function UColorToRGB(Color: TColor): TColorRef;
  73. begin
  74. Result := TColorRef(ColorToRGB(Color));
  75. end;
  76. function UDrawTextBiDiModeFlags(const Control: TControl; const Flags: UINT): UINT;
  77. begin
  78. Result := UINT(Control.DrawTextBiDiModeFlags(Integer(Flags)));
  79. end;
  80. {$ENDIF}
  81. end.