-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunitcommon.pas
409 lines (355 loc) · 10.3 KB
/
unitcommon.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
unit UnitCommon;
// Copyright 2022-2025 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
SysUtils;
type
TProcedureOfObject = procedure of object;
TProcedureOfObjectString = procedure(var S: String) of object;
TGlobalCounter = class sealed (TObject)
public
class function NextValue(): Int64; static;
end;
generic TNumArraySorter<T> = class(TObject)
public
type
TSortCompareProc = function(X, Y: T): Integer;
strict private
FSortCompareProc: TSortCompareProc;
public
constructor Create(ASortCompareProc: TSortCompareProc); virtual;
procedure SortArray(var A: Array of T);
end;
{ TCommonFunctions }
TCommonFunctions = class sealed (TObject)
public
const
NonBreakSpace: RawByteString = #$c2 + #$a0;
NarrowNonBreakSpace: RawByteString = #$e2 + #$80 + #$af;
public
class function GlobalClassNameGenerator(C: TClass): String; static;
class function GlobalObjectNameGenerator(Obj: TObject): String; static;
class function SpectrumCharToUtf8(const Is128K: Boolean; const S: RawByteString): RawByteString; static;
class procedure ConvertCodePageFromCp1252ToUtf8(var S: AnsiString); static;
class procedure CallRandomizeIfNotCalledAlready(); static;
class function AdjLineBreaks(const S: RawByteString; Style: TTextLineBreakStyle
): RawByteString; static;
strict private
class function CheckStartForDecimal(const S: String): Boolean; static;
public
class function TryStrToIntDecimal(const S: String; out N: Int32): Boolean; static;
class function TryStrToInt64Decimal(const S: String; out N: Int64): Boolean; static;
end;
implementation
class function TCommonFunctions.GlobalClassNameGenerator(C: TClass): String;
begin
if C = nil then
Result := 'z'
else begin
Result := C.ClassName;
Result := StringReplace(Trim(Result), '.', '_', [rfReplaceAll]);
end;
Result := Result + TGlobalCounter.NextValue().ToHexString(3);
end;
class function TCommonFunctions.GlobalObjectNameGenerator(Obj: TObject): String;
begin
if Assigned(Obj) then
Result := GlobalClassNameGenerator(Obj.ClassType)
else
Result := GlobalClassNameGenerator(TClass(nil));
end;
class function TCommonFunctions.SpectrumCharToUtf8(const Is128K: Boolean;
const S: RawByteString): RawByteString;
const
UdgDelta = $90 - Ord('A'); // used for UDG area ($90-$A4)
BlockCodes: array [0..15] of RawByteString = (
' ',
#$e2#$96#$9d, // quadrant upper right
#$e2#$96#$98, // quadrant upper left
#$e2#$96#$80, // upper half block
#$e2#$96#$97, // quadrant lower right
#$e2#$96#$90, // right half block
#$e2#$96#$9a, // quadrant upper left and lower right
#$e2#$96#$9c, // quadrant upper left and upper right and lower right
#$e2#$96#$96, // quadrant lower left
#$e2#$96#$9e, // quadrant upper right and lower left
#$e2#$96#$8c, // left half block
#$e2#$96#$9b, // quadrant upper left and upper right and lower left
#$e2#$96#$84, // lower half block
#$e2#$96#$9f, // quadrant upper right and lower left and lower right
#$e2#$96#$99, // quadrant upper left and lower left and lower right
#$e2#$96#$88 // full block
);
BasicConstants: array [0..92] of RawByteString = (
'SPECTRUM', 'PLAY', // these two are additional keywords on 128k models
'RND', 'INKEY$', 'PI', 'FN', 'POINT', 'SCREEN$', 'ATTR', 'AT', 'TAB', 'VAL$',
'CODE', 'VAL', 'LEN', 'SIN', 'COS', 'TAN', 'ASN', 'ACS', 'ATN', 'LN', 'EXP',
'INT', 'SQR', 'SGN', 'ABS', 'PEEK', 'IN', 'USR', 'STR$', 'CHR$', 'NOT', 'BIN',
'OR', 'AND', '<=', '>=', '<>', 'LINE', 'THEN', 'TO', 'STEP', 'DEF FN', 'CAT',
'FORMAT', 'MOVE', 'ERASE', 'OPEN #', 'CLOSE #', 'MERGE', 'VERIFY', 'BEEP',
'CIRCLE', 'INK', 'PAPER', 'FLASH', 'BRIGHT', 'INVERSE', 'OVER', 'OUT', 'LPRINT',
'LLIST', 'STOP', 'READ', 'DATA', 'RESTORE', 'NEW', 'BORDER', 'CONTINUE', 'DIM',
'REM', 'FOR', 'GO TO', 'GO SUB', 'INPUT', 'LOAD', 'LIST', 'LET', 'PAUSE',
'NEXT', 'POKE', 'PRINT', 'PLOT', 'RUN', 'SAVE', 'RANDOMIZE', 'IF', 'CLS',
'DRAW', 'CLEAR', 'RETURN', 'COPY'
);
var
I: Integer;
L: Integer;
J: Integer;
K: Integer;
KJ: Integer;
CharLengths: array of Integer;
S1: RawByteString;
begin
Result := '';
L := Length(S);
SetLength(CharLengths{%H-}, L);
I := 0;
J := -1;
while I < L do begin
Inc(I);
if S[I] = #08 then begin // backspace, delete the previous character
if J >= 0 then begin
SetLength(Result, Length(Result) - CharLengths[J]);
Dec(J);
end;
end else begin
KJ := 0;
case S[I] of
#$0..#$05, #$07, #$0a..#$0c, #$0e, #$0f, #$18..#$1f:
S1 := '?';
#$06:
S1 := ' ';
#$09:
S1 := '';
#$10..#$15:
begin
S1 := '';
KJ := 1;
Inc(I);
end;
#$16, #$17:
begin
S1 := ' ';
KJ := 2;
I := I + KJ;
end;
#$5e:
S1 := #$e2 + #$86 + #$91; // upwards arrow symbol
#$60:
S1 := #$c2 + #$a3; // pound sign
#$7f:
S1 := #$c2 + #$a9; // copyright sign
#$80..#$8F: // block characters
S1 := BlockCodes[Ord(S[I]) - $80];
#$90..#$ff:
if (S[I] <= #$a2) or ((not Is128K) and (S[I] <= #$a4)) then begin
// UDG - user defined graphics - 21 characters, by default same as letters A-U
// On 128K models the last two bytes in UDG area are mapped to
// additional two keywords, leaving 19 characters for UDG.
// The best we can do is use these keywords if model is 128K when inserting tape
S1 := AnsiChar(Ord(S[I]) - UdgDelta);
end else begin
S1 := BasicConstants[Ord(S[I]) - $a3];
if (Length(Result) > 0) and (not (Result[Length(Result)] in [' ', #13])) then begin
K := J;
while K >= 0 do begin
if CharLengths[K] > 0 then begin
S1 := ' ' + S1;
Break;
end;
Dec(K);
end;
end;
if (I < Length(S)) and (not (S[I + 1] in [' ', #13])) then
S1 := S1 + ' ';
end;
otherwise
S1 := S[I];
end;
Result := Result + S1;
if I < L then
repeat
Inc(J);
if J >= Length(CharLengths) then
SetLength(CharLengths, J * 7 div 5 + 2);
CharLengths[J] := Length(S1);
S1 := '';
Dec(KJ);
until KJ < 0;
end;
end;
SetLength(CharLengths, 0);
end;
class procedure TCommonFunctions.ConvertCodePageFromCp1252ToUtf8(var S: AnsiString);
begin
// https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
SetCodePage(RawByteString(S), 1252, False); // set to cp1252
SetCodePage(RawByteString(S), CP_UTF8, True); // convert
end;
class procedure TCommonFunctions.CallRandomizeIfNotCalledAlready();
{$push}
{$J+}
const
RandomizeAlreadyCalled: Boolean = False;
{$pop}
begin
if not RandomizeAlreadyCalled then begin
RandomizeAlreadyCalled := True;
Randomize;
end;
end;
class function TCommonFunctions.CheckStartForDecimal(const S: String): Boolean;
var
P, Pe: PAnsiChar;
begin
P := PAnsiChar(S);
Pe := P + Length(S);
while (P < Pe) and (P^ <= AnsiChar($20)) do
Inc(P);
if P^ = '-' then
Inc(P);
case P^ of
'0':
if not ((P + 1)^ in [#0, '0'..'9']) then
Exit(False);
'1'..'9':
;
otherwise
Exit(False);
end;
Result := True;
end;
class function TCommonFunctions.TryStrToIntDecimal(const S: String; out N: Int32
): Boolean;
begin
Result := CheckStartForDecimal(S) and TryStrToInt(S, N);
end;
class function TCommonFunctions.TryStrToInt64Decimal(const S: String; out
N: Int64): Boolean;
begin
Result := CheckStartForDecimal(S) and TryStrToInt64(S, N);
end;
class function TCommonFunctions.AdjLineBreaks(const S: RawByteString;
Style: TTextLineBreakStyle): RawByteString;
var
L: SizeInt;
L2: SizeInt;
J: SizeInt;
I: SizeInt;
C: AnsiChar;
begin
L := Length(S);
if L = 0 then
Exit(S);
if Style = TTextLineBreakStyle.tlbsLF then
C := #10
else
C := #13;
J := 0;
I := 0;
if Style = TTextLineBreakStyle.tlbsCRLF then begin
L2 := L * 6 div 5;
SetLength(Result, L2);
while I < L do begin
Inc(I);
Inc(J);
if J >= L2 then begin
L2 := L2 + ((Int64(L) - I) * J * 9) div (Int64(I) * 8) + 4;
SetLength(Result, L2);
end;
case S[I] of
#13:
begin
Result[J] := #13;
inc(J);
Result[J] := #10;
if (I < L) and (S[I + 1] = #10) then
Inc(I);
end;
#10:
Result[J] := C;
otherwise
Result[J] := S[I];
end;
end;
end else begin
SetLength(Result, L);
while I < L do begin
Inc(I);
Inc(J);
case S[I] of
#13:
begin
Result[J] := C;
if (I < L) and (S[I + 1] = #10) then
Inc(I);
end;
#10:
Result[J] := C;
otherwise
Result[J] := S[I];
end;
end;
end;
SetLength(Result, J);
end;
{ TGlobalCounter }
class function TGlobalCounter.NextValue: Int64;
{$push}
{$J+}
const
GlobalCounter: Int64 = 0;
{$pop}
begin
Result := GlobalCounter;
Inc(GlobalCounter);
end;
{ TNumArraySorter }
constructor TNumArraySorter.Create(ASortCompareProc: TSortCompareProc);
begin
inherited Create;
FSortCompareProc := ASortCompareProc;
end;
procedure TNumArraySorter.SortArray(var A: array of T);
type
P_T = ^T;
var
Pivot, N: T;
procedure InternalQSort(PFirst, PLast: P_T);
var
PLeft, PRight: P_T;
begin
PLeft := PFirst;
PRight := PLast;
Pivot := (PLeft + (PRight - PLeft) div 2)^;
repeat
while FSortCompareProc(PLeft^, Pivot) < 0 do
Inc(PLeft);
while FSortCompareProc(Pivot, PRight^) < 0 do
Dec(PRight);
if PLeft <= PRight then begin
N := PLeft^;
PLeft^ := PRight^;
PRight^ := N;
Inc(PLeft);
Dec(PRight);
end;
until PLeft > PRight;
if PRight > PFirst then
InternalQSort(PFirst, PRight);
if PLeft < PLast then
InternalQSort(PLeft, PLast);
end;
var
L: SizeInt;
begin
L := Length(A) - 1;
if L > 0 then
InternalQSort(@A[0], @A[L]);
end;
end.