Skip to content

Commit

Permalink
Merge pull request #485 from cynkra/b-481-encoding
Browse files Browse the repository at this point in the history
fix encoding issue
  • Loading branch information
moodymudskipper authored Sep 19, 2024
2 parents 7402060 + bff5e5c commit e15055a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 210 deletions.
2 changes: 1 addition & 1 deletion R/character-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ construct_string <- function(x, unicode_representation, escape, mode = "string",

encoding <- Encoding(x)
locale_is_utf8 <- l10n_info()$`UTF-8`
if (!locale_is_utf8) {
if (encoding == "latin1" || !locale_is_utf8) {
return(construct_string_from_byte_value(x, encoding, mode, protect))
}

Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/_snaps/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,22 @@
y = "loooooooooooooooooooooooooooooooooong_enough_for_multiline_output"
)

# non UTF-8 encodings with UTF-8 system

Code
x <- iconv("hello world", to = "latin1")
construct(x)
Output
"hello\xa0world" |> (`Encoding<-`)("latin1")
Code
x <- iconv("こんにちは", to = "shift_jis")
construct(x)
Output
"\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd"
Code
x <- "hello world"
Encoding(x) <- "latin1"
construct(x)
Output
"hello\xc2\xa0world" |> (`Encoding<-`)("latin1")

136 changes: 0 additions & 136 deletions tests/testthat/_snaps/s3-R6.new.md

This file was deleted.

73 changes: 0 additions & 73 deletions tests/testthat/_snaps/s3-R6ClassGenerator.new.md

This file was deleted.

13 changes: 13 additions & 0 deletions tests/testthat/test-encoding.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ test_that("Encoding", {
))
)
})

test_that("non UTF-8 encodings with UTF-8 system", {
skip_if(!l10n_info()$`UTF-8`)
expect_snapshot({
x <- iconv("hello\U{A0}world", to = "latin1")
construct(x)
x <- iconv("こんにちは", to = "shift_jis")
construct(x)
x <- "hello\U{A0}world"
Encoding(x) <- "latin1"
construct(x)
})
})

0 comments on commit e15055a

Please sign in to comment.