Skip to content

Commit

Permalink
Add unicode option to as.yaml (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
viking committed Jan 17, 2013
1 parent 29e2022 commit 3774e67
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/R/as.yaml.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
`as.yaml` <-
function(x, line.sep = c('\n', '\r\n', '\r'), indent = 2, omap = FALSE, column.major = TRUE) {
function(x, line.sep = c('\n', '\r\n', '\r'), indent = 2, omap = FALSE, column.major = TRUE, unicode = TRUE) {
line.sep <- match.arg(line.sep)
.Call("as.yaml", x, line.sep, indent, omap, column.major, PACKAGE="yaml")
.Call("as.yaml", x, line.sep, indent, omap, column.major, unicode, PACKAGE="yaml")
}
6 changes: 6 additions & 0 deletions pkg/inst/tests/test_as_yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,9 @@ test_that("test should escape strings", {
result <- as.yaml("12345")
expect_equal("'12345'\n", result)
})

test_that("unicode strings are not escaped", {
x <- list('име' = 'Александар', 'презиме' = 'Благотић')
result <- as.yaml(x, unicode = TRUE)
expect_equal("име: Александар\nпрезиме: Благотић\n", result, label = result)
})
3 changes: 2 additions & 1 deletion pkg/man/as.yaml.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
Convert an R object into a YAML string
}
\usage{
as.yaml(x, line.sep = c("\n", "\r\n", "\r"), indent = 2, omap = FALSE, column.major = TRUE)
as.yaml(x, line.sep = c("\n", "\r\n", "\r"), indent = 2, omap = FALSE, column.major = TRUE, unicode = TRUE)
}
\arguments{
\item{x}{ the object to be converted }
\item{line.sep}{ the line separator character(s) to use }
\item{indent}{ the number of spaces to use for indenting }
\item{omap}{ determines whether or not to convert a list to a YAML omap; see Details }
\item{column.major}{ determines how to convert a data.frame; see Details }
\item{unicode}{ determines whether or not to allow unescaped unicode characters in output }
}
\details{
If you set the \code{omap} option to TRUE, as.yaml will create ordered maps
Expand Down
16 changes: 12 additions & 4 deletions pkg/src/r-ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1729,18 +1729,19 @@ emit_object(emitter, event, obj, tag, omap, column_major)
}

SEXP
as_yaml(s_obj, s_line_sep, s_indent, s_omap, s_column_major)
as_yaml(s_obj, s_line_sep, s_indent, s_omap, s_column_major, s_unicode)
SEXP s_obj;
SEXP s_line_sep;
SEXP s_indent;
SEXP s_omap;
SEXP s_column_major;
SEXP s_unicode;
{
SEXP retval;
yaml_emitter_t emitter;
yaml_event_t event;
s_emitter_output output;
int status, line_sep, indent, omap, column_major;
int status, line_sep, indent, omap, column_major, unicode;
const char *c_line_sep;

c_line_sep = CHAR(STRING_ELT(s_line_sep, 0));
Expand Down Expand Up @@ -1789,7 +1790,14 @@ as_yaml(s_obj, s_line_sep, s_indent, s_omap, s_column_major)
}
column_major = LOGICAL(s_column_major)[0];

if (!isLogical(s_unicode) || length(s_unicode) != 1) {
error("argument `unicode` must be either TRUE or FALSE");
return R_NilValue;
}
unicode = LOGICAL(s_unicode)[0];

yaml_emitter_initialize(&emitter);
yaml_emitter_set_unicode(&emitter, unicode);
yaml_emitter_set_break(&emitter, line_sep);
yaml_emitter_set_indent(&emitter, indent);

Expand All @@ -1798,7 +1806,7 @@ as_yaml(s_obj, s_line_sep, s_indent, s_omap, s_column_major)
yaml_emitter_set_output(&emitter, as_yaml_write_handler, &output);

/* FIXME: get the encoding from R */
yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
yaml_stream_start_event_initialize(&event, YAML_ANY_ENCODING);
if (!(status = yaml_emitter_emit(&emitter, &event)))
goto done;

Expand Down Expand Up @@ -1842,7 +1850,7 @@ as_yaml(s_obj, s_line_sep, s_indent, s_omap, s_column_major)

R_CallMethodDef callMethods[] = {
{"yaml.load", (DL_FUNC)&load_yaml_str, 3},
{"as.yaml", (DL_FUNC)&as_yaml, 5},
{"as.yaml", (DL_FUNC)&as_yaml, 6},
{NULL, NULL, 0}
};

Expand Down

0 comments on commit 3774e67

Please sign in to comment.