labs-edaf30/lab1/editor.cc
2024-11-12 12:53:50 +01:00

34 lines
735 B
C++

#include "editor.h"
#include <string>
using std::string;
using size_type = Editor::size_type;
size_type Editor::get_size() const
{
return text.size();
}
size_type Editor::find_left_par(size_type pos) const {
if (pos >= text.size() || (text[pos] != ')' && text[pos] != ']' && text[pos] != '}')) {
return std::string::npos;
}
char right_par = text[pos];
char left_par = (right_par == ')') ? '(' : (right_par == ']') ? '[' : '{';
int balance = 0;
for (int i = pos; i >= 0; --i) {
if (text[i] == right_par) {
balance++;
} else if (text[i] == left_par) {
balance--;
if (balance == 0) {
return i;
}
}
}
}