Skip to content

Commit 0196517

Browse files
Moonpucklihuiba
authored andcommitted
Fix incorrect char usage in estring trim function
The trim function previously used 'char' type for comparing characters, which causes sign-extension issues when character values are in the range 128~255. Change to use 'unsigned char' ensures correct comparison and fixes the bug.
1 parent 3b4be8f commit 0196517

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

common/estring.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ struct charset : std::bitset<256>
4646
// charset(const char (&s) [N]) :
4747
// charset(std::string_view(s, N - 1)) { }
4848

49-
// bool test(char ch) const
50-
// {
51-
// return std::bitset<256>::test((unsigned char)ch);
52-
// }
53-
// bitset& set(char ch, bool value = true)
54-
// {
55-
// return std::bitset<256>::set((unsigned char)ch, value);
56-
// }
49+
bool test(char ch) const
50+
{
51+
return std::bitset<256>::test((unsigned char)ch);
52+
}
53+
bitset& set(char ch, bool value = true)
54+
{
55+
return std::bitset<256>::set((unsigned char)ch, value);
56+
}
5757
};
5858

5959
class estring;

common/test/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,9 @@ TEST(estring, test)
874874
auto ps = estring::snprintf("%d%d%d", 2, 3, 4);
875875
EXPECT_EQ(ps, "234");
876876

877-
estring as = " \tasdf \t\r\n";
877+
estring as = " \tasdf中文 \t\r\n";
878878
auto trimmed = as.trim();
879-
EXPECT_EQ(trimmed, "asdf");
879+
EXPECT_EQ(trimmed, "asdf中文");
880880

881881
EXPECT_EQ(estring_view("234423").to_uint64(), 234423);
882882
EXPECT_EQ(estring_view("-234423").to_int64(), -234423);

0 commit comments

Comments
 (0)