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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/CharacterTypes.h>
#include <AK/GenericLexer.h>
#include <AK/Optional.h>
namespace AK {
class DateTimeLexer : public GenericLexer {
public:
constexpr explicit DateTimeLexer(StringView input)
: GenericLexer(input)
{
}
Optional<StringView> consume_year()
{
if (tell_remaining() < 4)
return {};
for (auto i = 0; i < 4; ++i) {
if (!is_ascii_digit(peek(i)))
return {};
}
return consume(4);
}
Optional<StringView> consume_month()
{
if (tell_remaining() < 2)
return {};
auto tens = peek();
if (tens != '0' && tens != '1')
return {};
auto ones = peek(1);
if (!is_ascii_digit(ones))
return {};
if (tens == '0') { // 01, 02, 03, 04, 05, 06, 07, 08, 09
if (ones == '0')
return {};
} else if (ones > '2') { // 10, 11, 12
return {};
}
return consume(2);
}
Optional<StringView> consume_day()
{
if (tell_remaining() < 2)
return {};
auto tens = peek();
if (tens < '0' || tens > '3')
return {};
auto ones = peek(1);
if (!is_ascii_digit(ones))
return {};
if (tens == '0') { // 01, 02, 03, 04, 05, 06, 07, 08, 09
if (ones == '0')
return {};
} else if (tens == '3') { // 30, 31
if (ones != '0' && ones != '1')
return {};
} else if (!is_ascii_digit(ones)) { // 10 - 29
return {};
}
return consume(2);
}
Optional<StringView> consume_sign()
{
if (!tell_remaining())
return {};
if (next_is("\xE2\x88\x92"sv))
return consume(3);
else if (next_is('-') || next_is('+'))
return consume(1);
else
return {};
};
Optional<StringView> consume_hours()
{
if (tell_remaining() < 2)
return {};
char tens = peek();
if (tens != '0' && tens != '1' && tens != '2')
return {};
char ones = peek(1);
if (!is_ascii_digit(ones) || (tens == '2' && ones > '3'))
return {};
return consume(2);
}
Optional<StringView> consume_minutes_or_seconds()
{
if (tell_remaining() < 2)
return {};
char tens = peek();
if (tens < '0' || tens > '5')
return {};
if (!is_ascii_digit(peek(1)))
return {};
return consume(2);
}
Optional<StringView> consume_fractional_seconds()
{
if (!tell_remaining())
return {};
auto length = min(tell_remaining(), 9);
for (size_t i = 0; i < length; ++i) {
if (is_ascii_digit(peek(i)))
continue;
length = i;
break;
}
return consume(length);
}
};
}
#if USING_AK_GLOBALLY
using AK::DateTimeLexer;
#endif
|