-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorCoder.cpp
More file actions
91 lines (56 loc) · 1.89 KB
/
ColorCoder.cpp
File metadata and controls
91 lines (56 loc) · 1.89 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Created by Kristal Hong on 11/16/23.
//
#include "ColorCoder.h"
void ColorCoder::color(MultiText::iterator begin, MultiText::iterator end, sf::Color color) {
for (auto it = begin; it != end; ++it) {
it->setColor(color);
}
}
sf::Color ColorCoder::getColor(const std::string& string) {
return sf::Color::Blue;
}
sf::Color ColorCoder::getColor(const char& letter) {
if (isOperator(letter)) {
return sf::Color::Green;
}
if (isNumber(letter)) {
return sf::Color::Red;
}
return sf::Color::Black;
}
void ColorCoder::color(MultiText::iterator& begin, MultiText::iterator& end) {
std::string highlightString = MultiText::getWord(begin, end);
// std::cout << "highlightString: " << highlightString << "." <<std::endl;
if (isWord (highlightString)){
// std::cout << highlightString << std::endl;
sf::Color theColor = getColor(highlightString);
color(begin, end, theColor);
}
}
void ColorCoder::color(MultiText &multiText) {
auto start = multiText.begin();
auto end = multiText.begin();
while (start != multiText.end()) {
while (end != multiText.end() && end->getString()[0] != ' ') {
++end;
}
color(start, end);
start = end;
// Skip any spaces
while (start != multiText.end() && start->getString()[0] == ' ') {
++start;
}
// // Move 'end' to 'start' for the next iteration
end = start;
}
}
bool ColorCoder::isWord(const std::string& string) {
return string == "int" || string == "double" || string == "float" || string == "char";
}
bool ColorCoder::isNumber(const Letter& letter) {
return isdigit(letter.getChar());
}
bool ColorCoder::isOperator(const Letter& letter) {
return letter.getChar() == '+' || letter.getChar() == '-' || letter.getChar() == '*' || letter.getChar() == '/';
}