Skip to main content

The stringutil.h File Reference

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

Included Headers

#include <string> #include <string_view>

Functions Index

voidsubstituteInplace (std::string &s, std::string_view toReplace, std::string_view replaceWith)

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

std::stringsubstituteStringView (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. More...

std::string_viewstripWhiteSpace (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. More...

voidaddTerminalCharIfMissing (std::string &s, char c)
template <size_t N>
boolliteral_at (const char *data, const char(&str)[N])

returns TRUE iff data points to a substring that matches string literal str More...

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

returns TRUE iff data points to a substring that matches string literal str More...

Description

Some helper functions for std::string.

Functions

addTerminalCharIfMissing()

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

Definition at line 84 of file stringutil.h.

84inline 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}

Referenced by checkAndOpenFile, fileToString, parseFile and parseInput.

literal_at()

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

98bool 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}

Reference qstrncmp.

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()

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

106bool 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}

Reference qstrncmp.

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.

72inline 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}

Reference 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.

29inline 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}

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.

50inline 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}

Referenced by escapeAlias and replaceAliases.


Generated via doxygen2docusaurus by Doxygen 1.14.0.