From 020699724fe3ae16b5f9af218dcafdde6bd27150 Mon Sep 17 00:00:00 2001
From: Patrick Ziegler
Date: Sat, 10 Feb 2024 12:51:34 +0100
Subject: [PATCH] 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
---
CHANGELOG.md | 1 +
src/drawtypes/label.cpp | 9 +++++----
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 010b678f..c4050623 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp
index e5ba8911..4b58deae 100644
--- a/src/drawtypes/label.cpp
+++ b/src/drawtypes/label.cpp
@@ -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' : ' ');
}
}