Skip to content

Commit c170b0d

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 70ecea5 commit c170b0d

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
@@ -45,14 +45,14 @@ struct charset : std::bitset<256>
4545
// charset(const char (&s) [N]) :
4646
// charset(std::string_view(s, N - 1)) { }
4747

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

5858
class estring_view : public std::string_view

common/test/test.cpp

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

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

879879
EXPECT_EQ(estring_view("234423").to_uint64(), 234423);
880880
EXPECT_EQ(estring_view("-234423").to_int64(), -234423);

0 commit comments

Comments
 (0)