Keyple Util C++ Library - 2.4.0
Component of the Keyple C++ middleware
Matcher.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include <cstdio>
15#include <sstream>
16#include <string>
17
18#include "keyple/core/util/cpp/Matcher.hpp"
19#include "keyple/core/util/cpp/Pattern.hpp"
20#include "keyple/core/util/cpp/exception/IndexOutOfBoundsException.hpp"
21
22namespace keyple {
23namespace core {
24namespace util {
25namespace cpp {
26
27using keyple::core::util::cpp::exception::IndexOutOfBoundsException;
28
29Matcher::Matcher(const Pattern* parent, const std::string& text)
30: mParentPattern(parent)
31, mText(text)
32{
33 /* Put fields to initial states */
34 reset();
35}
36
37bool
38Matcher::match(const int from, const int anchor) const
39{
40 (void)from;
41 (void)anchor;
42
43 if (std::regex_search(mText, mParentPattern->mPattern))
44 return true;
45
46 return false;
47}
48
49bool
50Matcher::matches()
51{
52 return match(mFrom, ENDANCHOR);
53}
54
55std::string
56Matcher::replaceAll(const std::string& replacement) const
57{
58 std::stringstream ss;
59
60 ss << std::regex_replace(mText, mParentPattern->mPattern, replacement);
61
62 return ss.str();
63}
64
65bool
66Matcher::find()
67{
68 int nextSearchIndex = mLast;
69 if (nextSearchIndex == mFirst)
70 nextSearchIndex++;
71
72 /* If next search starts before region, start it at region */
73 if (nextSearchIndex < mFrom)
74 nextSearchIndex = mFrom;
75
76 /* If next search starts beyond region then it fails */
77 if (nextSearchIndex > mTo) {
78 // for (int i = 0; i < (int)groups.size(); i++)
79 // groups[i] = -1;
80 return false;
81 }
82
83 return search(nextSearchIndex);
84}
85
86bool
87Matcher::find(const int start)
88{
89 int limit = getTextLength();
90
91 if (start < 0 || start > limit)
92 throw IndexOutOfBoundsException("Illegal start index");
93
94 reset();
95
96 return search(start);
97}
98
99const std::string
100Matcher::group(const int group) const
101{
102 if (group < 0 || group > static_cast<int>(mGroups.size()))
103 throw IndexOutOfBoundsException("No group" + std::to_string(group));
104
105 return mGroups[group];
106}
107
108const std::string
109Matcher::group() const
110{
111 return group(0);
112}
113
114bool
115Matcher::search(const int from)
116{
117 mSubs = mText.substr(from, mText.length() - from);
118
119 if (std::regex_search(mSubs, mGroups, mParentPattern->mPattern))
120 return true;
121
122 return false;
123}
124
125int
126Matcher::getTextLength() const
127{
128 return static_cast<int>(mText.length());
129}
130
131Matcher*
132Matcher::reset()
133{
134 mFirst = -1;
135 mLast = 0;
136 mOldLast = -1;
137
138 for (int i = 0; i < static_cast<int>(mGroups.size()); i++) {
139 } // groups[i] = -1;
140 for (int i = 0; i < static_cast<int>(mLocals.size()); i++) mLocals[i] = -1;
141
142 mLastAppendPosition = 0;
143 mFrom = 0;
144 mTo = getTextLength();
145
146 return this;
147}
148
149} /* namespace cpp */
150} /* namespace util */
151} /* namespace core */
152} /* namespace keyple */