aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2019-12-02 20:37:23 -0800
committerRichard Walters <rwalters@digitalstirling.com>2019-12-02 20:37:23 -0800
commitcf01ac1f05f1b339882e09ba6fc7748be3f78575 (patch)
tree635427453fc816160f218ffa235e1cfe30687272
parent605f42feb57f5ef1f988a62730f65afd9923c417 (diff)
Support first > last for range constructor of CharacterSet
-rw-r--r--src/CharacterSet.cpp4
-rw-r--r--test/src/CharacterSetTests.cpp14
2 files changed, 18 insertions, 0 deletions
diff --git a/src/CharacterSet.cpp b/src/CharacterSet.cpp
index 5287806..d0b31a4 100644
--- a/src/CharacterSet.cpp
+++ b/src/CharacterSet.cpp
@@ -9,6 +9,7 @@
#include "CharacterSet.hpp"
+#include <algorithm>
#include <set>
namespace Uri {
@@ -51,6 +52,9 @@ namespace Uri {
CharacterSet::CharacterSet(char first, char last)
: impl_(new Impl)
{
+ if (first > last) {
+ std::swap(first, last);
+ }
for (char c = first; c < last + 1; ++c) {
(void)impl_->charactersInSet.insert(c);
}
diff --git a/test/src/CharacterSetTests.cpp b/test/src/CharacterSetTests.cpp
index 7013127..3663ee6 100644
--- a/test/src/CharacterSetTests.cpp
+++ b/test/src/CharacterSetTests.cpp
@@ -43,6 +43,20 @@ TEST(CharacterSetTests, RangeConstructor) {
}
}
+TEST(CharacterSetTests, Range_Constructor_Reversed) {
+ Uri::CharacterSet cs('G', 'A');
+ for (char c = 0; c < 0x7F; ++c) {
+ if (
+ (c >= 'A')
+ && (c <= 'G')
+ ) {
+ ASSERT_TRUE(cs.Contains(c));
+ } else {
+ ASSERT_FALSE(cs.Contains(c));
+ }
+ }
+}
+
TEST(CharacterSetTests, InitializerListConstructor) {
Uri::CharacterSet cs1{'X'};
for (char c = 0; c < 0x7F; ++c) {