Skip to content

Commit

Permalink
fix #1557: readLines(textConnection()) may mangle characters in split…
Browse files Browse the repository at this point in the history
…_lines()
  • Loading branch information
yihui committed Jun 13, 2018
1 parent 85415e4 commit d2634f9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.20.3
Version: 1.20.4
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Adam", "Vogt", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- The `sql` engine now caches the result properly when the chunk option `output.var` is specified (thanks, @yutannihilation, #1544).

- `knit_params()` mangles UTF-8 text not representable in current locale (thanks, @kevinushey, #1557).

# CHANGES IN knitr VERSION 1.20

## NEW FEATURES
Expand Down
5 changes: 2 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,8 @@ read_rforge = function(path, project, extra = '') {
# because I think strsplit('', 'foo') should return '' instead of character(0)
split_lines = function(x) {
if (length(grep('\n', x)) == 0L) return(x)
con = textConnection(x)
on.exit(close(con))
readLines(con)
x[x == ''] = '\n'
unlist(strsplit(x, '\n'))
}

# if a string is encoded in UTF-8, convert it to native encoding
Expand Down
7 changes: 7 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ assert(
combine_words(c('a', 'b', 'c'), before = '``', after = "''") %==% "``a'', ``b'', and ``c''"
)

assert('split_lines() splits a character vector into lines by \\n', {
(split_lines('') %==% '')
(split_lines(NULL) %==% NULL)
(split_lines('a\nb') %==% c('a', 'b'))
(split_lines(c('a\nb', '', ' ', 'c')) %==% c('a', 'b', '', ' ', 'c'))
})

opts = list(fig.cap = 'Figure "caption" <>.', fig.lp = 'Fig:', label = 'foo')
assert(
'.img.cap() generates the figure caption and alt attribute',
Expand Down

0 comments on commit d2634f9

Please sign in to comment.