CPP-ARGON 4.0.0
Command-Line Argument Parser for C++20
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1// Copyright (c) 2023-2026 Jakub Musiał
2// This file is part of the CPP-ARGON project (https://github.com/SpectraL519/cpp-argon).
3// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
10#pragma once
11
13
14#include <algorithm>
15#include <sstream>
16#include <string_view>
17
18namespace argon::util {
19
26template <c_writable T>
27[[nodiscard]] std::string as_string(const T& value) noexcept {
28 std::ostringstream oss;
29 oss << value;
30 return oss.str();
31}
32
34[[nodiscard]] inline bool contains_whitespaces(std::string_view str) noexcept {
35 return std::ranges::any_of(str, [](unsigned char c) { return std::isspace(c); });
36}
37
46template <std::ranges::range R>
47requires(c_writable<std::ranges::range_value_t<R>>)
48[[nodiscard]] std::string join(const R& range, const std::string_view delimiter = ", ") {
49 std::ostringstream oss;
50
51 auto it = std::ranges::begin(range);
52 const auto end = std::ranges::end(range);
53 if (it != end) {
54 oss << *it;
55 ++it;
56 for (; it != end; ++it)
57 oss << delimiter << *it;
58 }
59
60 return oss.str();
61}
62
63} // namespace argon::util
std::string join(const R &range, const std::string_view delimiter=", ")
Joins elements of a range into a single string with a delimiter.
Definition string.hpp:48
std::string as_string(const T &value) noexcept
Converts a value to std::string.
Definition string.hpp:27
bool contains_whitespaces(std::string_view str) noexcept
Checks whether a string contains any whitespace characters.
Definition string.hpp:34
Provides the general concept definitions.