CPP-AP 3.0.1
Command-line argument parser for C++20
Loading...
Searching...
No Matches
string.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 <algorithm>
15#include <sstream>
16#include <string_view>
17
18namespace ap::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
47template <std::ranges::range R>
48requires(c_writable<std::ranges::range_value_t<R>>)
49[[nodiscard]] std::string join(const R& range, const std::string_view delimiter = ", ") {
50 std::ostringstream oss;
51
52 auto it = std::ranges::begin(range);
53 const auto end = std::ranges::end(range);
54 if (it != end) {
55 oss << *it;
56 ++it;
57 for (; it != end; ++it)
58 oss << delimiter << *it;
59 }
60
61 return oss.str();
62}
63
64} // namespace ap::util
std::string as_string(const T &value) noexcept
Converts a value to std::string.
Definition string.hpp:27
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:49
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.