ModelViewer 0.1
Template for CPP projects
Loading...
Searching...
No Matches
StringHelpers.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2026
3** StringHelpers.hpp
4** File description:
5** StringHelpers.hpp
6*/
7
8#ifndef MODELVIEWER_STRINGHELPERS_HPP
9#define MODELVIEWER_STRINGHELPERS_HPP
10
11// Source - https://stackoverflow.com/a
12// Posted by Evan Teran, modified by community. See post 'Timeline' for change
13// history Retrieved 2026-01-02, License - CC BY-SA 4.0
14
15#include <algorithm>
16#include <cctype>
17#include <locale>
18
20
21inline bool isNotSpace(const unsigned char ch) noexcept {
22 return std::isspace(ch) == 0;
23}
24
29inline void ltrim(std::string &s) {
30 s.erase(s.begin(), std::ranges::find_if(s, isNotSpace));
31}
32
37inline void rtrim(std::string &s) {
38 s.erase(std::find_if(s.rbegin(), s.rend(),
39 [](unsigned char ch) { return !std::isspace(ch); })
40 .base(),
41 s.end());
42}
43
48inline void trim(std::string &s) {
49 rtrim(s);
50 ltrim(s);
51}
52
58inline std::string ltrim_copy(std::string s) {
59 ltrim(s);
60 return s;
61}
62
68inline std::string rtrim_copy(std::string s) {
69 rtrim(s);
70 return s;
71}
72
78inline std::string trim_copy(std::string s) {
79 trim(s);
80 return s;
81}
82
89inline std::string normalizePath(std::string p) {
90 const std::string fileScheme = "file://";
91 if (p.rfind(fileScheme, 0) == 0) {
92 p.erase(0, fileScheme.size());
93 }
94 if (p.size() >= 3 && p[0] == '/' &&
95 std::isalpha(static_cast<unsigned char>(p[1])) && p[2] == ':') {
96 p.erase(0, 1);
97 }
98 return p;
99}
100
101} // namespace model_viewer::string_helpers
102
103#endif // MODELVIEWER_STRINGHELPERS_HPP
Definition StringHelpers.hpp:19
void ltrim(std::string &s)
Trim from the start (in place)
Definition StringHelpers.hpp:29
void trim(std::string &s)
Trim from both ends (in place)
Definition StringHelpers.hpp:48
bool isNotSpace(const unsigned char ch) noexcept
Definition StringHelpers.hpp:21
std::string trim_copy(std::string s)
Trim from both ends (copying)
Definition StringHelpers.hpp:78
std::string ltrim_copy(std::string s)
Trim from the start (copying)
Definition StringHelpers.hpp:58
void rtrim(std::string &s)
Trim from the end (in place)
Definition StringHelpers.hpp:37
std::string rtrim_copy(std::string s)
Trim from the end (copying)
Definition StringHelpers.hpp:68
std::string normalizePath(std::string p)
Normalize a file path by removing "file://" scheme and leading slash on Windows paths.
Definition StringHelpers.hpp:89