NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
stringexpression.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2020 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#ifndef STRINGEXPRESSION_HPP
20#define STRINGEXPRESSION_HPP
21
22#include <string>
23#include "../utils/tools.hpp"
24
25namespace NumeRe
26{
27
35 {
36 std::string& sLine;
37 std::string sAssignee;
38 size_t nEqPos;
39
40
49 StringExpression(std::string& _sLine, const std::string& sCache = "") : sLine(_sLine), sAssignee(sCache), nEqPos(1)
50 {
52 }
53
54
64 bool isAssignmentOperator(size_t eq_pos) const
65 {
66 if (!eq_pos || eq_pos >= sLine.length())
67 return false;
68
69 return sLine[eq_pos - 1] != '!' && sLine[eq_pos - 1] != '<' && sLine[eq_pos - 1] != '>' && sLine[eq_pos + 1] != '=' && sLine[eq_pos - 1] != '=';
70 }
71
72
84 {
85 // Do nothing, if the position is already 0
86 if (!nEqPos)
87 return;
88
89 nEqPos = 0;
90
91 if (sLine.find('=') == std::string::npos)
92 return;
93
94 size_t nQuotes = 0;
95
96 // Search for the operator while considering the
97 // quotation marks in the expression
98 for (size_t i = 0; i < sLine.length(); i++)
99 {
100 if (sLine[i] == '"' && (!i || sLine[i-1] != '\\'))
101 nQuotes++;
102
103 if (nQuotes % 2)
104 continue;
105
106 if (sLine[i] == '(' || sLine[i] == '[' || sLine[i] == '{')
107 i += getMatchingParenthesis(sLine.substr(i));
108
109 if (sLine[i] == '=' && isAssignmentOperator(i))
110 {
111 nEqPos = i;
112 break;
113 }
114 }
115 }
116
117
125 void split()
126 {
127 if (nEqPos)
128 {
129 sAssignee = sLine.substr(0, nEqPos);
130 sLine.erase(0, nEqPos+1);
131 nEqPos = 0;
132 }
133
135 }
136 };
137
138}
139
140#endif // STRINGEXPRESSION_HPP
141
unsigned int getMatchingParenthesis(const StringView &)
Returns the position of the closing parenthesis.
Definition: tools.cpp:414
void StripSpaces(std::string &)
Removes leading and trailing white spaces and tabulator characters.
This struct encodes a string expression. It tracks the position of an equal sign in the expression in...
void split()
Splits expression and its assignee and sets the assignment operator position to 0.
void findAssignmentOperator()
Searches for the assignment operator (the equal sign separating expression and assignee)....
bool isAssignmentOperator(size_t eq_pos) const
This member function determines, whether the equal sign at eq_pos is an assignment operator and no bo...
StringExpression(std::string &_sLine, const std::string &sCache="")
Constructor of this structure. Searches for the equal sign upon construction.