File: accessors.R

package info (click to toggle)
r-cran-urltools 1.7.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 516 kB
  • sloc: cpp: 1,234; ansic: 303; sh: 13; makefile: 2
file content (237 lines) | stat: -rw-r--r-- 6,717 bytes parent folder | download | duplicates (2)
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
#'@title Get or set a URL's scheme
#'@description as in the lubridate package, individual components of a URL
#'can be both extracted or set using the relevant function call - see the
#'examples.
#'@aliases scheme
#'@rdname scheme
#'
#'@param x a URL, or vector of URLs
#'
#'@param value a replacement value (or vector of replacement values)
#'for x's scheme.
#'
#'@seealso \code{\link{domain}}, \code{\link{port}}, \code{\link{path}},
#'\code{\link{parameters}} and \code{\link{fragment}} for other accessors.
#'
#'@examples
#'#Get a component
#'example_url <- "http://cran.r-project.org/submit.html"
#'scheme(example_url)
#'
#'#Set a component
#'scheme(example_url) <- "https"
#'
#'# NA out the URL
#'scheme(example_url) <- NA_character_
#'@import methods
#'@export
scheme <- function(x){
  return(get_component_(x,0))
}

"scheme<-" <- function(x, value) standardGeneric("scheme<-")
#'@rdname scheme
#'@export
setGeneric("scheme<-", useAsDefault = function(x, value){
  if(length(value) == 0 && is.null(value)){
    return(rm_component_(x, 0))
  }
  return(set_component_r(x, 0, value, "://"))
})

#'@title Get or set a URL's domain
#'@description as in the lubridate package, individual components of a URL
#'can be both extracted or set using the relevant function call - see the
#'examples.
#'@aliases domain
#'@rdname domain
#'
#'@param x a URL, or vector of URLs
#'
#'@param value a replacement value (or vector of replacement values)
#'for x's scheme.
#'
#'@seealso \code{\link{scheme}}, \code{\link{port}}, \code{\link{path}},
#'\code{\link{parameters}} and \code{\link{fragment}} for other accessors.
#'
#'@examples
#'#Get a component
#'example_url <- "http://cran.r-project.org/submit.html"
#'domain(example_url)
#'
#'#Set a component
#'domain(example_url) <- "en.wikipedia.org"
#'@export
domain <- function(x){
  return(get_component_(x,1))
}
"domain<-" <- function(x, value) standardGeneric("domain<-")
#'@rdname domain
#'@export
setGeneric("domain<-", useAsDefault = function(x, value){
  if(length(value) == 0 && is.null(value)){
    return(rm_component_(x, 1))
  }
  return(set_component_(x, 1, value))
})

#'@title Get or set a URL's port
#'@description as in the lubridate package, individual components of a URL
#'can be both extracted or set using the relevant function call - see the
#'examples.
#'
#'@aliases port
#'@rdname port
#'
#'@param x a URL, or vector of URLs
#'
#'@param value a replacement value (or vector of replacement values)
#'for x's port. If NULL, the port will be entirely removed.
#'
#'@seealso \code{\link{scheme}}, \code{\link{domain}}, \code{\link{path}},
#'\code{\link{parameters}} and \code{\link{fragment}} for other accessors.
#'
#'@examples
#'# Get the port
#'example_url <- "http://cran.r-project.org:80/submit.html"
#'port(example_url)
#'
#'# Set the port
#'port(example_url) <- "12"
#'
#'# Remove the port
#'port(example_url) <- NULL
#'@export
port <- function(x){
  return(get_component_(x,2))
}
"port<-" <- function(x, value) standardGeneric("port<-")
#'@rdname port
#'@export
setGeneric("port<-", useAsDefault = function(x, value){
  if(length(value) == 0 && is.null(value)){
    return(rm_component_(x, 2))
  }
  return(set_component_f(x, 2, value, ":"))
})

#'@title Get or set a URL's path
#'@description as in the lubridate package, individual components of a URL
#'can be both extracted or set using the relevant function call - see the
#'examples.
#'@aliases path
#'@rdname path
#'
#'@param x a URL, or vector of URLs
#'
#'@param value a replacement value (or vector of replacement values)
#'for x's path. If NULL, the path will be removed entirely.
#'
#'@seealso \code{\link{scheme}}, \code{\link{domain}}, \code{\link{port}},
#'\code{\link{parameters}} and \code{\link{fragment}} for other accessors.
#'
#'@examples
#'# Get the path
#'example_url <- "http://cran.r-project.org:80/submit.html"
#'path(example_url)
#'
#'# Set the path
#'path(example_url) <- "bin/windows/"
#'
#'# Remove the path
#'path(example_url) <- NULL
#'@export
path <- function(x){
  return(get_component_(x,3))
}
"path<-" <- function(x, value) standardGeneric("path<-")
#'@rdname path
#'@export
setGeneric("path<-", useAsDefault = function(x, value){
  if(length(value) == 0 && is.null(value)){
    return(rm_component_(x, 3))
  }
  return(set_component_f(x, 3, value, "/"))
})

#'@title Get or set a URL's parameters
#'@description as in the lubridate package, individual components of a URL
#'can be both extracted or set using the relevant function call - see the
#'examples.
#'
#'@aliases parameters
#'@rdname parameters
#'
#'@param x a URL, or vector of URLs
#'
#'@param value a replacement value (or vector of replacement values)
#'for x's parameters. If NULL, the parameters will be removed entirely.
#'
#'@seealso \code{\link{scheme}}, \code{\link{domain}}, \code{\link{port}},
#'\code{\link{path}} and \code{\link{fragment}} for other accessors.
#'
#'@examples
#'# Get the parameters
#'example_url <- "http://en.wikipedia.org/wiki/Aaron_Halfaker?debug=true"
#'parameters(example_url)
#'
#'# Set the parameters
#'parameters(example_url) <- "debug=false"
#'
#'# Remove the parameters
#'parameters(example_url) <- NULL
#'@export
parameters <- function(x){
  return(get_component_(x,4))
}
"parameters<-" <- function(x, value) standardGeneric("parameters<-")
#'@rdname parameters
#'@export
setGeneric("parameters<-", useAsDefault = function(x, value){
  if(length(value) == 0 && is.null(value)){
    return(rm_component_(x, 4))
  }
  return(set_component_f(x, 4, value, "?"))
})

#'@title Get or set a URL's fragment
#'@description as in the lubridate package, individual components of a URL
#'can be both extracted or set using the relevant function call - see the
#'examples.
#'@aliases fragment
#'@rdname fragment
#'
#'@param x a URL, or vector of URLs
#'
#'@param value a replacement value (or vector of replacement values)
#'for x's fragment. If NULL, the fragment will be removed entirely.
#'
#'@seealso \code{\link{scheme}}, \code{\link{domain}}, \code{\link{port}},
#'\code{\link{path}} and \code{\link{parameters}} for other accessors.
#'
#'@examples
#'#Get a component
#'example_url <- "http://en.wikipedia.org/wiki/Aaron_Halfaker?debug=true#test"
#'fragment(example_url)
#'
#'#Set a component
#'fragment(example_url) <- "production"
#'
#'#Remove a component
#'fragment(example_url) <- NULL
#'@export
#'@rdname fragment
#'@export
fragment <- function(x){
  return(get_component_(x,5))
}

"fragment<-" <- function(x, value) standardGeneric("fragment<-")
#'@rdname fragment
#'@export
setGeneric("fragment<-", useAsDefault = function(x, value){
  if(length(value) == 0 && is.null(value)){
    return(rm_component_(x, 5))
  }
  return(set_component_f(x, 5, value, "#"))
})