VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Strings.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <map>
5#include <optional>
6#include <string>
7#include <string_view>
8#include <vector>
9
10namespace VoltMod
11{
12
13/** `{token}` -> replacement, for @ref Strings::SubstituteTokens and the text built on it. */
14using Tokens = std::map<std::string, std::string>;
15
16/** @p text as an int64_t, or nullopt unless it is entirely digits (with an optional sign). */
17std::optional<int64_t> ParseInt64(std::string_view text);
18
19/** @p text as a uint64_t, or nullopt unless it is entirely digits. A leading `-` is rejected
20 * rather than wrapped, which is the whole reason this is not @ref ParseInt64 with a cast. */
21std::optional<uint64_t> ParseUInt64(std::string_view text);
22
23/**
24 * Parse a human duration string into seconds. Accepts a bare integer (seconds) or an
25 * integer with a unit suffix: `s` seconds, `m` minutes, `h` hours, `d` days, `w` weeks
26 * (case-insensitive). The literals `0`, `perm`, and `permanent` mean "permanent" (returns 0).
27 * Surrounding whitespace is ignored. This is the single canonical duration grammar;
28 * Time::ParseDuration delegates here.
29 * Returns -1 on parse failure or int overflow, 0 for permanent, otherwise seconds.
30 */
31int ParseDuration(std::string_view text);
32
33/** @brief Collection of static string manipulation utilities. */
35{
36public:
37 static std::string ToLower(std::string_view str);
38 static std::string Trim(std::string_view str);
39 static std::string Join(const std::vector<std::string>& parts, std::string_view delimiter);
40
41 /**
42 * Join @p parts with @p delimiter, skipping empty ones.
43 *
44 * For text assembled from optional pieces - a ban notice's reason, expiry and appeal link,
45 * say. Skipping is the point: a piece that is not configured must not leave a dangling
46 * delimiter or a bare label behind it.
47 */
48 static std::string JoinNonEmpty(const std::vector<std::string>& parts, std::string_view delimiter);
49 static bool StartsWith(std::string_view str, std::string_view prefix);
50 static bool ContainsIgnoreCase(std::string_view str, std::string_view substr);
51
52 /** Replace each `{key}` occurrence in @p text with its mapped value. */
53 static std::string SubstituteTokens(std::string text, const Tokens& tokens);
54
55 /** Escape `& < > " '` as HTML entities for safe embedding in center-HTML panels. */
56 static std::string EscapeHtml(std::string_view text);
57
58 /**
59 * Quote @p value as a single console-command argument: wrapped in `"`, with `"` and `\`
60 * escaped.
61 *
62 * The engine's command parser splits on whitespace and treats `;` as a command separator,
63 * so an unquoted value containing either is truncated or runs a second command. A newline
64 * cannot be escaped by quoting at all - it always ends the line - which is why
65 * @ref ConVars::ExecuteServerCommand rejects one outright rather than relying on this.
66 */
67 static std::string QuoteConsoleArg(std::string_view value);
68
69 /**
70 * Truncate to at most `maxBytes` bytes plus `ellipsis`, cutting at a UTF-8 sequence
71 * boundary so multibyte text (e.g. Cyrillic) never renders a split character.
72 */
73 static std::string TruncateUtf8(std::string_view text, std::size_t maxBytes, std::string_view ellipsis = "...");
74
75 static bool IsNumeric(std::string_view str);
76
77 /** Copy @p text into a caller's C error buffer of @p size bytes, cut to fit and NUL-terminated. */
78 static void CopyToBuffer(char* buffer, std::size_t size, std::string_view text) noexcept;
79
80 /** Row display text, HTML-escaped: the (UTF-8-safely truncated) name, or @p id when unnamed. */
81 static std::string DisplayNameOr(int64_t id, std::string_view name, std::size_t maxBytes = 20);
82};
83
84} // namespace VoltMod
Collection of static string manipulation utilities.
Definition Strings.hpp:35
static std::string Trim(std::string_view str)
Definition Strings.cpp:107
static void CopyToBuffer(char *buffer, std::size_t size, std::string_view text) noexcept
Definition Strings.cpp:257
static std::string DisplayNameOr(int64_t id, std::string_view name, std::size_t maxBytes=20)
Definition Strings.cpp:269
static bool ContainsIgnoreCase(std::string_view str, std::string_view substr)
Definition Strings.cpp:153
static std::string QuoteConsoleArg(std::string_view value)
Definition Strings.cpp:216
static std::string JoinNonEmpty(const std::vector< std::string > &parts, std::string_view delimiter)
Definition Strings.cpp:130
static bool StartsWith(std::string_view str, std::string_view prefix)
Definition Strings.cpp:148
static std::string SubstituteTokens(std::string text, const Tokens &tokens)
Definition Strings.cpp:176
static std::string TruncateUtf8(std::string_view text, std::size_t maxBytes, std::string_view ellipsis="...")
Definition Strings.cpp:233
static std::string Join(const std::vector< std::string > &parts, std::string_view delimiter)
Definition Strings.cpp:114
static std::string ToLower(std::string_view str)
Definition Strings.cpp:100
static bool IsNumeric(std::string_view str)
Definition Strings.cpp:248
static std::string EscapeHtml(std::string_view text)
Definition Strings.cpp:185
std::optional< int64_t > ParseInt64(std::string_view text)
Definition Strings.cpp:13
int ParseDuration(std::string_view text)
Definition Strings.cpp:35
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::optional< uint64_t > ParseUInt64(std::string_view text)
Definition Strings.cpp:24
std::map< std::string, std::string > Tokens
Definition Strings.hpp:14