Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
stringutil.h
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2024 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
#ifndef STRINGUTIL_H
17
#define STRINGUTIL_H
18
19
#include <string>
20
#include <string_view>
21
22
/** @file
23
* @brief Some helper functions for std::string
24
*/
25
26
/** Replaces occurrences of substring \a toReplace in string \a s with string \a replaceWith.
27
* Modifies \a s in place.
28
*/
29
inline
void
substituteInplace
(std::string &s,
30
std::string_view toReplace,std::string_view replaceWith)
31
{
32
std::string buf;
33
size_t
pos = 0;
34
size_t
prevPos = 0;
35
buf.reserve(s.length());
36
37
while
((pos=s.find(toReplace, prevPos))!=std::string::npos)
38
{
39
buf.append(s, prevPos, pos - prevPos);
40
buf += replaceWith;
41
prevPos = pos + toReplace.length();
42
}
43
buf.append(s, prevPos, s.size() - prevPos);
44
s.swap(buf);
45
}
46
47
/** Returns a new string where occurrences of substring \a toReplace in string \a s are replaced by
48
* string \a replaceWith.
49
*/
50
inline
std::string
substituteStringView
(std::string_view s,
51
std::string_view toReplace,std::string_view replaceWith)
52
{
53
std::string buf;
54
size_t
pos = 0;
55
size_t
prevPos = 0;
56
buf.reserve(s.length());
57
58
while
((pos=s.find(toReplace, prevPos))!=std::string::npos)
59
{
60
buf.append(s, prevPos, pos - prevPos);
61
buf += replaceWith;
62
prevPos = pos + toReplace.length();
63
}
64
buf.append(s, prevPos, s.size() - prevPos);
65
return
buf;
66
}
67
68
69
/** Given a string view \a s, returns a new, narrower view on that string, skipping over any
70
* leading or trailing whitespace characters.
71
*/
72
inline
std::string_view
stripWhiteSpace
(std::string_view s)
73
{
74
static
auto
isspace = [](
char
c){
return
c==
' '
|| c==
'\t'
|| c==
'\n'
|| c==
'\r'
; };
75
size_t
sl = s.length();
76
if
(sl==0 || (!isspace(s[0]) && !isspace(s[sl-1])))
return
s;
77
size_t
start=0,
end
=sl-1;
78
while
(start<sl && isspace(s[start])) start++;
79
if
(start==sl)
return
s.substr(0,0);
// only whitespace
80
while
(
end
>start && isspace(s[
end
]))
end
--;
81
return
s.substr(start,
end
+1-start);
82
}
83
84
inline
void
addTerminalCharIfMissing
(std::string &s,
char
c)
85
{
86
if
(s.empty())
87
{
88
s+=c;
89
}
90
else
91
{
92
if
(s[s.length()-1]!=c) s+=c;
93
}
94
}
95
96
/// returns TRUE iff \a data points to a substring that matches string literal \a str
97
template
<
size_t
N>
98
bool
literal_at
(
const
char
*data,
const
char
(&str)[N])
99
{
100
size_t
len = N-1;
// exclude 0 terminator
101
return
data!=
nullptr
&& data[0]==str[0] &&
qstrncmp
(data+1,str+1,len-1)==0;
102
}
103
104
/// returns TRUE iff \a data points to a substring that matches string literal \a str
105
template
<
size_t
N>
106
bool
literal_at
(std::string_view data,
const
char
(&str)[N])
107
{
108
size_t
len = N-1;
// exclude 0 terminator
109
return
len<=data.size() && data[0]==str[0] &&
qstrncmp
(data.data()+1,str+1,len-1)==0;
110
}
111
112
#endif
// STRINGUTIL_H
end
DirIterator end(const DirIterator &) noexcept
Definition
dir.cpp:175
qstrncmp
int qstrncmp(const char *str1, const char *str2, size_t len)
Definition
qcstring.h:75
substituteStringView
std::string substituteStringView(std::string_view s, std::string_view toReplace, std::string_view replaceWith)
Returns a new string where occurrences of substring toReplace in string s are replaced by string repl...
Definition
stringutil.h:50
addTerminalCharIfMissing
void addTerminalCharIfMissing(std::string &s, char c)
Definition
stringutil.h:84
literal_at
bool literal_at(const char *data, const char(&str)[N])
returns TRUE iff data points to a substring that matches string literal str
Definition
stringutil.h:98
substituteInplace
void substituteInplace(std::string &s, std::string_view toReplace, std::string_view replaceWith)
Replaces occurrences of substring toReplace in string s with string replaceWith.
Definition
stringutil.h:29
stripWhiteSpace
std::string_view stripWhiteSpace(std::string_view s)
Given a string view s, returns a new, narrower view on that string, skipping over any leading or trai...
Definition
stringutil.h:72
src
stringutil.h
Generated by
1.17.0