Skip to content
Snippets Groups Projects
Unverified Commit 01fb2ba5 authored by Bensong Liu's avatar Bensong Liu
Browse files

add rlib::string.starts_with, ends_with

parent 55a022d9
No related branches found
No related tags found
No related merge requests found
......@@ -374,6 +374,26 @@ namespace rlib {
return *this;
}
bool starts_with(const std::string &what) const {
if(size() < what.size()) return false;
std::string::value_type diffBits = 0;
for(auto i = 0; i < what.size(); ++i) {
diffBits = diffBits | (what[i] ^ (*this)[i]);
}
return diffBits == 0;
}
bool ends_with(const std::string &what) const {
if(size() < what.size()) return false;
std::string::value_type diffBits = 0;
auto offset = size() - what.size();
for(auto i = 0; i < what.size(); ++i) {
diffBits = diffBits | (what[i] ^ (*this)[offset+i]);
}
return diffBits == 0;
}
template <typename... Args>
string &format(Args... args) {
return operator=(std::move(impl::format_string(*this, args ...)));
......
......@@ -49,6 +49,18 @@ TEST_CASE("rlib::string others", "[string_op]") {
test_str = "hello world \n abc def some random ";
auto test_str2 = test_str;
REQUIRE(" "_rs.join(test_str.split(' ')) == test_str2);
const rlib::string test_str3 = "|123||";
REQUIRE(test_str3.starts_with("") == true);
REQUIRE(test_str3.starts_with("|") == true);
REQUIRE(test_str3.starts_with("|123||") == true);
REQUIRE(test_str3.starts_with("1123||") == false);
REQUIRE(test_str3.starts_with("|123|||") == false);
REQUIRE(test_str3.ends_with("") == true);
REQUIRE(test_str3.ends_with("|") == true);
REQUIRE(test_str3.ends_with("23||") == true);
REQUIRE(test_str3.ends_with("1123||") == false);
REQUIRE(test_str3.ends_with("123|||") == false);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment