Added TMVCSqidsEncoder, IMVCSqidsEncoder and relative sample (sqids_showcase.dproj)

This commit is contained in:
Daniele Teti 2024-05-23 10:04:31 +02:00
parent a559b34f6a
commit 43478dbdbf
5 changed files with 1464 additions and 1 deletions

View File

@ -0,0 +1,160 @@
object MainForm: TMainForm
Left = 0
Top = 0
Caption = 'Sqids Showcase Sample'
ClientHeight = 441
ClientWidth = 545
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'Segoe UI'
Font.Style = []
OnCreate = FormCreate
TextHeight = 21
object Label1: TLabel
Left = 8
Top = 13
Width = 62
Height = 21
Caption = 'Alphabet'
end
object Label2: TLabel
Left = 8
Top = 77
Width = 79
Height = 21
Caption = 'Min Lenght'
end
object lblMinSize: TLabel
Left = 463
Top = 104
Width = 74
Height = 32
Alignment = taCenter
AutoSize = False
Caption = '0'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -24
Font.Name = 'Segoe UI'
Font.Style = []
ParentFont = False
Layout = tlCenter
end
object EditAlphabet: TEdit
Left = 8
Top = 40
Width = 449
Height = 29
TabOrder = 0
end
object TrackBarMinLength: TTrackBar
Left = 8
Top = 104
Width = 449
Height = 45
TabOrder = 1
OnChange = TrackBarMinLengthChange
end
object btnShuffle: TButton
Left = 463
Top = 40
Width = 75
Height = 29
Caption = 'Shuffle'
TabOrder = 2
end
object PageControl1: TPageControl
Left = 8
Top = 168
Width = 530
Height = 265
ActivePage = TabSheet1
TabOrder = 3
object TabSheet1: TTabSheet
Caption = 'Encode'
object Label3: TLabel
Left = 16
Top = 13
Width = 345
Height = 21
Caption = 'Number or numbers (comma separated e.g. 1,2,3)'
end
object Label4: TLabel
Left = 16
Top = 125
Width = 101
Height = 21
Caption = 'Encoded Sqids'
end
object EditNumbers: TEdit
Left = 16
Top = 40
Width = 481
Height = 29
TabOrder = 0
Text = '1,2,3'
end
object btnEncode: TButton
Left = 16
Top = 82
Width = 180
Height = 30
Caption = 'Encode to Sqids'
TabOrder = 1
OnClick = btnEncodeClick
end
object EditSqidsOutput: TEdit
Left = 16
Top = 152
Width = 481
Height = 29
ReadOnly = True
TabOrder = 2
end
end
object TabSheet2: TTabSheet
Caption = 'Decode'
ImageIndex = 1
object Label5: TLabel
Left = 16
Top = 134
Width = 205
Height = 21
Caption = 'Decoded number or numbers'
end
object Label6: TLabel
Left = 16
Top = 13
Width = 101
Height = 21
Caption = 'Encoded Sqids'
end
object EditIntegersOutput: TEdit
Left = 16
Top = 161
Width = 481
Height = 29
ReadOnly = True
TabOrder = 0
end
object btnDecode: TButton
Left = 16
Top = 82
Width = 180
Height = 30
Caption = 'Decode to Integers'
TabOrder = 1
OnClick = btnDecodeClick
end
object EditSqidsInput: TEdit
Left = 16
Top = 40
Width = 481
Height = 29
TabOrder = 2
end
end
end
end

View File

@ -0,0 +1,89 @@
unit MainFormU;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, MVCFramework.Commons,
Vcl.ComCtrls;
type
TMainForm = class(TForm)
EditAlphabet: TEdit;
Label1: TLabel;
TrackBarMinLength: TTrackBar;
Label2: TLabel;
btnShuffle: TButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label3: TLabel;
EditNumbers: TEdit;
btnEncode: TButton;
EditSqidsOutput: TEdit;
Label4: TLabel;
Label5: TLabel;
EditIntegersOutput: TEdit;
btnDecode: TButton;
Label6: TLabel;
EditSqidsInput: TEdit;
lblMinSize: TLabel;
procedure FormCreate(Sender: TObject);
procedure btnEncodeClick(Sender: TObject);
procedure TrackBarMinLengthChange(Sender: TObject);
procedure btnDecodeClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.btnDecodeClick(Sender: TObject);
begin
var lSqids: IMVCSqidsEncoder := TMVCSqidsEncoder.Create(
EditAlphabet.Text, TrackBarMinLength.Position);
var lIntegers := lSqids.Decode(EditSqidsInput.Text);
var lOutput := '';
for var I in lIntegers do
begin
lOutput := lOutput + ',' + I.ToString;
end;
EditIntegersOutput.Text := lOutput.Substring(1);
end;
procedure TMainForm.btnEncodeClick(Sender: TObject);
begin
var lSqids: IMVCSqidsEncoder := TMVCSqidsEncoder.Create(
EditAlphabet.Text, TrackBarMinLength.Position);
var lPieces := String(EditNumbers.Text).Split([',']);
var lIntegers: TArray<UInt64> := [];
for var lPiece in lPieces do
begin
lIntegers := lIntegers + [StrToInt(lPiece)];
end;
EditSqidsOutput.Text := lSqids.Encode(lIntegers);
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
PageControl1.ActivePageIndex := 0;
EditAlphabet.Text := TMVCSqids.DEFAULT_ALPHABET;
TrackBarMinLength.Position := 5;
TrackBarMinLengthChange(TrackBarMinLength);
end;
procedure TMainForm.TrackBarMinLengthChange(Sender: TObject);
begin
lblMinSize.Caption := TrackBarMinLength.Position.ToString;
end;
end.

View File

@ -0,0 +1,14 @@
program sqids_showcase;
uses
Vcl.Forms,
MainFormU in 'MainFormU.pas' {MainForm};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.

File diff suppressed because it is too large Load Diff

View File

@ -777,16 +777,43 @@ type
TMVCSqids = class sealed
private
class var fInstance: TSqids;
class function GetInstance: TSqids;
public
const DEFAULT_ALPHABET = sqids.DEFAULT_ALPHABET;
const DEFAULT_MIN_LENGTH = sqids.DEFAULT_MIN_LENGTH;
const MIN_ALPHABET_LENGTH = sqids.MIN_ALPHABET_LENGTH;
const MAX_ALPHABET_LENGTH = sqids.MAX_ALPHABET_LENGTH;
class var SQIDS_ALPHABET: String;
class var SQIDS_MIN_LENGTH: Integer;
class destructor Destroy;
class function GetInstance: TSqids;
{ sqids }
class function SqidToInt(const Sqid: String): UInt64;
class function IntToSqid(const Value: UInt64): String;
end;
IMVCSqidsEncoder = interface
['{DFD71A1A-36B0-4EC6-8E8E-35405BB93CA7}']
function Encode(aNumbers: TArray<UInt64>): string;
function EncodeSingle(aNumber: UInt64): string;
function Decode(aId: string): TArray<UInt64>;
function DecodeSingle(aId: string): UInt64;
end;
TMVCSqidsEncoder = class(TInterfacedObject, IMVCSqidsEncoder)
protected
fSqids: TSqids;
public
constructor Create(AAlphabet: string; AMinLength: Byte; ABlockList: TArray<string>); overload;
constructor Create(AAlphabet: string = DEFAULT_ALPHABET; AMinLength: Byte = DEFAULT_MIN_LENGTH); overload;
constructor Create(AMinLength: Byte); overload;
constructor Create(ABlockList: TArray<string>); overload;
function Encode(aNumbers: TArray<UInt64>): string;
function EncodeSingle(aNumber: UInt64): string;
function Decode(aId: string): TArray<UInt64>;
function DecodeSingle(aId: string): UInt64;
end;
function dotEnv: IMVCDotEnv; overload;
procedure dotEnvConfigure(const dotEnvDelegate: TFunc<IMVCDotEnv>);
@ -1872,6 +1899,50 @@ begin
Result := GdotEnv;
end;
{ TMVCSqidsEncoder }
constructor TMVCSqidsEncoder.Create(AAlphabet: string; AMinLength: Byte;
ABlockList: TArray<string>);
begin
inherited Create;
fSqids := TSqids.Create(AAlphabet,AMinLength, ABlockList);
end;
constructor TMVCSqidsEncoder.Create(AAlphabet: string; AMinLength: Byte);
begin
Create(AAlphabet, AMinLength, DEFAULT_BLOCKLIST);
end;
constructor TMVCSqidsEncoder.Create(AMinLength: Byte);
begin
Create(DEFAULT_ALPHABET, AMinLength, DEFAULT_BLOCKLIST);
end;
constructor TMVCSqidsEncoder.Create(ABlockList: TArray<string>);
begin
Create(DEFAULT_ALPHABET, DEFAULT_MIN_LENGTH, ABlockList);
end;
function TMVCSqidsEncoder.Decode(AId: string): TArray<UInt64>;
begin
Result := fSqids.Decode(AId);
end;
function TMVCSqidsEncoder.DecodeSingle(AId: string): UInt64;
begin
Result := fSqids.DecodeSingle(AId);
end;
function TMVCSqidsEncoder.Encode(ANumbers: TArray<UInt64>): string;
begin
Result := fSqids.Encode(ANumbers);
end;
function TMVCSqidsEncoder.EncodeSingle(ANumber: UInt64): string;
begin
Result := fSqids.EncodeSingle(ANumber);
end;
initialization
gLock := TObject.Create;