简体   繁体   English

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

[英](How) can I use the Boost String Algorithms Library with c strings (char pointers)?

Is it possible to somehow adapt a c-style string/buffer ( char* or wchar_t* ) to work with the Boost String Algorithms Library ? 是否可以通过某种方式使c样式的字符串/缓冲区( char*wchar_t* )与Boost String Algorithms库一起使用

That is, for example, it's trim algorithm has the following declaration: 也就是说,例如,它的trim算法具有以下声明:

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 . 实现 (查找trim_left_if )要求序列类型具有成员函数erase

How could I use that with a raw character pointer / c string buffer? 如何将其与原始字符指针/ c字符串缓冲区一起使用?

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? @Vitaly问: 为什么不能从char缓冲区创建std :: string,然后在算法中使用它?

The reason I have char* at all is that I'd like to use a few algorthims on our existing codebase. 我完全拥有char *的原因是,我想在现有代码库中使用一些算法。 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. 将所有char缓冲区重构为字符串将比其应有的工作还要多,并且在更改或改编某些内容时,能够将给定算法应用于碰巧存在于当前代码中的任何c样式字符串,将是一个很好的选择。

Using a string would mean to (a) copy char* to string, (b) apply algorithm to string and (c) copy string back into char buffer. 使用字符串将意味着(a)将char *复制到字符串,(b)将算法应用于字符串,以及(c)将字符串复制回char缓冲区。

For the SequenceT -type operations, you probably have to use std::string . 对于SequenceT类型的操作,您可能必须使用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 . 如果要自己实现,则必须满足创建,销毁,值语义等方面的更多要求。从根本上讲,最终将实现std::string

The RangeT -type operations might be, however, usable on char* s using the iterator_range from Boost.Range library. 但是,可以使用Boost.Range库中的iterator_rangechar*使用RangeT类型的操作。 I didn't try it, though. 不过我没有尝试。

There exist some code which implements a std::string like string with a fixed buffer. 存在一些实现std::string 代码 ,例如带有固定缓冲区的string。 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. 为此,我在xstr::xstringxstr::fixed_char_buf添加了一个构造函数,以xstr::xstring缓冲区,正在使用的缓冲区的大小以及缓冲区的最大大小。 Further I replaced the SIZE template argument with a member variable and changed the internal char array into a char pointer. 此外,我用成员变量替换了SIZE模板参数,并将内部char数组更改为char指针。

The xstr code is a bit old and will not compile without trouble on newer compilers but it needs some minor changes. xstr代码有些旧,在较新的编译器上不会毫无困难地进行编译,但需要进行一些细微的更改。 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. 无论如何,这可能是编写自己的字符串适配器的一个好的开始。

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. 我不知道您要针对的平台,但是在大多数现代计算机(包括ARM之类的移动计算机)上,内存副本是如此之快,您甚至不必浪费时间优化内存副本。 I say - wrap char* in std::string and check whether the performance suits your needs. 我说-将char*包装在std::string并检查性能是否满足您的需求。 Don't waste time on premature optimization. 不要将时间浪费在过早的优化上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM