-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathShortGUID.pas
107 lines (95 loc) · 2.69 KB
/
ShortGUID.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
unit ShortGUID;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils, NetEncoding;
Type
TShortGUID = Class
// A wrapper for dealing with base64 encoded GUIDs
// Based on: https://github.com/csharpvitamins/CSharpVitamins.ShortGuid
private
Class Var
Base64: TBase64Encoding;
public
Class Constructor Create;
Class Function GUIDToStr(const GUID: TGUID): String;
Class Function GUIDToUrl(const GUID: TGUID): String;
Class Function StrToGUID(const GUID: String): TGUID;
Class Function UrlToGUID(url: String): TGUID;
Class Destructor Destroy;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Class Constructor TShortGUID.Create;
begin
Base64 := TBase64Encoding.Create(0);
end;
Class Function TShortGUID.GUIDToStr(const GUID: TGUID): String;
begin
Result := Copy(Base64.EncodeBytesToString(GUID.ToByteArray),1,22);
end;
Class Function TShortGUID.GUIDToUrl(const GUID: TGUID): String;
begin
// Convert GUID to string
Result := GUIDToStr(GUID);
// Replace / with _
var SlashPos := Pos('/',Result);
while SlashPos > 0 do
begin
Result[SlashPos] := '_';
SlashPos := Pos('/',Result);
end;
// Replace + with -
var PlusPos := Pos('+',Result);
while PlusPos > 0 do
begin
Result[PlusPos] := '-';
PlusPos := Pos('+',Result);
end;
end;
Class Function TShortGUID.StrToGUID(const GUID: String): TGUID;
begin
case Length(GUID) of
// Short GUID...
22: Result := TGUID.Create(Base64.DecodeStringToBytes(GUID+'=='));
// Regular GUID...
38: result := TGUID.Create(GUID);
else raise Exception.Create('Invalid GUID');
end;
end;
Class Function TShortGUID.UrlToGUID(url: String): TGUID;
begin
if Length(url) = 22 then
begin
// Replace _ with /
var UnderscorePos := Pos('_',url);
while UnderscorePos > 0 do
begin
url[UnderscorePos] := '/';
UnderscorePos := Pos('_',url);
end;
// Replace - with +
var MinusPos := Pos('-',url);
while MinusPos > 0 do
begin
url[MinusPos] := '+';
MinusPos := Pos('-',url);
end;
// Convert url to GUID
Result := StrToGUID(url);
end else
raise Exception.Create('Invalid url');
end;
Class Destructor TShortGUID.Destroy;
begin
Base64.Free;
end;
end.