Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/Sources/Version.cpp
blob: 9bede400fe17ce8b55eed3224f2390bbfd6b53f4 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

#include <array>
#include <fmt/format.h>
#include <limits>

#include "Progmasoft/Tazelish/Version.hpp"

namespace Progmasoft::Tazelish
{
    namespace
    {

        constexpr std::string_view kUnstable = "unstable";
        constexpr std::string_view kNightly = "nightly";

        std::optional<Channel>
        ParseChannel(std::string_view text)
        {
            if (text == kUnstable)
            {
                return Channel::kUnstable;
            }
            if (text == kNightly)
            {
                return Channel::kNightly;
            }
            return std::nullopt;
        }

        std::string_view
        ChannelName(Channel channel)
        {
            return channel == Channel::kUnstable ? kUnstable : kNightly;
        }

        bool
        IsDigit(char character)
        {
            return character >= '0' && character <= '9';
        }

        // A number of digits only, without a leading zero unless it is zero.
        std::optional<std::uint32_t>
        ParseNumber(std::string_view text)
        {
            if (text.empty() || (text.size() > 1 && text.front() == '0'))
            {
                return std::nullopt;
            }
            std::uint64_t value = 0;
            for (const char character : text)
            {
                if (!IsDigit(character))
                {
                    return std::nullopt;
                }
                value
                    = value * 10 + static_cast<std::uint64_t>(character - '0');
                if (value > std::numeric_limits<std::uint32_t>::max())
                {
                    return std::nullopt;
                }
            }
            return static_cast<std::uint32_t>(value);
        }

        std::optional<Release>
        ParseRelease(std::string_view text)
        {
            std::array<std::uint32_t, 4> numbers{};
            for (std::size_t index = 0; index < numbers.size(); ++index)
            {
                const std::size_t dot = text.find('.');
                const bool last = index + 1 == numbers.size();
                if (last != (dot == std::string_view::npos))
                {
                    return std::nullopt;
                }
                const auto number = ParseNumber(text.substr(0, dot));
                if (!number)
                {
                    return std::nullopt;
                }
                numbers[index] = *number;
                text = last ? std::string_view{} : text.substr(dot + 1);
            }
            return Release{ numbers[0], numbers[1], numbers[2], numbers[3] };
        }

        // Exactly [width] digits; leading zeros belong to a date.
        std::optional<std::uint32_t>
        ParseFixed(std::string_view text, std::size_t width)
        {
            if (text.size() != width)
            {
                return std::nullopt;
            }
            std::uint32_t value = 0;
            for (const char character : text)
            {
                if (!IsDigit(character))
                {
                    return std::nullopt;
                }
                value
                    = value * 10 + static_cast<std::uint32_t>(character - '0');
            }
            return value;
        }

        bool
        IsLeapYear(std::uint32_t year)
        {
            return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
        }

        std::uint32_t
        DaysInMonth(std::uint32_t year, std::uint32_t month)
        {
            constexpr std::array<std::uint32_t, 12> kDays
                = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            return month == 2 && IsLeapYear(year) ? 29 : kDays[month - 1];
        }

        // YYYY-MM-DD-channel, with a date the calendar has.
        std::optional<Dated>
        ParseDated(std::string_view text)
        {
            constexpr std::size_t kDateLength = 10;
            if (text.size() <= kDateLength + 1 || text[4] != '-'
                || text[7] != '-' || text[kDateLength] != '-')
            {
                return std::nullopt;
            }
            const auto year = ParseFixed(text.substr(0, 4), 4);
            const auto month = ParseFixed(text.substr(5, 2), 2);
            const auto day = ParseFixed(text.substr(8, 2), 2);
            const auto channel = ParseChannel(text.substr(kDateLength + 1));
            if (!year || !month || !day || !channel || *year == 0 || *month == 0
                || *month > 12 || *day == 0
                || *day > DaysInMonth(*year, *month))
            {
                return std::nullopt;
            }
            return Dated{ static_cast<std::uint16_t>(*year),
                          static_cast<std::uint8_t>(*month),
                          static_cast<std::uint8_t>(*day),
                          *channel };
        }

    } // namespace

    std::optional<Version>
    ParseVersion(std::string_view text)
    {
        if (const auto channel = ParseChannel(text))
        {
            return Newest{ *channel };
        }
        if (const auto release = ParseRelease(text))
        {
            return *release;
        }
        if (const auto dated = ParseDated(text))
        {
            return *dated;
        }
        return std::nullopt;
    }

    std::string
    FormatVersion(const Version &version)
    {
        if (const auto *release = std::get_if<Release>(&version))
        {
            return fmt::format("{}.{}.{}.{}",
                               release->majorNumber,
                               release->semimajorNumber,
                               release->minorNumber,
                               release->patchNumber);
        }
        if (const auto *newest = std::get_if<Newest>(&version))
        {
            return std::string(ChannelName(newest->channel));
        }
        const auto &dated = std::get<Dated>(version);
        return fmt::format("{:04}-{:02}-{:02}-{}",
                           dated.year,
                           dated.month,
                           dated.day,
                           ChannelName(dated.channel));
    }

} // namespace Progmasoft::Tazelish