libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_pattern.hpp
Go to the documentation of this file.
1/*! \file
2 * \brief gbl::Pattern C++ bindings for GblPattern
3 * \ingroup strings
4 *
5 * This file contains the type and OO API for gbl::Pattern,
6 * the C++ binding layer around GblPattern.
7 *
8 * \author 2023 Falco Girgis
9 * \copyright MIT License
10 */
11
12#ifndef GIMBAL_PATTERN_HPP
13#define GIMBAL_PATTERN_HPP
14
15#include "gimbal_pattern.h"
16#include <string>
17#include <iostream>
18#include <compare>
19
20namespace gbl {
21
22/*! OO C++ binding object around GblPattern
23 * \ingroup strings
24 *
25 * gbl::Pattern represents a compiled regular
26 * expression string, providing an API around
27 * the pattern matching/regex engine.
28 *
29 * \sa GblPattern
30 */
31class Pattern {
32private:
33 const GblPattern* pPattern_ = nullptr;
34
35public:
36 Pattern(const char* pRegExp) noexcept:
37 pPattern_(GblPattern_create(pRegExp)) {}
38
39 Pattern(const GblPattern* pPattern) noexcept:
40 pPattern_(pPattern) {}
41
42 Pattern(const Pattern& rhs) noexcept:
43 pPattern_(GblPattern_ref(rhs.pPattern_)) {}
44
45 Pattern(Pattern&& rhs) noexcept:
46 pPattern_(rhs.pPattern_)
47 {
48 rhs.pPattern_ = nullptr;
49 }
50
51 ~Pattern() noexcept {
52 GblPattern_unref(pPattern_);
53 }
54
55 bool isValid() const noexcept { return pPattern_; }
56
57 std::size_t refCount() const noexcept { return GblPattern_refCount(pPattern_); }
58
59 std::size_t bytes() const noexcept { return GblPattern_bytes(pPattern_); }
60
61 std::string string() const noexcept;
62
63 operator const GblPattern*() const noexcept { return pPattern_; }
64
65 Pattern& operator=(const char* pPattern) noexcept {
66 GblPattern_unref(pPattern_);
67 pPattern_ = GblPattern_create(pPattern);
68 return *this;
69 }
70
71 Pattern& operator=(const GblPattern* pPattern) noexcept {
72 GblPattern_unref(pPattern_);
73 pPattern_ = pPattern;
74 return *this;
75 }
76
77 Pattern& operator=(const Pattern& rhs) noexcept {
78 GblPattern_unref(pPattern_);
79 pPattern_ = GblPattern_ref(rhs.pPattern_);
80 return *this;
81 }
82
83 Pattern& operator=(Pattern&& rhs) noexcept {
84 GblPattern_unref(pPattern_);
85 pPattern_ = rhs.pPattern_;
86 rhs.pPattern_ = nullptr;
87 return *this;
88 }
89
90 bool match(const char* pString,
91 GblStringView* pView=nullptr,
92 int* pCount=nullptr) const noexcept
93 {
94 return GblPattern_match(*this, pString, pView, pCount);
95 }
96
97 bool matchNot(const char* pString,
98 GblStringView* pView=nullptr,
99 int* pCount=nullptr) const noexcept
100 {
101 return GblPattern_matchNot(*this, pString, pView, pCount);
102 }
103
104 bool matchExact(const char* pString) const noexcept {
105 return GblPattern_matchExact(*this, pString);
106 }
107
108 bool matchCount(const char* pString) const noexcept {
109 return GblPattern_matchCount(*this, pString);
110 }
111
112 friend std::ostream& operator<<(std::ostream &os, const Pattern& pat) {
113 os << pat.string();
114 return os;
115 }
116};
117
118inline auto operator<=>(const Pattern& lhs, const GblPattern* pRhs) noexcept {
119 const auto result = GblPattern_compare(lhs, pRhs);
120
121 if(result < 0)
122 return std::strong_ordering::less;
123 else if(result > 0)
124 return std::strong_ordering::greater;
125 else
126 return std::strong_ordering::equal;
127}
128
129}
130
131#endif // GIMBAL_PATTERN_HPP
OO C++ binding object around GblPattern.
const GblPattern * GblPattern_ref(const GblPattern *pSelf)
Returns a new reference to an existing pattern, incrementing its refcount.
GblRefCount GblPattern_refCount(const GblPattern *pSelf)
Returns the number of active references held to the given compiled pattern.
size_t GblPattern_matchCount(const GblPattern *pSelf, const char *pString)
Returns the number of pattern matches found in pString.
GblBool GblPattern_matchExact(const GblPattern *pSelf, const char *pString)
Returns GBL_TRUE if the given string EXACTLY matches the given pattern or GBL_FALSE otherwise.
const GblPattern * GblPattern_create(const char *pRegExp)
Compiles the given regular expression into a pre-processed GblPattern.
size_t GblPattern_bytes(const GblPattern *pSelf)
Counts the total size of a compiled pattern and returns it in bytes.
GblRefCount GblPattern_unref(const GblPattern *pSelf)
Releases a reference to a pattern, deallocating it upon reaching zero.