Skip to content

Commit d88ff16

Browse files
convert to utf8 if needed before comparing strings
1 parent 7e1dc48 commit d88ff16

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/dplyr.h

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <Rinternals.h>
77
#include <R_ext/Rdynload.h>
88

9+
#define UTF8_MASK (1<<3)
10+
#define ASCII_MASK (1<<6)
11+
12+
#define IS_ASCII(x) (LEVELS(x) & ASCII_MASK)
13+
#define IS_UTF8(x) (LEVELS(x) & UTF8_MASK)
14+
915
namespace dplyr {
1016

1117
struct envs {

src/mask.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#include "dplyr.h"
22

3+
SEXP as_utf8(SEXP s) {
4+
if (!IS_UTF8(s) && !IS_ASCII(s)) {
5+
s = Rf_mkCharCE(Rf_translateCharUTF8(s), CE_UTF8);
6+
}
7+
return s;
8+
}
9+
310
R_xlen_t find_first(SEXP haystack, SEXP needle) {
11+
needle = as_utf8(needle);
412
R_xlen_t n = XLENGTH(haystack);
513
R_xlen_t i_name = 0;
614
for (; i_name < n; i_name++) {
7-
if (needle == STRING_ELT(haystack, i_name)) break;
15+
if (needle == as_utf8(STRING_ELT(haystack, i_name))) break;
816
}
917

1018
return i_name;

0 commit comments

Comments
 (0)