Files
scylladb/utils/from_chars_exactly.hh
Avi Kivity 224dc34089 utils: introduce from_chars_exactly()
This is a replacement for boost::lexical_cast (but without its
long dependency chain). It wraps std::from_chars(), providing a less
flexible but also more concise interface.
2025-01-09 11:14:49 +02:00

27 lines
792 B
C++

// Copyright (C) 2025-present ScyllaDB
// SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
#pragma once
#include <charconv>
#include <concepts>
#include <string_view>
namespace utils {
// Parses a string into a number, throwing an exception if the entire string is not consumed
// or a conversion error happens. The exception is created by calling the provided callable.
template <typename T>
requires std::integral<T> || std::floating_point<T>
T
from_chars_exactly(std::string_view str, std::invocable<std::string_view> auto&& make_exception) {
T result;
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
if (ptr != str.data() + str.size() || ec != std::errc{}) {
throw make_exception(str);
}
return result;
}
}