-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.inl
133 lines (112 loc) · 5.42 KB
/
Code.inl
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
///
/// Langulus::Flow
/// Copyright (c) 2017 Dimo Markov <[email protected]>
/// Part of the Langulus framework, see https://langulus.com
///
/// SPDX-License-Identifier: GPL-3.0-or-later
///
#pragma once
#include "Code.hpp"
namespace Langulus::Flow
{
/// Convert a number type to text
/// Notice that this constructor explicitly avoids character types
/// @param number - the number to stringify
template<CT::BuiltinNumber T> requires (not CT::Character<T>)
LANGULUS(INLINED) Code::Code(const T& number)
: Code {Text::FromNumber(number)} {}
/// Remove elements from the left side of Code code
/// @param offset - the number of elements to discard from the front
/// @return a shallow-copied container with the correct offset
LANGULUS(INLINED)
Code Code::RightOf(Offset offset) const IF_UNSAFE(noexcept) {
return offset < mCount ? Text::Select(offset) : Code {};
}
/// Remove elements from the right side of Code code
/// @param offset - the number of elements to remain in container
/// @return a shallow-copied container with the correct offset
LANGULUS(INLINED)
Code Code::LeftOf(Offset offset) const IF_UNSAFE(noexcept) {
return offset > 0 ? Text::Select(0, offset) : Code {};
}
/// Check if the Code container begins with special elements, such as
/// special characters or escape sequences, like colors
/// @return true if the first symbol is special
LANGULUS(INLINED)
bool Code::StartsWithSpecial() const noexcept {
const auto& letter = (*this)[0];
return letter > 0 and letter < 32;
}
/// Check if the Code container begins with skippable elements, such as
/// tabs or spaces, comment blocks, or special character sequences (TODO)
/// @return true if the first symbol is skippable
LANGULUS(INLINED)
bool Code::StartsWithSkippable() const noexcept {
if (IsEmpty())
return false;
const auto& letter = (*this)[0];
if (letter > 0 and letter <= 32)
return true;
const auto asview = Token {*this};
return asview.starts_with("//") or asview.starts_with("/*");
}
/// Check if the Code container begins with skippable elements
/// @return true if the first symbol is a spacer
LANGULUS(INLINED)
bool Code::EndsWithSkippable() const noexcept {
return *last() > 0 and *last() <= 32;
}
/// Check if the Code code container begins with a letter or underscore
/// @return true if the first symbol is a letter/underscore
LANGULUS(INLINED)
bool Code::StartsWithLetter() const noexcept {
const auto c = (*this)[0];
return IsAlpha(c) or c == '_';
}
/// Check if the Code code container ends with a letter or underscore
/// @return true if the last symbol is a letter/underscore
LANGULUS(INLINED)
bool Code::EndsWithLetter() const noexcept {
const auto c = last();
return IsAlpha(*c) or *c == '_';
}
/// Check if the Code code container begins with a number
/// @return true if the first symbol is a number
LANGULUS(INLINED)
bool Code::StartsWithDigit() const noexcept {
return IsDigit((*this)[0]);
}
/// Check if the Code code container ends with a number
/// @return true if the last symbol is a number
LANGULUS(INLINED)
bool Code::EndsWithDigit() const noexcept {
return IsDigit(*last());
}
/// Concatenate two text containers
/// @param rhs - right hand side
/// @return the concatenated text container
template<class T> requires CT::Codifiable<Deint<T>> LANGULUS(INLINED)
Code Code::operator + (T&& rhs) const {
return Text::ConcatInner<Code>(Forward<T>(rhs));
}
/// Concatenate (destructively) text containers
/// @param rhs - right hand side
/// @return a reference to this container
template<class T> requires CT::Codifiable<Deint<T>> LANGULUS(INLINED)
Code& Code::operator += (T&& rhs) {
return Text::ConcatRelativeInner<Code>(Forward<T>(rhs));
}
} // namespace Langulus::Flow
namespace Langulus
{
/// Make a code literal
LANGULUS(INLINED)
Flow::Code operator ""_code(const char* text, ::std::size_t size) {
return Anyness::Text::From(text, size);
}
/// Make a code literal and parse it
LANGULUS(INLINED)
auto operator ""_parse(const char* text, ::std::size_t size) {
return Flow::Code(Anyness::Text::From(text, size)).Parse();
}
} // namespace Langulus