summaryrefslogtreecommitdiff
path: root/lowlevelunit.pas
blob: 2fb1710859105eaf6d10da07e807090781cbfcb0 (plain)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
unit lowlevelunit;

{$mode objfpc}{$H+}

interface

uses
  math, Classes, SysUtils, RegExpr, process, FPimage;

{$IFDEF CPU64}
type
  longestOrdinal = int64;
{$ELSE}
type
  longestOrdinal = longint;
{$ENDIF}

type
  generic tArray<t> = array of t;
  tInt64Array = specialize tArray<int64>;
  tLongintArray = specialize tArray<longint>;
  pTLongintArray = ^tLongintArray;
  tLongintArrayArray = specialize tArray<tLongintArray>;
  pTLongintArrayArray = ^tLongintArrayArray;
  tSingleArray = specialize tArray<single>;
  pTSingleArray = ^tSingleArray;
  tInt64Point = array['x'..'y'] of int64;
  tIntPoint = array['x'..'y'] of longint;
  tExtPoint = array['x'..'y'] of extended;
  tExt3dPoint = array['x'..'z'] of extended;
  t2x2Longint = array['x'..'y','x'..'y'] of longint;
  t2x2Extended = array['x'..'y','x'..'y'] of extended;
  t2x2Int64 = array['x'..'y','x'..'y'] of int64;
  tGerade = record
    a,r: tExtPoint;
  end;
  tExtPointArray = specialize tArray<tExtPoint>;
  tExt3dPointArray = specialize tArray<tExt3dPoint>;
  pTExtPointArray = ^tExtPointArray;
  tExtendedArray = specialize tArray<extended>;
  pTExtendedArray = ^tExtendedArray;
  tDoubleArray = specialize tArray<single>;
  tInt32Array = specialize tArray<int32>;
  tByteArray = specialize tArray<byte>;
  tStringArray = specialize tArray<string>;
  tIntPointArray = specialize tArray<tIntPoint>;
  tInt64PointArray = specialize tArray<tInt64Point>;
  tBooleanArray = specialize tArray<boolean>;
  tExtendedArrayArray = specialize tArray<tExtendedArray>;
  tIntPointArrayArray = specialize tArray<tIntPointArray>;
  pTIntPointArrayArray = ^tIntPointArrayArray;
  tGeradenArray = specialize tArray<tGerade>;
  tRGB = record
    rgbBlue : byte;
    rgbGreen: byte;
    rgbRed  : byte;
  end;
  tRGBArray = specialize tArray<tRGB>;
  tKodierung = (kUnbekannt,k32BitSignedInteger);
  tWarnstufe = (wsStreng,wsLasch);
  tGenauigkeit = (gSingle,gDouble,gExtended);
  tKantenFilterTyp = (kfTiefpass,kfHochpass);
  tRegexTyp = (rtKein,rtFpc,rtShell);

operator = (x1,x2: t2x2Extended): boolean; inline;
operator = (x1,x2: t2x2Int64): boolean; inline;
operator = (x1,x2: tInt64Point): boolean; inline;
operator = (x1,x2: tIntPoint): boolean; inline;
operator = (x1,x2: tExtPoint): boolean; inline;
operator + (x1,x2: tExt3dPoint): tExt3dPoint; inline;
operator + (x1,x2: t2x2Extended): t2x2Extended; inline;
operator + (x1,x2: tInt64Point): tInt64Point; inline;
operator + (x1,x2: tIntPoint): tIntPoint; inline;
operator + (x1,x2: tExtPoint): tExtPoint; inline;
operator - (x1,x2: t2x2Extended): t2x2Extended; inline;
operator - (x1,x2: tInt64Point): tInt64Point; inline;
operator - (x1,x2: tIntPoint): tIntPoint; inline;
operator - (x1,x2: tExtPoint): tExtPoint; inline;
operator - (x1,x2: tExt3dPoint): tExt3dPoint; inline;
operator * (a: extended; x: t2x2Extended): t2x2Extended; inline;
operator * (a: int64; x: tInt64Point): tInt64Point; inline;
operator * (a: extended; x: tInt64Point): tExtPoint; inline;
operator * (a: longint; x: tIntPoint): tIntPoint; inline;
operator * (a: extended; x: tIntPoint): tExtPoint; inline;
operator * (a: extended; x: tExtPoint): tExtPoint; inline;
operator * (a: extended; x: tExt3dPoint): tExt3dPoint; inline;
operator * (x1,x2: tIntPoint): int64; inline;
operator * (x1,x2: tInt64Point): int64; inline;
operator * (x1,x2: tExtPoint): extended; inline;
operator * (x1,x2: tExt3dPoint): extended; inline;
operator * (m: t2x2Extended; x: tExtPoint): tExtPoint; inline;

function round(x: tExtPoint): tInt64Point; overload;
function symmetrischModulo(x,m: tInt64Point): tInt64Point; overload;
function symmetrischModulo(x: tExtPoint; m: tInt64Point): tExtPoint; overload;
function modulo(x,m: tInt64Point): tInt64Point;
function naechsterSchnittpunkt(ich: tGerade; dieAnderen: tGeradenArray): tExtPoint;

function signSqr(x: extended): extended; inline;
function myTimeToStr(t: extended): string;
function cmpStr(s1,s2: string): longint;
function mitte(s1,s2: string): string;
function myFloatToStr(x: extended): string; overload;
function myFloatToStr(x: extended; ex: string): string; overload;
function myStrToFloat(s: string): extended;
function myIntToStr(i,dg: longint): string; overload;
function myIntToStr(i,dg: longint; fill: string): string; overload;
function max(g1,g2: tGenauigkeit): tGenauigkeit; inline; overload;
function wertGroesze(g: tGenauigkeit): longint;
function tKodierungToStr(k: tKodierung): string;

function floatToStrTrunc(f: extended; dig: longint; laessig: boolean): string;
function binOpPos(op: char; s: string): integer;
function fktPos(fkt,s: string): integer;
procedure gibAus(s: string; ausgaben: byte);
function strToGen(out gen: tGenauigkeit; s: string): boolean;
function genToStr(gen: tGenauigkeit): string;
function mischeFarben(a,b: tRGB; x: extended): tRGB; inline;
function orFarben(a,b: tRGB): tRGB; inline;
function andFarben(a,b: tRGB): tRGB; inline;
function wertZuFarbe(x: extended; p: tRGBArray): tRGB;
function tFPColor2tRgb(c: tFPColor): tRGB; inline;
function tRgb2tFPColor(c: tRGB): tFPColor; inline;
function rgb(r,g,b: byte): tRGB; inline;
function strToTRGB(s: string; out rgb: tRGB): boolean;
procedure myDebugLnThreadLog(s: string);
procedure cleanupLogs;
procedure cleanupLog(tid: ptrUInt);
procedure raiseAndDumpExceptionCallStack(msg: string);
procedure dumpExceptionCallStack(e: exception);
function startetMit(start: string; var s: string; trimmen: boolean = true): boolean;
function endetMit(ende: string; var s: string): boolean;
function trimAll(s: string): string;
function erstesArgument(var s: string; trenner: string = ' '; trimmen: boolean = true): string; inline;
function umbrechen(s,trenner: string; klammernBeachten: boolean = false; zeilenanfang: string = ''): string; inline;
function unEscape(s: string): string;
function myDateTimeToStr(t: tDateTime): string;
function t2x2ExtendedToStr(p: t2x2Extended): string;
function t2x2LongintToStr(p: t2x2Longint): string;
function tExtPointToStr(p: tExtPoint): string;
function tIntPointToStr(p: tIntPoint): string;
function strToTIntPoint(s: string): tIntPoint;
function tInt64PointToStr(p: tInt64Point): string;
procedure fehler(s: string);
function point(x: tIntPoint): tPoint; overload;
function point(x: tInt64Point): tPoint; overload;
function intPoint(x,y: longint): tIntPoint;
function int64Point(x,y: int64): tInt64Point;
function extPoint(x,y: extended): tExtPoint; overload;
function extPoint(x: tInt64Point): tExtPoint; overload;
function _2x2Longint(xx,xy,yx,yy: longint): t2x2Longint;
function _2x2Extended(xx,xy,yx,yy: extended): t2x2Extended;
function ext3dPoint(x,y,z: extended): tExt3dPoint;
function gerade(a,r: tExtPoint): tGerade;
function dumpGerade(g: tGerade): string;
function dumpGeradenArray(ga: tGeradenArray): string;

function hexDump(p: pointer; cnt: longint): string;
function base64ToBin(var s: string): boolean;
function base64Decode(const s: string; out i: qword): boolean; overload;
function base64Decode(const s: string; out i: longword): boolean; overload;
function base64Encode(i: longword): string; overload;
function base64Encode(i,siz: longword): string; overload;
function mirrorBits(qw: qword): qword; overload;
function mirrorBits(lw: longword): longword; overload;
function mirrorBits(w: word): word; overload;
function mirrorBits(b: byte): byte; overload;

function zusammenFassen(s1,s2: string): string;
function intervallAusrollen(s: string): string;
function myUtf8Encode(s: string): string;
function permutation(len: longint): tLongintArray; overload;
function permutation(len,rest,maxBasis: longint; basen: tLongintArray): tLongintArray; overload;
procedure llPermutation(len,offset: longint; var ar: tLongintArray); inline;
procedure fuegeSortiertHinzu(x: extended; var xa: tExtendedArray);

function readALine(var f: file): string;
function readAnAndorString(var f: file; const len: int64; checkEOL: boolean): string; overload;
function readAnAndorString(var f: file; checkEOL: boolean): string; overload;

procedure splitStrToInt(s: string; out ia: tLongintArray);
function intArrayToStr(ia: tLongintArray): string;
function vergleicheStrings(s1,s2: string): integer;
function pruefSumme(s: string; m: longestOrdinal): longestOrdinal; inline;

function unEscapeCommas(s: string): string;
function escape(s,toe,es: string): string;
function escapeStringToRegex(s: string; typ: tRegexTyp; extras: string = ''): string; inline;

function findeRekursiv(pfad,muster: string; out datei: string): boolean;

var
  base64Chars: array[0..63] of char;
  base64CharsInvers: array[char] of byte;

const
  __ausgabenMaske: byte = 0;

implementation

uses
  matheunit;


// überladene operatoren *******************************************************

operator = (x1,x2: t2x2Extended): boolean;
var
  c,d: char;
begin
  result:=true;
  for c:='x' to 'y' do
    for d:='x' to 'y' do
      result:=result and (x1[c,d]=x2[c,d]);
end;

operator = (x1,x2: t2x2Int64): boolean;
var
  c,d: char;
begin
  result:=true;
  for c:='x' to 'y' do
    for d:='x' to 'y' do
      result:=result and (x1[c,d]=x2[c,d]);
end;

operator = (x1,x2: tInt64Point): boolean;
var
  c: char;
begin
  result:=true;
  for c:='x' to 'y' do
    result:=result and (x1[c]=x2[c]);
end;

operator = (x1,x2: tIntPoint): boolean;
var
  c: char;
begin
  result:=true;
  for c:='x' to 'y' do
    result:=result and (x1[c]=x2[c]);
end;

operator = (x1,x2: tExtPoint): boolean;
var
  c: char;
begin
  result:=true;
  for c:='x' to 'y' do
    result:=result and (x1[c]=x2[c]);
end;

operator + (x1,x2: t2x2Extended): t2x2Extended;
var
  c,d: char;
begin
  for c:='x' to 'y' do
    for d:='x' to 'y' do
      result[c,d]:=x1[c,d]+x2[c,d];
end;

operator + (x1,x2: tInt64Point): tInt64Point;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=x1[c]+x2[c];
end;

operator + (x1,x2: tIntPoint): tIntPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=x1[c]+x2[c];
end;

operator + (x1,x2: tExtPoint): tExtPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=x1[c]+x2[c];
end;

operator + (x1,x2: tExt3dPoint): tExt3dPoint;
var
  c: char;
begin
  for c:='x' to 'z' do
    result[c]:=x1[c]+x2[c];
end;

operator - (x1,x2: t2x2Extended): t2x2Extended;
var
  c,d: char;
begin
  for c:='x' to 'y' do
    for d:='x' to 'y' do
      result[c,d]:=x1[c,d]-x2[c,d];
end;

operator - (x1,x2: tInt64Point): tInt64Point;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=x1[c]-x2[c];
end;

operator - (x1,x2: tIntPoint): tIntPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=x1[c]-x2[c];
end;

operator - (x1,x2: tExtPoint): tExtPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=x1[c]-x2[c];
end;

operator - (x1,x2: tExt3dPoint): tExt3dPoint;
var
  c: char;
begin
  for c:='x' to 'z' do
    result[c]:=x1[c]-x2[c];
end;

operator * (a: extended; x: t2x2Extended): t2x2Extended;
var
  c,d: char;
begin
  for c:='x' to 'y' do
    for d:='x' to 'y' do
      result[c,d]:=a*x[c,d];
end;

operator * (a: int64; x: tInt64Point): tInt64Point;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=a*x[c];
end;

operator * (a: extended; x: tInt64Point): tExtPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=a*x[c];
end;

operator * (a: longint; x: tIntPoint): tIntPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=a*x[c];
end;

operator * (a: extended; x: tIntPoint): tExtPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=a*x[c];
end;

operator * (x1,x2: tIntPoint): int64;
var
  c: char;
begin
  result:=0;
  for c:='x' to 'y' do
    result:=result + x1[c]*x2[c];
end;

operator * (x1,x2: tInt64Point): int64;
var
  c: char;
begin
  result:=0;
  for c:='x' to 'y' do
    result:=result + x1[c]*x2[c];
end;

operator * (a: extended; x: tExtPoint): tExtPoint;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=a*x[c];
end;

operator * (a: extended; x: tExt3dPoint): tExt3dPoint;
var
  c: char;
begin
  for c:='x' to 'z' do
    result[c]:=a*x[c];
end;

operator * (x1,x2: tExtPoint): extended;
begin
  result:=x1['x']*x2['x'] + x1['y']*x2['y'];
end;

operator * (x1,x2: tExt3dPoint): extended;
begin
  result:=x1['x']*x2['x'] + x1['y']*x2['y'];
end;

operator * (m: t2x2Extended; x: tExtPoint): tExtPoint;
begin
  result:=extPoint(
    m['x']['x'] * x['x'] + m['x']['y'] * x['y'],
    m['y']['x'] * x['x'] + m['y']['y'] * x['y']
  );
end;

// allgemeine Funktionen *******************************************************

function round(x: tExtPoint): tInt64Point;
var
  c: char;
begin
  for c:='x' to 'y' do
    result[c]:=round(x[c]);
end;

function symmetrischModulo(x,m: tInt64Point): tInt64Point;
var
  c: char;
begin
  for c:='x' to 'y' do begin
    result[c]:=x[c];
    while 2*result[c]<-m[c] do
      result[c]:=result[c]+m[c];
    while 2*result[c]>=m[c] do
      result[c]:=result[c]-m[c];
  end;
end;

function symmetrischModulo(x: tExtPoint; m: tInt64Point): tExtPoint;
var
  c: char;
begin
  for c:='x' to 'y' do begin
    result[c]:=x[c];
    while 2*result[c]<-m[c] do
      result[c]:=result[c]+m[c];
    while 2*result[c]>=m[c] do
      result[c]:=result[c]-m[c];
  end;
end;

function modulo(x,m: tInt64Point): tInt64Point;
var
  c: char;
begin
  for c:='x' to 'y' do begin
    result[c]:=x[c];
    while result[c]<0 do
      result[c]:=result[c]+m[c];
    while result[c]>=m[c] do
      result[c]:=result[c]-m[c];
  end;
end;

function naechsterSchnittpunkt(ich: tGerade; dieAnderen: tGeradenArray): tExtPoint;
var
  i:      longestOrdinal;
  lMin,l: extended;
  n:      tExtPoint;
begin
  lMin:=-1;
  for i:=0 to length(dieAnderen)-1 do begin
    n:=extPoint(dieAnderen[i].r['y'],-dieAnderen[i].r['x']);
    if n * ich.r = 0 then // Gerade parallel zu r
      continue;
    l:=(n*(dieAnderen[i].a-ich.a))/(n*ich.r);
    if l<0 then // Schnittpunkt auf der falschen Seite
      continue;
    if (l<lMin) or (lMin<0) then
      lMin:=l;
  end;
  if lMin<0 then
    fehler(
      'Konnte keinen Schnittpunkt (auf dieser Seite) finden (' +
      dumpGerade(ich) + ' geschnitten mit ' +
      dumpGeradenArray(dieAnderen) +
      ')!');
  result:=ich.a + lMin*ich.r;
end;

function signSqr(x: extended): extended;
begin
  result:=sign(x)*sqr(x);
end;

function myTimeToStr(t: extended): string;
var
  tim: int64;
begin
  tim:=floor(t*24*60*60);
  result:=intToStr(tim mod 10)+'s';
  tim:=tim div 10;
  if tim=0 then exit;
  result:=intToStr(tim mod 6)+result;
  tim:=tim div 6;
  if tim=0 then exit;
  result:=intToStr(tim mod 10)+'min '+result;
  tim:=tim div 10;
  if tim=0 then exit;
  result:=intToStr(tim mod 6)+result;
  tim:=tim div 6;
  if tim=0 then exit;
  result:=intToStr(tim mod 24)+'h '+result;
  tim:=tim div 24;
  if tim=0 then exit;
  result:=' '+result;
  if (tim mod 7)<>1 then
    result:='e'+result;
  result:=intToStr(tim mod 7)+'Tag'+result;
  tim:=tim div 7;
  if tim=0 then exit;
  result:=' '+result;
  if tim<>1 then
    result:='n'+result;
  result:=intToStr(tim)+'Woche'+result;
end;

function cmpStr(s1,s2: string): longint;
var
  i: longint;
begin
  for i:=1 to min(length(s1),length(s2)) do
    if s1[i]<>s2[i] then begin
      result:=2*byte(s1[i]>s2[i])-1;
      exit;
    end;
  if length(s1)<>length(s2) then begin
    result:=2*byte(length(s1)>length(s2))-1;
    exit;
  end;
  result:=0;
end;

function mitte(s1,s2: string): string;
var
  i:    longint;
  w,nw: word;
begin
  setLength(result,max(length(s1),length(s2)));
  w:=0;
  for i:=length(result) downto 1 do begin // result:= "s1+s2";
    if i<=length(s1) then
      w:=w+byte(s1[i]);
    if i<=length(s2) then
      w:=w+byte(s2[i]);
    result[i]:=char(w and $ff);
    w:=w shr 8;
  end;
  result:=char(w)+result;
  w:=0;
  for i:=1 to length(result) do begin
    nw:=byte(odd(byte(result[i])+w));
    result[i]:=char((byte(result[i])+w) div 2);
    w:=nw shl 8;
  end;
  if w<>0 then
    result:=result+char(w div 2);
  if result[1]<>#0 then begin
    writeln('Fehler bei der Mittenfindeung!');
    halt;
  end;
  delete(result,1,1);
end;

function myFloatToStr(x: extended): string;
begin
  result:=myFloatToStr(x,'e');
end;

function myFloatToStr(x: extended; ex: string): string;
var
  i,e: longint;
begin
  e:=0;
  if x<0 then begin
    result:='-';
    x:=-x;
  end
  else
    result:='';
  if x=0 then begin
    result:='0';
    exit;
  end;
  while x<1 do begin
    dec(e);
    x:=x*10;
  end;
  while x>=10 do begin
    inc(e);
    x:=x/10;
  end;
  result:=result+char(ord('0')+floor(x))+'.';
  for i:=0 to 20 do begin
    x:=(x-floor(x))*10;
    result:=result+char(ord('0')+floor(x));
  end;
  if e<>0 then
    result:=result+ex+intToStr(e);
end;

function myStrToFloat(s: string): extended;
var
  i,e: longint;
  neg: boolean;
begin
  if pos('e',s)>0 then begin
    e:=strToInt(rightStr(s,length(s)-pos('e',s)));
    delete(s,pos('e',s),length(s));
  end
  else e:=0;
  if pos('.',s)=0 then begin
    result:=strToInt(s)*power(10,e);
    exit;
  end;
  neg:=leftStr(s,1)='-';
  if neg then
    delete(s,1,1);
  if pos('.',s)=2 then begin
    result:=0;
    for i:=length(s) downto 3 do
      result:=result/10 + ord(s[i])-ord('0');
    result:=result/10 + ord(s[1])-ord('0');
  end
  else result:=strToFloat(s);
  result:=result*power(10,e);
  if neg then
    result:=-result;
end;

function myIntToStr(i,dg: longint): string;
begin
  result:=myIntToStr(i,dg,' ');
end;

function myIntToStr(i,dg: longint; fill: string): string;
begin
  result:=intToStr(i);
  while length(result)<dg do
    result:=fill+result;
end;

function max(g1,g2: tGenauigkeit): tGenauigkeit;
begin
  if g1>g2 then result:=g1
  else result:=g2;
end;

function wertGroesze(g: tGenauigkeit): longint;
begin
  case g of
    gSingle: result:=sizeOf(single);
    gDouble: result:=sizeOf(double);
    gExtended: result:=sizeOf(extended);
    else
      fehler('Unbekannter Genauigkeitstyp!');
  end;
end;

function tKodierungToStr(k: tKodierung): string;
begin
  case k of
    kUnbekannt:
      result:='kUnbekannt';
    k32BitSignedInteger:
      result:='k32BitSignedInteger';
    else
      result:='NONE';
  end{of Case};
end;

// allgemeine Funktionen *******************************************************

function floatToStrTrunc(f: extended; dig: longint; laessig: boolean): string;
begin
  result:=intToStr(round(f*power(10,dig)));
  result:=copy(result,1,length(result)-dig)+'.'+copy(result,length(result)-dig+1,dig);
  if laessig then
    while result[length(result)]='0' do
      delete(result,length(result),1);
  if result[length(result)]='.' then
    delete(result,length(result),1);
end;

function binOpPos(op: char; s: string): integer;
begin
  result:=0;
  repeat
    result:=result+max(1,pos(op,copy(s,result+1,length(s)-result)));
  until ((result>1) and not (s[result-1] in ['+','-','*','/','(','e','E','^'])) or (result>length(s)) or (s[result]<>op);
  if (result>length(s)) or (s[result]<>op) then result:=0;
end;

function fktPos(fkt,s: string): integer;
var
  tmp: longint;
begin
  result:=pos(fkt,s);
  while (result<>0) and ((result+length(fkt)>length(s)) or not (s[result+length(fkt)] in [' ','('])) do begin
    tmp:=pos(fkt,copy(s,result+1,length(s)));
    if tmp=0 then result:=0
    else result:=result + tmp;
  end;
end;

procedure gibAus(s: string; ausgaben: byte);
begin
  if odd(ausgaben) and not odd(__ausgabenMaske) then myDebugLnThreadLog(s);
  if odd(ausgaben div 2) and not odd(__ausgabenMaske div 2) then writeln(s);
end;

function strToGen(out gen: tGenauigkeit; s: string): boolean;
begin
  result:=true;
  if (s='float') or (s='single') then
    begin
    gen:=gSingle;
    exit;
    end;
  if s='double' then
    begin
    gen:=gDouble;
    exit;
    end;
  if s='extended' then
    begin
    gen:=gExtended;
    exit;
    end;
  gibAus('Kenne Genauigkeitstyp '''+s+''' nicht!',3);
  result:=false;
end;

function genToStr(gen: tGenauigkeit): string;
begin
  case gen of
    gSingle:
      result:='single';
    gDouble:
      result:='double';
    gExtended:
      result:='extended';
    else
      result:='NONE';
  end{of case};
end;

function mischeFarben(a,b: tRGB; x: extended): tRGB;
begin
  result.rgbRed:=min(255,max(0,round(a.rgbRed*(1-x)+b.rgbRed*x)));
  result.rgbGreen:=min(255,max(0,round(a.rgbGreen*(1-x)+b.rgbGreen*x)));
  result.rgbBlue:=min(255,max(0,round(a.rgbBlue*(1-x)+b.rgbBlue*x)));
end;

function orFarben(a,b: tRGB): tRGB;
begin
  result.rgbRed:=a.rgbRed or b.rgbRed;
  result.rgbGreen:=a.rgbGreen or b.rgbGreen;
  result.rgbBlue:=a.rgbBlue or b.rgbBlue;
end;

function andFarben(a,b: tRGB): tRGB;
begin
  result.rgbRed:=a.rgbRed and b.rgbRed;
  result.rgbGreen:=a.rgbGreen and b.rgbGreen;
  result.rgbBlue:=a.rgbBlue and b.rgbBlue;
end;

function wertZuFarbe(x: extended; p: tRGBArray): tRGB;
var
  i: longint;
begin
  x:=min(1,max(0,x))*(length(p)-1);
  i:=floor(x);
  if i>=(length(p)-1) then begin
    result.rgbRed:=p[length(p)-1].rgbRed;
    result.rgbGreen:=p[length(p)-1].rgbGreen;
    result.rgbBlue:=p[length(p)-1].rgbBlue;
    exit;
  end;
  if i<0 then begin
    result.rgbRed:=p[0].rgbRed;
    result.rgbGreen:=p[0].rgbGreen;
    result.rgbBlue:=p[0].rgbBlue;
    exit;
  end;
  x:=x-i;
  result:=mischeFarben(p[i],p[i+1],x);
end;

function tFPColor2tRgb(c: tFPColor): tRGB;
begin
  result.rgbRed:=c.red;
  result.rgbGreen:=c.green;
  result.rgbBlue:=c.blue;
end;

function tRgb2tFPColor(c: tRGB): tFPColor;
begin
  result.red:=c.rgbRed;
  result.green:=c.rgbGreen;
  result.blue:=c.rgbBlue;
end;

function rgb(r,g,b: byte): tRGB;
begin
  result.rgbRed:=r;
  result.rgbGreen:=g;
  result.rgbBlue:=b;
end;

function strToTRGB(s: string; out rgb: tRGB): boolean;
var
  i: longint;
begin
  result:=false;
  if length(s)=6 then begin
    result:=true;
    for i:=1 to 6 do
      result:=result and (s[i] in ['0'..'9','a'..'f','A'..'F']);
    if result then begin
      i:=strToInt('$'+s);
      rgb.rgbRed:=  (i and $ff0000) shr 16;
      rgb.rgbGreen:=(i and $00ff00) shr 8;
      rgb.rgbBlue:=  i and $0000ff;
      exit;
    end;
  end;
end;

procedure myDebugLnThreadLog(s: string);
var
  f:  textFile;
  id: ptrUInt;
begin
  id:=getThreadID;
  assignFile(f,'Log'+intToStr(id));
  try
    if fileExists('Log'+intToStr(id)) then append(f)
    else rewrite(f);
  except
    exit;
  end;
  writeln(f,intToStr(id)+': '+s);
  closeFile(f);
end;

procedure cleanupLogs;
var
  sR:  tSearchRec;
  err: longint;
begin
  err:=findFirst('Log*',$3f,sR);
  while err=0 do begin
    deleteFile(sR.name);
    err:=findnext(sR);
  end;
  findclose(sR);
end;

procedure cleanupLog(tid: ptrUInt);
var
  s:    string;
  zeit: extended;
begin
  s:='Log'+intToStr(tid);
  zeit:=now+1/24/60/60;
  while (not fileExists(s)) and (zeit>now) do
    sleep(100);
  if fileExists(s) then
    deleteFile(s)
  else
    fehler('Datei '''+s+''' kann nicht gelöscht werden, da sie nicht existiert!');
end;

procedure raiseAndDumpExceptionCallStack(msg: string);
var
  e: exception;
begin
  e:=exception.create(msg);
  dumpExceptionCallStack(e);
  raise e;
end;

procedure dumpExceptionCallStack(e: exception);
var
  i:      integer;
  frames: pPointer;
  report: string;
begin
  report := 'Program exception! ' + lineEnding +
    'Stacktrace:' + lineEnding + lineEnding;
  if e <> nil then begin
    report := report + 'Exception class: ' + e.className + lineEnding +
    'Message: ' + e.message + lineEnding;
  end;
  report := report + backTraceStrFunc(exceptAddr);
  frames := exceptFrames;
  for i := 0 to exceptFrameCount - 1 do
    report := report + lineEnding + backTraceStrFunc(frames[i]);
  gibAus(report,3);
end;

function startetMit(start: string; var s: string; trimmen: boolean = true): boolean;
begin
  result:=leftStr(s,length(start))=start;
  if result then begin
    s:=rightStr(s,length(s)-length(start));
    if trimmen then
      s:=trim(s);
  end;
end;

function endetMit(ende: string; var s: string): boolean;
begin
  result:=rightStr(s,length(ende))=ende;
  if result then
    s:=trim(leftStr(s,length(s)-length(ende)));
end;

function trimAll(s: string): string;
begin
  result:=s;
  while pos(' ',result)>0 do
    delete(result,pos(' ',result),1);
end;

function erstesArgument(var s: string; trenner: string; trimmen: boolean): string;
begin
  result:=copy(s,1,pos(trenner,s+trenner)-1);
  delete(s,1,length(result)+length(trenner));
  if not trimmen then exit;
  s:=trim(s);
  result:=trim(result);
end;

function umbrechen(s,trenner: string; klammernBeachten: boolean = false; zeilenanfang: string = ''): string; inline;
var
  i,kl: longint;
begin
  result:='';
  i:=1;
  kl:=0;
  while i<=length(s) do begin
    if klammernBeachten and (s[i] in ['(','[','{']) then
      inc(kl);
    if klammernBeachten and (s[i] in [')',']','}']) then
      dec(kl);
    if (kl=0) and (copy(s,i,length(trenner))=trenner) then begin
      result:=result+#13+zeilenanfang+copy(s,1,i-1);
      delete(s,1,i-1+length(trenner));
      i:=0;
    end;
    inc(i);
  end;
  result:=result+#13+zeilenanfang+s;
  delete(result,1,1+length(zeilenanfang));
end;

function unEscape(s: string): string;
var
  i:     longint;
  ec,qt: boolean;
begin
  result:='';
  ec:=false;
  qt:=false;
  for i:=1 to length(s) do
    if ec then begin
      result:=result+s[i];
      ec:=false;
    end else
    case s[i] of
      '"','''': qt:=not qt;
      '\': ec:=true;
      else result:=result+s[i];
    end{of case};
  if qt then
    fehler('Anführungszeichen sind nicht ausgeglichen in '''+s+'''!');
  if ec then
    fehler(''''+s+''' endet auf einen aktiven Deaktivierer!');
end;

function myDateTimeToStr(t: tDateTime): string;
begin
  result:=formatDateTime('YYYY.MM.DD_hh.mm.ss',t);
end;

function t2x2ExtendedToStr(p: t2x2Extended): string;
begin
  result:=
    floattostr(p['x','x'])+' .. '+floattostr(p['x','y'])+' x '+
    floattostr(p['y','x'])+' .. '+floattostr(p['y','y']);
end;

function t2x2LongintToStr(p: t2x2Longint): string;
begin
  result:=
    intToStr(p['x','x'])+' .. '+intToStr(p['x','y'])+' x '+
    intToStr(p['y','x'])+' .. '+intToStr(p['y','y']);
end;

function tExtPointToStr(p: tExtPoint): string;
begin
  result:=floattostr(p['x'])+';'+floattostr(p['y']);
end;

function tIntPointToStr(p: tIntPoint): string;
begin
  result:=intToStr(p['x'])+';'+intToStr(p['y']);
end;

function strToTIntPoint(s: string): tIntPoint;
begin
  result['x']:=strToInt(erstesArgument(s,';'));
  result['y']:=strToInt(s);
end;

function tInt64PointToStr(p: tInt64Point): string;
begin
  result:=intToStr(p['x'])+';'+intToStr(p['y']);
end;

procedure fehler(s: string);
begin
  gibAus(s,1);
  raise exception.create(s);
end;

function point(x: tIntPoint): tPoint;
begin
  result.x:=x['x'];
  result.y:=x['y'];
end;

function point(x: tInt64Point): tPoint;
begin
  result.x:=x['x'];
  result.y:=x['y'];
end;

function intPoint(x,y: longint): tIntPoint;
begin
  result['x']:=x;
  result['y']:=y;
end;

function int64Point(x,y: int64): tInt64Point;
begin
  result['x']:=x;
  result['y']:=y;
end;

function extPoint(x,y: extended): tExtPoint;
begin
  result['x']:=x;
  result['y']:=y;
end;

function extPoint(x: tInt64Point): tExtPoint;
begin
  result['x']:=x['x'];
  result['y']:=x['y'];
end;

function _2x2Longint(xx,xy,yx,yy: longint): t2x2Longint;
begin
  result['x','x']:=xx;
  result['x','y']:=xy;
  result['y','x']:=yx;
  result['y','y']:=yy;
end;

function _2x2Extended(xx,xy,yx,yy: extended): t2x2Extended;
begin
  result['x','x']:=xx;
  result['x','y']:=xy;
  result['y','x']:=yx;
  result['y','y']:=yy;
end;

function ext3dPoint(x,y,z: extended): tExt3dPoint;
begin
  result['x']:=x;
  result['y']:=y;
  result['z']:=z;
end;

function gerade(a,r: tExtPoint): tGerade;
begin
  result.a:=a;
  result.r:=r;
end;

function dumpGerade(g: tGerade): string;
begin
  result:=
    'a: (' + tExtPointToStr(g.a) + ') ' +
    'r: (' + tExtPointToStr(g.r) + ')';
end;

function dumpGeradenArray(ga: tGeradenArray): string;
var
  i: longestOrdinal;
begin
  result:='{';
  for i:=0 to length(ga)-1 do begin
    if i>0 then
      result:=result+', ';
    result:=result+dumpGerade(ga[i]);
  end;
  result:=result+'}';
end;

function hexDump(p: pointer; cnt: longint): string;
var
  i: longint;
begin
  result:='';
  for i:=cnt-1 downto 0 do
    result:=result+intToHex((pByte(p)+i)^,2);
end;

function base64ToBin(var s: string): boolean;
var
  i,j: longint;
  t:   string;
  b:   byte;
begin
  t:='';
  result:=false;
  for i:=1 to length(s) do begin
    b:=base64CharsInvers[s[i]];
    if b=255 then exit;
    for j:=5 downto 0 do
      t:=t+char(ord('0')+byte(odd(b shr j)));
  end;
  s:=t;
  result:=true;
end;

function base64Decode(const s: string; out i: qword): boolean;
var
  j: longint;
  b: byte;
begin
  i:=0;
  result:=false;
  for j:=1 to length(s) do begin
    b:=base64CharsInvers[s[j]];
    if b=255 then exit;
    i:=(i shl 6) or b;
  end;
  result:=true;
end;

function base64Decode(const s: string; out i: longword): boolean;
var
  j: qword;
begin
  result:=base64Decode(s,j);
  if result then
    i:=j
  else
    i:=0;
end;

function base64Encode(i: longword): string;
begin
  result:=base64Encode(i,(8*sizeOf(i)+7) div 6);
end;

function base64Encode(i,siz: longword): string;
var
  j: longint;
begin
  result:='';
  for j:=0 to siz-1 do begin
    result:=base64Chars[i and $3f]+result;
    i:=i shr 6;
  end;
end;

function mirrorBits(qw: qword): qword;
begin
  result:=
    (mirrorBits(longword(qw and $ffffffff)) shl 32) or
      mirrorBits(longword(qw shr 32));
end;

function mirrorBits(lw: longword): longword;
begin
  result:=
    (mirrorBits(word(lw and $ffff)) shl 16) or
      mirrorBits(word(lw shr 16));
end;

function mirrorBits(w: word): word;
begin
  result:=
    (mirrorBits(byte(w and $ff)) shl 8) or
      mirrorBits(byte(w shr 8));
end;

function mirrorBits(b: byte): byte;
begin
  result:=
      byte(odd(b shr 7)) or
    (byte(odd(b shr 6)) shl 1) or
    (byte(odd(b shr 5)) shl 2) or
    (byte(odd(b shr 4)) shl 3) or
    (byte(odd(b shr 3)) shl 4) or
    (byte(odd(b shr 2)) shl 5) or
    (byte(odd(b shr 1)) shl 6) or
    (byte(odd(b)) shl 7);
end;

function zusammenFassen(s1,s2: string): string;
var
  i: longint;
begin
  if istGanzZahl(s1) and istGanzZahl(s2) then begin
    i:=strToInt(s1)+strToInt(s2);
    result:=intToStr(i);
    if (startetMit('+',s1) or startetMit('+',s2)) and (i>=0) then
      result:='+'+result;
  end
  else if s1=s2 then result:=s1
  else fehler('Ich kann '''+s1+''' und '''+s2+''' nicht zusammenfassen!');
end;

function intervallAusrollen(s: string): string;
var
  i: longint;
begin
  i:=strToInt(erstesArgument(s,'..'));
  result:='';
  while i<=strToInt(s) do begin
    result:=result+' '+intToStr(i);
    inc(i);
  end;
  result:=trim(result);
end;

function myUtf8Encode(s: string): string;
const
  falsch:  string = #$c4#$d6#$dc#$df#$e4#$f6#$fc;
  richtig: array[1..7] of string = ('Ä','Ö','Ü','ß','ä','ö','ü');
var
  i: longint;
begin
  result:=s;
  for i:=1 to length(richtig) do
    while pos(falsch[i],result)>0 do
      result:=leftStr(result,pos(falsch[i],result)-1)+richtig[i]+rightStr(result,length(result)-pos(falsch[i],result));
end;

function permutation(len: longint): tLongintArray;
var
  basen:                tLongintArray;
  rest,i,ilen,best,off: longint;
const
  maxBasis = 8;
begin
  rest:=len;
  ilen:=len;
  setLength(basen,0);

  repeat
    for i:=maxBasis downto 2 do
      while ((rest mod i) = 0) and (rest>maxBasis) do begin
        setLength(basen,length(basen)+1);
        basen[length(basen)-1]:=i;
        rest:=rest div i;
      end;
    if rest>maxBasis then begin
      best:=maxBasis;
      for i:=maxBasis-1 downto 2 do
        if i*((rest+i-1) div i)-rest < best*((rest+best-1) div best)-rest then
          best:=i;
      off:=best*((rest+best-1) div best)-rest;  // Bedeutung von off: rest -> rest + off
      ilen:=(ilen div rest)*(rest+off);
      rest:=rest+off;
    end;
  until rest<=maxBasis;

  // len = rest * \prod_i basis[i]
  result:=permutation(ilen,rest,maxBasis,basen);

  if ilen>len then begin
    off:=0;
    i:=0;
    while i+off<ilen do
      if result[i+off]>=len then
        inc(off)
      else begin
        result[i]:=result[i+off];
        inc(i);
      end;
    setLength(result,len);
  end;
end;

function permutation(len,rest,maxBasis: longint; basen: tLongintArray): tLongintArray;
var
  i,block,position,element: longint;
  mergePerm:                tLongintArray;
  werte:                    array[boolean] of tLongintArray;
  aWerte:                   boolean;
begin
  for aWerte:=false to true do
    setLength(werte[aWerte],len);

  aWerte:=false;

  for block:=0 to (len div rest)-1 do          // Initialisierung der $\prod_i basis[i]$ Blöcke zu je $rest$ permutierten Zahlen
    llPermutation(rest,block*rest,werte[aWerte]);

  setLength(mergePerm,maxBasis);

  for i:=0 to length(basen)-1 do begin
    for block:=0 to (len div (rest*basen[i]))-1 do begin
      for position:=0 to rest-1 do begin
        llPermutation(basen[i],0,mergePerm);
        for element:=0 to basen[i]-1 do
          werte[not aWerte,block*rest*basen[i] + position*basen[i] + mergePerm[element]]:=
            werte[aWerte,block*rest*basen[i] + element*rest + position] + element*rest;
      end;
    end;

    aWerte:=not aWerte;
    rest:=rest*basen[i];
  end;

  setLength(mergePerm,0);

  setLength(result,length(werte[aWerte]));
  if length(result)>0 then
    move(werte[aWerte][0],result[0],length(result)*sizeOf(result[0]));

  for aWerte:=false to true do
    setLength(werte[aWerte],0);
end;

procedure llPermutation(len,offset: longint; var ar: tLongintArray);
var
  wert,i,j: longint;
begin
  for i:=0 to len-1 do
    ar[i+offset]:=-1;
  for i:=0 to len-1 do begin
    wert:=random(len-i);
    j:=0;
    while j<=wert do begin
      wert:=wert + byte(ar[j+offset]>=0);
      inc(j);
    end;
    ar[wert+offset]:=i;
  end;
  for i:=0 to len-1 do
    if ar[i+offset]=-1 then
      fehler('permutation: '+intToStr(i)+' wurde nicht verteilt!');
end;

procedure fuegeSortiertHinzu(x: extended; var xa: tExtendedArray);
var
  mi,ma,i: longint;
begin
  mi:=0;
  ma:=length(xa)-1;
  while mi<=ma do begin
    i:=(mi+ma) div 2;
    if x<xa[i] then
      ma:=i-1
    else if x>xa[i] then
      mi:=i+1
    else
      exit;
  end;
  if mi<>ma+1 then
    fehler('Bisektion fehlgeschlagen! ('+intToStr(mi)+' = mi <> ma+1 = '+intToStr(ma+1)+')');
  setLength(xa,length(xa)+1);
  for i:=length(xa)-1 downto mi+1 do
    xa[i]:=xa[i-1];
  xa[mi]:=x;
end;

function readALine(var f: file): string;
var
  fPos,fSize,rLen: int64;
begin
  fPos:=filePos(f);
  fSize:=fileSize(f);
  rLen:=0;
  result:='';
  while (pos(#10,result)=0) and (rLen+fPos<fSize) do begin
    rLen:=min(rLen+1024,fSize-fPos);
    setLength(result,rLen);
    blockRead(f,result[1],rLen);
    seek(f,fPos);
  end;
  if pos(#10,result)<>0 then begin
    rLen:=pos(#10,result);
    setLength(result,rLen-1);
  end;
  seek(f,fPos+rLen);
end;

function readAnAndorString(var f: file; const len: int64; checkEOL: boolean): string;
begin
  setLength(result,len+2*byte(checkEOL));
  if length(result)>0 then
    blockRead(f,result[1],length(result));
  if checkEOL then begin
    if rightStr(result,2)<>' '#10 then
      raise exception.create('readAnAndorString: EOL check failed: '''+result+'''!');
    setLength(result,length(result)-2);
  end;
end;

function readAnAndorString(var f: file; checkEOL: boolean): string;
begin
  result:=readALine(f);
  result:=readAnAndorString(f,strToInt64(result),checkEOL);
end;

procedure splitStrToInt(s: string; out ia: tLongintArray);
begin
  setLength(ia,0);
  while s<>'' do begin
    setLength(ia,length(ia)+1);
    ia[length(ia)-1]:=strToInt(erstesArgument(s,' ',false));
  end;
end;

function intArrayToStr(ia: tLongintArray): string;
var
  i: longint;
begin
  result:='';
  for i:=0 to length(ia)-1 do begin
    if i>0 then
      result:=result + ' ';
    result:=result + intToStr(ia[i]);
  end;

end;

function vergleicheStrings(s1,s2: string): integer;
var
  i: longint;
begin
  result:=0;
  i:=1;
  while (i<=length(s1)) and (i<=length(s2)) do begin
    if s1[i]<s2[i] then begin
      result:=-1;
      exit;
    end;
    if s1[i]>s2[i] then begin
      result:=1;
      exit;
    end;
    inc(i);
  end;
  if length(s1)<length(s2) then begin
    result:=-1;
    exit;
  end;
  if length(s1)>length(s2) then begin
    result:=1;
    exit;
  end;
  result:=0;
end;

function pruefSumme(s: string; m: longestOrdinal): longestOrdinal;
var
  i: longint;
begin
  result:=0;
  for i:=1 to length(s) do
    result:=((result*256)+ord(s[i])) mod m;
end;

function unEscapeCommas(s: string): string;
begin
  result:=s;
  while pos('\,',result)>0 do
    delete(result,pos('\,',result),1);
end;

function escape(s,toe,es: string): string;
var
  i,j: longint;
  b:   boolean;
begin
  result:='';
  for i:=1 to length(s) do begin
    b:=false;
    for j:=1 to length(toe) do
      b:=b or (toe[j]=s[i]);
    if b then result:=result+es;
    result:=result+s[i];
  end;
end;

function escapeStringToRegex(s: string; typ: tRegexTyp; extras: string = ''): string;
begin
  case typ of
    rtKein:
      result:=s;
    rtFpc:
      result:=escape(s,'\.[+^$'+extras,'\');
    rtShell:
      result:=escape(s,'\.[^$'+extras,'\');
  end{of case};
end;

function findeRekursiv(pfad,muster: string; out datei: string): boolean;
var
  sr:  tSearchRec;
  err: integer;
begin
  result:=false;
  err:=findFirst(pfad+'/'+muster,faAnyFile and not faDirectory,sr);
  while (err=0) and not result do begin
    if (sr.attr and faDirectory)=0 then begin
      result:=true;
      datei:=pfad+'/'+sr.name;
      break;
    end;
    err:=findNext(sr);
  end;
  findClose(sr);
  if result then
    exit;
  err:=findFirst(pfad+'/*',faAnyFile,sr);
  while (err=0) and not result do begin
    if (sr.name<>'.') and (sr.name<>'..') and ((sr.attr and faDirectory)<>0) then
      result:=findeRekursiv(pfad+'/'+sr.name,muster,datei);
    err:=findNext(sr);
  end;
  findClose(sr);
end;

var
  b: byte;

begin
  for b:=0 to 25 do begin
    base64Chars[b]:=   char(b+ord('A'));
    base64Chars[b+26]:=char(b+ord('a'));
  end;
  for b:=0 to 9 do
    base64Chars[52+b]:=char(b+ord('0'));
  base64Chars[62]:='+';
  base64Chars[63]:='/';
  for b:=0 to 255 do
    base64CharsInvers[char(b)]:=255;
  for b:=0 to 63 do
    base64CharsInvers[base64Chars[b]]:=b;
end.