From 3774e67717943964591bc04787c1bbc33ad33294 Mon Sep 17 00:00:00 2001 From: Jeremy Stephens Date: Thu, 17 Jan 2013 17:54:18 -0600 Subject: [PATCH] Add unicode option to as.yaml (fixes #4) --- pkg/R/as.yaml.R | 4 ++-- pkg/inst/tests/test_as_yaml.R | 6 ++++++ pkg/man/as.yaml.Rd | 3 ++- pkg/src/r-ext.c | 16 ++++++++++++---- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/R/as.yaml.R b/pkg/R/as.yaml.R index 38a55e2..b92c0c1 100644 --- a/pkg/R/as.yaml.R +++ b/pkg/R/as.yaml.R @@ -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") } diff --git a/pkg/inst/tests/test_as_yaml.R b/pkg/inst/tests/test_as_yaml.R index a275911..640da7d 100644 --- a/pkg/inst/tests/test_as_yaml.R +++ b/pkg/inst/tests/test_as_yaml.R @@ -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) +}) diff --git a/pkg/man/as.yaml.Rd b/pkg/man/as.yaml.Rd index 683274f..e78ceb2 100644 --- a/pkg/man/as.yaml.Rd +++ b/pkg/man/as.yaml.Rd @@ -5,7 +5,7 @@ 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 } @@ -13,6 +13,7 @@ \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 diff --git a/pkg/src/r-ext.c b/pkg/src/r-ext.c index 9b93b17..4b761e0 100644 --- a/pkg/src/r-ext.c +++ b/pkg/src/r-ext.c @@ -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)); @@ -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); @@ -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; @@ -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} };