Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eventsub/string): minor cleanups #5973

Merged
merged 3 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
- Bugfix: Fixed channel point redemptions with messages not showing up if PubSub is disconnected. (#5948)
- Bugfix: Fixed the input font not immediately updating when zooming in/out. (#5960)
- Dev: Subscriptions to PubSub channel points redemption topics now use no auth token, making it continue to work during PubSub shutdown. (#5947)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915, #5916, #5930, #5935, #5932, #5943, #5952, #5953, #5968)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915, #5916, #5930, #5935, #5932, #5943, #5952, #5953, #5968, #5973)
- Dev: Remove unneeded platform specifier for toasts. (#5914)
- Dev: Highlight checks now use non-capturing groups for the boundaries. (#5784)
- Dev: Removed unused PubSub whisper code. (#5898)
Expand Down
47 changes: 23 additions & 24 deletions lib/twitch-eventsub-ws/include/twitch-eventsub-ws/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ namespace chatterino::eventsub::lib {
/// is **not** null-terminated.
struct String {
constexpr String() noexcept = default;
constexpr String(std::string_view sv)
String(std::string_view sv)
: flags(sv.length() & LENGTH_MASK)
{
if (sv.length() <= SSO_CAPACITY)
char *data = this->storage.inPlace;

if (sv.length() > SSO_CAPACITY)
{
std::memcpy(this->storage.inPlace, sv.data(), sv.length());
return;
data = new char[sv.length()];
this->flags |= ALLOC_BIT;
this->storage.data = data;
}

this->flags |= ALLOC_BIT;
auto *data = new char[sv.length()];
std::memcpy(data, sv.data(), sv.length());
this->storage.data = data;
for (size_t i = 0; i < sv.length(); i++)
{
data[i] = sv[i];
}
}

~String()
Expand Down Expand Up @@ -117,7 +120,7 @@ struct String {
return this->storage.qt;
}

QAnyStringView view() const noexcept
constexpr QAnyStringView view() const noexcept
{
if (this->isQt())
{
Expand Down Expand Up @@ -158,15 +161,6 @@ struct String {
static_assert((LENGTH_MASK & ALLOC_BIT) == 0);
static_assert((LENGTH_MASK & QT_BIT) == 0);

constexpr size_t length() const noexcept
{
if ((this->flags & QT_BIT) != 0)
{
return this->storage.qt.length();
}
return this->flags & LENGTH_MASK;
}

mutable union Storage {
constexpr Storage() noexcept
{
Expand All @@ -178,9 +172,6 @@ struct String {
Storage(const Storage &) = delete;
Storage &operator=(const Storage &) = delete;

// we can memcpy QStrings as they're relocatable
static_assert(QTypeInfo<QString>::isRelocatable != 0);

Storage(Storage &&other) noexcept
{
Storage::move(std::addressof(other), this);
Expand All @@ -198,10 +189,18 @@ struct String {
private:
static void move(Storage *from, Storage *to)
{
// we can memcpy QStrings as they're relocatable
static_assert(QTypeInfo<QString>::isRelocatable != 0);

// copy `from` -> `to`
std::memcpy(to, from, sizeof(Storage));
// clear `from`
std::memset(from, 0, sizeof(Storage));
std::memcpy(static_cast<void *>(to), from, sizeof(Storage));
#ifndef NDEBUG
// Mark `from` as unused.
// Because the parent `String` sets the flags to 0,
// we don't _need_ to overwrite the data. In debug mode we write a
// tombstone value here.
std::memset(static_cast<void *>(from), 0xff, sizeof(Storage));
#endif
}
} storage;
static_assert(sizeof(Storage) == sizeof(QString));
Expand Down
31 changes: 30 additions & 1 deletion lib/twitch-eventsub-ws/tests/src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,39 @@ TEST(String, MoveAssign)
TEST(String, QtLifetime)
{
QString qt;

{
String s(REALLY_LONG);
qt = s.qt();
ASSERT_FALSE(qt.isDetached()); // s holds the string too
}
ASSERT_TRUE(qt.isDetached());

// move assignments
{
String s(REALLY_LONG);
qt = s.qt();
ASSERT_FALSE(qt.isDetached());
s = {};
ASSERT_TRUE(qt.isDetached());
}
ASSERT_TRUE(qt.isDetached());

{
String s(REALLY_LONG);
qt = s.qt();
ASSERT_FALSE(qt.isDetached());
s = {LONGEST_SSO};
ASSERT_TRUE(qt.isDetached());
}
ASSERT_TRUE(qt.isDetached());

{
String s(REALLY_LONG);
qt = s.qt();
ASSERT_TRUE(!qt.isDetached()); // s holds the string too
ASSERT_FALSE(qt.isDetached());
s = {REALLY_LONG};
ASSERT_TRUE(qt.isDetached());
}
ASSERT_TRUE(qt.isDetached());
}
Loading