SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1#pragma once
2
3#include <charconv>
4#include <concepts>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include <sourcepp/Math.h>
10
12
13[[nodiscard]] bool contains(std::string_view s, char c);
14
23[[nodiscard]] bool matches(std::string_view in, std::string_view search);
24
25[[nodiscard]] bool iequals(std::string_view s1, std::string_view s2);
26
27void ltrim(std::string& s);
28
29[[nodiscard]] std::string_view ltrim(std::string_view s);
30
31void rtrim(std::string& s);
32
33[[nodiscard]] std::string_view rtrim(std::string_view s);
34
35void trim(std::string& s);
36
37[[nodiscard]] std::string_view trim(std::string_view s);
38
39void ltrim(std::string& s, std::string_view chars);
40
41[[nodiscard]] std::string_view ltrim(std::string_view s, std::string_view chars);
42
43void rtrim(std::string& s, std::string_view chars);
44
45[[nodiscard]] std::string_view rtrim(std::string_view s, std::string_view chars);
46
47void trim(std::string& s, std::string_view chars);
48
49[[nodiscard]] std::string_view trim(std::string_view s, std::string_view chars);
50
51[[nodiscard]] std::vector<std::string> split(std::string_view s, char delim);
52
53void toLower(std::string& input);
54
55[[nodiscard]] std::string toLower(std::string_view input);
56
57void toUpper(std::string& input);
58
59[[nodiscard]] std::string toUpper(std::string_view input);
60
61[[nodiscard]] std::string createRandom(uint16_t length = 32, std::string_view chars = "0123456789_abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ");
62
63[[nodiscard]] std::string generateUUIDv4();
64
65[[nodiscard]] std::string padNumber(int64_t number, int width, char pad = '0');
66
67void normalizeSlashes(std::string& path, bool stripSlashPrefix = false, bool stripSlashSuffix = true);
68
69void denormalizeSlashes(std::string& path, bool stripSlashPrefix = false, bool stripSlashSuffix = true);
70
71std::from_chars_result toBool(std::string_view number, bool& out, int base = 10);
72
73inline std::from_chars_result toInt(std::string_view number, std::integral auto& out, int base = 10) {
74 return std::from_chars(number.data(), number.data() + number.size(), out, base);
75}
76
77inline std::from_chars_result toFloat(std::string_view number, std::floating_point auto& out) {
78#ifdef __APPLE__
79 // Piece of shit compiler
80 out = std::stof(std::string{number});
81 return {number.data(), {}};
82#else
83 return std::from_chars(number.data(), number.data() + number.size(), out);
84#endif
85}
86
87} // namespace sourcepp::string
std::string createRandom(uint16_t length=32, std::string_view chars="0123456789_abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Definition: String.cpp:165
std::from_chars_result toFloat(std::string_view number, std::floating_point auto &out)
Definition: String.h:77
bool contains(std::string_view s, char c)
Definition: String.cpp:20
void normalizeSlashes(std::string &path, bool stripSlashPrefix=false, bool stripSlashSuffix=true)
Definition: String.cpp:206
std::from_chars_result toBool(std::string_view number, bool &out, int base=10)
Definition: String.cpp:226
void ltrim(std::string &s)
Definition: String.cpp:67
std::vector< std::string > split(std::string_view s, char delim)
Definition: String.cpp:135
std::from_chars_result toInt(std::string_view number, std::integral auto &out, int base=10)
Definition: String.h:73
void denormalizeSlashes(std::string &path, bool stripSlashPrefix=false, bool stripSlashSuffix=true)
Definition: String.cpp:216
bool matches(std::string_view in, std::string_view search)
A very basic regex-like pattern checker for ASCII strings.
Definition: String.cpp:24
std::string padNumber(int64_t number, int width, char pad='0')
Definition: String.cpp:201
void rtrim(std::string &s)
Definition: String.cpp:78
void toUpper(std::string &input)
Definition: String.cpp:155
void trim(std::string &s)
Definition: String.cpp:89
bool iequals(std::string_view s1, std::string_view s2)
Definition: String.cpp:61
void toLower(std::string &input)
Definition: String.cpp:145
std::string generateUUIDv4()
Definition: String.cpp:177