Skip to content

Commit 7362ce3

Browse files
misc: use hyprutils for string replaceAll (#512)
1 parent 264fb8b commit 7362ce3

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

src/renderer/widgets/IWidget.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chrono>
66
#include <unistd.h>
77
#include <pwd.h>
8+
#include <hyprutils/string/String.hpp>
89
#include <hyprutils/string/VarList.hpp>
910

1011
using namespace Hyprutils::String;
@@ -48,16 +49,6 @@ Vector2D IWidget::posFromHVAlign(const Vector2D& viewport, const Vector2D& size,
4849
return pos;
4950
}
5051

51-
static void replaceAll(std::string& str, const std::string& from, const std::string& to) {
52-
if (from.empty())
53-
return;
54-
size_t pos = 0;
55-
while ((pos = str.find(from, pos)) != std::string::npos) {
56-
str.replace(pos, from.length(), to);
57-
pos += to.length();
58-
}
59-
}
60-
6152
static void replaceAllAttempts(std::string& str) {
6253

6354
const size_t ATTEMPTS = g_pHyprlock->getPasswordFailedAttempts();
@@ -133,29 +124,29 @@ IWidget::SFormatResult IWidget::formatString(std::string in) {
133124
Debug::log(WARN, "Error in formatString, user_gecos null. Errno: ", errno);
134125

135126
IWidget::SFormatResult result;
136-
replaceAll(in, "$DESC", std::string{user_gecos ? user_gecos : ""});
137-
replaceAll(in, "$USER", std::string{username ? username : ""});
138-
replaceAll(in, "<br/>", std::string{"\n"});
127+
replaceInString(in, "$DESC", std::string{user_gecos ? user_gecos : ""});
128+
replaceInString(in, "$USER", std::string{username ? username : ""});
129+
replaceInString(in, "<br/>", std::string{"\n"});
139130

140131
if (in.contains("$TIME12")) {
141-
replaceAll(in, "$TIME12", getTime12h());
132+
replaceInString(in, "$TIME12", getTime12h());
142133
result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000;
143134
}
144135

145136
if (in.contains("$TIME")) {
146-
replaceAll(in, "$TIME", getTime());
137+
replaceInString(in, "$TIME", getTime());
147138
result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000;
148139
}
149140

150141
if (in.contains("$FAIL")) {
151142
const auto FAIL = g_pAuth->getLastFailText();
152-
replaceAll(in, "$FAIL", FAIL.has_value() ? FAIL.value() : "");
143+
replaceInString(in, "$FAIL", FAIL.has_value() ? FAIL.value() : "");
153144
result.allowForceUpdate = true;
154145
}
155146

156147
if (in.contains("$PROMPT")) {
157148
const auto PROMPT = g_pAuth->getLastPrompt();
158-
replaceAll(in, "$PROMPT", PROMPT.has_value() ? PROMPT.value() : "");
149+
replaceInString(in, "$PROMPT", PROMPT.has_value() ? PROMPT.value() : "");
159150
result.allowForceUpdate = true;
160151
}
161152

src/renderer/widgets/PasswordInputField.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@
22
#include "../Renderer.hpp"
33
#include "../../core/hyprlock.hpp"
44
#include "../../core/Auth.hpp"
5+
#include <hyprutils/string/String.hpp>
56
#include <algorithm>
67

7-
static void replaceAll(std::string& str, const std::string& from, const std::string& to) {
8-
if (from.empty())
9-
return;
10-
size_t pos = 0;
11-
while ((pos = str.find(from, pos)) != std::string::npos) {
12-
str.replace(pos, from.length(), to);
13-
pos += to.length();
14-
}
15-
}
8+
using namespace Hyprutils::String;
169

1710
CPasswordInputField::CPasswordInputField(const Vector2D& viewport_, const std::unordered_map<std::string, std::any>& props, const std::string& output) :
1811
outputStringPort(output), shadow(this, props, viewport_) {
@@ -325,11 +318,11 @@ void CPasswordInputField::updatePlaceholder() {
325318
if (displayFail) {
326319
g_pHyprlock->addTimer(std::chrono::milliseconds(configFailTimeoutMs), failTimeoutCallback, nullptr);
327320
placeholder.currentText = configFailText;
328-
replaceAll(placeholder.currentText, "$FAIL", AUTHFEEDBACK);
329-
replaceAll(placeholder.currentText, "$ATTEMPTS", std::to_string(placeholder.failedAttempts));
321+
replaceInString(placeholder.currentText, "$FAIL", AUTHFEEDBACK);
322+
replaceInString(placeholder.currentText, "$ATTEMPTS", std::to_string(placeholder.failedAttempts));
330323
} else {
331324
placeholder.currentText = configPlaceholderText;
332-
replaceAll(placeholder.currentText, "$PROMPT", AUTHFEEDBACK);
325+
replaceInString(placeholder.currentText, "$PROMPT", AUTHFEEDBACK);
333326
}
334327

335328
placeholder.resourceID =

0 commit comments

Comments
 (0)