From fa5f32738e32918c7e4861fe4fe1dd65028b3f88 Mon Sep 17 00:00:00 2001 From: shrektan Date: Mon, 9 Sep 2019 22:10:58 +0800 Subject: [PATCH] setnames() should treat different Encoding as non-equal strings --- R/data.table.R | 6 +++--- inst/tests/tests.Rraw | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/R/data.table.R b/R/data.table.R index 3f97544372..43855bf68d 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -2411,9 +2411,9 @@ setnames = function(x,old,new,skip_absent=FALSE) { if (length(old) != ncol) stop("Can't assign ",length(old)," names to a ",ncol," column data.table") if (anyNA(names(x))) { # if x somehow has some NA names, which() needs help to return them, #2475 - w = which((names(x) != old) | (is.na(names(x)) & !is.na(old))) + w = which((names(x) != old) | (Encoding(names(x)) != Encoding(old)) | (is.na(names(x)) & !is.na(old))) } else { - w = which(names(x) != old) + w = which(names(x) != old | (Encoding(names(x)) != Encoding(old))) } if (!length(w)) return(invisible(x)) # no changes new = old[w] @@ -2442,7 +2442,7 @@ setnames = function(x,old,new,skip_absent=FALSE) { } } } - if (any(w <- new==names(x)[i])) { + if (any(w <- new==names(x)[i] & Encoding(new)==Encoding(names(x)[i]))) { w = which(!w) new = new[w] i = i[w] diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index d705901860..9e2c1f2754 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15936,6 +15936,18 @@ test(2100.14, fifelse(c(T,F,NA),c(1,1,1),c(2,2,2),NA), c(1,2,NA)) DT = data.table(id=1:3, v=4:6, key="id") test(2101, DT[.(logical())], data.table(id=logical(), v=integer(), key="id")) +# setnames: names with same content but different encoding is considered as non-equal +utf8 = c("\u00e7ile", "\u00de") +latin1 = iconv(utf8, from = "UTF-8", to = "latin1") +tbl = as.data.table(setNames(list(1, 2), latin1)) +test(2102.01, Encoding(names(tbl)), c('latin1', 'latin1')) +setnames(tbl, utf8) +test(2102.02, Encoding(names(tbl)), c('UTF-8', 'UTF-8')) +setnames(tbl, 1:2, latin1) +test(2102.03, Encoding(names(tbl)), c('latin1', 'latin1')) +# will fail now because of #3844. should be uncommented when #3844 is resolved. +# setnames(tbl, latin1, utf8) +# test(2102.04, Encoding(names(tbl)), c('UTF-8', 'UTF-8')) ################################### # Add new tests above this line #