CPP-AP 2.7.0
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 <algorithm>
15#include <sstream>
16#include <string_view>
17
18namespace ap::detail {
19
25template <c_writable T>
26[[nodiscard]] std::string as_string(const T& value) noexcept {
27 std::ostringstream oss;
28 oss << value;
29 return oss.str();
30}
31
33[[nodiscard]] inline bool contains_whitespaces(std::string_view str) noexcept {
34 return std::ranges::any_of(str, [](unsigned char c) { return std::isspace(c); });
35}
36
45template <std::ranges::range R>
46requires(c_writable<std::ranges::range_value_t<R>>)
47[[nodiscard]] std::string join(const R& range, const std::string_view delimiter = ", ") {
48 std::ostringstream oss;
49
50 auto it = std::ranges::begin(range);
51 const auto end = std::ranges::end(range);
52 if (it != end) {
53 oss << *it;
54 ++it;
55 for (; it != end; ++it)
56 oss << delimiter << *it;
57 }
58
59 return oss.str();
60}
61
62} // namespace ap::detail
Provides the general concept definitions.
bool contains_whitespaces(std::string_view str) noexcept
Checks whether a string contains any whitespace characters.
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.