(如何)我可以将 Boost 字符串算法库与 c 字符串(字符指针)一起使用吗?

发布于 2024-12-13 19:24:33 字数 1081 浏览 3 评论 0原文

是否可以以某种方式调整 C 样式字符串/缓冲区(char*wchar_t*)以与 Boost 字符串算法库

也就是说,例如,它的 trim 算法具有以下声明:

template<typename SequenceT> 
void trim(SequenceT &, const std::locale & = std::locale());

实现(查找trim_left_if)要求序列类型具有成员函数删除

我如何将其与原始字符指针/c 字符串缓冲区一起使用?

char* pStr = getSomeCString(); // example, could also be something like wchar_t buf[256];
...
boost::trim(pStr); // HOW?

理想情况下,算法将直接在提供的缓冲区上工作。(尽可能。如果算法需要在“字符串”中分配额外的空间,它显然无法工作。)


@Vitaly 询问: 为什么不能从 char 缓冲区创建 std::string 然后在算法中使用它?

我拥有 char* 的原因是我想在现有的算法上使用一些算法代码库。将所有字符缓冲区重构为字符串的工作量会大于其价值,并且在更改或调整某些内容时,如果能够将给定的算法应用于当前代码中恰好存在的任何 c 样式字符串,那就太好了。

使用字符串意味着 (a) 将 char* 复制到字符串,(b) 将算法应用于字符串,以及 (c) 将字符串复制回 char 缓冲区。

Is it possible to somehow adapt a c-style string/buffer (char* or wchar_t*) to work with the Boost String Algorithms Library?

That is, for example, it's trimalgorithm has the following declaration:

template<typename SequenceT> 
void trim(SequenceT &, const std::locale & = std::locale());

and the implementation (look for trim_left_if) requires that the sequence type has a member function erase.

How could I use that with a raw character pointer / c string buffer?

char* pStr = getSomeCString(); // example, could also be something like wchar_t buf[256];
...
boost::trim(pStr); // HOW?

Ideally, the algorithms would work directly on the supplied buffer. (As far as possible. it obviously can't work if an algorithm needs to allocate additional space in the "string".)


@Vitaly asks: why can't you create a std::string from char buffer and then use it in algorithms?

The reason I have char* at all is that I'd like to use a few algorthims on our existing codebase. Refactoring all the char buffers to string would be more work than it's worth, and when changing or adapting something it would be nice to just be able to apply a given algorithm to any c-style string that happens to live in the current code.

Using a string would mean to (a) copy char* to string, (b) apply algorithm to string and (c) copy string back into char buffer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

待"谢繁草 2024-12-20 19:24:33

对于 SequenceT 类型的操作,您可能必须使用 std::string。如果您想自己实现它,则必须满足创建、销毁、值语义等更多要求。您基本上最终会得到 std::string 的实现。

然而,RangeT 类型的操作可能可以在使用 iterator_range 来自 Boost.Range 库。不过我没有尝试过。

For the SequenceT-type operations, you probably have to use std::string. If you wanted to implement that by yourself, you'd have to fulfill many more requirements for creation, destruction, value semantics etc. You'd basically end up with your implementation of std::string.

The RangeT-type operations might be, however, usable on char*s using the iterator_range from Boost.Range library. I didn't try it, though.

孤千羽 2024-12-20 19:24:33

存在一些 代码 ,它实现了 std::string 类似字符串固定缓冲区。通过一些修补,您可以修改此代码以创建使用外部缓冲区的字符串类型:

char buffer[100];
strcpy(buffer, "   HELLO   ");

xstr::xstring<xstr::fixed_char_buf<char> >
    str(buffer, strlen(buffer), sizeof(buffer));

boost::algorithm::trim(str);
buffer[str.size()] = 0;

std::cout << buffer << std::endl;   // prints "HELLO"

为此,我向 xstr::xstringxstr::fixed_char_buf 添加了一个构造函数获取缓冲区、正在使用的缓冲区的大小以及缓冲区的最大大小。此外,我用成员变量替换了 SIZE 模板参数,并将内部 char 数组更改为 char 指针。

xstr 代码有点旧,在较新的编译器上编译时不会出现问题,但它需要一些小的更改。此外,我只添加了本例中所需的内容。如果您想真正使用它,您需要进行更多更改以确保它不能使用未初始化的内存。

不管怎样,这可能是编写自己的字符串适配器的一个良好的开始。

There exist some code which implements a std::string like string with a fixed buffer. With some tinkering you can modify this code to create a string type which uses an external buffer:

char buffer[100];
strcpy(buffer, "   HELLO   ");

xstr::xstring<xstr::fixed_char_buf<char> >
    str(buffer, strlen(buffer), sizeof(buffer));

boost::algorithm::trim(str);
buffer[str.size()] = 0;

std::cout << buffer << std::endl;   // prints "HELLO"

For this I added an constructor to xstr::xstring and xstr::fixed_char_buf to take the buffer, the size of the buffer which is in use and the maximum size of the buffer. Further I replaced the SIZE template argument with a member variable and changed the internal char array into a char pointer.

The xstr code is a bit old and will not compile without trouble on newer compilers but it needs some minor changes. Further I only added the things needed in this case. If you want to use this for real, you need to make some more changes to make sure it can not use uninitialized memory.

Anyway, it might be a good start for writing you own string adapter.

诗酒趁年少 2024-12-20 19:24:33

我不知道您的目标平台是什么,但在大多数现代计算机(包括 ARM 等移动计算机)上,内存复制速度非常快,您甚至不应该浪费时间来优化内存复制。我说 - 将 char* 包装在 std::string 中并检查性能是否满足您的需求。不要在过早的优化上浪费时间。

I don't know what platform you're targeting, but on most modern computers (including mobile ones like ARM) memory copy is so fast you shouldn't even waste your time optimizing memory copies. I say - wrap char* in std::string and check whether the performance suits your needs. Don't waste time on premature optimization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文