Source file debian.ml
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
open Dose_common
module Pcre = Re_pcre
include Util.Logging (struct
let label = "dose_versioning.debian"
end)
let is_digit = function '0' .. '9' -> true | _ -> false
let skip_while_from i f w m =
let rec loop i = if i = m then i else if f w.[i] then loop (i + 1) else i in
loop i
let extract_epoch x =
try
let ci = String.index x ':' in
let epoch = String.sub x 0 ci
and rest = String.sub x (ci + 1) (String.length x - ci - 1) in
(epoch, rest)
with Not_found -> ("", x)
let x =
try
let di = String.rindex x '-' in
let before = String.sub x 0 di in
let after = String.sub x (di + 1) (String.length x - di - 1) in
(before, after)
with Not_found -> (x, "")
let compare_chars c1 c2 =
match c1 with
| '~' -> ( match c2 with '~' -> 0 | _ -> -1)
| 'a' .. 'z' | 'A' .. 'Z' -> (
match c2 with
| '~' -> 1
| 'a' .. 'z' | 'A' .. 'Z' -> Char.compare c1 c2
| _ -> -1)
| _ -> (
match c2 with
| '~' | 'a' .. 'z' | 'A' .. 'Z' -> 1
| _ -> Char.compare c1 c2)
let skip_zeros x xi xl = skip_while_from xi (fun c -> c = '0') x xl
let compare_chunks x y =
let xl = String.length x and yl = String.length y in
let rec loop_lexical xi yi =
assert (xi <= xl && yi <= yl) ;
match (xi = xl, yi = yl) with
| (true, true) -> 0
| (true, false) ->
let ys = skip_zeros y yi yl in
if ys = yl then 0 else if y.[ys] = '~' then 1 else -1
| (false, true) ->
let xs = skip_zeros x xi xl in
if xs = xl then 0 else if x.[xs] = '~' then -1 else 1
| (false, false) -> (
match (is_digit x.[xi], is_digit y.[yi]) with
| (true, true) ->
compare_numerical (skip_zeros x xi xl) (skip_zeros y yi yl)
| (true, false) ->
if y.[yi] = '~' then 1 else -1
| (false, true) ->
if x.[xi] = '~' then -1 else 1
| (false, false) ->
let comp = compare_chars x.[xi] y.[yi] in
if comp = 0 then loop_lexical (xi + 1) (yi + 1) else comp)
and compare_numerical xi yi =
assert (xi = xl || (xi < xl && x.[xi] <> '0')) ;
assert (yi = yl || (yi < yl && y.[yi] <> '0')) ;
let xn = skip_while_from xi is_digit x xl
and yn = skip_while_from yi is_digit y yl in
let comp = compare (xn - xi) (yn - yi) in
if comp = 0 then
loop_numerical xi yi yn
else
comp
and loop_numerical xi yi yn =
assert (xi <= xl && yi <= yn && yn <= yl) ;
if yi = yn then
loop_lexical xi yi
else
let comp = Char.compare x.[xi] y.[yi] in
if comp = 0 then loop_numerical (xi + 1) (yi + 1) yn else comp
in
loop_lexical 0 0
let compare (x : string) (y : string) =
let normalize_comp_result x = if x = 0 then 0 else if x < 0 then -1 else 1 in
if x = y then 0
else
let (e1, rest1) = extract_epoch x and (e2, rest2) = extract_epoch y in
let e_comp = compare_chunks e1 e2 in
if e_comp <> 0 then normalize_comp_result e_comp
else
let (u1, r1) = extract_revision rest1
and (u2, r2) = extract_revision rest2 in
let u_comp = compare_chunks u1 u2 in
if u_comp <> 0 then normalize_comp_result u_comp
else normalize_comp_result (compare_chunks r1 r2)
let equal (x : string) (y : string) = if x = y then true else compare x y = 0
type version_analysis =
| Native of string * string * string
| NonNative of string * string * string * string
let binnmu_regexp = Pcre.regexp "^(.*)\\+(b[\\d]+)$"
let v =
try
let subs = Pcre.extract ~rex:binnmu_regexp v in
(subs.(1), subs.(2))
with Not_found -> (v, "")
let decompose v =
let (epoch, rest) = extract_epoch v in
let (upstream_complete, revision_complete) = extract_revision rest in
if revision_complete = "" then
let (upstream, binnmu) = extract_binnmu upstream_complete in
Native (epoch, upstream, binnmu)
else
let (revision, binnmu) = extract_binnmu revision_complete in
NonNative (epoch, upstream_complete, revision, binnmu)
let compose = function
| Native ("", upstream, "") -> upstream
| Native (epoch, upstream, "") -> epoch ^ ":" ^ upstream
| Native ("", upstream, binnmu) -> upstream ^ "+" ^ binnmu
| Native (epoch, upstream, binnmu) -> epoch ^ ":" ^ upstream ^ "+" ^ binnmu
| NonNative ("", upstream, revision, "") -> upstream ^ "-" ^ revision
| NonNative (epoch, upstream, revision, "") ->
epoch ^ ":" ^ upstream ^ "-" ^ revision
| NonNative ("", upstream, revision, binnmu) ->
upstream ^ "-" ^ revision ^ "+" ^ binnmu
| NonNative (epoch, upstream, revision, binnmu) ->
epoch ^ ":" ^ upstream ^ "-" ^ revision ^ "+" ^ binnmu
let strip_epoch_binnmu v =
match decompose v with
| Native (_, upstream, _) -> Native ("", upstream, "")
| NonNative (_, upstream, revision, _) ->
NonNative ("", upstream, revision, "")
let strip_epoch v =
match decompose v with
| Native (_, upstream, binnmu) -> Native ("", upstream, binnmu)
| NonNative (_, upstream, revision, binnmu) ->
NonNative ("", upstream, revision, binnmu)
let v = fst (extract_epoch v)