自制礼帽:看起来相同但不完全相同
Homemade toupper: looks the same but not identical
对于我的优化,我想在 Rcpp 中获得一个像样的 toupper
。我是 C++ 的新手,至于知道我已经做到了:
#include <Rcpp.h>
using namespace Rcpp;
void C_toupper_String(const String& s) {
for (char *p =(char *)s.get_cstring();*p!=0;p++) *p = toupper(*p);
}
// [[Rcpp::export]]
StringVector C_toupper(StringVector const& vecteur) {
StringVector res=clone(vecteur);
for (int i(0); i < res.size(); ++i) {
C_toupper_String(res[i]);
}
return res;
}
/*** R
teststring <- "HeY I arNaud"
C_toupper(teststring)
toupper(teststring)
identical(C_toupper(teststring),toupper(teststring))
*/
但是,它没有正常工作。
> C_toupper(teststring)
[1] "HEY I ARNAUD"
> toupper(teststring)
[1] "HEY I ARNAUD"
> identical(C_toupper(teststring),toupper(teststring))
[1] FALSE
有什么问题吗?如果可能的话,我不想将 String
转换为 std::string
,因为我想了解发生了什么:进入 C++ 的目的是能够避免复制和转换。
谢谢,
阿尔诺
为什么不使用 C++ 库的单行代码?除非你 真正 有 C++ 经验,否则你不太可能击败它。下面的代码只是缩进显示在这里,它是我的R会话中的一行。
R> Rcpp::cppFunction("std::string tU(std::string s) { std::string u(s); \
for (unsigned int i=0; i<u.length(); i++) u[i] = std::toupper(u[i]); return(u); }")
R> tU("aBcDe")
[1] "ABCDE"
R>
为什么这两个字符串不测试 identical
的问题很难解释——这两个字符串在检查它们的原始字节(通过 charToRaw
)时看起来肯定是一样的,它们不携带属性,并且它们没有编码集。所以,实际上,它们应该是相同的。
要解开这个谜团,我们需要了解您的 C++ 代码实际上在做什么。更具体地说,C_toupper_String
中的 C 风格转换在做什么。由于他们的危险,you should never use C-style casts。您的代码 运行 纯粹是因为该转换而出现问题。
为什么?因为String::get_cstring
returns char const*
. You are casting it to char*
and thereby casting away its const
ness. This can be safe, but only if the underlying storage isn’t const
. Otherwise it’s undefined behaviour (UB)。由于代码重写(例如优化),UB 的影响很难预测。在这种情况下,它似乎生成了破坏 R 字符串内部结构的代码。
你基本上不能修改Rcpp::String
对象,他们不允许这样做。但是,如果您只是想避免复制,那么您的代码无论如何都达不到目的,因为您的 C_toupper
函数在第一步中明确复制了输入。
正如德克所说,解决这个问题的正确方法是使用可用的 Rcpp API。在字符串修改的情况下,这意味着将您的输入转换为 std::string
,执行修改,然后再转换回来。这确实复制了,但您当前的代码也是如此。这是编写此代码的一种好方法:
#include <Rcpp.h>
#include <cctype>
#include <string>
// [[Rcpp::export]]
Rcpp::StringVector C_toupper(Rcpp::StringVector const& vec) {
std::vector<std::string> res(vec.begin(), vec.end());
for (std::string& str : res) {
for (char& c : str) c = std::toupper(c);
}
return Rcpp::wrap(res);
}
请注意,此 有时会 产生错误结果,因为 std::toupper
从根本上无法处理某些 Unicode 特征。 R 的 toupper
做得更好,但也有一些问题。 正确的 解决方案是使用 {stringr} 或 {stringi} 包。
对于我的优化,我想在 Rcpp 中获得一个像样的 toupper
。我是 C++ 的新手,至于知道我已经做到了:
#include <Rcpp.h>
using namespace Rcpp;
void C_toupper_String(const String& s) {
for (char *p =(char *)s.get_cstring();*p!=0;p++) *p = toupper(*p);
}
// [[Rcpp::export]]
StringVector C_toupper(StringVector const& vecteur) {
StringVector res=clone(vecteur);
for (int i(0); i < res.size(); ++i) {
C_toupper_String(res[i]);
}
return res;
}
/*** R
teststring <- "HeY I arNaud"
C_toupper(teststring)
toupper(teststring)
identical(C_toupper(teststring),toupper(teststring))
*/
但是,它没有正常工作。
> C_toupper(teststring)
[1] "HEY I ARNAUD"
> toupper(teststring)
[1] "HEY I ARNAUD"
> identical(C_toupper(teststring),toupper(teststring))
[1] FALSE
有什么问题吗?如果可能的话,我不想将 String
转换为 std::string
,因为我想了解发生了什么:进入 C++ 的目的是能够避免复制和转换。
谢谢,
阿尔诺
为什么不使用 C++ 库的单行代码?除非你 真正 有 C++ 经验,否则你不太可能击败它。下面的代码只是缩进显示在这里,它是我的R会话中的一行。
R> Rcpp::cppFunction("std::string tU(std::string s) { std::string u(s); \
for (unsigned int i=0; i<u.length(); i++) u[i] = std::toupper(u[i]); return(u); }")
R> tU("aBcDe")
[1] "ABCDE"
R>
为什么这两个字符串不测试 identical
的问题很难解释——这两个字符串在检查它们的原始字节(通过 charToRaw
)时看起来肯定是一样的,它们不携带属性,并且它们没有编码集。所以,实际上,它们应该是相同的。
要解开这个谜团,我们需要了解您的 C++ 代码实际上在做什么。更具体地说,C_toupper_String
中的 C 风格转换在做什么。由于他们的危险,you should never use C-style casts。您的代码 运行 纯粹是因为该转换而出现问题。
为什么?因为String::get_cstring
returns char const*
. You are casting it to char*
and thereby casting away its const
ness. This can be safe, but only if the underlying storage isn’t const
. Otherwise it’s undefined behaviour (UB)。由于代码重写(例如优化),UB 的影响很难预测。在这种情况下,它似乎生成了破坏 R 字符串内部结构的代码。
你基本上不能修改Rcpp::String
对象,他们不允许这样做。但是,如果您只是想避免复制,那么您的代码无论如何都达不到目的,因为您的 C_toupper
函数在第一步中明确复制了输入。
正如德克所说,解决这个问题的正确方法是使用可用的 Rcpp API。在字符串修改的情况下,这意味着将您的输入转换为 std::string
,执行修改,然后再转换回来。这确实复制了,但您当前的代码也是如此。这是编写此代码的一种好方法:
#include <Rcpp.h>
#include <cctype>
#include <string>
// [[Rcpp::export]]
Rcpp::StringVector C_toupper(Rcpp::StringVector const& vec) {
std::vector<std::string> res(vec.begin(), vec.end());
for (std::string& str : res) {
for (char& c : str) c = std::toupper(c);
}
return Rcpp::wrap(res);
}
请注意,此 有时会 产生错误结果,因为 std::toupper
从根本上无法处理某些 Unicode 特征。 R 的 toupper
做得更好,但也有一些问题。 正确的 解决方案是使用 {stringr} 或 {stringi} 包。