~kiwec/ke

ke/src/Document.cc -rw-r--r-- 941 bytes
1cc8481d — Wolf Clément Fix compilation 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <math.h>
#include <wchar.h>
#include "Document.h"
#include "Line.h"

Line Text::find_line(wchar_t* pos) {
	auto line = Line(this, 0, 1);
	while(line.next().begin() < pos) {
		line = line.next();
	}
	return line;
}

Line Text::get_line(uint line_number) {
	return get_line(Line(this, 0, 1), line_number);
}

Line Text::get_line(Line closest, uint wanted_line_number) {
	assert(wanted_line_number > 0);
	assert(wanted_line_number <= nb_lines);

	while(closest.number() < wanted_line_number) {
		closest = closest.next();
	}
	while(closest.number() > wanted_line_number) {
		closest = closest.prev();
	}
	return closest;
}

uint Text::get_nb_lines() {
	return nb_lines;
}

std::pair<uint, uint> Text::search(const wchar_t* str) {
	auto res = wcsstr(m_data, str);
	auto pos = res - m_data;
	if(res) {
		auto line = find_line(res);
		return std::pair(line.number(), static_cast<uint>(pos - line.start()));
	}
	return std::pair(0, 0);
}