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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___CHRONO_PARSER_STD_FORMAT_SPEC_H
#define _LIBCPP___CHRONO_PARSER_STD_FORMAT_SPEC_H
#include <__config>
#include <__format/concepts.h>
#include <__format/format_error.h>
#include <__format/format_parse_context.h>
#include <__format/formatter_string.h>
#include <__format/parser_std_format_spec.h>
#include <string_view>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
namespace __format_spec {
// By not placing this constant in the formatter class it's not duplicated for char and wchar_t
inline constexpr __fields __fields_chrono_fractional{
.__precision_ = true, .__locale_specific_form_ = true, .__type_ = false};
inline constexpr __fields __fields_chrono{.__locale_specific_form_ = true, .__type_ = false};
/// Flags available or required in a chrono type.
///
/// The caller of the chrono formatter lists the types it has available and the
/// validation tests whether the requested type spec (e.g. %M) is available in
/// the formatter.
/// When the type in the chrono-format-spec isn't present in the data a
/// \ref format_error is thrown.
enum class __flags {
__second = 0x1,
__minute = 0x2,
__hour = 0x4,
__time = __hour | __minute | __second,
__day = 0x8,
__month = 0x10,
__year = 0x20,
__weekday = 0x40,
__month_day = __day | __month,
__month_weekday = __weekday | __month,
__year_month = __month | __year,
__date = __day | __month | __year | __weekday,
__date_time = __date | __time,
__duration = 0x80 | __time,
__time_zone = 0x100,
__clock = __date_time | __time_zone
};
_LIBCPP_HIDE_FROM_ABI constexpr __flags operator&(__flags __lhs, __flags __rhs) {
return static_cast<__flags>(static_cast<unsigned>(__lhs) & static_cast<unsigned>(__rhs));
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_second(__flags __flags) {
if ((__flags & __flags::__second) != __flags::__second)
std::__throw_format_error("The supplied date time doesn't contain a second");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_minute(__flags __flags) {
if ((__flags & __flags::__minute) != __flags::__minute)
std::__throw_format_error("The supplied date time doesn't contain a minute");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_hour(__flags __flags) {
if ((__flags & __flags::__hour) != __flags::__hour)
std::__throw_format_error("The supplied date time doesn't contain an hour");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_time(__flags __flags) {
if ((__flags & __flags::__time) != __flags::__time)
std::__throw_format_error("The supplied date time doesn't contain a time");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_day(__flags __flags) {
if ((__flags & __flags::__day) != __flags::__day)
std::__throw_format_error("The supplied date time doesn't contain a day");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_month(__flags __flags) {
if ((__flags & __flags::__month) != __flags::__month)
std::__throw_format_error("The supplied date time doesn't contain a month");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_year(__flags __flags) {
if ((__flags & __flags::__year) != __flags::__year)
std::__throw_format_error("The supplied date time doesn't contain a year");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_date(__flags __flags) {
if ((__flags & __flags::__date) != __flags::__date)
std::__throw_format_error("The supplied date time doesn't contain a date");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_date_or_duration(__flags __flags) {
if (((__flags & __flags::__date) != __flags::__date) && ((__flags & __flags::__duration) != __flags::__duration))
std::__throw_format_error("The supplied date time doesn't contain a date or duration");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_date_time(__flags __flags) {
if ((__flags & __flags::__date_time) != __flags::__date_time)
std::__throw_format_error("The supplied date time doesn't contain a date and time");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_weekday(__flags __flags) {
if ((__flags & __flags::__weekday) != __flags::__weekday)
std::__throw_format_error("The supplied date time doesn't contain a weekday");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_duration(__flags __flags) {
if ((__flags & __flags::__duration) != __flags::__duration)
std::__throw_format_error("The supplied date time doesn't contain a duration");
}
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_time_zone(__flags __flags) {
if ((__flags & __flags::__time_zone) != __flags::__time_zone)
std::__throw_format_error("The supplied date time doesn't contain a time zone");
}
template <class _CharT>
class _LIBCPP_TEMPLATE_VIS __parser_chrono {
public:
_LIBCPP_HIDE_FROM_ABI constexpr auto
__parse(basic_format_parse_context<_CharT>& __parse_ctx, __fields __fields, __flags __flags)
-> decltype(__parse_ctx.begin()) {
const _CharT* __begin = __parser_.__parse(__parse_ctx, __fields);
const _CharT* __end = __parse_ctx.end();
if (__begin == __end)
return __begin;
const _CharT* __last = __parse_chrono_specs(__begin, __end, __flags);
__chrono_specs_ = basic_string_view<_CharT>{__begin, __last};
return __last;
}
__parser<_CharT> __parser_;
basic_string_view<_CharT> __chrono_specs_;
private:
_LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
__parse_chrono_specs(const _CharT* __begin, const _CharT* __end, __flags __flags) {
_LIBCPP_ASSERT(__begin != __end,
"When called with an empty input the function will cause "
"undefined behavior by evaluating data not in the input");
if (*__begin != _CharT('%') && *__begin != _CharT('}'))
std::__throw_format_error("Expected '%' or '}' in the chrono format-string");
do {
switch (*__begin) {
case _CharT('{'):
std::__throw_format_error("The chrono-specs contains a '{'");
case _CharT('}'):
return __begin;
case _CharT('%'):
__parse_conversion_spec(__begin, __end, __flags);
[[fallthrough]];
default:
// All other literals
++__begin;
}
} while (__begin != __end && *__begin != _CharT('}'));
return __begin;
}
/// \pre *__begin == '%'
/// \post __begin points at the end parsed conversion-spec
_LIBCPP_HIDE_FROM_ABI constexpr void
__parse_conversion_spec(const _CharT*& __begin, const _CharT* __end, __flags __flags) {
++__begin;
if (__begin == __end)
std::__throw_format_error("End of input while parsing the modifier chrono conversion-spec");
switch (*__begin) {
case _CharT('n'):
case _CharT('t'):
case _CharT('%'):
break;
case _CharT('S'):
__format_spec::__validate_second(__flags);
break;
case _CharT('M'):
__format_spec::__validate_minute(__flags);
break;
case _CharT('p'): // TODO FMT does the formater require an hour or a time?
case _CharT('H'):
case _CharT('I'):
__validate_hour(__flags);
break;
case _CharT('r'):
case _CharT('R'):
case _CharT('T'):
case _CharT('X'):
__format_spec::__validate_time(__flags);
break;
case _CharT('d'):
case _CharT('e'):
__format_spec::__validate_day(__flags);
break;
case _CharT('b'):
case _CharT('h'):
case _CharT('B'):
__parser_.__month_name_ = true;
[[fallthrough]];
case _CharT('m'):
__format_spec::__validate_month(__flags);
break;
case _CharT('y'):
case _CharT('C'):
case _CharT('Y'):
__format_spec::__validate_year(__flags);
break;
case _CharT('j'):
__parser_.__day_of_year_ = true;
__format_spec::__validate_date_or_duration(__flags);
break;
case _CharT('g'):
case _CharT('G'):
case _CharT('U'):
case _CharT('V'):
case _CharT('W'):
__parser_.__week_of_year_ = true;
[[fallthrough]];
case _CharT('x'):
case _CharT('D'):
case _CharT('F'):
__format_spec::__validate_date(__flags);
break;
case _CharT('c'):
__format_spec::__validate_date_time(__flags);
break;
case _CharT('a'):
case _CharT('A'):
__parser_.__weekday_name_ = true;
[[fallthrough]];
case _CharT('u'):
case _CharT('w'):
__parser_.__weekday_ = true;
__validate_weekday(__flags);
__format_spec::__validate_weekday(__flags);
break;
case _CharT('q'):
case _CharT('Q'):
__format_spec::__validate_duration(__flags);
break;
case _CharT('E'):
__parse_modifier_E(__begin, __end, __flags);
break;
case _CharT('O'):
__parse_modifier_O(__begin, __end, __flags);
break;
case _CharT('z'):
case _CharT('Z'):
// Currently there's no time zone information. However some clocks have a
// hard-coded "time zone", for these clocks the information can be used.
// TODO FMT implement time zones.
__format_spec::__validate_time_zone(__flags);
break;
default: // unknown type;
std::__throw_format_error("The date time type specifier is invalid");
}
}
/// \pre *__begin == 'E'
/// \post __begin is incremented by one.
_LIBCPP_HIDE_FROM_ABI constexpr void
__parse_modifier_E(const _CharT*& __begin, const _CharT* __end, __flags __flags) {
++__begin;
if (__begin == __end)
std::__throw_format_error("End of input while parsing the modifier E");
switch (*__begin) {
case _CharT('X'):
__format_spec::__validate_time(__flags);
break;
case _CharT('y'):
case _CharT('C'):
case _CharT('Y'):
__format_spec::__validate_year(__flags);
break;
case _CharT('x'):
__format_spec::__validate_date(__flags);
break;
case _CharT('c'):
__format_spec::__validate_date_time(__flags);
break;
case _CharT('z'):
// Currently there's no time zone information. However some clocks have a
// hard-coded "time zone", for these clocks the information can be used.
// TODO FMT implement time zones.
__format_spec::__validate_time_zone(__flags);
break;
default:
std::__throw_format_error("The date time type specifier for modifier E is invalid");
}
}
/// \pre *__begin == 'O'
/// \post __begin is incremented by one.
_LIBCPP_HIDE_FROM_ABI constexpr void
__parse_modifier_O(const _CharT*& __begin, const _CharT* __end, __flags __flags) {
++__begin;
if (__begin == __end)
std::__throw_format_error("End of input while parsing the modifier O");
switch (*__begin) {
case _CharT('S'):
__format_spec::__validate_second(__flags);
break;
case _CharT('M'):
__format_spec::__validate_minute(__flags);
break;
case _CharT('I'):
case _CharT('H'):
__format_spec::__validate_hour(__flags);
break;
case _CharT('d'):
case _CharT('e'):
__format_spec::__validate_day(__flags);
break;
case _CharT('m'):
__format_spec::__validate_month(__flags);
break;
case _CharT('y'):
__format_spec::__validate_year(__flags);
break;
case _CharT('U'):
case _CharT('V'):
case _CharT('W'):
__parser_.__week_of_year_ = true;
__format_spec::__validate_date(__flags);
break;
case _CharT('u'):
case _CharT('w'):
__parser_.__weekday_ = true;
__format_spec::__validate_weekday(__flags);
break;
case _CharT('z'):
// Currently there's no time zone information. However some clocks have a
// hard-coded "time zone", for these clocks the information can be used.
// TODO FMT implement time zones.
__format_spec::__validate_time_zone(__flags);
break;
default:
std::__throw_format_error("The date time type specifier for modifier O is invalid");
}
}
};
} // namespace __format_spec
#endif //_LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___CHRONO_PARSER_STD_FORMAT_SPEC_H
|