Debian Codegraph @

R/fs_bytes.R

raw
1 units <- c(
2 'B' = 1,
3 'K' = 1024,
4 'M' = 1024^2,
5 'G' = 1024^3,
6 'T' = 1024^4,
7 'P' = 1024^5,
8 'E' = 1024^6,
9 'Z' = 1024^7,
10 'Y' = 1024^8
11 )
12
13 #' Human readable file sizes
14 #'
15 #' Construct, manipulate and display vectors of file sizes. These are numeric
16 #' vectors, so you can compare them numerically, but they can also be compared
17 #' to human readable values such as '10MB'.
18 #'
19 #' @param x A numeric or character vector. Character representations can use
20 #' shorthand sizes (see examples).
21 #' @examples
22 #' fs_bytes("1")
23 #' fs_bytes("1K")
24 #' fs_bytes("1Kb")
25 #' fs_bytes("1Kib")
26 #' fs_bytes("1MB")
27 #'
28 #' fs_bytes("1KB") < "1MB"
29 #'
30 #' sum(fs_bytes(c("1MB", "5MB", "500KB")))
31 #' @name fs_bytes
32 #' @export
33 as_fs_bytes <- function(x) {
34 UseMethod("as_fs_bytes")
35 }
36
37 #' @export
38 #' @rdname fs_bytes
39 fs_bytes <- as_fs_bytes
40
41 new_fs_bytes <- function(x) {
42 class(x) <- c("fs_bytes", "numeric")
43 x
44 }
45 setOldClass(c("fs_bytes", "numeric"), numeric())
46
47 #' @export
48 as_fs_bytes.default <- function(x = numeric()) {
49 if (length(x) == 0) {
50 return(new_fs_bytes(numeric()))
51 }
52 x <- as.character(x)
53 m <- captures(
54 x,
55 regexpr(
56 "^(?<size>[[:digit:].]+)\\s*(?<unit>[KMGTPEZY]?)i?[Bb]?$",
57 x,
58 perl = TRUE
59 )
60 )
61 m$unit[m$unit == ""] <- "B"
62 new_fs_bytes(unname(as.numeric(m$size) * units[m$unit]))
63 }
64
65 #' @export
66 as_fs_bytes.fs_bytes <- function(x) {
67 return(x)
68 }
69
70 #' @export
71 as_fs_bytes.numeric <- function(x) {
72 new_fs_bytes(x)
73 }
74
75 # Adapted from https://github.com/gaborcsardi/prettyunits
76 # Aims to be consistent with ls -lh, so uses 1024 KiB units, 3 or less digits etc.
77 #' @export
78 format.fs_bytes <- function(
79 x,
80 scientific = FALSE,
81 digits = 3,
82 drop0trailing = TRUE,
83 ...
84 ) {
85 bytes <- unclass(x)
86
87 exponent <- pmin(floor(log(bytes, 1024)), length(units) - 1)
88 res <- round(bytes / 1024^exponent, 2)
89 unit <- ifelse(exponent == 0, "", names(units)[exponent + 1])
90
91 ## Zero bytes
92 res[bytes == 0] <- 0
93 unit[bytes == 0] <- ""
94
95 ## NA and NaN bytes
96 res[is.na(bytes)] <- NA_real_
97 res[is.nan(bytes)] <- NaN
98 unit[is.na(bytes)] <- "" # Includes NaN as well
99
100 res <- format(
101 res,
102 scientific = scientific,
103 digits = digits,
104 drop0trailing = drop0trailing,
105 ...
106 )
107
108 paste0(res, unit)
109 }
110
111 #' @export
112 as.character.fs_bytes <- format.fs_bytes
113
114 #' @export
115 print.fs_bytes <- function(x, ...) {
116 cat(format.fs_bytes(x, ...))
117 }
118
119 #' @export
120 sum.fs_bytes <- function(x, ...) {
121 new_fs_bytes(NextMethod())
122 }
123
124 #' @export
125 min.fs_bytes <- function(x, ...) {
126 new_fs_bytes(NextMethod())
127 }
128
129 #' @export
130 max.fs_bytes <- function(x, ...) {
131 new_fs_bytes(NextMethod())
132 }
133
134 #' @export
135 `[.fs_bytes` <- function(x, i, ...) {
136 new_fs_bytes(NextMethod("["))
137 }
138
139 #' @export
140 `[[.fs_bytes` <- function(x, i, ...) {
141 new_fs_bytes(NextMethod("[["))
142 }
143
144 #' @export
145 # Adapted from Ops.numeric_version
146 Ops.fs_bytes <- function(e1, e2) {
147 if (nargs() == 1L) {
148 stop(
149 sprintf("unary '%s' not defined for \"fs_bytes\" objects", .Generic),
150 call. = FALSE
151 )
152 }
153
154 boolean <- switch(
155 .Generic,
156 `+` = TRUE,
157 `-` = TRUE,
158 `*` = TRUE,
159 `/` = TRUE,
160 `^` = TRUE,
161 `<` = TRUE,
162 `>` = TRUE,
163 `==` = TRUE,
164 `!=` = TRUE,
165 `<=` = TRUE,
166 `>=` = TRUE,
167 FALSE
168 )
169 if (!boolean) {
170 stop(
171 sprintf("'%s' not defined for \"fs_bytes\" objects", .Generic),
172 call. = FALSE
173 )
174 }
175 e1 <- as_fs_bytes(e1)
176 e2 <- as_fs_bytes(e2)
177 NextMethod(.Generic)
178 }
179
180 pillar_shaft.fs_bytes <- function(x, ...) {
181 pillar::new_pillar_shaft_simple(format.fs_bytes(x), align = "right", ...)
182 }
183
184 type_sum.fs_bytes <- function(x) {
185 "fs::bytes"
186 }
187
188 # All functions below registered in .onLoad
189
190 vec_ptype2.fs_bytes.fs_bytes <- function(x, y, ...) {
191 x
192 }
193 vec_ptype2.fs_bytes.double <- function(x, y, ...) {
194 x
195 }
196 vec_ptype2.double.fs_bytes <- function(x, y, ...) {
197 y
198 }
199
200 # Note order of class is the opposite as for ptype2
201 vec_cast.fs_bytes.fs_bytes <- function(x, to, ...) {
202 x
203 }
204 vec_cast.fs_bytes.double <- function(x, to, ...) {
205 as_fs_bytes(x)
206 }
207 vec_cast.double.fs_bytes <- function(x, to, ...) {
208 unclass(x)
209 }
210
211 vec_ptype2.fs_bytes.integer <- function(x, y, ...) {
212 x
213 }
214 vec_ptype2.integer.fs_bytes <- function(x, y, ...) {
215 y
216 }
217
218 vec_cast.fs_bytes.integer <- function(x, to, ...) {
219 as_fs_bytes(x)
220 }
221 vec_cast.integer.fs_bytes <- function(x, to, ...) {
222 unclass(x)
223 }