Skip to content

Commit

Permalink
sstring: add sstring::front()
Browse files Browse the repository at this point in the history
as required by the C++20 standard, std::string should provide

constexpr const charT& front() const;
constexpr charT& front();

so let's add them to sstring. so that sstring can be used in lieu of
std::string, when appropriate.

see also https://eel.is/c++draft/basic.string#string.access

Signed-off-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed Jul 24, 2023
1 parent 416a247 commit e055ef2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/seastar/core/sstring.hh
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,29 @@ public:
replace(p, p, beg, end);
}


/**
* Returns a read/write reference to the data at the first
* element of the string.
* This function shall not be called on empty strings.
*/
reference
front() noexcept {
assert(!empty());
return *str();
}

/**
* Returns a read-only (constant) reference to the data at the first
* element of the string.
* This function shall not be called on empty strings.
*/
const_reference
front() const noexcept {
assert(!empty());
return *str();
}

/**
* Returns a read/write reference to the data at the last
* element of the string.
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/sstring_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ BOOST_AUTO_TEST_CASE(test_add_literal_to_sstring) {
BOOST_REQUIRE_EQUAL("x" + sstring("y"), sstring("xy"));
}

BOOST_AUTO_TEST_CASE(test_front) {
sstring s("abcde");
BOOST_CHECK_EQUAL(s.front(), 'a');
BOOST_CHECK_EQUAL(std::as_const(s).front(), 'a');
}

BOOST_AUTO_TEST_CASE(test_find_sstring) {
BOOST_REQUIRE_EQUAL(sstring("abcde").find('b'), 1u);
BOOST_REQUIRE_EQUAL(sstring("babcde").find('b',1), 2u);
Expand Down

0 comments on commit e055ef2

Please sign in to comment.