Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat universal: add insert method to SmallString
Browse files Browse the repository at this point in the history
fixes:
```shell
-c /Users/runner/work/userver/userver/universal/src/logging/impl/formatters/tskv.cpp
In file included from /Users/runner/work/userver/userver/universal/src/logging/impl/formatters/tskv.cpp:5:
In file included from /opt/homebrew/include/fmt/chrono.h:23:
In file included from /opt/homebrew/include/fmt/format.h:41:
/opt/homebrew/include/fmt/base.h:2027:5: error: no member named 'insert' in 'userver::utils::SmallString<4096>'
  c.insert(c.end(), begin, end);
  ~ ^
/opt/homebrew/include/fmt/format.h:535:10: note: in instantiation of function template specialization 'fmt::detail::copy<char, const char *, std::back_insert_iterator<userver::utils::SmallString<4096>>, 0>' requested here
  return copy<OutChar>(begin, end, out);
```

------------------------
Note: by creating a PR or an issue you automatically agree to the CLA. See [CONTRIBUTING.md](https://github.com/userver-framework/userver/blob/develop/CONTRIBUTING.md). Feel free to remove this note, the agreement holds.

Tests: протестировано CI

Pull Request resolved: #846
commit_hash:2f1034099fead100ba2f4f010a418fc2ce57c961
fdr400 committed Jan 30, 2025
1 parent 1886703 commit 470ce77
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions universal/include/userver/utils/small_string.hpp
Original file line number Diff line number Diff line change
@@ -142,6 +142,10 @@ class SmallString final {
/// @brief Append contents of a string_view to the string.
void append(std::string_view str);

/// @brief Inserts elements from range [begin, end) before pos.
template <class InputIt>
void insert(const_iterator pos, InputIt begin, InputIt end);

/// @brief Remove the last character from the string.
void pop_back();

@@ -276,6 +280,12 @@ void SmallString<N>::append(std::string_view str) {
data_.insert(data_.begin() + old_size, str.begin(), str.end());
}

template <std::size_t N>
template <class InputIt>
void SmallString<N>::insert(SmallString::const_iterator pos, InputIt begin, InputIt end) {
data_.insert(pos, begin, end);
}

template <std::size_t N>
void SmallString<N>::pop_back() {
data_.pop_back();
14 changes: 14 additions & 0 deletions universal/src/utils/small_string_test.cpp
Original file line number Diff line number Diff line change
@@ -69,6 +69,14 @@ TEST(SmallString, Append) {
EXPECT_EQ(str, "abcdabcd");
}

TEST(SmallString, Insert) {
constexpr std::string_view data{"ab"};

utils::SmallString<3> str("c");
str.insert(str.begin(), data.begin(), data.end());
EXPECT_EQ(str, "abc");
}

TEST(SmallString, SizeCapacity) {
utils::SmallString<10> str("abcd");
str.resize(3, '1');
@@ -153,6 +161,12 @@ TEST(SmallString, Format) {
EXPECT_EQ("abcd1234", fmt::format("{}{}", str1, str2));
}

TEST(SmallString, FormatTo) {
utils::SmallString<6> str("abc");
fmt::format_to(std::back_inserter(str), "d={}", 1);
EXPECT_EQ("abcd=1", str);
}

TEST(SmallString, Iterator) {
utils::SmallString<3> str("12345");
for (auto& c : str) c++;

0 comments on commit 470ce77

Please sign in to comment.