The other thing that commonly trips people up - beyond how base string manipulation functionality in C++ is - is how to convert between types. C++ is a strictly-typed language, and when you combine R and C++ and an underlying C++ or C library you get..well, a mess.

One of the things that rope provides is some simple wrapper functions for converting between types, pre-C++11. These aren't all-encompassing, but should make it easier to handle the situation where you have a char from some C function you're calling and really need it to be an actual string.

Alongside that, rope also contains functions to check whether strings are alphabetic, numeric, hexadecimal or alphanumeric, to provide a degree of safety and sanity to attempts at conversion.

Type checking

```{Rcpp eval = FALSE} bool is_alphanumeric(std::string str) std::vector < bool > is_alphanumeric(std::vector < std::string > strs) bool is_alpha(std::string str) std::vector < bool > is_alpha(std::vector < std::string > strs) bool is_numeric(std::string str) std::vector < bool > is_alphanumeric(std::vector < std::string > strs) bool is_numeric(std::string str) std::vector < bool > is_alphanumeric(std::vector < std::string > strs) bool is_hex(std::string str) std::vector < bool > is_hex(std::vector < std::string > strs)

If you're planning to do type conversion, or just verify that certain operations are workable, it's useful to have the ability to check.
`rope` provides functions to check whether a string is alphanumeric, alphabetic, numeric or hexadecimal before converting or manipulating them - or just to check they look like you'd expect them to look. These are fully vectorised if that's the route you want to take,
and return simple true or false values.

## Type conversion

C++ is a strongly-typed language; you can't just convert an integer to a string and be done with it! But sometimes you have to, and it's useful to have convenient wrappers that let you perform such an operation.

### Strings to integers
```{Rcpp eval = FALSE}
int as_int(std::string str)
std::vector < int > as_int(std::vector < std::string > strs)

Integers are the same in R and C++ (in fact, R integer values are C integer values), and these functions let you take a string ("12") and turn it into an integer(12). A word of warning: if you throw something in that isn't an integer ("12.3", or "rutabaga") it'll return either 0, indicating that the conversion failed entirely, or the value represented by the characters prior to the first non-numeric character. As illustrations, the example strings would produce integers of 12 and 0 respectively. What it won't do is actively throw an error; this will be worked on.

Strings to doubles

```{Rcpp eval = FALSE} double as_double(std::string str) std::vector < double > as_double(std::vector < std::string > strs)

C++'s `double` values are the same as R's `numeric`; they're floating-point values like 3.141593 (yum. Pie.) `as_double` lets you
take strings and convert them to doubles. The same warning as with `as_integer` applies.

## Integers or doubles to strings

```{Rcpp eval = FALSE}
std::string as_string(int x)
std::vector < std::string > as_string(std::vector < int > x)
std::string as_string(double x)
std::vector < std::string > as_string(std::vector < double > x)

Conversions in the other direction work too, and are identical to string-to-X conversions with the difference that you really can't have a numeric value that fails to translate to a string value. So, no warnings (so far) there.

Chars to strings

{Rcpp eval = FALSE} std::string as_string(char x) std::vector < std::string > as_string(std::vector < char > x)

Lots of C code that you might have to integrate with doesn't use strings, since those are a C++ addition: instead they use the char type, a singularly annoying pain to play with. One of the as_string implementations contains a converter from char to strings, and works perfectly happily; again, it's hard to put something in a char that isn't acceptable in a string, so there are no caveats here.



PeteHaitch/rope documentation built on May 8, 2019, 1:32 a.m.