libcruft-util/parse/si.cpp

54 lines
1.4 KiB
C++
Raw Normal View History

2011-08-29 14:37:47 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2011-08-29 14:37:47 +10:00
*
2019-03-18 14:08:06 +11:00
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
2011-08-29 14:37:47 +10:00
*/
#include "si.hpp"
#include "value.hpp"
2011-08-29 14:37:47 +10:00
#include "../std.hpp"
2011-08-29 14:37:47 +10:00
#include "preprocessor.hpp"
2019-03-18 14:08:06 +11:00
///////////////////////////////////////////////////////////////////////////////
template <typename ValueT>
cruft::expected<ValueT, std::errc>
cruft::parse::si (cruft::view<char const*> const src)
2019-03-18 14:08:06 +11:00
{
auto remain = src;
ValueT dst = cruft::parse::value<ValueT> (remain);
2019-03-18 14:08:06 +11:00
switch (remain.size ()) {
case 0:
return dst;
case 1:
switch (remain[0]) {
case 'K': case 'k': return dst * 1024;
case 'M': case 'm': return dst * 1024 * 1024;
case 'G': case 'g': return dst * 1024 * 1024 * 1024;
case 'T': case 't': return dst * 1024 * 1024 * 1024 * 1024;
}
2022-08-18 13:11:38 +10:00
[[fallthrough]];
2019-03-18 14:08:06 +11:00
default:
return cruft::unexpected (std::errc::invalid_argument);
}
}
///////////////////////////////////////////////////////////////////////////////
#define INSTANTIATE(KLASS) template cruft::expected<KLASS,std::errc> cruft::parse::si (cruft::view<char const*>);
2019-03-18 14:08:06 +11:00
MAP0 (INSTANTIATE,
u16, u32, u64,
i16, i32, i64,
f32, f64
)