Website Summary Files Commits Branches @commit @diff @stats
path: root / Week05 / Exercise02 / Text.cpp
blob: b6143c04c7d68948d3532b802fecb3981391472e (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "Text.h"

void Text::free() {
	delete[] lines;
}

void Text::copyFrom(const Text& other) {
	lineCount = other.lineCount;

	lines = new String[lineCount];
	for (unsigned i = 0; i < lineCount; ++i)
		lines[i] = other.lines[i];
}

Text::Text() {
	lines = nullptr;
	lineCount = 0;
}

Text::~Text() {
	free();
}

Text::Text(const Text& other) {
	copyFrom(other);
}

Text& Text::operator=(const Text& other) {
	if (this != &other) {
		free();
		copyFrom(other);
	}
	return *this;
}

Text::Text(Text&& other) {
	lines = other.lines;
	lineCount = other.lineCount;

	other.lines = nullptr;
	other.lineCount = 0;
}

unsigned Text::LineCount() const {
	return lineCount;
}

unsigned Text::WordCount() const {
	unsigned totalCount = 0;

	for (unsigned i = 0; i < lineCount; ++i)
		totalCount += lines[i].CountWords();

	return totalCount;
}

Text Text::SubText(unsigned startLine, unsigned startCol, unsigned endLine, unsigned endCol) const {
	Text result;
	result.lineCount = endLine - startLine + 1;
	result.lines = new String[lineCount];

	for (unsigned i = 0; i < result.lineCount; ++i) {
		result.lines[i] = lines[i + startLine].Substring(startCol, endCol - startCol + 1);
	}

	return result;
}