aboutsummaryrefslogtreecommitdiff
path: root/src/CharacterSet.cpp
blob: d0b31a4d6aa981653b40894922385566d6d6513d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 * @file IsCharacterInSet.cpp
 *
 * This module contains the implementation of the
 * Uri::CharacterSet class.
 *
 * © 2018 by Richard Walters
 */

#include "CharacterSet.hpp"

#include <algorithm>
#include <set>

namespace Uri {

    /**
     * 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() noexcept = default;
    CharacterSet::CharacterSet(const CharacterSet& other)
        : impl_(new Impl(*other.impl_))
    {
    }
    CharacterSet::CharacterSet(CharacterSet&& other) noexcept = default;
    CharacterSet& CharacterSet::operator=(const CharacterSet& other) {
        if (this != &other) {
            *impl_ = *other.impl_;
        }
        return *this;
    }
    CharacterSet& CharacterSet::operator=(CharacterSet&& other) noexcept = 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)
    {
        if (first > last) {
            std::swap(first, last);
        }
        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 characterSet = characterSets.begin();
            characterSet != characterSets.end();
            ++characterSet
        ) {
            impl_->charactersInSet.insert(
                characterSet->impl_->charactersInSet.begin(),
                characterSet->impl_->charactersInSet.end()
            );
        }
    }

    bool CharacterSet::Contains(char c) const {
        return impl_->charactersInSet.find(c) != impl_->charactersInSet.end();
    }

}