StormByte C++ Library 1.2.0
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
string.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte.
5 *
6 * StormByte is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
25
26#include <algorithm>
27#include <cstddef>
28#include <cmath>
29#include <cctype>
30#include <iomanip>
31#include <locale>
32#include <queue>
33#include <sstream>
34#include <string>
35#include <string_view>
36#include <utility>
37#include <vector>
38
50namespace StormByte::String {
55 enum class STORMBYTE_PUBLIC Format : unsigned short {
56 Raw,
59 };
60
65 constexpr STORMBYTE_PUBLIC std::string Indent(const int& level) noexcept {
66 return level == 0 ? std::string() : std::string(level, '\t');
67 }
68
74 constexpr STORMBYTE_PUBLIC bool IsNumeric(std::string_view str) noexcept {
75 return !str.empty() && std::all_of(str.begin(), str.end(), [](unsigned char c) {
76 return std::isdigit(c) != 0;
77 });
78 }
79
85 STORMBYTE_PUBLIC std::string ToLower(std::string_view str) noexcept;
86
92 STORMBYTE_PUBLIC std::string ToUpper(std::string_view str) noexcept;
93
99 STORMBYTE_PUBLIC std::queue<std::string> Explode(std::string_view str, const char delimiter);
100
105 STORMBYTE_PUBLIC std::vector<std::string> Split(std::string_view str);
106
114 template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, wchar_t>>>
115 STORMBYTE_PUBLIC std::string HumanReadable(const T& number, const Format& format, const std::string& locale = "en_US.UTF-8") noexcept;
116
122 STORMBYTE_PUBLIC std::string UTF8Encode(std::wstring_view ws);
123
129 STORMBYTE_PUBLIC std::wstring UTF8Decode(std::string_view s);
130
135 STORMBYTE_PUBLIC std::string SanitizeNewlines(std::string_view str) noexcept;
136
141 STORMBYTE_PUBLIC std::string FromByteVector(const std::vector<std::byte>& byte_vector) noexcept;
142
147 STORMBYTE_PUBLIC std::vector<std::byte> ToByteVector(std::string_view str) noexcept;
148
153 STORMBYTE_PUBLIC std::string RemoveWhitespace(std::string_view str) noexcept;
154
159 STORMBYTE_PUBLIC bool IsInteger(std::string_view str) noexcept;
160}
String helpers (case, split, UTF-8, human-readable numbers).
std::wstring UTF8Decode(std::string_view s)
UTF-8 text to wide string.
std::vector< std::byte > ToByteVector(std::string_view str) noexcept
Copies text bytes into a vector<std::byte>.
std::string HumanReadable(const T &number, const Format &format, const std::string &locale="en_US.UTF-8") noexcept
Formats an arithmetic value (not wchar_t).
std::queue< std::string > Explode(std::string_view str, const char delimiter)
Splits on delimiter into a queue (empty tokens kept).
std::string ToUpper(std::string_view str) noexcept
Uppercase copy of str.
bool IsInteger(std::string_view str) noexcept
true when str parses as an integer (optional leading +/-).
Format
How HumanReadable prints a number.
Definition string.hxx:55
@ Raw
Unscaled decimal.
@ HumanReadableNumber
Grouped / locale number.
@ HumanReadableBytes
Byte units (KiB, MiB, …).
std::string ToLower(std::string_view str) noexcept
Lowercase copy of str.
std::string SanitizeNewlines(std::string_view str) noexcept
Normalizes \r\n to \n.
std::string UTF8Encode(std::wstring_view ws)
Wide text to UTF-8.
constexpr std::string Indent(const int &level) noexcept
level tab characters, or empty when level == 0.
Definition string.hxx:65
std::string RemoveWhitespace(std::string_view str) noexcept
Strips every whitespace character.
std::vector< std::string > Split(std::string_view str)
Splits on ASCII whitespace into a vector.
constexpr bool IsNumeric(std::string_view str) noexcept
true when every character is a decimal digit (std::isdigit).
Definition string.hxx:74
std::string FromByteVector(const std::vector< std::byte > &byte_vector) noexcept
Interprets bytes as char and builds a string.
STL namespace.
#define STORMBYTE_PUBLIC
Definition visibility.h:28