CPP-AP 2.2.6
Command-line argument parser for C++20
Loading...
Searching...
No Matches
str_utility.hpp
Go to the documentation of this file.
1// Copyright (c) 2023-2025 Jakub MusiaƂ
2// This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap).
3// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
10#pragma once
11
12#include "concepts.hpp"
13
14#include <sstream>
15#include <string_view>
16
17namespace ap::detail {
18
24template <c_writable T>
25[[nodiscard]] std::string as_string(const T& value) noexcept {
26 std::ostringstream oss;
27 oss << value;
28 return oss.str();
29}
30
39template <std::ranges::range R>
40requires(c_writable<std::ranges::range_value_t<R>>)
41[[nodiscard]] std::string join(const R& range, const std::string_view delimiter = ", ") {
42 std::ostringstream oss;
43
44 auto it = std::ranges::begin(range);
45 const auto end = std::ranges::end(range);
46 if (it != end) {
47 oss << *it;
48 ++it;
49 for (; it != end; ++it)
50 oss << delimiter << *it;
51 }
52
53 return oss.str();
54}
55
56} // namespace ap::detail
Provides the general concept definitions.
std::string as_string(const T &value) noexcept
Converts a value to std::string.
std::string join(const R &range, const std::string_view delimiter=", ")
Joins elements of a range into a single string with a delimiter.