Debian Codegraph @

tools/internal/check_packages/packageutils.pas

raw
1 {
2 Copyright 2024-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Various utilities of check_packages. }
17 unit PackageUtils;
18
19 interface
20
21 uses Classes;
22
23 { Does given String match a regular expression, and if yes -- add all matches
24 to Matches.
25 Note that Matches are not cleared at the beginning.
26
27 Matches are added in the order of regular expression sub-expressions.
28 Match number 0 is the entire matched string (substring of S).
29
30 When compiled with FPC, this uses FPC RegExpr unit with TRegExpr class.
31 When compiled with Delphi, this uses Delphi RegularExpressions unit with TRegEx record.
32
33 @bold(The FPC and Delphi implementations are not guaranteed to be perfectly compatible.)
34 Using this routine is only safe for the subset of regular expressions that are compatible
35 between FPC and Delphi implementations.
36 That is also why we don't add such thing to CGE, it would be too fragile.
37 But it is good enough for check_package tool. }
38 function StringMatchesRegexp(const S, RegexpPattern: String;
39 const Matches: TStrings): Boolean;
40
41 { Compare 2 lists of files. May change the order.
42 The CaseSensitive property of lists doesn't matter.
43 Makes exception in case they differ. }
44 procedure CompareFilesLists(const A, B: TStringList; const FailureMessage: String);
45
46 implementation
47
48 uses SysUtils, CastleRegexpr;
49
50 function StringMatchesRegexp(const S, RegexpPattern: String;
51 const Matches: TStrings): Boolean;
52 var
53 R: TRegExpr;
54 I: Integer;
55 begin
56 R := TRegExpr.Create;
57 try
58 R.Expression := RegexpPattern;
59 Result := R.Exec(S);
60 if Result then
61 begin
62 for I := 0 to R.SubExprMatchCount do
63 Matches.Add(R.Match[I]);
64 end;
65 finally FreeAndNil(R) end;
66 end;
67
68 procedure CompareFilesLists(const A, B: TStringList; const FailureMessage: String);
69 begin
70 A.Sort;
71 B.Sort;
72 if A.Count <> B.Count then
73 raise Exception.CreateFmt('%s: different number of files: %d vs %d',
74 [FailureMessage, A.Count, B.Count]);
75 if AnsiCompareFileName(A.Text, B.Text) <> 0 then
76 raise Exception.CreateFmt('%s: different files', [FailureMessage]);
77 end;
78
79 end.