1. LET'S TALK ABOUT STRING
OPERATIONS IN C++17
string_view, searchers and conversion routines
Bartłomiej Filipek, bfilipek.com18th September 2018
2. About me
See my coding blog at:
www.bfilipek.com
11y+ experience
Currently @Xara.com
Text related features for advanced
document editors
Somehow addicted to C++
Xara Cloud Demo
C++17 In Detail
25. Searchers
• default_searcher
• boyer_moore_searcher
• boyer_moore_horspool_searcher
template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last);
template<class ForwardIterator, class Searcher>
ForwardIterator search(ForwardIterator first, ForwardIterator last, const Searcher& searcher);
26. Searchers
P: word
T: There would have been a time for such a word
^
|
word
https://www.youtube.com/watch?v=4Xyhb72LCX4
Bad character rule
Good Suffix Rule
http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/bmen.htm
http://www.cs.jhu.edu/~langmea/resources/lecture_notes/boyer_moore.pdf
http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
27. Searchers – some code
std::string testString = "Hello Super World";
std::string needle = "Super";
auto it = search(testString.begin(), testString.end(),
boyer_moore_searcher(needle.begin(), needle.end()));
if (it == testString.end())
cout << "The string " << needle << " not foundn";
29. Summary
string_view – good potential, with some risks!
Conversion routines – finally!
Searchers – nice addition!
Notas do Editor
How many copies?
The first one is for str.
• The second one is for the second argument in StartFromWordStr - the argument is const
string& so since we pass const char* it will create a new string.
• The third one comes from substr which returns a new string.
• Then we might also have another copy or two - as the object is returned from the function.
But usually, the compiler can optimise and elide the copies (especially since C++17 when Copy
Elision became mandatory in that case).
• If the string is short, then there might be no heap allocation as Small String Optimisation¹.
What else?
When matching and finding a mismatch we know that “u” doesn’t occur in “word” so we can skip the next two positions.
When matching and finding a mismatch we know that “u” doesn’t occur in “word” so we can skip the next two positions.