-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
158 lines (108 loc) · 3.47 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# b64 <img src="man/figures/logo.svg" align="right" height="139" alt="" />
<!-- badges: start -->
[![R-CMD-check](https://github.com/extendr/b64/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/extendr/b64/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of b64 is to provide a very fast, lightweight, and vectorized base64 encoder and decoder.
## Installation
You can install the development version of b64 like so:
```r
pak::pak("extendr/b64")
```
## Example
Encode to base64 using `encode()`.
```{r example}
library(b64)
hello <- encode("Hello, from extendr")
hello
```
Decode using `decode()`. Note that the returned object will always have the `"blob"` class. To achieve 0 dependencies, the `blob` package is only listed as a suggested dependency but if you attach it, its print method will be used.
```{r}
library(blob)
decoded <- decode(hello)
decoded
```
We can convert the decoded base64 to characters and see how it worked.
```{r}
rawToChar(decoded[[1]])
```
### Vectorized
Both `encode()` and `decode()` are vectorized.
```{r}
lorem <- unlist(lorem::ipsum(5, 1, 5))
lorem
encoded <- encode(lorem)
encoded
```
We can decode all of these using `decode()` as well.
```{r}
decode(encoded)
```
## Encoding and decoding files
`b64` shines when encoding and decoding files. `encode_file()` and `decode_file()` both work by reading a file as a stream making it far faster than the alternative.
```{r message = FALSE, warn = FALSE}
tmp <- tempfile()
fp <- "https://github.com/datablist/sample-csv-files/raw/main/files/leads/leads-100000.csv"
download.file(fp, tmp)
bench::mark(
b64 = encode_file(tmp),
base64enc = base64enc::base64encode(tmp)
)
```
While the encoding is very impressive, better yet is the decoding performance.
```{r}
# create a temp file
tmp2 <- tempfile()
# encode it and write to tmep file
encode_file(tmp) |>
charToRaw() |>
writeBin(tmp2)
bench::mark(
b64 = decode_file(tmp2),
base64enc = base64enc::base64decode(file(tmp2))
)
```
## Alternative engines
Out of the box, `b64` provides a number of pre-configured engines that can be used. The function `engine()` allows you to choose one of these different engines For example, `engine("url_safe")` provides a standard engine that uses a url-safe alphabet with padding.
```{r}
url_engine <- engine("url_safe")
url_safe_encoded <- encode("\xfa\xec U", url_engine)
url_safe_encoded
```
If we try to decode this using the standard engine, we will encounter an error.
```{r error=TRUE}
decode(url_safe_encoded)
```
We can use our new engine to decode it.
```{r}
decode(url_safe_encoded, url_engine)
```
### Custom Engines
We can create custom engines with `new_engine()`. This allows us to provide our on alphabet and configuration.
We can use one of the many predefined alphabets or create one our selves with `new_alphabet()`. We can also specify our engine config using `new_config()` which lets us choose whether or not to pad and how to handle decoding.
```{r}
my_eng <- new_engine(
alphabet("crypt"),
new_config(TRUE, TRUE, "none")
)
```
This engine can be used to encode or decode text.
```{r}
txt <- "lorem ipsum sit dolor amet"
encode(txt, my_eng)
```
Compare this to the standard encoder:
```{r}
encode(txt)
```