From 7256cba34fa2aa0607fed6abbfa5e0dcffb8e4d1 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Fri, 18 Sep 2020 10:14:45 -0700 Subject: [PATCH 1/4] convert strings to UTF-8 before converting --- .gitignore | 5 +++++ R/as.yaml.R | 3 ++- R/utils.R | 12 ++++++++++++ inst/tests/test_as_yaml.R | 6 ++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 R/utils.R diff --git a/.gitignore b/.gitignore index dfa5c22..c990e55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,10 @@ build Session.vim yaml_*.tar.gz .Renv-version +*.Rproj *.o *.so +*.dll +.Rproj.user +.Rbuildignore +.Rhistory diff --git a/R/as.yaml.R b/R/as.yaml.R index 5cd2e33..4969d15 100644 --- a/R/as.yaml.R +++ b/R/as.yaml.R @@ -3,10 +3,11 @@ function(x, line.sep = c('\n', '\r\n', '\r'), indent = 2, omap = FALSE, column.major = TRUE, unicode = TRUE, precision = getOption('digits'), indent.mapping.sequence = FALSE, handlers = NULL) { + x <- as.utf8(x) line.sep <- match.arg(line.sep) res <- .Call(C_serialize_to_yaml, x, line.sep, indent, omap, column.major, unicode, precision, indent.mapping.sequence, handlers, - PACKAGE="yaml") + PACKAGE = "yaml") Encoding(res) <- "UTF-8" res } diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..0a2c3cc --- /dev/null +++ b/R/utils.R @@ -0,0 +1,12 @@ +`as.utf8` <- +function(x) { + + if (is.character(x)) { + enc2utf8(x) + } else if (is.list(x)) { + lapply(x, as.utf8) + } else { + x + } + +} \ No newline at end of file diff --git a/inst/tests/test_as_yaml.R b/inst/tests/test_as_yaml.R index e66eca1..b64118f 100644 --- a/inst/tests/test_as_yaml.R +++ b/inst/tests/test_as_yaml.R @@ -440,3 +440,9 @@ test_custom_tag_for_unnamed_list <- function() { result <- as.yaml(x) checkEquals(expected, result) } + +test_latin1_strings <- function() { + data <- list(description = enc2native("á")) + result <- as.yaml(data) + checkEquals(result, enc2utf8("description: á\n")) +} \ No newline at end of file From 5be7bcd8025b2fe31d70aa0f86b7d11eda6adafa Mon Sep 17 00:00:00 2001 From: Shawn Garbett Date: Mon, 13 Sep 2021 11:59:50 -0500 Subject: [PATCH 2/4] Modified as.utf8 pull request from kevinushey to be in Makefile --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 85a93a4..e870822 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,7 @@ SRCS = src/yaml_private.h \ LICENSE \ R/yaml.load.R \ R/zzz.R \ + R/utils.R \ R/yaml.load_file.R \ R/as.yaml.R \ R/read_yaml.R \ @@ -77,6 +78,7 @@ BUILD_SRCS = build/yaml/src/yaml_private.h \ build/yaml/LICENSE \ build/yaml/R/yaml.load.R \ build/yaml/R/zzz.R \ + build/yaml/R/utils.R \ build/yaml/R/yaml.load_file.R \ build/yaml/R/as.yaml.R \ build/yaml/R/read_yaml.R \ From 8bb78da4ac7a95cf9467b4a76deb4c7fb4f0834b Mon Sep 17 00:00:00 2001 From: Shawn Garbett Date: Mon, 13 Sep 2021 12:17:00 -0500 Subject: [PATCH 3/4] Modified as.utf8 to preserve attributes --- R/utils.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/R/utils.R b/R/utils.R index 0a2c3cc..e0e8f1a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,12 +1,14 @@ `as.utf8` <- function(x) { - if (is.character(x)) { - enc2utf8(x) + coded <- enc2utf8(x) + attributes(coded) <- attributes(x) + coded } else if (is.list(x)) { - lapply(x, as.utf8) + coded <- lapply(x, as.utf8) + attributes(coded) <- attributes(x) + coded } else { x } - } \ No newline at end of file From 1a90ba9c1c8815fdaaaf819f8843e99d8cbe54eb Mon Sep 17 00:00:00 2001 From: Shawn Garbett Date: Mon, 13 Sep 2021 14:21:01 -0500 Subject: [PATCH 4/4] Added test of issue #105 --- inst/tests/test_as_yaml.R | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/inst/tests/test_as_yaml.R b/inst/tests/test_as_yaml.R index b64118f..52e23c1 100644 --- a/inst/tests/test_as_yaml.R +++ b/inst/tests/test_as_yaml.R @@ -444,5 +444,12 @@ test_custom_tag_for_unnamed_list <- function() { test_latin1_strings <- function() { data <- list(description = enc2native("á")) result <- as.yaml(data) - checkEquals(result, enc2utf8("description: á\n")) -} \ No newline at end of file + checkEquals(result, "description: á\n") +} + +test_unknown_strings <- function() { + data <- list(x="\x9b") + result <- as.yaml(data) + checkEquals(result, "x: <9b>\n") +} +