# # # patch "packet.cc" # from [e5854ef14c0f8bfd6d308cc1adcecf2d2fe4ddc9] # to [94436e3bdf7352ffbb40e4d086b8ed08e207bfa2] # # patch "simplestring_xform.cc" # from [0ce9a86370da9e0043729c1bae322655035694be] # to [9ee64085fd6badec5b1ff9a25bab8cee8b169f8f] # # patch "simplestring_xform.hh" # from [eb4c9acbf8671b9f7114959f1e120bbc1430b67a] # to [0f62eaf3d669fa38fde3351bc29f166f7555c924] # ============================================================ --- packet.cc e5854ef14c0f8bfd6d308cc1adcecf2d2fe4ddc9 +++ packet.cc 94436e3bdf7352ffbb40e4d086b8ed08e207bfa2 @@ -42,7 +42,7 @@ packet_writer::consume_file_data(file_id base64 > packed; pack(dat.inner(), packed); ost << "[fdata " << ident << "]\n" - << trim_ws(packed()) << '\n' + << trim(packed()) << '\n' << "[end]\n"; } @@ -55,7 +55,7 @@ packet_writer::consume_file_delta(file_i pack(del.inner(), packed); ost << "[fdelta " << old_id << '\n' << " " << new_id << "]\n" - << trim_ws(packed()) << '\n' + << trim(packed()) << '\n' << "[end]\n"; } @@ -66,7 +66,7 @@ packet_writer::consume_revision_data(rev base64 > packed; pack(dat.inner(), packed); ost << "[rdata " << ident << "]\n" - << trim_ws(packed()) << '\n' + << trim(packed()) << '\n' << "[end]\n"; } @@ -77,8 +77,8 @@ packet_writer::consume_revision_cert(rev t.inner().ident.inner().made_from) << '\n' << " " << t.inner().name() << '\n' << " " << t.inner().key() << '\n' - << " " << trim_ws(encode_base64(t.inner().value)()) << "]\n" - << trim_ws(encode_base64(t.inner().sig)()) << '\n' + << " " << trim(encode_base64(t.inner().value)()) << "]\n" + << trim(encode_base64(t.inner().sig)()) << '\n' << "[end]\n"; } @@ -87,7 +87,7 @@ packet_writer::consume_public_key(rsa_ke rsa_pub_key const & k) { ost << "[pubkey " << ident() << "]\n" - << trim_ws(encode_base64(k)()) << '\n' + << trim(encode_base64(k)()) << '\n' << "[end]\n"; } @@ -96,8 +96,8 @@ packet_writer::consume_key_pair(rsa_keyp keypair const & kp) { ost << "[keypair " << ident() << "]\n" - << trim_ws(encode_base64(kp.pub)()) << "#\n" - << trim_ws(encode_base64(kp.priv)()) << '\n' + << trim(encode_base64(kp.pub)()) << "#\n" + << trim(encode_base64(kp.priv)()) << '\n' << "[end]\n"; } @@ -106,7 +106,7 @@ packet_writer::consume_old_private_key(r old_arc4_rsa_priv_key const & k) { ost << "[privkey " << ident() << "]\n" - << trim_ws(encode_base64(k)()) << '\n' + << trim(encode_base64(k)()) << '\n' << "[end]\n"; } ============================================================ --- simplestring_xform.cc 0ce9a86370da9e0043729c1bae322655035694be +++ simplestring_xform.cc 9ee64085fd6badec5b1ff9a25bab8cee8b169f8f @@ -201,14 +201,34 @@ string } string -trim_ws(string const & s) +trim_left(string const & s, string const & chars) { string tmp = s; - string::size_type pos = tmp.find_last_not_of("\n\r\t "); + string::size_type pos = tmp.find_first_not_of(chars); if (pos < string::npos) + tmp = tmp.substr(pos); + return tmp; +} + +string +trim_right(string const & s, string const & chars) +{ + string tmp = s; + string::size_type pos = tmp.find_last_not_of(chars); + if (pos < string::npos) tmp.erase(++pos); - pos = tmp.find_first_not_of("\n\r\t "); + return tmp; +} + +string +trim(string const & s, string const & chars) +{ + string tmp = s; + string::size_type pos = tmp.find_last_not_of(chars); if (pos < string::npos) + tmp.erase(++pos); + pos = tmp.find_first_not_of(chars); + if (pos < string::npos) tmp = tmp.substr(pos); return tmp; } @@ -319,11 +339,16 @@ UNIT_TEST(simplestring_xform, split_into UNIT_TEST_CHECK(words[2]() == "bar"); } -UNIT_TEST(simplestring_xform, strip_ws) +UNIT_TEST(simplestring_xform, trimming) { - UNIT_TEST_CHECK(trim_ws("\n leading space") == "leading space"); - UNIT_TEST_CHECK(trim_ws("trailing space \n") == "trailing space"); - UNIT_TEST_CHECK(trim_ws("\t\n both \r \n\r\n") == "both"); + UNIT_TEST_CHECK(trim_right(":foobar:", ":") == ":foobar"); + UNIT_TEST_CHECK(trim_left(":foobar:", ":") == "foobar:"); + UNIT_TEST_CHECK(trim(":foobar:", ":") == "foobar"); + + UNIT_TEST_CHECK(trim("\n leading space") == "leading space"); + UNIT_TEST_CHECK(trim("trailing space \n") == "trailing space"); + UNIT_TEST_CHECK(trim("\t\n both \r \n\r\n") == "both"); + UNIT_TEST_CHECK(remove_ws(" I like going\tfor walks\n ") == "Ilikegoingforwalks"); } ============================================================ --- simplestring_xform.hh eb4c9acbf8671b9f7114959f1e120bbc1430b67a +++ simplestring_xform.hh 0f62eaf3d669fa38fde3351bc29f166f7555c924 @@ -105,9 +105,18 @@ std::string remove_ws(std::string const // remove all whitespace std::string remove_ws(std::string const & s); -// remove leading and trailing whitespace -std::string trim_ws(std::string const & s); +// remove leading chars from string +std::string trim_left(std::string const & s, + std::string const & chars = "\n\r\t "); +// remove trailing chars from string +std::string trim_right(std::string const & s, + std::string const & chars = "\n\r\t "); + +// remove leading and trailing chars from string +std::string trim(std::string const & s, + std::string const & chars = "\n\r\t "); + // Local Variables: // mode: C++ // fill-column: 76