Learn R Programming

oro.dicom (version 0.3.7)

subRaw: Pattern Replacement for Raw Vectors

Description

subRaw searchs for substring pattern within a raw vector x and replaces it if found with replacement.

Usage

subRaw(pattern, replacement, x, offset=1L, ignore.case=FALSE,
       fixed=TRUE, all=FALSE)

Arguments

pattern
raw vector containing the pattern to be matched and replaced by replacement.

This is passed to grepRaw, which supports the use of regular expressions in pattern if

replacement
a replacement for matched pattern.
x
a raw vector where matches are sought, or an object which can be coerced by charToRaw to a raw vector.
offset
An integer passed to grepRaw specifying the offset from which the search should start. Must be positive. The beginning of line is defined to be at that offset so "^" will match there. If all, offset only applies to
ignore.case
logical passed to grepRaw: if 'FALSE', the pattern matching is _case sensitive_ and if 'TRUE', case is ignored during matching.
fixed
logical passed to grepRaw: If 'TRUE', 'pattern' is a pattern to be matched as is.

NOTE: The default is TRUE here, even though the default for the fixed argument in grepRaw is FALSE.

all
logical. If 'TRUE' all matches are returned, otherwise just the first one.

Value

  • a raw vector matching x except with any subsequence of bytes matching pattern replaced by replacement.

Details

1. found <- grepRaw(pattern, x=x, offset=offset, ignore.case=ignore.case, fixed=fixed)

2. if(length(found)>0) find the match and replace x with c(x[1:(start-1)], replace, c[(end+1):length(x)]). Also, if(all) recurse.

3. return(x)

See Also

array, readDICOM, storage.mode

Examples

Run this code
##
## example
##
abcdef <- charToRaw('abcdef')
ab. <- subRaw(charToRaw('ab'), charToRaw('d'), abcdef)

# replace first two bytes
stopifnot(
all.equal(ab., charToRaw('dcdef') )
)

# replace lst two bytes
stopifnot(
all.equal(subRaw(charToRaw('ef'), charToRaw('g'), abcdef),
          charToRaw('abcdg') )
)

# replace nothing
stopifnot(
all.equal(subRaw(charToRaw('noop'), charToRaw('c'), abcdef),
          abcdef )
)

# replace all
ababab <- charToRaw('ababab')
ab3 <- subRaw(charToRaw('ab'), charToRaw('g'), ababab,
               all=TRUE)

stopifnot(
all.equal(ab3, charToRaw('ggg') )
)

Run the code above in your browser using DataLab