(How) can I use a library of string acceleration algorithms with c strings (char pointers)?

Is it possible to adapt the c-style string / buffer ( char* or wchar_t* ) in some way to work with the Library of string acceleration algorithms ?

That is, for example, the trim algorithm has the following declaration:

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

and the implementation (search trim_left_if ) requires the sequence type to have an erase member erase .

How can I use this with the raw character pointer buffer / c?

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

Ideally, the algorithms will work directly on the supplied buffer. (As far as possible, it obviously cannot work if the algorithm needs to allocate additional space in the "row".)


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

The reason I have a char * is because I would like to use several algorithms on our existing code base. Refactoring all char buffers into a string will be more work than it costs, and when changing or adapting something, it would be nice to just apply this algorithm to any c-style string that lives in the current code.

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

+7
source share
3 answers

For operations like SequenceT you probably have to use std::string . If you want to implement this yourself, you will have to fulfill many more requirements for the creation, destruction, semantics of values, etc. You will basically finish your implementation of std::string .

However, RangeT -type operations can be used on char* using iterator_range from the Boost.Range library. I have not tried, though.

+5
source

There is some code that implements std::string like a fixed buffer string. With some mastering, you can modify this code to create a string type that 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" 

To do this, I added a constructor to xstr::xstring and xstr::fixed_char_buf to take the buffer, the size of the buffer used, and the maximum size of the buffer. Next, I replaced the SIZE template argument with a member variable and changed the internal char array to a char pointer.

The xstr code xstr bit outdated and will not compile without problems on new compilers, but it needs minor changes. Further, I added only what is needed in this case. If you want to use this for real, you need to make a few more changes to make sure that it cannot use uninitialized memory.

In any case, this may be a good start to writing your own string adapter.

+1
source

I don’t know what platform you are aiming for, but on most modern computers (including mobile computers such as ARM) the memory copy is so fast that you don’t even waste time optimizing the memory copies. I say - wrap char* in std::string and check if the performance suits your needs. Do not waste time on premature optimization.

0
source

All Articles