Skip to content
Open
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
21 changes: 21 additions & 0 deletions include/util/string.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

const std::string WHITESPACE = " \n\r\t\f\v";

Expand Down Expand Up @@ -31,6 +34,24 @@ inline std::string toLower(const std::string& str) {
return result;
}

// Finds the last occurrence of `c` that is not nested inside parentheses,
// scanning from the end. Unbalanced brackets are tolerated: a stray '(' never
// pushes the depth below zero, and a stray ')' only shields what precedes it.
// Returns std::string::npos when there is no such occurrence.
inline size_t rfindOutsideParens(std::string_view s, char c) {
int depth = 0;
for (size_t i = s.size(); i-- > 0;) {
if (s[i] == ')') {
++depth;
} else if (s[i] == '(') {
if (depth > 0) --depth;
} else if (s[i] == c && depth == 0) {
return i;
}
}
return std::string::npos;
}

inline std::vector<std::string> split(std::string_view s, std::string_view delimiter,
int max_splits = -1) {
std::vector<std::string> result;
Expand Down
16 changes: 7 additions & 9 deletions src/modules/hyprland/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,15 @@ void Language::onEvent(const std::string& ev) {
spdlog::warn("hyprland language received malformed event payload: {}", ev);
return;
}
// Last comma before variants parenthesis, eg:
// Both halves of "<keyboard>,<layout>" may contain commas of their own: the
// keyboard in its vendor string, the layout inside its variant parentheses, eg
// activelayout>>micro-star-int'l-co.,-ltd.-msi-gk50-elite-gaming-keyboard,English (US, intl.,
// with dead keys)
std::string beforeParenthesis;
auto parenthesisPos = payload.find_last_of('(');
if (parenthesisPos == std::string::npos) {
beforeParenthesis = payload;
} else {
beforeParenthesis = payload.substr(0, parenthesisPos);
}
const auto layoutSeparator = beforeParenthesis.find_last_of(',');
// The separator is therefore the last comma outside parentheses. Splitting on
// the last '(' instead would assume every bracket belongs to the layout, but
// keyboard names carry them too, eg "ite-tech.-inc.-ite-device(8910)-keyboard",
// which left no comma to find and dropped the event entirely (#4586).
const auto layoutSeparator = rfindOutsideParens(payload, ',');
if (layoutSeparator == std::string::npos) {
spdlog::warn("hyprland language received malformed layout payload: {}", ev);
return;
Expand Down
1 change: 1 addition & 0 deletions test/utils/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test_src = files(
'JsonParser.cpp',
'SafeSignal.cpp',
'format.cpp',
'string.cpp',
'sleeper_thread.cpp',
'command.cpp',
'command_line_stream.cpp',
Expand Down
64 changes: 64 additions & 0 deletions test/utils/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "util/string.hpp"

#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#else
#include <catch2/catch.hpp>
#endif

// The layout half of a hyprland "activelayout" payload, split the way
// Language::onEvent splits it.
static std::string layoutOf(const std::string& payload) {
const auto sep = rfindOutsideParens(payload, ',');
if (sep == std::string::npos) return "";
return payload.substr(sep + 1);
}

TEST_CASE("rfindOutsideParens", "[string]") {
SECTION("returns npos when the character is absent") {
REQUIRE(rfindOutsideParens("abc", ',') == std::string::npos);
REQUIRE(rfindOutsideParens("", ',') == std::string::npos);
}

SECTION("finds the last match when no parentheses are present") {
REQUIRE(rfindOutsideParens("a,b,c", ',') == 3);
}

SECTION("ignores matches nested inside parentheses") {
REQUIRE(rfindOutsideParens("a,b(c,d)", ',') == 1);
REQUIRE(rfindOutsideParens("a(b,c)d(e,f)", ',') == std::string::npos);
}

SECTION("handles nested parentheses") { REQUIRE(rfindOutsideParens("a,b(c(d,e),f)", ',') == 1); }

SECTION("tolerates unbalanced parentheses") {
// A stray '(' left of the match must not shield it.
REQUIRE(rfindOutsideParens("a(b,c", ',') == 3);
// A stray ')' must not drive the depth negative and swallow later matches.
REQUIRE(rfindOutsideParens("a)b,c", ',') == 3);
}
}

// Regression test for https://github.com/Alexays/Waybar/issues/4586
TEST_CASE("hyprland activelayout payload split", "[string]") {
SECTION("keyboard name containing brackets, layout without") {
// Splitting on the last '(' picked the keyboard's own bracket, leaving no
// comma behind it, so the event was discarded and the layout never updated.
REQUIRE(layoutOf("ite-tech.-inc.-ite-device(8910)-keyboard,Russian") == "Russian");
}

SECTION("layout containing commas inside its variant parentheses") {
REQUIRE(layoutOf("micro-star-int'l-co.,-ltd.-msi-gk50-elite-gaming-keyboard,"
"English (US, intl., with dead keys)") ==
"English (US, intl., with dead keys)");
}

SECTION("brackets in the keyboard name and commas in the layout variant") {
REQUIRE(layoutOf("ite-tech.-inc.-ite-device(8910)-keyboard,English (US, intl.)") ==
"English (US, intl.)");
}

SECTION("plain keyboard name and layout") {
REQUIRE(layoutOf("at-translated-set-2-keyboard,English (US)") == "English (US)");
}
}