fix(label): Misbehaving min-length tokens with non-ASCII characters (#3090)

* Changed bit count for label to use UTF8 standard

Fixes #3074

* label: Calculate length only once

The length calculation has to traverse the whole string

---------

Co-authored-by: nklloyd <nicholask.lloyd@gmail.com>
This commit is contained in:
Patrick Ziegler 2024-02-10 12:51:34 +01:00 committed by GitHub
parent 8e04f15ed6
commit 020699724f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 4 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Token min-length calculations would behave differently when non-ASCII characters appear in the token ([`#3074`](https://github.com/polybar/polybar/issues/3074), [`#3087`](https://github.com/polybar/polybar/pull/3087)) by [@nklloyd](https://github.com/nklloyd)
- `internal/backlight`: Module could display the literal `%percentage%` token if the backlight reports a value of 0 at startup ([`#3081`](https://github.com/polybar/polybar/pull/3081)) by [@unclechu](https://github.com/unclechu)
## [3.7.1] - 2023-11-27

View File

@ -82,14 +82,15 @@ namespace drawtypes {
for (auto&& tok : m_tokens) {
string repl{replacement};
size_t len = string_util::char_len(repl);
if (token == tok.token) {
if (tok.max != 0_z && string_util::char_len(repl) > tok.max) {
if (tok.max != 0_z && len > tok.max) {
repl = string_util::utf8_truncate(std::move(repl), tok.max) + tok.suffix;
} else if (tok.min != 0_z && repl.length() < tok.min) {
} else if (tok.min != 0_z && len < tok.min) {
if (tok.rpadding) {
repl.append(tok.min - repl.length(), ' ');
repl.append(tok.min - len, ' ');
} else {
repl.insert(0_z, tok.min - repl.length(), tok.zpad ? '0' : ' ');
repl.insert(0_z, tok.min - len, tok.zpad ? '0' : ' ');
}
}