SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
indentationUtils.cpp
Go to the documentation of this file.
1
/*
2
Copyright (c) 2026. Giulio Cocconi
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <
ui/documents/code/indentationUtils.hpp
>
19
20
#include <algorithm>
21
#include <cctype>
22
23
namespace
SILICON::ui::indentation
{
24
25
std::string_view
trimLeft
(std::string_view value)
26
{
27
const
auto
first = std::ranges::find_if_not(
28
value, [](
const
unsigned
char
character) {
return
std::isspace(character); });
29
value.remove_prefix(
static_cast<
std::size_t
>
(std::distance(value.begin(), first)));
30
return
value;
31
}
32
33
std::string_view
trimRight
(std::string_view value)
34
{
35
while
(!value.empty() && std::isspace(
static_cast<
unsigned
char
>
(value.back())))
36
value.remove_suffix(1);
37
return
value;
38
}
39
40
std::string
leadingWhitespace
(
const
std::string_view line)
41
{
42
const
auto
first = std::ranges::find_if_not(line, [](
const
unsigned
char
character) {
43
return
character ==
' '
|| character ==
'\t'
;
44
});
45
return
std::string(line.begin(), first);
46
}
47
48
std::string
codePortion
(
const
std::string_view line,
const
std::regex& strippedPortions)
49
{
50
std::string result(line);
51
using
Iterator = std::string_view::const_iterator;
52
for
(std::regex_iterator<Iterator> matches(line.begin(), line.end(), strippedPortions),
53
end;
54
matches != end; ++matches) {
55
const
auto
start =
static_cast<
std::size_t
>
(matches->position());
56
const
auto
length =
static_cast<
std::size_t
>
(matches->length());
57
std::fill_n(result.begin() +
static_cast<
std::ptrdiff_t
>
(start), length,
' '
);
58
}
59
return
result;
60
}
61
62
bool
regexSearch
(
const
std::string_view value,
const
std::regex& expression)
63
{
64
return
std::regex_search(value.begin(), value.end(), expression);
65
}
66
67
bool
endsWithWord
(
const
std::string_view value,
const
std::string_view word)
68
{
69
const
auto
trimmed =
trimRight
(value);
70
if
(!trimmed.ends_with(word))
71
return
false
;
72
const
auto
start = trimmed.size() - word.size();
73
return
start == 0
74
|| !(std::isalnum(
static_cast<
unsigned
char
>
(trimmed[start - 1]))
75
|| trimmed[start - 1] ==
'_'
);
76
}
77
78
void
addIndentLevel
(std::string&
indentation
,
const
int
indentWidth)
79
{
80
indentation
.append(
static_cast<
std::size_t
>
(std::max(1, indentWidth)),
' '
);
81
}
82
83
void
removeIndentLevel
(std::string&
indentation
,
const
int
indentWidth)
84
{
85
// Strip the trailing indentation level regardless of tab/space mix.
86
int
remaining = std::max(1, indentWidth);
87
while
(remaining > 0 && !
indentation
.empty()) {
88
const
char
back =
indentation
.back();
89
if
(back ==
'\t'
) {
90
indentation
.pop_back();
91
remaining = 0;
92
}
else
if
(back ==
' '
) {
93
indentation
.pop_back();
94
--remaining;
95
}
else
{
96
break
;
97
}
98
}
99
}
100
101
}
// namespace SILICON::ui::indentation
indentationUtils.hpp
SILICON::ui::indentation
Definition
indentationUtils.cpp:23
SILICON::ui::indentation::leadingWhitespace
std::string leadingWhitespace(const std::string_view line)
Definition
indentationUtils.cpp:40
SILICON::ui::indentation::trimRight
std::string_view trimRight(std::string_view value)
Definition
indentationUtils.cpp:33
SILICON::ui::indentation::endsWithWord
bool endsWithWord(const std::string_view value, const std::string_view word)
Definition
indentationUtils.cpp:67
SILICON::ui::indentation::trimLeft
std::string_view trimLeft(std::string_view value)
Definition
indentationUtils.cpp:25
SILICON::ui::indentation::regexSearch
bool regexSearch(const std::string_view value, const std::regex &expression)
Definition
indentationUtils.cpp:62
SILICON::ui::indentation::addIndentLevel
void addIndentLevel(std::string &indentation, const int indentWidth)
Definition
indentationUtils.cpp:78
SILICON::ui::indentation::codePortion
std::string codePortion(const std::string_view line, const std::regex &strippedPortions)
Definition
indentationUtils.cpp:48
SILICON::ui::indentation::removeIndentLevel
void removeIndentLevel(std::string &indentation, const int indentWidth)
Definition
indentationUtils.cpp:83
src
ui
documents
code
indentationUtils.cpp
Generated by
1.17.0