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