Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active January 24, 2021 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutannihilation/1ce85a2abdeea4f59ce86fda44dd8ca4 to your computer and use it in GitHub Desktop.
Save yutannihilation/1ce85a2abdeea4f59ce86fda44dd8ca4 to your computer and use it in GitHub Desktop.
Rcpp::cppFunction('
void foo() {
// works fine if I change the pattern to one without []
std::regex reg("[0-9]");
}
', includes = "#include <regex>")
# works
Sys.setlocale("LC_ALL", 'English')
foo()
# freezes
Sys.setlocale("LC_ALL", 'Japanese')
foo()
cpp11::cpp_source(
code =
'#include <regex>
[[cpp11::register]]
void foo() {
std::regex reg("[0-9]");
}
')
# works
Sys.setlocale("LC_ALL", 'English')
foo()
# freezes
Sys.setlocale("LC_ALL", 'Japanese')
foo()
@yutannihilation
Copy link
Author

yutannihilation commented Jan 24, 2021

This should work...

cpp11::cpp_source(
  code = 
    '#include <regex>
[[cpp11::register]]
void foo() {
  std::regex reg("[0-9]", std::regex::ECMAScript & !std::regex::collate);
}
')

c.f. https://en.cppreference.com/w/cpp/regex/syntax_option_type

@yutannihilation
Copy link
Author

cpp11::cpp_source(
  code = '
#include <regex>

[[cpp11::register]]
bool grepl(const std::string pattern_, const std::string x) {
  std::regex::flag_type ECMA_no_collate = static_cast<std::regex::flag_type>(std::regex::ECMAScript & !std::regex::collate);
  std::regex pattern(pattern_, ECMA_no_collate);
  std::smatch match;
  
  return std::regex_search(x, match, pattern);
}
')

grepl("[f]", "foo")
grepl("[e]", "foo")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment