-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstringutils.pas
225 lines (199 loc) · 5.05 KB
/
stringutils.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
unit StringUtils;
{$mode objfpc}{$h+}
interface
uses
Ansi,
Base64, Classes, StrUtils, SysUtils;
type
TTokens = Array of String;
function Base64Decode(ABase64: String): String;
function PipeToAnsi(AText: String): String;
function SecToDHMS(ASec: LongInt): String;
function SecToHM(ASec: LongInt): String;
function SecToHMS(ASec: LongInt): String;
function SecToMS(ASec: LongInt): String;
function SethStrip(AText: String): String;
function StrToTok(AText: String; ADelim: Char): TTokens;
function TokToStr(ATokens: TTokens; ADelim: Char): String;
function TokToStr(ATokens: TTokens; ADelim: Char; AStartIndex: Integer): String;
implementation
{ From: https://sites.google.com/a/gorilla3d.com/fpc-docs/built-in-units/base64/tbase64decodingstream }
function Base64Decode(ABase64: String): String;
var
DecodedStream: TStringStream;
EncodedStream: TStringStream;
Decoder: TBase64DecodingStream;
begin
EncodedStream := TStringStream.Create(ABase64);
DecodedStream := TStringStream.Create('');
Decoder := TBase64DecodingStream.Create(EncodedStream);
DecodedStream.CopyFrom(Decoder, Decoder.Size);
Result := DecodedStream.DataString;
DecodedStream.Free;
EncodedStream.Free;
Decoder.Free;
end;
{
Convert PIPE codes to ANSI escape sequences
Pipe codes are in format |BF
Where B is background value and F is foreground, in base 16 (hex)
So range is |00 .. |7F
}
function PipeToAnsi(AText: String): String;
var
Attr: Integer;
I: Integer;
S: String;
begin
if (Length(AText) >= 3) then
begin
I := 0;
S := '';
while (I < Length(AText)) do
begin
Inc(I);
if (AText[I] = '|') and (Length(AText) - I >= 2) then
begin
Attr := StrToIntDef('$' + Copy(AText, I + 1, 2), -1);
if (Attr <> -1) then
begin
Inc(I, 2);
if (Attr > 127) then
Dec(Attr, 128);
S := S + AnsiTextAttr(Attr);
end else
S := S + '|';
end else
S := S + AText[I];
end;
end else
S := AText;
Result := S;
end;
{
Returns a number of seconds formatted as:
1d 1h 1m 1s
0 values are not returned, so 3601 becomes
1h 1s
}
function SecToDHMS(ASec: LongInt): String;
var
D, H, M, S: Integer;
begin
D := ASec div 86400;
ASec := ASec mod 86400;
H := ASec div 3600;
ASec := ASec mod 3600;
M := ASec div 60;
S := ASec mod 60;
Result := IntToStr(D) + 'd ' + IntToStr(H) + 'h ' + IntToStr(M) + 'm ' + IntToStr(S) + 's';
end;
{
Returns a number of seconds formatted as:
HH:MM
}
function SecToHM(ASec: LongInt): String;
var
H, M: Integer;
begin
H := ASec div 3600;
ASec := ASec mod 3600;
M := ASec div 60;
Result := AddChar('0', IntToStr(H), 2) + ':' + AddChar('0', IntToStr(M), 2);
end;
{
Returns a number of seconds formatted as:
HH:MM:SS
}
function SecToHMS(ASec: LongInt): String;
var
H, M, S: Integer;
begin
H := ASec div 3600;
ASec := ASec mod 3600;
M := ASec div 60;
S := ASec mod 60;
Result := AddChar('0', IntToStr(H), 2) + ':' + AddChar('0', IntToStr(M), 2) + ':' + AddChar('0', IntToStr(S), 2);
end;
{
Returns a number of seconds formatted as:
MM:SS
}
function SecToMS(ASec: LongInt): String;
var
M, S: Integer;
begin
M := ASec div 60;
S := ASec mod 60;
Result := AddChar('0', IntToStr(M), 2) + ':' + AddChar('0', IntToStr(S), 2);
end;
function SethStrip(AText: String): String;
var
I: Integer;
S: String;
begin
I := 1;
S := '';
while (I <= Length(AText)) do
begin
if (AText[I] = '`') then
begin
// It's a "seth" code, so we need to do something about that
if (I = Length(AText)) then
begin
// End of string, nothing to do
I += 1;
end else
begin
// A least one character after the `
if (AText[I + 1] = 'r') then
begin
// It's `r#, which requires three characters to be deleted
I += 3;
end else
begin
// Any other combo just requires two characters to be deleted
I += 2;
end;
end;
end else
begin
S += AText[I];
I += 1;
end;
end;
Result := S;
end;
function StrToTok(AText: String; ADelim: Char): TTokens;
var
I: Integer;
TokenCount: Integer;
begin
TokenCount := Length(AText) - Length(StringReplace(AText, ADelim, '', [rfReplaceAll])) + 1;
SetLength(Result, TokenCount + 1);
for I := 1 to TokenCount do
begin
Result[I] := ExtractDelimited(I, AText, [ADelim]);
end;
end;
function TokToStr(ATokens: TTokens; ADelim: Char): String;
begin
Result := TokToStr(ATokens, ADelim, 1);
end;
function TokToStr(ATokens: TTokens; ADelim: Char; AStartIndex: Integer): String;
var
I: Integer;
S: String;
begin
S := '';
if (AStartIndex <= High(ATokens)) then
begin
S := ATokens[AStartIndex];
for I := AStartIndex + 1 to High(ATokens) do
begin
S := S + ADelim + ATokens[I];
end;
end;
Result := S;
end;
end.