Skip to content

Commit 4ab18ab

Browse files
committed
make chars convert to unsigned first before comparing (solving issue #357)
1 parent 30c00c1 commit 4ab18ab

5 files changed

Lines changed: 13 additions & 9 deletions

File tree

include/ctre/atoms_characters.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ template <auto V> struct character {
4040
}
4141
}
4242
}
43-
return value == V;
43+
return static_cast<std::make_unsigned_t<CharT>>(value) == V;
4444
}
4545
};
4646

@@ -79,7 +79,7 @@ template <auto A, auto B> struct char_range {
7979
}
8080
}
8181
}
82-
return (value >= A) && (value <= B);
82+
return (static_cast<std::make_unsigned_t<CharT>>(value) >= A) && (static_cast<std::make_unsigned_t<CharT>>(value) <= B);
8383
}
8484
};
8585
using word_chars = set<char_range<'A','Z'>, char_range<'a','z'>, char_range<'0','9'>, character<'_'> >;

include/ctre/atoms_unicode.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ template <auto Type, auto Value> using make_property = property<std::remove_cons
2828
// unicode TS#18 level 1.2 general_category
2929
template <uni::detail::binary_prop Property> struct binary_property<uni::detail::binary_prop, Property> {
3030
template <typename CharT> inline static constexpr bool match_char(CharT c, const flags &) noexcept {
31-
return uni::detail::get_binary_prop<Property>(static_cast<char32_t>(c));
31+
return uni::detail::get_binary_prop<Property>(static_cast<char32_t>(static_cast<std::make_unsigned_t<CharT>>(c)));
3232
}
3333
};
3434

single-header/ctre-unicode.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ template <auto V> struct character {
15981598
}
15991599
}
16001600
}
1601-
return value == V;
1601+
return static_cast<std::make_unsigned_t<CharT>>(value) == V;
16021602
}
16031603
};
16041604

@@ -1637,7 +1637,7 @@ template <auto A, auto B> struct char_range {
16371637
}
16381638
}
16391639
}
1640-
return (value >= A) && (value <= B);
1640+
return (static_cast<std::make_unsigned_t<CharT>>(value) >= A) && (static_cast<std::make_unsigned_t<CharT>>(value) <= B);
16411641
}
16421642
};
16431643
using word_chars = set<char_range<'A','Z'>, char_range<'a','z'>, char_range<'0','9'>, character<'_'> >;
@@ -1899,7 +1899,7 @@ template <auto Type, auto Value> using make_property = property<std::remove_cons
18991899
// unicode TS#18 level 1.2 general_category
19001900
template <uni::detail::binary_prop Property> struct binary_property<uni::detail::binary_prop, Property> {
19011901
template <typename CharT> inline static constexpr bool match_char(CharT c, const flags &) noexcept {
1902-
return uni::detail::get_binary_prop<Property>(static_cast<char32_t>(c));
1902+
return uni::detail::get_binary_prop<Property>(static_cast<char32_t>(static_cast<std::make_unsigned_t<CharT>>(c)));
19031903
}
19041904
};
19051905

single-header/ctre.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ template <auto V> struct character {
15951595
}
15961596
}
15971597
}
1598-
return value == V;
1598+
return static_cast<std::make_unsigned_t<CharT>>(value) == V;
15991599
}
16001600
};
16011601

@@ -1634,7 +1634,7 @@ template <auto A, auto B> struct char_range {
16341634
}
16351635
}
16361636
}
1637-
return (value >= A) && (value <= B);
1637+
return (static_cast<std::make_unsigned_t<CharT>>(value) >= A) && (static_cast<std::make_unsigned_t<CharT>>(value) <= B);
16381638
}
16391639
};
16401640
using word_chars = set<char_range<'A','Z'>, char_range<'a','z'>, char_range<'0','9'>, character<'_'> >;
@@ -1896,7 +1896,7 @@ template <auto Type, auto Value> using make_property = property<std::remove_cons
18961896
// unicode TS#18 level 1.2 general_category
18971897
template <uni::detail::binary_prop Property> struct binary_property<uni::detail::binary_prop, Property> {
18981898
template <typename CharT> inline static constexpr bool match_char(CharT c, const flags &) noexcept {
1899-
return uni::detail::get_binary_prop<Property>(static_cast<char32_t>(c));
1899+
return uni::detail::get_binary_prop<Property>(static_cast<char32_t>(static_cast<std::make_unsigned_t<CharT>>(c)));
19001900
}
19011901
};
19021902

tests/matching3.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,7 @@ TEST_NOT_MATCH(233, "[^]", "");
246246
TEST_MATCH(234, "[^]+", "x");
247247
TEST_MATCH(235, "[^]?", "");
248248

249+
// issue #357 (unsigned chars vs signed chars)
250+
TEST_MATCH(236, "«««", "«««");
251+
252+

0 commit comments

Comments
 (0)