Libkleo

stringutils.h
1/*
2 utils/stringutils.h
3
4 This file is part of libkleopatra
5 SPDX-FileCopyrightText: 2021 g10 Code GmbH
6 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#pragma once
12
13#include "kleo_export.h"
14
15#include <optional>
16#include <string>
17#include <string_view>
18#include <vector>
19
20namespace Kleo
21{
22
23/**
24 * Splits the string @p sv into substrings wherever the character @p c occurs,
25 * and returns the list of those strings. If @p maxParts is greater than 0 then
26 * the string is split in at most @p maxParts substrings.
27 */
28KLEO_EXPORT std::vector<std::string_view> split(std::string_view sv, char c, unsigned maxParts = 0);
29
30/**
31 * Converts the vector @p stringViews of string views to a vector of strings.
32 */
33KLEO_EXPORT std::vector<std::string> toStrings(const std::vector<std::string_view> &stringViews);
34
35/**
36 * Returns true if the string @p sv begins with the string @p prefix, false
37 * otherwise.
38 */
39inline bool startsWith(std::string_view sv, std::string_view prefix)
40{
41#ifdef __cpp_lib_starts_ends_with
42 return sv.starts_with(prefix);
43#else
44 return sv.substr(0, prefix.size()) == prefix;
45#endif
46}
47
48/**
49 * Converts the string @p sv to an int value. Returns a value-less object if the
50 * conversions fails.
51 *
52 * Uses std::from_chars for the conversion. Additionally, this function verifies
53 * that the complete string was converted.
54 */
55KLEO_EXPORT std::optional<int> svToInt(std::string_view sv);
56}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:02:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.