Doxygen
Loading...
Searching...
No Matches
stringutil.h File Reference

Some helper functions for std::string. More...

#include <string>
#include <string_view>
+ Include dependency graph for stringutil.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void substituteInplace (std::string &s, std::string_view toReplace, std::string_view replaceWith)
 Replaces occurrences of substring toReplace in string s with string replaceWith.
 
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 replaceWith.
 
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 trailing whitespace characters.
 
void addTerminalCharIfMissing (std::string &s, char c)
 

Detailed Description

Some helper functions for std::string.

Definition in file stringutil.h.

Function Documentation

◆ addTerminalCharIfMissing()

void addTerminalCharIfMissing ( std::string & s,
char c )
inline

Definition at line 84 of file stringutil.h.

85{
86 if (s.empty())
87 {
88 s+=c;
89 }
90 else
91 {
92 if (s[s.length()-1]!=c) s+=c;
93 }
94}

Referenced by fileToString(), parseFile(), and parseInput().

◆ stripWhiteSpace()

std::string_view stripWhiteSpace ( std::string_view s)
inline

Given a string view s, returns a new, narrower view on that string, skipping over any leading or trailing whitespace characters.

Definition at line 72 of file stringutil.h.

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}
DirIterator end(const DirIterator &) noexcept
Definition dir.cpp:175

References end().

Referenced by DefinitionImpl::_setDocumentation(), addValidAliasToMap(), endBrief(), VhdlDocGen::getClass(), handleInheritanceGraph(), handleToc(), FileDefImpl::hasDetailedDescription(), DefinitionImpl::operator=(), parseIncludeOptions(), DefinitionImpl::setDocumentation(), DefinitionMixin< Base >::setDocumentation(), DefinitionMutable::setDocumentation(), MemberDefImpl::setDocumentation(), toDefinition(), Markdown::Private::writeBlockQuote(), and FileDefImpl::writeBriefDescription().

◆ substituteInplace()

void substituteInplace ( std::string & s,
std::string_view toReplace,
std::string_view replaceWith )
inline

Replaces occurrences of substring toReplace in string s with string replaceWith.

Modifies s in place.

Definition at line 29 of file stringutil.h.

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}

Referenced by replaceAliasArguments().

◆ substituteStringView()

std::string substituteStringView ( std::string_view s,
std::string_view toReplace,
std::string_view replaceWith )
inline

Returns a new string where occurrences of substring toReplace in string s are replaced by string replaceWith.

Definition at line 50 of file stringutil.h.

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}

Referenced by escapeAlias(), and replaceAliases().