0

I wanna to cut CRLF at end of the vector, but my code is not working (at first loop of while - equal is calling and returns false). In debug mode "i" == 0 and have "ptr" value == "0x002e4cfe"

string testS = "\r\n\r\n\r\n<-3 CRLF Testing trim new lines 3 CRLF->\r\n\r\n\r\n";
vector<uint8> _data; _data.clear();
_data.insert(_data.end(), testS.begin(), testS.end());
vector<uint8>::iterator i = _data.end();
uint32 bytesToCut = 0;

while(i != _data.begin()) {
    if(equal(i - 1, i, "\r\n")) {
        bytesToCut += 2;                
        --i; if(i == _data.begin()) return; else --i;
    } else {
        if(bytesToCut) _data.erase(_data.end() - bytesToCut, _data.end());
        return;
    }                    
}

Thanks a lot for your answers. But i need version with iterators, because my code is used when i parsing chunked http transfering data, which is writed to vector and i need func, which would take a pointer to a vector and iterator defining the position to remove CRLF backwards. And all my problems, i think, apparently enclosed in iterators.

1
  • 'but my code is not working' is pretty vague! Improve your question ... Jan 27, 2014 at 17:12

4 Answers 4

0

Your code is invalid at least due to setting incorrect range in algorithm std::equal

if(equal(i - 1, i, "\r\n")) {

In this expression you compare only one element of the vector pointed by iterator i - 1 with '\r'. You have to write something as

if(equal(i - 2, i, "\r\n")) {

If you need to remove pairs "\r\n" from the vector then I can suggest the following approach (I used my own variable names and included testing output):

std::string s = "\r\n\r\n\r\n<-3 CRLF Testing trim new lines 3 CRLF->\r\n\r\n\r\n";
std::vector<unsigned char> v( s.begin(), s.end() );

std::cout << v.size() << std::endl;

auto last = v.end();
auto prev = v.end();

while ( prev != v.begin() && *--prev == '\n' && prev != v.begin() && *--prev == '\r' )
{
    last = prev;
}

v.erase( last, v.end() );

std::cout << v.size() << std::endl;
0
0

instead if re inventing th wheel you can the existing STL algo with something like:

std::string s;
s = s.substr(0, s.find_last_not_of(" \r\n"));
0
0

If you need to just trim '\r' & '\n' from the end then simple substr will do:

std::string str = "\r\n\r\n\r\nSome string\r\n\r\n\r\n";

size_t newLength = str.length();
while (str[newLength - 1] == '\r' || str[newLength - 1] == '\n') newLength--;
str = str.substr(0, newLength);

std::cout << str;

Don't sweat small stuff :)


Removing all '\r' and '\n' could be simple as (C++03):

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string str = "\r\n\r\n\r\nSome string\r\n\r\n\r\n";
    str.erase(std::remove(str.begin(), str.end(), '\r'), str.end());
    str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
    std::cout << str;
}

or:

bool isUnwantedChar(char c) {
    return (c == '\r' || c == '\n');
}

int main() {
    std::string str = "\r\n\r\n\r\nSome string\r\n\r\n\r\n";
    str.erase(std::remove_if(str.begin(), str.end(), isUnwantedChar), str.end());
    std::cout << str;
}
1
  • Note that std::remove does not actually remove the elements, but just positions the non-removed elements at the start of the container. std::string.erase can do the actual removal without the need for your loop. Jan 27, 2014 at 17:35
0

First of all, your vector initialization is ... non-optimal. All you needed to do is:

string testS = "\r\n\r\n\r\n<-3 CRLF Testing trim new lines 3 CRLF->\r\n\r\n\r\n";
vector<uint8> _data(testS.begin(), testS.end());

Second, if you wanted to remove the \r and \n characters, you could have done it in the string:

testS.erase(std::remove_if(testS.begin(), testS.end(), [](char c)
{
    return c == '\r' || c == '\n';
}), testS.end());

If you wanted to do it in the vector, it is the same basic process:

_data.erase(std::remove_if(_data.begin(), _data.end(), [](uint8 ui)
{
    return ui == static_cast<uint8>('\r') || ui == static_cast<uint8>('\n');
}), _data.end());

Your problem is likely due to the usage of invalidated iterators in your loop (that has several other logical issues, but since it shouldn't exist anyway, I won't touch on) that removes elements 1-by-1.

If you wanted to remove the items just from the end of the string/vector, it would be slightly different, but still the same basic pattern:

int start = testS.find_first_not_of("\r\n", 0); // finds the first non-\r\n character in the string
int end = testS.find_first_of("\r\n", start); // find the first \r\n character after real characters
// assuming neither start nor end are equal to std::string::npos - this should be checked
testS.erase(testS.begin() + end, testS.end()); // erase the `\r\n`s at the end of the string.

or alternatively (if \r\n can be in the middle of the string as well):

std::string::reverse_iterator rit = std::find_if_not(testS.rbegin(), testS.rend(), [](char c)
{
    return c == '\r' || c == '\n';
});
testS.erase(rit.base(), testS.end());
6
  • It looks like OP wants to remove CRLF at end of string. Your version removes it everywhere. It's not clear from the question if he wants to remove only CRLF appearing as a sequence, or any CR/LF character at the end.
    – interjay
    Jan 27, 2014 at 17:35
  • @interjay True. The wording of the original post was not clear. I'll add another version to remove it just from the end. Jan 27, 2014 at 17:38
  • @Zac Howland, thanks, it is very compact, but I need to remove only the final newline characters, to the first character different from them (from the end). P. S. My algorithm similar to this, but cleaning these characters from the beginning of the vector works fine.
    – Alex Shul
    Jan 27, 2014 at 17:38
  • @Slinner The update includes versions that can remove just the end as well. Jan 27, 2014 at 17:55
  • @Zac Howland std::string::reverse_iterator rit = std::find_if_not(testS.rbegin(), testS.rend(), [](char c) { return c == '\r' || c == '\n'; }); Please tell me what it is? How its called? (Declaration of predicative function within the list of arguments passed function find_if_not) Where can I find information about the syntax of these records?
    – Alex Shul
    Jan 30, 2014 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.