-
Notifications
You must be signed in to change notification settings - Fork 5
/
EncodingHelper.pas
62 lines (44 loc) · 1.58 KB
/
EncodingHelper.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
unit EncodingHelper;
interface
uses
System.SysUtils;
type
TEncodingHelper = class helper for TEncoding
public
function GetCharCount(const Bytes: PByte; const ByteIndex, ByteCount: integer): integer; overload;
function GetChars(const Bytes: PByte; const ByteIndex, ByteCount: integer): TCharArray; overload;
function GetString(const Bytes: PByte; const ByteIndex, ByteCount: integer): string; overload;
end;
implementation
uses
System.SysConst;
{$POINTERMATH ON}
type
EncodingAccess = class(TEncoding)
end;
{ TEncodingHelper }
function TEncodingHelper.GetCharCount(const Bytes: PByte; const ByteIndex, ByteCount: integer): integer;
begin
result := EncodingAccess(Self).GetCharCount(Bytes + ByteIndex, ByteCount);
end;
function TEncodingHelper.GetChars(const Bytes: PByte; const ByteIndex, ByteCount: integer): TCharArray;
var
Len: integer;
begin
Len := EncodingAccess(Self).GetCharCount(Bytes + ByteIndex, ByteCount);
if (ByteCount > 0) and (Len = 0) then
raise EEncodingError.CreateRes(@SNoMappingForUnicodeCharacter);
SetLength(Result, Len);
EncodingAccess(Self).GetChars(Bytes + ByteIndex, ByteCount, PChar(Result), Len);
end;
function TEncodingHelper.GetString(const Bytes: PByte; const ByteIndex, ByteCount: integer): string;
var
Len: integer;
begin
Len := EncodingAccess(Self).GetCharCount(Bytes + ByteIndex, ByteCount);
if (ByteCount > 0) and (Len = 0) then
raise EEncodingError.CreateRes(@SNoMappingForUnicodeCharacter);
SetLength(Result, Len);
EncodingAccess(Self).GetChars(Bytes + ByteIndex, ByteCount, PChar(Result), Len);
end;
end.