Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit a62762e

Browse files
committed
fix(#1424): trim user input to prevent user not able to exit from interactive chat
1 parent 176ae70 commit a62762e

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

engine/commands/chat_completion_cmd.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "server_start_cmd.h"
88
#include "utils/engine_constants.h"
99
#include "utils/logging_utils.h"
10+
#include "utils/string_utils.h"
1011

1112
namespace commands {
1213
namespace {
@@ -100,6 +101,8 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
100101
break;
101102
}
102103
}
104+
105+
string_utils::Trim(user_input);
103106
if (user_input == kExitChat) {
104107
break;
105108
}

engine/test/components/test_string_utils.cc

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#include <cmath>
2-
#include <sstream>
31
#include "gtest/gtest.h"
42
#include "utils/string_utils.h"
3+
54
class StringUtilsTestSuite : public ::testing::Test {};
5+
66
TEST_F(StringUtilsTestSuite, ParsePrompt) {
77
{
88
std::string prompt =
@@ -94,3 +94,87 @@ TEST_F(StringUtilsTestSuite, TestEndsWithWithEmptySuffix) {
9494
auto suffix = "";
9595
EXPECT_TRUE(string_utils::EndsWith(input, suffix));
9696
}
97+
98+
TEST_F(StringUtilsTestSuite, EmptyString) {
99+
std::string s = "";
100+
string_utils::Trim(s);
101+
EXPECT_EQ(s, "");
102+
}
103+
104+
TEST_F(StringUtilsTestSuite, NoWhitespace) {
105+
std::string s = "hello";
106+
string_utils::Trim(s);
107+
EXPECT_EQ(s, "hello");
108+
}
109+
110+
TEST_F(StringUtilsTestSuite, LeadingWhitespace) {
111+
std::string s = " hello";
112+
string_utils::Trim(s);
113+
EXPECT_EQ(s, "hello");
114+
}
115+
116+
TEST_F(StringUtilsTestSuite, TrailingWhitespace) {
117+
std::string s = "hello ";
118+
string_utils::Trim(s);
119+
EXPECT_EQ(s, "hello");
120+
}
121+
122+
TEST_F(StringUtilsTestSuite, BothEndsWhitespace) {
123+
std::string s = " hello ";
124+
string_utils::Trim(s);
125+
EXPECT_EQ(s, "hello");
126+
}
127+
128+
TEST_F(StringUtilsTestSuite, ExitString) {
129+
std::string s = "exit() ";
130+
string_utils::Trim(s);
131+
EXPECT_EQ(s, "exit()");
132+
}
133+
134+
TEST_F(StringUtilsTestSuite, AllWhitespace) {
135+
std::string s = " ";
136+
string_utils::Trim(s);
137+
EXPECT_EQ(s, "");
138+
}
139+
140+
TEST_F(StringUtilsTestSuite, MixedWhitespace) {
141+
std::string s = " \t\n hello world \r\n ";
142+
string_utils::Trim(s);
143+
EXPECT_EQ(s, "hello world");
144+
}
145+
146+
TEST_F(StringUtilsTestSuite, EqualStrings) {
147+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("hello", "hello"));
148+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("WORLD", "WORLD"));
149+
}
150+
151+
TEST_F(StringUtilsTestSuite, DifferentCaseStrings) {
152+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("Hello", "hElLo"));
153+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("WORLD", "world"));
154+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("MiXeD", "mIxEd"));
155+
}
156+
157+
TEST_F(StringUtilsTestSuite, EmptyStrings) {
158+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("", ""));
159+
}
160+
161+
TEST_F(StringUtilsTestSuite, DifferentStrings) {
162+
EXPECT_FALSE(string_utils::EqualsIgnoreCase("hello", "world"));
163+
EXPECT_FALSE(string_utils::EqualsIgnoreCase("HELLO", "hello world"));
164+
}
165+
166+
TEST_F(StringUtilsTestSuite, DifferentLengthStrings) {
167+
EXPECT_FALSE(string_utils::EqualsIgnoreCase("short", "longer string"));
168+
EXPECT_FALSE(string_utils::EqualsIgnoreCase("LONG STRING", "long"));
169+
}
170+
171+
TEST_F(StringUtilsTestSuite, SpecialCharacters) {
172+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("Hello!", "hElLo!"));
173+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("123 ABC", "123 abc"));
174+
EXPECT_FALSE(string_utils::EqualsIgnoreCase("Hello!", "Hello"));
175+
}
176+
177+
TEST_F(StringUtilsTestSuite, UnicodeCharacters) {
178+
EXPECT_TRUE(string_utils::EqualsIgnoreCase("café", "CAFÉ"));
179+
EXPECT_FALSE(string_utils::EqualsIgnoreCase("café", "cafe"));
180+
}

engine/utils/string_utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ struct ParsePromptResult {
1313
std::string ai_prompt;
1414
};
1515

16+
inline void Trim(std::string& s) {
17+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
18+
return !std::isspace(ch);
19+
}));
20+
s.erase(std::find_if(s.rbegin(), s.rend(),
21+
[](unsigned char ch) { return !std::isspace(ch); })
22+
.base(),
23+
s.end());
24+
}
25+
1626
inline bool EqualsIgnoreCase(const std::string& a, const std::string& b) {
1727
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
1828
[](char a, char b) { return tolower(a) == tolower(b); });

0 commit comments

Comments
 (0)