-
Notifications
You must be signed in to change notification settings - Fork 305
/
version.R
435 lines (390 loc) · 12.7 KB
/
version.R
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
# File src/library/base/R/version.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2023 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/
## A simple S3 class for numeric versions (including package versions),
## and associated methods.
## We represent "vectors" of numeric versions as lists of sequences of
## integers, as obtained by splitting the version strings on the
## separators. By default, only valid version specs (sequences of
## integers of suitable length), separated by '.' or '-', are allowed.
## If strictness is turned off, invalid specs result in integer()
## (rather than NA) to keep things simple. (Note: using NULL would make
## subscripting more cumbersome ...)
## (In fact, the underlying mechanism could easily be extended to more
## general alphanumeric version specs. E.g., one could allow "letters"
## in version numbers by replacing the non-sep characters in the version
## string by their ASCII codes. However, this is not straightforward:
## alternatively, one could use an extended scheme with special markup
## for alpha, beta, release candidate, release, and patch versions, as
## used by many open source programs. See e.g. the version::AlphaBeta
## module on CPAN.)
.make_numeric_version <-
function(x, strict = TRUE, regexp, classes = NULL)
{
## Internal creator for numeric version objects.
nms <- names(x)
if(!length(x)) {
y <- list()
} else {
if(!is.character(x)) {
msg <- gettextf("invalid non-character version specification 'x' (type: %s)",
typeof(x))
if(tolower(Sys.getenv("_R_CHECK_STOP_ON_INVALID_NUMERIC_VERSION_INPUTS_")
!= "false"))
stop(msg, domain = NA)
else
warning(msg, domain = NA, immediate. = TRUE)
if(nzchar(Sys.getenv("_R_CALLS_INVALID_NUMERIC_VERSION_"))) {
## Showing the call stack as part of warning() may
## truncate, so do it via message() ...
calls <- sys.calls()
msg <- paste0(gettext("Calls"), ":\n",
paste0(sprintf("%2i: ", seq_along(calls)),
vapply(calls, deparse1, "",
collapse = "\n "),
collapse = "\n"))
message(msg, domain = NA)
}
}
x <- as.character(x)
y <- rep.int(list(integer()), length(x))
valid_numeric_version_regexp <- sprintf("^%s$", regexp)
ok <- grepl(valid_numeric_version_regexp, x)
if(strict && !all(i <- (ok | is.na(x))))
stop(gettextf("invalid version specification %s",
paste(sQuote(unique(x[!i])), collapse = ", ")),
call. = FALSE, domain = NA)
y[ok] <- lapply(strsplit(x[ok], "[.-]"), as.integer)
}
names(y) <- nms
class(y) <- unique(c(classes, "numeric_version"))
y
}
## Basic numeric versions.
numeric_version <-
function(x, strict = TRUE) {
## Be nice.
if(is.numeric_version(x))
return(x)
.make_numeric_version(x, strict,
.standard_regexps()$valid_numeric_version)
}
is.numeric_version <-
function(x)
inherits(x, "numeric_version")
as.numeric_version <-
function(x)
{
if(is.numeric_version(x)) x
else if(is.package_version(x)) {
## Pre 2.6.0 is.package_version() compatibility code ...
## Simplify eventually ...
structure(x, class = c(class(x), "numeric_version"))
}
else if(is.list(x) && all(vapply(x, is.integer, NA))) {
bad <- vapply(x,
function(e) anyNA(e) || any(e < 0L),
NA)
if(any(bad)) {
x[bad] <- rep.int(list(integer()), sum(bad))
}
class(x) <- "numeric_version"
x
}
else numeric_version(x)
}
## Package versions must have at least two integers, corresponding to
## major and minor.
package_version <-
function(x, strict = TRUE)
{
## Be nice.
if(is.package_version(x))
return(x)
## Special-case R version lists.
## Currently, do this here for backward compatibility.
## Should this be changed eventually?
if(is.list(x) && all(c("major", "minor") %in% names(x)))
return(R_system_version(paste(x[c("major", "minor")],
collapse = ".")))
.make_numeric_version(x, strict,
.standard_regexps()$valid_package_version,
"package_version")
}
is.package_version <-
function(x)
inherits(x, "package_version")
as.package_version <-
function(x)
if(is.package_version(x)) x else package_version(x)
## R system versions must have exactly three integers.
## (Not sure if reduced strictness makes a lot of sense here.)
R_system_version <-
function(x, strict = TRUE)
.make_numeric_version(x, strict,
.standard_regexps()$valid_R_system_version,
c("R_system_version", "package_version"))
getRversion <-
function()
package_version(R.version)
## Workhorses.
.encode_numeric_version <-
function(x)
{
strlpad <- function(x, char, width)
paste0(strrep(char, width - nchar(x)), x)
strrpad <- function(x, char, width)
paste0(x, strrep(char, width - nchar(x)))
if(!is.numeric_version(x)) stop("wrong class")
classes <- class(x)
nms <- names(x)
x <- unclass(x)
lens <- lengths(x)
y <- lapply(x, function(e) sprintf("%o", e))
## Maximal number of octal digits needed.
width <- max(nchar(unlist(y)), 0L)
## Left-pad octals with zeros to common width, collapse, and
## right-pad with zeros to total common width.
y <- vapply(y,
function(e)
paste(strlpad(e, "0", width), collapse = ""),
"")
y <- strrpad(y, "0", max(nchar(y), 0L))
structure(ifelse(lens > 0L, y, NA_character_),
width = width, lens = lens, .classes = classes, names = nms)
}
## <NOTE>
## Currently unused.
.decode_numeric_version <-
function(x)
{
width <- attr(x, "width")
y <- Map(function(elt, len) {
if(is.na(elt)) return(integer())
first <- seq(from = 1L, length.out = len, by = width)
last <- seq(from = width, length.out = len, by = width)
strtoi(substring(elt, first, last), 8L)
},
x, attr(x, "lens"))
names(y) <- names(x)
class(y) <- unique(c(attr(x, ".classes"), "numeric_version"))
y
}
## </NOTE>
## Methods.
`[.numeric_version` <-
function(x, i, j)
{
y <- if(missing(j))
unclass(x)[i]
else
lapply(unclass(x)[i], `[`, j)
## Change sequences which are NULL or contains NAs to integer().
bad <- vapply(y, function(t) is.null(t) || anyNA(t), NA)
if(any(bad))
y[bad] <- rep.int(list(integer()), sum(bad))
class(y) <- class(x)
y
}
`[<-.numeric_version` <-
function(x, i, j, value)
{
y <- unclass(x)
if(missing(j))
y[i] <- unclass(as.numeric_version(value))
else {
## Listify value as needed and validate.
if(!is.list(value)) value <- list(value)
value <- lapply(value, as.integer)
if(any(vapply(value,
function(e) anyNA(e) || any(e < 0L),
NA)))
stop("invalid 'value'")
## Listify j as needed.
if(!is.list(j)) j <- list(j)
y[i] <- Map(`[<-`, y[i], j, value)
}
class(y) <- class(x)
y
}
`[[.numeric_version` <-
function(x, ..., exact = NA)
{
if(...length() < 2L)
structure(list(unclass(x)[[..., exact=exact]]), class = oldClass(x))
else
unclass(x)[[..1, exact=exact]][..2]
}
## allowed forms
## x[[i]] <- "1.2.3"; x[[i]] <- 1L:3L; x[[c(i,j)]] <- <single integer>
## x[[i,j]] <- <single integer>
`[[<-.numeric_version` <-
function(x, ..., value)
{
z <- unclass(x)
if(nargs() < 4L) {
if(length(..1) < 2L) {
if(is.character(value) && length(value) == 1L)
value <- unclass(as.numeric_version(value))[[1L]]
else if(!is.integer(value)) stop("invalid 'value'")
} else {
value <- as.integer(value)
if(length(value) != 1L) stop("invalid 'value'")
}
z[[..1]] <- value
} else {
value <- as.integer(value)
if(length(value) != 1L) stop("invalid 'value'")
z[[..1]][..2] <- value
}
structure(z, class = oldClass(x))
}
Ops.numeric_version <-
function(e1, e2)
{
if(nargs() == 1L)
stop(gettextf("unary '%s' not defined for \"numeric_version\" objects",
.Generic), domain = NA)
boolean <- switch(.Generic, "<" = , ">" = , "==" = , "!=" = ,
"<=" = , ">=" = TRUE, FALSE)
if(!boolean)
stop(gettextf("'%s' not defined for \"numeric_version\" objects",
.Generic), domain = NA)
if(!is.numeric_version(e1)) e1 <- as.numeric_version(e1)
if(!is.numeric_version(e2)) e2 <- as.numeric_version(e2)
op <- get(.Generic, mode = "function")
op(.Internal(compareNumericVersion(e1, e2)), 0L)
}
Summary.numeric_version <-
function(..., na.rm)
{
ok <- switch(.Generic, max = , min = , range = TRUE, FALSE)
if(!ok)
stop(gettextf("%s not defined for \"numeric_version\" objects",
.Generic), domain = NA)
x <- do.call(c, lapply(list(...), as.numeric_version))
v <- xtfrm(x)
if(!na.rm && length(pos <- which(is.na(v)))) {
y <- x[pos[1L]]
if(as.character(.Generic) == "range")
c(y, y)
else
y
}
else
switch(.Generic,
max = x[which.max(v)],
min = x[which.min(v)],
range = x[c(which.min(v), which.max(v))])
}
as.character.numeric_version <-
function(x, ...)
as.character(format(x))
as.data.frame.numeric_version <- as.data.frame.vector
as.list.numeric_version <-
function(x, ...)
{
nms <- names(x)
names(x) <- NULL
y <- lapply(seq_along(x), function(i) x[i])
names(y) <- nms
y
}
c.numeric_version <-
function(..., recursive = FALSE)
{
x <- lapply(list(...), as.numeric_version)
## Try to preserve common extension classes.
## Note that this does not attempt to turn character strings into
## *package* versions if possible.
classes <- if(length(unique(lapply(x, class))) == 1L)
class(x[[1L]])
else
"numeric_version"
structure(unlist(x, recursive = FALSE), class = classes)
}
duplicated.numeric_version <-
function(x, incomparables = FALSE, ...)
{
x <- .encode_numeric_version(x)
NextMethod("duplicated")
}
format.numeric_version <-
function(x, ...)
{
x <- unclass(x)
y <- rep.int(NA_character_, length(x))
names(y) <- names(x)
ind <- lengths(x) > 0L
y[ind] <- unlist(lapply(x[ind], paste, collapse = "."))
y
}
is.na.numeric_version <-
function(x)
is.na(.encode_numeric_version(x))
`is.na<-.numeric_version` <-
function(x, value)
{
x[value] <- list(integer())
x
}
anyNA.numeric_version <-
function(x, recursive = FALSE)
{
## <NOTE>
## Assuming *valid* numeric_version objects, we could simply do:
## any(lengths(unclass(x)) == 0L)
## </NOTE>
anyNA(.encode_numeric_version(x))
}
print.numeric_version <-
function(x, quote = FALSE, ...)
{
y <- as.character(x)
if(!length(y))
writeLines(gettext("<0 elements>"))
else
print(ifelse(is.na(y), NA_character_, sQuote(y)), quote=quote, ...)
invisible(x)
}
rep.numeric_version <-
function(x, ...)
structure(NextMethod("rep"), class = oldClass(x))
unique.numeric_version <-
function(x, incomparables = FALSE, ...)
x[!duplicated(x, incomparables, ...)]
xtfrm.numeric_version <-
function(x)
{
x <- .encode_numeric_version(x)
NextMethod("xtfrm")
}
## <NOTE>
## Versions of R prior to 2.6.0 had only a package_version class.
## We now have package_version extend numeric_version.
## We only provide named subscripting for package versions.
## </NOTE>
`$.package_version` <-
function(x, name)
{
name <- pmatch(name, c("major", "minor", "patchlevel"))
x <- unclass(x)
switch(name,
major = vapply(x, `[`, 0L, 1L),
minor = vapply(x, `[`, 0L, 2L),
patchlevel = vapply(x, `[`, 0L, 3L))
}