Skip to content

Commit 6b231e5

Browse files
author
thirdwing
committed
unit test
1 parent b0db9ff commit 6b231e5

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

inst/include/Rcpp/String.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@ namespace Rcpp {
373373
}
374374

375375
inline void set_encoding( cetype_t encoding ) {
376-
enc = encoding;
377-
if (data != NULL)
378-
data = Rf_mkCharCE(Rf_translateCharUTF8(data), enc);
376+
if (enc != encoding) {
377+
enc = encoding;
378+
if (data != NULL)
379+
data = Rf_mkCharCE(Rf_translateCharUTF8(data), enc);
380+
}
379381
}
380382

381383
inline void set_encoding(const char* encoding) {

inst/unitTests/cpp/String.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,21 @@ String test_push_front(String x) {
6161
x.push_front("abc");
6262
return x;
6363
}
64+
65+
// [[Rcpp::export]]
66+
String test_String_encoding(String x) {
67+
return x.get_encoding();
68+
}
69+
70+
// [[Rcpp::export]]
71+
String test_String_set_encoding(String x) {
72+
String y(x);
73+
y.set_encoding("UTF-8");
74+
return y;
75+
}
76+
77+
// [[Rcpp::export]]
78+
String test_String_ctor_encoding(String x) {
79+
String y(x, "UTF-8");
80+
return y;
81+
}

inst/unitTests/runit.String.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,15 @@ if (.runThisTest) {
5454
res <- test_push_front("def")
5555
checkIdentical(res, "abcdef")
5656
}
57+
58+
test.String.encoding <- function() {
59+
a <- b <- "å"
60+
Encoding(a) <- "unknown"
61+
Encoding(b) <- "UTF-8"
62+
checkEquals(test_String_encoding(a), "unknown")
63+
checkEquals(test_String_encoding(b), "UTF-8")
64+
checkEquals(Encoding(test_String_set_encoding(a)), "UTF-8")
65+
checkEquals(Encoding(test_String_ctor_encoding(a)), "UTF-8")
66+
}
67+
5768
}

0 commit comments

Comments
 (0)