-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremovecommonprefix.pas
85 lines (70 loc) · 1.56 KB
/
removecommonprefix.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
unit RemoveCommonPrefix;
{$mode delphi}
interface
uses
Classes, SysUtils;
type
StringArray = array of String;
function RemoveCommonPrefix(Filenames: StringArray): StringArray;
implementation
function Min(x, y: Integer): Integer;
begin
if x > y then
Result := y
else
Result := x;
end;
function MinLength(Filenames: StringArray): Integer;
var
i: Integer;
m: Integer;
begin
m := Length(Filenames[0]);
for i := 1 to Length(Filenames) - 1 do
begin
m := Min(m, Length(Filenames[i]));
end;
{ m - 1 because we need to keep at least one character on the filenames }
Result := m - 1;
end;
function AreEqualAtCharacterIndex(Filenames: StringArray; Index: Integer): Boolean;
var
i: Integer;
begin
i := 1;
while (i < Length(Filenames)) and (Filenames[0][Index] = Filenames[i][Index]) do
Inc(i);
Result := i >= Length(Filenames);
end;
function CommonPrefixLength(Filenames: StringArray): Integer;
var
i: Integer;
min: Integer;
begin
min := MinLength(Filenames);
i := 1;
while (i <= min) and (AreEqualAtCharacterIndex(Filenames, i)) do
Inc(i);
Result := i - 1;
end;
function RemoveCommonPrefix(Filenames: StringArray): StringArray;
var
i: Integer;
prefixLength: Integer;
begin
prefixLength := CommonPrefixLength(Filenames);
if prefixLength <= 0 then
begin
Result := Filenames;
end
else
begin
SetLength(Result, Length(Filenames));
for i := 0 to Length(Filenames) - 1 do
begin
Result[i] := Copy(Filenames[i], prefixLength + 1, Length(Filenames[i]) -
prefixLength);
end;
end;
end;
end.