Skip to content

Commit

Permalink
YT-22593: More trivial TString->std::string migrations
Browse files Browse the repository at this point in the history
[nodiff:runtime]
commit_hash:1ba799aed1703ab7c6304b6da7090b3337f768dd
  • Loading branch information
babenko committed Feb 22, 2025
1 parent aeba7bc commit 95fabce
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 62 deletions.
57 changes: 29 additions & 28 deletions library/cpp/yt/coding/unittests/varint_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ using ::testing::Values;

////////////////////////////////////////////////////////////////////////////////

class TWriteVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, TString> >
class TWriteVarIntTest
: public ::testing::TestWithParam<std::tuple<ui64, std::string>>
{ };

TEST_P(TWriteVarIntTest, Serialization)
{
ui64 value = std::get<0>(GetParam());
TString rightAnswer = std::get<1>(GetParam());
std::string rightAnswer = std::get<1>(GetParam());

TStringStream outputStream;
WriteVarUint64(&outputStream, value);
Expand All @@ -30,13 +31,13 @@ TEST_P(TWriteVarIntTest, Serialization)

////////////////////////////////////////////////////////////////////////////////

class TReadVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, TString> >
class TReadVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, std::string> >
{ };

TEST_P(TReadVarIntTest, Serialization)
{
ui64 rightAnswer = std::get<0>(GetParam());
TString input = std::get<1>(GetParam());
auto input = TString(std::get<1>(GetParam()));

TStringInput inputStream(input);
ui64 value;
Expand All @@ -56,34 +57,34 @@ TEST(TReadVarIntTest, Overflow)

auto ValuesForVarIntTests = Values(
// Simple cases.
std::make_tuple(0x0ull, TString("\x00", 1)),
std::make_tuple(0x1ull, TString("\x01", 1)),
std::make_tuple(0x2ull, TString("\x02", 1)),
std::make_tuple(0x3ull, TString("\x03", 1)),
std::make_tuple(0x4ull, TString("\x04", 1)),
std::make_tuple(0x0ull, std::string("\x00", 1)),
std::make_tuple(0x1ull, std::string("\x01", 1)),
std::make_tuple(0x2ull, std::string("\x02", 1)),
std::make_tuple(0x3ull, std::string("\x03", 1)),
std::make_tuple(0x4ull, std::string("\x04", 1)),

// The following "magic numbers" are critical points for varint encoding.
std::make_tuple((1ull << 7) - 1, TString("\x7f", 1)),
std::make_tuple((1ull << 7), TString("\x80\x01", 2)),
std::make_tuple((1ull << 14) - 1, TString("\xff\x7f", 2)),
std::make_tuple((1ull << 14), TString("\x80\x80\x01", 3)),
std::make_tuple((1ull << 21) - 1, TString("\xff\xff\x7f", 3)),
std::make_tuple((1ull << 21), TString("\x80\x80\x80\x01", 4)),
std::make_tuple((1ull << 28) - 1, TString("\xff\xff\xff\x7f", 4)),
std::make_tuple((1ull << 28), TString("\x80\x80\x80\x80\x01", 5)),
std::make_tuple((1ull << 35) - 1, TString("\xff\xff\xff\xff\x7f", 5)),
std::make_tuple((1ull << 35), TString("\x80\x80\x80\x80\x80\x01", 6)),
std::make_tuple((1ull << 42) - 1, TString("\xff\xff\xff\xff\xff\x7f", 6)),
std::make_tuple((1ull << 42), TString("\x80\x80\x80\x80\x80\x80\x01", 7)),
std::make_tuple((1ull << 49) - 1, TString("\xff\xff\xff\xff\xff\xff\x7f", 7)),
std::make_tuple((1ull << 49), TString("\x80\x80\x80\x80\x80\x80\x80\x01", 8)),
std::make_tuple((1ull << 56) - 1, TString("\xff\xff\xff\xff\xff\xff\xff\x7f", 8)),
std::make_tuple((1ull << 56), TString("\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9)),
std::make_tuple((1ull << 63) - 1, TString("\xff\xff\xff\xff\xff\xff\xff\xff\x7f", 9)),
std::make_tuple((1ull << 63), TString("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10)),
std::make_tuple((1ull << 7) - 1, std::string("\x7f", 1)),
std::make_tuple((1ull << 7), std::string("\x80\x01", 2)),
std::make_tuple((1ull << 14) - 1, std::string("\xff\x7f", 2)),
std::make_tuple((1ull << 14), std::string("\x80\x80\x01", 3)),
std::make_tuple((1ull << 21) - 1, std::string("\xff\xff\x7f", 3)),
std::make_tuple((1ull << 21), std::string("\x80\x80\x80\x01", 4)),
std::make_tuple((1ull << 28) - 1, std::string("\xff\xff\xff\x7f", 4)),
std::make_tuple((1ull << 28), std::string("\x80\x80\x80\x80\x01", 5)),
std::make_tuple((1ull << 35) - 1, std::string("\xff\xff\xff\xff\x7f", 5)),
std::make_tuple((1ull << 35), std::string("\x80\x80\x80\x80\x80\x01", 6)),
std::make_tuple((1ull << 42) - 1, std::string("\xff\xff\xff\xff\xff\x7f", 6)),
std::make_tuple((1ull << 42), std::string("\x80\x80\x80\x80\x80\x80\x01", 7)),
std::make_tuple((1ull << 49) - 1, std::string("\xff\xff\xff\xff\xff\xff\x7f", 7)),
std::make_tuple((1ull << 49), std::string("\x80\x80\x80\x80\x80\x80\x80\x01", 8)),
std::make_tuple((1ull << 56) - 1, std::string("\xff\xff\xff\xff\xff\xff\xff\x7f", 8)),
std::make_tuple((1ull << 56), std::string("\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9)),
std::make_tuple((1ull << 63) - 1, std::string("\xff\xff\xff\xff\xff\xff\xff\xff\x7f", 9)),
std::make_tuple((1ull << 63), std::string("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10)),

// Boundary case.
std::make_tuple(static_cast<ui64>(-1), TString("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", 10))
std::make_tuple(static_cast<ui64>(-1), std::string("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", 10))
);

INSTANTIATE_TEST_SUITE_P(ValueParametrized, TWriteVarIntTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ TYPED_TEST(CompactVectorTest, InsertEnd)
{
SCOPED_TRACE("InsertEnd");

TCompactVector<TString, 5> vector;
TCompactVector<std::string, 5> vector;
for (int index = 0; index < 10; ++index) {
vector.insert(vector.end(), ToString(index));
}
Expand All @@ -286,7 +286,7 @@ TYPED_TEST(CompactVectorTest, ShrinkToSmall)
{
SCOPED_TRACE("ShrinkToSmall");

TCompactVector<TString, 5> vector;
TCompactVector<std::string, 5> vector;
for (int index = 0; index < 10; ++index) {
vector.shrink_to_small();
vector.push_back(ToString(index));
Expand Down Expand Up @@ -1061,19 +1061,19 @@ TEST(CompactVectorTest, InitializerList) {
}

TEST(CompactVectorTest, AssignToShorter) {
TCompactVector<TString, 4> lhs;
TCompactVector<TString, 4> rhs;
TCompactVector<std::string, 4> lhs;
TCompactVector<std::string, 4> rhs;
rhs.emplace_back("foo");
lhs = rhs;
EXPECT_EQ(1U, lhs.size());
EXPECT_EQ("foo", lhs[0]);
}

TEST(CompactVectorTest, AssignToLonger) {
TCompactVector<TString, 4> lhs;
TCompactVector<std::string, 4> lhs;
lhs.emplace_back("bar");
lhs.emplace_back("baz");
TCompactVector<TString, 4> rhs;
TCompactVector<std::string, 4> rhs;
rhs.emplace_back("foo");
lhs = rhs;
EXPECT_EQ(1U, lhs.size());
Expand Down
6 changes: 3 additions & 3 deletions library/cpp/yt/exception/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ void AddAttributes(TSimpleException::TAttributes& attrs, TRange&& range)

////////////////////////////////////////////////////////////////////////////////

TSimpleException::TSimpleException(TString message)
TSimpleException::TSimpleException(std::string message)
: Message_(std::move(message))
, What_(Message_)
{ }

TSimpleException::TSimpleException(
const std::exception& exception,
TString message)
std::string message)
: InnerException_(std::current_exception())
, Message_(std::move(message))
, What_(Message_ + "\n" + exception.what())
Expand All @@ -43,7 +43,7 @@ const char* TSimpleException::what() const noexcept
return What_.c_str();
}

const TString& TSimpleException::GetMessage() const
const std::string& TSimpleException::GetMessage() const
{
return Message_;
}
Expand Down
10 changes: 5 additions & 5 deletions library/cpp/yt/exception/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class TSimpleException
{ ex <<= std::forward<TValue>(operand) } -> std::same_as<TSimpleException&>;
};

explicit TSimpleException(TString message);
explicit TSimpleException(std::string message);
TSimpleException(
const std::exception& exception,
TString message);
std::string message);

const std::exception_ptr& GetInnerException() const;
const char* what() const noexcept override;

const TString& GetMessage() const;
const std::string& GetMessage() const;

const TAttributes& GetAttributes() const &;
TAttributes&& GetAttributes() &&;
Expand Down Expand Up @@ -65,8 +65,8 @@ class TSimpleException

private:
const std::exception_ptr InnerException_;
const TString Message_;
const TString What_;
const std::string Message_;
const std::string What_;

TAttributes Attributes_;
};
Expand Down
4 changes: 2 additions & 2 deletions library/cpp/yt/misc/guid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TGuid TGuid::FromString(TStringBuf str)
TGuid guid;
if (!FromString(str, &guid)) {
throw TSimpleException(Sprintf("Error parsing GUID \"%s\"",
TString(str).c_str()));
std::string(str).c_str()));
}
return guid;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ TGuid TGuid::FromStringHex32(TStringBuf str)
TGuid guid;
if (!FromStringHex32(str, &guid)) {
throw TSimpleException(Sprintf("Error parsing Hex32 GUID \"%s\"",
TString(str).c_str()));
str.data()));
}
return guid;
}
Expand Down
2 changes: 1 addition & 1 deletion library/cpp/yt/misc/thread_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TStringBuf TThreadName::ToStringBuf() const

////////////////////////////////////////////////////////////////////////////////

TThreadName::TThreadName(const TString& name)
TThreadName::TThreadName(TStringBuf name)
{
Length = std::min<int>(TThreadName::BufferCapacity - 1, name.length());
::memcpy(Buffer.data(), name.data(), Length);
Expand Down
2 changes: 1 addition & 1 deletion library/cpp/yt/misc/thread_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NYT {
struct TThreadName
{
TThreadName() = default;
TThreadName(const TString& name);
TThreadName(TStringBuf name);

TStringBuf ToStringBuf() const;

Expand Down
2 changes: 1 addition & 1 deletion library/cpp/yt/string/enum-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void FormatEnum(TStringBuilderBase* builder, T value, bool lowerCase)
}

template <class T>
TString FormatEnum(T value)
std::string FormatEnum(T value)
{
TStringBuilder builder;
FormatEnum(&builder, value, /*lowerCase*/ true);
Expand Down
8 changes: 4 additions & 4 deletions library/cpp/yt/string/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void ThrowMalformedEnumValueException(TStringBuf typeName, TStringBuf value)
}

template <bool ThrowOnError>
std::optional<TString> DecodeEnumValueImpl(TStringBuf value)
std::optional<std::string> DecodeEnumValueImpl(TStringBuf value)
{
auto camelValue = UnderscoreCaseToCamelCase(value);
auto underscoreValue = CamelCaseToUnderscoreCase(camelValue);
Expand All @@ -34,19 +34,19 @@ std::optional<TString> DecodeEnumValueImpl(TStringBuf value)

} // namespace NDetail

std::optional<TString> TryDecodeEnumValue(TStringBuf value)
std::optional<std::string> TryDecodeEnumValue(TStringBuf value)
{
return NDetail::DecodeEnumValueImpl<false>(value);
}

TString DecodeEnumValue(TStringBuf value)
std::string DecodeEnumValue(TStringBuf value)
{
auto decodedValue = NDetail::DecodeEnumValueImpl<true>(value);
YT_VERIFY(decodedValue);
return *decodedValue;
}

TString EncodeEnumValue(TStringBuf value)
std::string EncodeEnumValue(TStringBuf value)
{
return CamelCaseToUnderscoreCase(value);
}
Expand Down
8 changes: 4 additions & 4 deletions library/cpp/yt/string/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace NYT {

////////////////////////////////////////////////////////////////////////////////

std::optional<TString> TryDecodeEnumValue(TStringBuf value);
TString DecodeEnumValue(TStringBuf value);
TString EncodeEnumValue(TStringBuf value);
std::optional<std::string> TryDecodeEnumValue(TStringBuf value);
std::string DecodeEnumValue(TStringBuf value);
std::string EncodeEnumValue(TStringBuf value);

template <class T>
std::optional<T> TryParseEnum(TStringBuf str, bool enableUnknown = false);
Expand All @@ -24,7 +24,7 @@ template <class T>
void FormatEnum(TStringBuilderBase* builder, T value, bool lowerCase);

template <class T>
TString FormatEnum(T value);
std::string FormatEnum(T value);

////////////////////////////////////////////////////////////////////////////////

Expand Down
4 changes: 2 additions & 2 deletions library/cpp/yt/string/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,15 @@ TStringBuf FormatBool(bool value)

////////////////////////////////////////////////////////////////////////////////

void TruncateStringInplace(TString* string, int lengthLimit, TStringBuf truncatedSuffix)
void TruncateStringInplace(std::string* string, int lengthLimit, TStringBuf truncatedSuffix)
{
if (std::ssize(*string) > lengthLimit) {
string->resize(lengthLimit);
string->append(truncatedSuffix);
}
}

TString TruncateString(TString string, int lengthLimit, TStringBuf truncatedSuffix)
std::string TruncateString(std::string string, int lengthLimit, TStringBuf truncatedSuffix)
{
TruncateStringInplace(&string, lengthLimit, truncatedSuffix);
return string;
Expand Down
5 changes: 2 additions & 3 deletions library/cpp/yt/string/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ TStringBuf FormatBool(bool value);

inline constexpr TStringBuf DefaultTruncatedMessage = "...<truncated>";

void TruncateStringInplace(TString* string, int lengthLimit, TStringBuf truncatedSuffix = DefaultTruncatedMessage);

TString TruncateString(TString string, int lengthLimit, TStringBuf truncatedSuffix = DefaultTruncatedMessage);
void TruncateStringInplace(std::string* string, int lengthLimit, TStringBuf truncatedSuffix = DefaultTruncatedMessage);
std::string TruncateString(std::string string, int lengthLimit, TStringBuf truncatedSuffix = DefaultTruncatedMessage);

////////////////////////////////////////////////////////////////////////////////

Expand Down
4 changes: 2 additions & 2 deletions library/cpp/yt/string/unittests/guid_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace {

static_assert(CFormattable<TGuid>);

TString CanonicalToString(TGuid value)
std::string CanonicalToString(TGuid value)
{
return Sprintf("%x-%x-%x-%x",
value.Parts32[3],
Expand Down Expand Up @@ -48,7 +48,7 @@ TEST(TGuidTest, FormatAllSymbols)
TEST(TGuidTest, ByteOrder)
{
auto guid = TGuid::FromStringHex32("12345678ABCDEF0112345678ABCDEF01");
TString bytes{reinterpret_cast<const char*>(&(guid.Parts32[0])), 16};
std::string bytes{reinterpret_cast<const char*>(&(guid.Parts32[0])), 16};
EXPECT_EQ(HexEncode(bytes), "01EFCDAB7856341201EFCDAB78563412");
}

Expand Down

0 comments on commit 95fabce

Please sign in to comment.