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

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

#include <string>
#include <string_view>
#include "containers.h"
#include "regex.h"
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)
template<size_t N>
bool literal_at (const char *data, const char(&str)[N])
 returns true iff data points to a substring that matches string literal str
template<size_t N>
bool literal_at (std::string_view data, const char(&str)[N])
 returns true iff data points to a substring that matches string literal str
StringVector split (const std::string &s, const std::string &delimiter)
 split input string s by string delimiter delimiter.
StringVector split (const std::string &s, const reg::Ex &delimiter)
 split input string s by regular expression delimiter delimiter.
std::string join (const StringVector &sv, const std::string &delimiter)
 create a string where the string in the vector are joined by the given delimiter
size_t findIndex (const StringVector &sv, const std::string &s)
 find the index of a string in a vector of strings, returns std::string::npos if the string could not be found
size_t findIndex (const std::string &s, const reg::Ex &re)
 find the index of the first occurrence of pattern re in a string s returns std::string::npos if the pattern could not be found
std::string removeEmptyLines (const std::string &s)

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 87 of file stringutil.h.

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

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

◆ findIndex() [1/2]

size_t findIndex ( const std::string & s,
const reg::Ex & re )
inline

find the index of the first occurrence of pattern re in a string s returns std::string::npos if the pattern could not be found

Definition at line 175 of file stringutil.h.

176{
178 return reg::search(s,match,re) ? static_cast<size_t>(match.position()) : std::string::npos;
179}
Object representing the matching results.
Definition regex.h:154
bool search(std::string_view str, Match &match, const Ex &re, size_t pos)
Search in a given string str starting at position pos for a match against regular expression re.
Definition regex.cpp:847
bool match(std::string_view str, Match &match, const Ex &re)
Matches a given string str for a match against regular expression re.
Definition regex.cpp:858

References reg::search().

◆ findIndex() [2/2]

size_t findIndex ( const StringVector & sv,
const std::string & s )
inline

find the index of a string in a vector of strings, returns std::string::npos if the string could not be found

Definition at line 167 of file stringutil.h.

168{
169 auto it = std::find(sv.begin(),sv.end(),s);
170 return it!=sv.end() ? static_cast<size_t>(it-sv.begin()) : std::string::npos;
171}

Referenced by initUCF(), VhdlDocGen::parseForBinding(), VhdlDocGen::parseUCF(), DotFilePatcher::run(), and VhdlDocGen::writeFormatString().

◆ join()

std::string join ( const StringVector & sv,
const std::string & delimiter )
inline

create a string where the string in the vector are joined by the given delimiter

Definition at line 153 of file stringutil.h.

154{
155 std::string result;
156 bool first=true;
157 for (const auto &s : sv)
158 {
159 if (!first) result+=delimiter;
160 first=false;
161 result+=s;
162 }
163 return result;
164}

Referenced by handleAnchor(), handleCite(), handleFormatBlock(), and handleImage().

◆ literal_at() [1/2]

template<size_t N>
bool literal_at ( const char * data,
const char(&) str[N] )

returns true iff data points to a substring that matches string literal str

Definition at line 101 of file stringutil.h.

102{
103 size_t len = N-1; // exclude 0 terminator
104 return data!=nullptr && data[0]==str[0] && dstrncmp(data+1,str+1,len-1)==0;
105}
int dstrncmp(const char *str1, const char *str2, size_t len)
Definition dstring.h:60

References dstrncmp().

Referenced by computeIndent(), convertMapFile(), detab(), extractCopyDocId(), getConvertLatexMacro(), isExplicitPage(), isNewline(), Markdown::Private::isSpecialCommand(), Markdown::process(), Markdown::Private::processNmdash(), readSVGSize(), removeIdsAndMarkers(), skipOverFileAndLineCommands(), stripIndentation(), stripLeadingAndTrailingEmptyLines(), stripTrailingWhiteSpace(), and PlantumlManager::writePlantUMLSource().

◆ literal_at() [2/2]

template<size_t N>
bool literal_at ( std::string_view data,
const char(&) str[N] )

returns true iff data points to a substring that matches string literal str

Definition at line 109 of file stringutil.h.

110{
111 size_t len = N-1; // exclude 0 terminator
112 return len<=data.size() && data[0]==str[0] && dstrncmp(data.data()+1,str+1,len-1)==0;
113}

References dstrncmp().

◆ removeEmptyLines()

std::string removeEmptyLines ( const std::string & s)
inline

Definition at line 181 of file stringutil.h.

182{
183 std::string out;
184 out.reserve(s.length());
185 const char *p=s.data();
186 if (p)
187 {
188 char c = 0;
189 while ((c=*p++))
190 {
191 if (c=='\n')
192 {
193 const char *e = p;
194 while (*e==' ' || *e=='\t') e++;
195 if (*e=='\n')
196 {
197 p=e;
198 }
199 else out+=c;
200 }
201 else
202 {
203 out+=c;
204 }
205 }
206 }
207 return out;
208}

Referenced by substituteHtmlKeywords(), and substituteLatexKeywords().

◆ split() [1/2]

StringVector split ( const std::string & s,
const reg::Ex & delimiter )
inline

split input string s by regular expression delimiter delimiter.

returns a vector of non-empty strings that are between the delimiters

Definition at line 134 of file stringutil.h.

135{
136 StringVector result;
137 reg::Iterator iter(s, delimiter);
139 size_t p=0;
140 for ( ; iter != end; ++iter)
141 {
142 const auto &match = *iter;
143 size_t i=match.position();
144 size_t l=match.length();
145 if (i>p) result.push_back(s.substr(p,i-p));
146 p=i+l;
147 }
148 if (p<s.length()) result.push_back(s.substr(p));
149 return result;
150}
Class to iterate through matches.
Definition regex.h:239
std::vector< std::string > StringVector
Definition containers.h:33
DirIterator end(const DirIterator &) noexcept
Definition dir.cpp:176

References end().

◆ split() [2/2]

StringVector split ( const std::string & s,
const std::string & delimiter )
inline

split input string s by string delimiter delimiter.

returns a vector of non-empty strings that are between the delimiters

Definition at line 117 of file stringutil.h.

118{
119 StringVector result;
120 size_t prev = 0, pos = 0, len = s.length();
121 do
122 {
123 pos = s.find(delimiter, prev);
124 if (pos == std::string::npos) pos = len;
125 if (pos>prev) result.push_back(s.substr(prev,pos-prev));
126 prev = pos + delimiter.length();
127 }
128 while (pos<len && prev<len);
129 return result;
130}

Referenced by VHDLOutlineParser::addProto(), VHDLOutlineParser::addVhdlType(), FlowChart::alignCommentNode(), anonymous_namespace{tagreader.cpp}::TagFileParser::buildLists(), VHDLOutlineParser::checkInlineCode(), checkVhdlString(), VHDLOutlineParser::createFunction(), VhdlDocGen::findArchitecture(), CitationManager::generatePage(), getFilteredImageAttributes(), VhdlDocGen::getIndexWord(), DocParser::handleCite(), DocPara::handleCommand(), DocParser::handleImage(), DocPara::handleInclude(), Qhp::initialize(), VhdlDocGen::parseForBinding(), VhdlDocGen::parseForConfig(), parseIncludeOptions(), writeFuncProto(), VhdlDocGen::writeInlineClassLink(), and VhdlDocGen::writeRecUnitDocu().

◆ 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 75 of file stringutil.h.

76{
77 static auto isspace = [](char c){ return c==' ' || c=='\t' || c=='\n' || c=='\r'; };
78 size_t sl = s.length();
79 if (sl==0 || (!isspace(s[0]) && !isspace(s[sl-1]))) return s;
80 size_t start=0, end=sl-1;
81 while (start<sl && isspace(s[start])) start++;
82 if (start==sl) return s.substr(0,0); // only whitespace
83 while (end>start && isspace(s[end])) end--;
84 return s.substr(start,end+1-start);
85}

References end().

Referenced by DefinitionImpl::_setDocumentation(), endBrief(), VhdlDocGen::getClass(), handleInheritanceGraph(), handleToc(), FileDefImpl::hasDetailedDescription(), DefinitionImpl::operator=(), parseIncludeOptions(), DefinitionImpl::setDocumentation(), DefinitionMixin< Base >::setDocumentation(), DefinitionMutable::setDocumentation(), MemberDefImpl::setDocumentation(), 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 32 of file stringutil.h.

34{
35 std::string buf;
36 size_t pos = 0;
37 size_t prevPos = 0;
38 buf.reserve(s.length());
39
40 while ((pos=s.find(toReplace, prevPos))!=std::string::npos)
41 {
42 buf.append(s, prevPos, pos - prevPos);
43 buf += replaceWith;
44 prevPos = pos + toReplace.length();
45 }
46 buf.append(s, prevPos, s.size() - prevPos);
47 s.swap(buf);
48}

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 53 of file stringutil.h.

55{
56 std::string buf;
57 size_t pos = 0;
58 size_t prevPos = 0;
59 buf.reserve(s.length());
60
61 while ((pos=s.find(toReplace, prevPos))!=std::string::npos)
62 {
63 buf.append(s, prevPos, pos - prevPos);
64 buf += replaceWith;
65 prevPos = pos + toReplace.length();
66 }
67 buf.append(s, prevPos, s.size() - prevPos);
68 return buf;
69}

Referenced by escapeAlias(), and replaceAliases().