aboutsummaryrefslogtreecommitdiff
path: root/src/IsCharacterInSet.cpp
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2018-07-01 16:58:37 -0700
committerRichard Walters <rwalters@digitalstirling.com>2018-07-01 16:58:37 -0700
commit16b5c56c4ecbbb5c6153f9e16228a8d8cf95c50d (patch)
tree9bf5689b45b08c4d6a9319651a6ca80e4f7830c6 /src/IsCharacterInSet.cpp
parentcdc3f449812d0d45a3ea271636d669eb05ba3751 (diff)
Refactoring
Added CharacterSet as a class to represent character sets, allowing us to build singletons and composite character sets more concisely.
Diffstat (limited to 'src/IsCharacterInSet.cpp')
-rw-r--r--src/IsCharacterInSet.cpp84
1 files changed, 68 insertions, 16 deletions
diff --git a/src/IsCharacterInSet.cpp b/src/IsCharacterInSet.cpp
index 82625e9..1b2882f 100644
--- a/src/IsCharacterInSet.cpp
+++ b/src/IsCharacterInSet.cpp
@@ -9,27 +9,79 @@
#include "IsCharacterInSet.hpp"
+#include <set>
+
namespace Uri {
- bool IsCharacterInSet(
- char c,
- std::initializer_list< char > characterSet
- ) {
+ /**
+ * This contains the private properties of the CharacterSet class.
+ */
+ struct CharacterSet::Impl {
+ /**
+ * This holds the characters in the set.
+ */
+ std::set< char > charactersInSet;
+ };
+
+ CharacterSet::~CharacterSet() = default;
+ CharacterSet::CharacterSet(const CharacterSet& other)
+ : impl_(new Impl(*other.impl_))
+ {
+ }
+ CharacterSet::CharacterSet(CharacterSet&& other) = default;
+ CharacterSet& CharacterSet::operator=(const CharacterSet& other) {
+ if (this != &other) {
+ *impl_ = *other.impl_;
+ }
+ return *this;
+ }
+ CharacterSet& CharacterSet::operator=(CharacterSet&& other) = default;
+
+ CharacterSet::CharacterSet()
+ : impl_(new Impl)
+ {
+ }
+
+ CharacterSet::CharacterSet(char c)
+ : impl_(new Impl)
+ {
+ (void)impl_->charactersInSet.insert(c);
+ }
+
+ CharacterSet::CharacterSet(char first, char last)
+ : impl_(new Impl)
+ {
+ for (char c = first; c < last + 1; ++c) {
+ (void)impl_->charactersInSet.insert(c);
+ }
+ }
+
+ CharacterSet::CharacterSet(
+ std::initializer_list< const CharacterSet > characterSets
+ )
+ : impl_(new Impl)
+ {
for (
- auto charInSet = characterSet.begin();
- charInSet != characterSet.end();
- ++charInSet
+ auto characterSet = characterSets.begin();
+ characterSet != characterSets.end();
+ ++characterSet
) {
- const auto first = *charInSet++;
- const auto last = *charInSet;
- if (
- (c >= first)
- && (c <= last)
- ) {
- return true;
- }
+ impl_->charactersInSet.insert(
+ characterSet->impl_->charactersInSet.begin(),
+ characterSet->impl_->charactersInSet.end()
+ );
}
- return false;
+ }
+
+ bool CharacterSet::Contains(char c) const {
+ return impl_->charactersInSet.find(c) != impl_->charactersInSet.end();
+ }
+
+ bool IsCharacterInSet(
+ char c,
+ const CharacterSet& characterSet
+ ) {
+ return characterSet.Contains(c);
}
}