2021-10-27 15:15:47 +02:00
|
|
|
#include "editor.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
using size_type = Editor::size_type;
|
|
|
|
|
2024-11-11 18:06:07 +01:00
|
|
|
size_type Editor::get_size() const { return text.size(); }
|
|
|
|
|
|
|
|
Editor::size_type Editor::find_left_par(size_type pos) const {
|
|
|
|
char right_par = text[pos];
|
|
|
|
char left_par;
|
|
|
|
|
|
|
|
// Determine the matching left parenthesis for the given right parenthesis
|
|
|
|
switch (right_par) {
|
|
|
|
case ')': left_par = '('; break;
|
|
|
|
case ']': left_par = '['; break;
|
|
|
|
case '}': left_par = '{'; break;
|
|
|
|
default: return string::npos; // Not a valid right parenthesis
|
|
|
|
}
|
|
|
|
|
|
|
|
int balance = 1; // Start with the right parenthesis at text[pos]
|
|
|
|
for (size_type i = pos; i-- > 0;) {
|
|
|
|
if (text[i] == left_par) {
|
|
|
|
balance--;
|
|
|
|
if (balance == 0) return i; // Found the matching left parenthesis
|
|
|
|
} else if (text[i] == right_par) {
|
|
|
|
balance++;
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 15:15:47 +02:00
|
|
|
|
2024-11-11 18:06:07 +01:00
|
|
|
return string::npos; // No matching left parenthesis found
|
2021-10-27 15:15:47 +02:00
|
|
|
}
|