使用boost::spirit::x3从std::string解析为boost::string_view
parsing from std::string into a boost::string_view using boost::spirit::x3
在我之前的问题中,有人建议我的boost::spirit::x3
解析器的性能可以通过使用raw
指令解析成boost::string_view
来提高。
然而,我很难让它编译。这是我发现的:
-
在
x3
之前,必须专门化assign_to_attribute_from_iterators
(参见例如这个SO答案)来处理raw
指令。 -
x3
现在使用move_to
free函数代替(参见例如这个SO答案)。
因此,我添加了一个move_to
重载,如果我从char*
解析:
#include <iostream>
#include <string>
#include <boost/utility/string_view.hpp>
namespace boost {
namespace spirit { namespace x3 { namespace traits {
template <typename It>
void move_to(It b, It e, boost::string_view& v)
{
v = boost::string_view(b, std::size_t(std::distance(b,e)));
}
} } }
} // namespace boost
#include <boost/spirit/home/x3.hpp>
namespace parser
{
namespace x3 = boost::spirit::x3;
using x3::char_;
using x3::raw;
const auto str = raw[ +~char_('_')] >> '_';
}
int main()
{
std::string input = "hello world_";
boost::string_view str;
parse(input.data(), input.data()+input.size(), parser::str, str);
std::cout << str;
}
<<p> 生活例子/strong> 但是,它不编译:
1)如果我使用std::string::const_iterator
parse(input.cbegin(), input.cend(), parser::str, str);
boost::string_view
的构造函数需要const char*
或std::string&
。
main.cpp:12:16: error: no matching function for call to 'boost::basic_string_view<char, std::char_traits<char> >::basic_string_view(__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >&, std::size_t)'
v = boost::string_view(b, std::size_t(std::distance(b,e)));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
生活例子
如何从std::string::const_iterator
实例化boost::string_view
?
2)如果boost/spirit/home/x3.hpp
在move_to
过载之前包含
生活例子
为什么没有选择我的过载?它不是比boost/spirit/home/x3/support/traits/move_to.hpp
中定义的任何重载都更好吗?无论包含的顺序如何,我如何确保重载被选中?
我只是写你想要的:
v = boost::string_view(&*b, std::distance(b,e));
您可能想要检查存储是否连续¹作为输入范围的概念检查。在这方面,还要求迭代器是随机访问的,并写:
v = boost::string_view(&*b, e-b);
¹这是string_view的要求
相关文章:
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- 如何从 boost::container::vector<std::string>::iterator 访问索引和对象?
- std::string references、std::regex和boost::filesystem的基本概念
- 将 std::string 转换为 boost::asio::streambuf
- 使用 boost::interprocess 共享内存时从 std::string 转换为 "MyShmString"
- 当我使用py 生成时,然后在macOS上boost.python包装器时,我会发现有关std :: string的错
- 如何在 Boost.Sprit.Qi 中从向量制作组合的 ascii::string 规则<string>?
- boost-python 当C++方法返回 std::map<string,X*>
- 从 boost::存档:binary_iarchive 反序列化 std::string 时出现错误的分配异常
- 从 C 字符串构造 boost::type_erasure::any,但存储为 std::string
- boost::asio::async_write with std::string
- 如何将std :: string转换为boost :: chrono :: nanseconds
- 使用Boost Karma替换STD :: stringstream double到STD :: String Conv
- 无法获得 boost::spirit parser&lexer 适用于 std::string 或 int 或 double 以外的令牌类型
- `boost::local_time::date_time` from `std::string`
- 从STD :: String Boost C 获取小时和几分钟
- 将Boost STD :: String :: const_iterator转换为字符串和INT C
- 为什么 boost::assign::list_of 不适用于对字符串、矢量<string>><?
- 如何将 std::vector<std::string> 转换为 boost::spirit 中结构的成员?
- 如何在<string> boost::p rogram_options 中指定矢量的默认值