[Rd] rawToChar(raw(0))

Henrik Bengtsson hb at stat.berkeley.edu
Wed May 21 19:12:05 CEST 2008


Hi,

right now we have (on R v2.7.0 patched (2008-04-23 r45466)) that:

> rawToChar(raw(0))
[1] ""
> rawToChar(raw(0), multiple=TRUE)
character(0)

Is this intended or should both return character(0)?  Personally, I
would prefer that an empty input vector returns an empty output
vector.  Same should then apply to charToRaw(), but right now we get:

> x <- character(0)
> charToRaw(x)
Error in charToRaw(x) : argument must be a character vector of length 1

I am aware of the new rules for R v2.8.0dev on truncating nuls in
character strings.  That would make charToRaw(x) return the same
regardless of x==character(0) or x=="" ("\0").  We have with R
v2.8.0dev:

> x <- ""
> charToRaw(x)
raw(0)

Is guess this is on purpose because "" == "<nul>" and "a" == "a<nul>",
but what would happen if one use the special rule that "<nul>" returns
as.raw(0) instead?  Would that break much downstream?

If one have it return as.raw(0), and add the rule that an empty input
returns an empty output, then charToRaw(rawToChar(x)) [and vice versa
rawToChar(charToRaw(x))] would always return 'x'.  With R v2.7.0 we
now have:

> x <- raw(0)
> identical(x, charToRaw(rawToChar(x)))
[1] TRUE

> x <- as.raw(0)
> identical(x, charToRaw(rawToChar(x)))
[1] TRUE

but the latter will be FALSE in R v2.8.0 (because of the new rules on '\0'):

> x <- as.raw(0)
> identical(x, charToRaw(rawToChar(x)))
[1] FALSE
Warning message:
In rawToChar(x) : truncating string with embedded nul: '\0'


Just some thoughts.

Cheers

Henrik



More information about the R-devel mailing list