labs-edaf30/lab1/editor.cc

35 lines
735 B
C++
Raw Permalink Normal View History

2021-10-27 15:15:47 +02:00
#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 {
2024-11-12 12:53:50 +01:00
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;
}
}
}
2021-10-27 15:15:47 +02:00
}