SlideShare a Scribd company logo
LET'S TALK ABOUT STRING
OPERATIONS IN C++17
string_view, searchers and conversion routines
Bartłomiej Filipek, bfilipek.com18th September 2018
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
The plan
 string_view
 Elementary conversion routines
 Searchers
 Summary
string_view
H E L L O W O R L D
Owning String
Non-Owning String-View
[start_ptr, length]
0
string_view
template<class CharT, class Traits = std::char_traits<CharT>>
class basic_string_view;
std::string_view std::basic_string_view<char>
std::wstring_view std::basic_string_view<wchar_t>
std::u16string_view std::basic_string_view<char16_t>
std::u32string_view std::basic_string_view<char32_t>
string_view creation
const char* cstr = "Hello World";
std::string_view sv1 { cstr };
std::cout << sv1 << ", len: " << sv1.size() << 'n';std::string str = "Hello String";
std::string_view sv3 = str;
std::cout << sv3 << ", len: " << sv3.size() << 'n';
Plus some more code…
constexpr basic_string_view() noexcept;
constexpr basic_string_view(const basic_string_view& other) noexcept = default;
constexpr basic_string_view(const CharT* s, size_type count);
constexpr basic_string_view(const CharT* s);
// from string:
operator std::basic_string_view<CharT, Traits>() const noexcept;
string_view operations
operator[]
at
front
back
data
size/length
max_size
empty
remove_prefix
remove_suffix
swap
copy (not constexpr)
substr - complexity O(1) and not O(n) as in std::string
compare
find
rfind
find_first_of
find_last_of
find_first_not_of
find_last_not_of
operators for lexicography compare: ==, !=, <=, >=, <, >
operator <<
Bonus, C++20:
starts_with
ends_with
How many string copies?
std::string StartFromWordStr(const std::string& strArg, const std::string& word)
{
return strArg.substr(strArg.find(word));
}
// call:
std::string str {"Hello Amazing Programming Environment" };
auto subStr = StartFromWordStr(str, "Programming Environment");
std::cout << subStr << "n";
How many copies?
std::string_view StartFromWord(std::string_view str, std::string_view word)
{
return str.substr(str.find(word));
}
// call:
std::string str {"Hello Amazing Programming Environment"};
auto subView = StartFromWord(str, "Programming Environment");
std::cout << subView << 'n';
Substr performance!
http://quick-bench.com/F1NGrjNtcNimqG2q6QzvHKPDpQY
More advanced example, string split
std::vector<std::string_view> splitSVStd(std::string_view strv, std::string_view delims = " ")
{
std::vector<std::string_view> output;
//output.reserve(strv.length() / 4);
auto first = strv.begin();
while (first != strv.end())
{
const auto second = std::find_first_of(first, std::cend(strv), std::cbegin(delims), std::cend(delims));
if (first != second)
{
output.emplace_back(strv.substr(std::distance(strv.begin(), first), std::distance(first, second)));
}
if (second == strv.end())
break;
first = std::next(second);
}
return output;
}
https://www.bfilipek.com/2018/07/string-view-perf.html
Split Performance
http://quick-bench.com/mhyUI8Swxu3As-RafVUSVfEZd64
SSO
 Currently, it’s 15 characters in MSVC (VS 2017)/GCC (8.1) or 22
characters in Clang (6.0).
Risks!
 Non null terminated strings
 Temporary objects
Risks – non null terminated strings
std::string s = "Hello World";
std::cout << s.size() << 'n';
std::string_view sv = s;
std::cout << sv.size() << 'n';
// 11 & 11
std::string s = "Hello World";
std::cout << s.size() << 'n';
std::string_view sv = s;
auto sv2 = sv.substr(0, 5);
std::cout << sv2.data() << 'n';
// again “Hello World” !
std::string number = "123.456";
std::string_view svNum { number.data(), 3 };
auto f = atof(svNum.data()); // should be 123, but is 123.456!
std::cout << f << 'n';
std::string s { sv.data(), sv.size() };
Risks – temporary objects!
std::string_view StartFromWord(std::string_view str, std::string_view word)
{
return str.substr(str.find(word)); // substr creates only a new view
}
auto str = "My Super"s;
auto sv = StartFromWord(str + " String", "Super");
Risks – temporary objects!
std::vector<int> GenerateVec()
{
return std::vector<int>(5, 1);
}
const std::vector<int>& refv = GenerateVec();
for (auto &elem : GenerateVec())
{
// ...
}
std::string func()
{
std::string s;
// build s...
return s;
}
std::string_view sv = func();
// no temp lifetime extension!
std::vector<int> CreateVector() { ... }
std::string GetString() { return "Hello"; }
auto &x = CreateVector()[10]; // arbitrary element!
auto pStr = GetString().c_str();
https://wg21.link/p0936
string_view - summary
 What do you think?
Elementary Conversion Routines
facility shortcomings
sprintf format string, locale, buffer overrun
snprintf format string, locale
sscanf format string, locale
atol locale, does not signal errors
strtol locale, ignores whitespace and 0x prefix
strstream locale, ignores whitespace
stringstream locale, ignores whitespace, memory allocation
num_put / num_get facets locale, virtual function
to_string locale, memory allocation
stoi etc.
locale, memory allocation, ignores whitespace and 0x
prefix, exception on error
https://wg21.link/p0067r5
Elementary Conversion Routines
10,000,000
(coliru)
10,000,000
(Laptop1)
50,000,000
(Laptop1)
50,000,000
(Lenovo)
50,000,000
(Laptop1 x64)
50,000,000
(Laptop2)
atol() 616 546 2,994 4,202 3,311 4,068
strtoul() 459 454 2,421 2,560 2,660 2,852
from_chars() 244 136 745 884 1,027 972
>> 1,484 7,299 37,590 47,072 31,351 48,116
stoul() 1,029 798 4,115 4,636 6,328 5,210
https://www.fluentcpp.com/2018/07/24/how-to-convert-a-string-to-an-int-in-c/
https://www.fluentcpp.com/2018/07/27/how-to-efficiently-convert-a-string-to-an-int-in-c/
Elementary Conversion Routines
 from_chars, to_chars
 No locale
 No memory allocation
 C-style?
Elementary Conversion Routines
std::from_chars_result from_chars(const char* first, const char* last, int &value, int base = 10);
std::from_chars_result from_chars(const char* first, const char* last, float& value,
std::chars_format fmt = std::chars_format::general);
struct from_chars_result {
const char* ptr;
std::errc ec;
};
struct to_chars_result {
char* ptr;
std::errc ec;
};
std::to_chars_result to_chars(char* first, char* last, int value, int base = 10);
std::to_chars_result to_chars(char* first, char* last, float value,
std::chars_format fmt, int precision);
Result
int value;
const auto res = std::from_chars(str.data(), str.data() + str.size(), value);
if (res.ec == std::errc::invalid_argument)
{
std::cout << "invalid argument!, res.p distance: " << 'n';
}
else if (res.ec == std::errc::result_out_of_range)
{
std::cout << "out of range! res.p distance: " << 'n';
}
else
{
std::cout << "value: " << value << 'n';
}
Twitter
https://twitter.com/StephanTLavavej
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);
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
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";
Searchers - performance
Summary
 string_view – good potential, with some risks!
 Conversion routines – finally!
 Searchers – nice addition!

More Related Content

What's hot

2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english
Jen Yee Hong
 
C++11
C++11C++11
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
LogeekNightUkraine
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
xu liwei
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
Open Gurukul
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
Geeks Anonymes
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
CyberPlusIndia
 
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI векеДмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Sergey Platonov
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 Autumn
Moriyoshi Koizumi
 
Design Patterns in Modern C++
Design Patterns in Modern C++Design Patterns in Modern C++
Design Patterns in Modern C++
Dmitri Nesteruk
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
Carson Wilber
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
Platonov Sergey
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
Sasha Goldshtein
 
HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3
Linaro
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
Kamil Witecki
 
Clang tidy
Clang tidyClang tidy
Clang tidy
Yury Yafimachau
 
C++ references
C++ referencesC++ references
C++ references
corehard_by
 
C
CC
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
Gor Nishanov,  C++ Coroutines – a negative overhead abstractionGor Nishanov,  C++ Coroutines – a negative overhead abstraction
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
Sergey Platonov
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
Jen Yee Hong
 

What's hot (20)

2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english
 
C++11
C++11C++11
C++11
 
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI векеДмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI веке
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 Autumn
 
Design Patterns in Modern C++
Design Patterns in Modern C++Design Patterns in Modern C++
Design Patterns in Modern C++
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
Clang tidy
Clang tidyClang tidy
Clang tidy
 
C++ references
C++ referencesC++ references
C++ references
 
C
CC
C
 
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
Gor Nishanov,  C++ Coroutines – a negative overhead abstractionGor Nishanov,  C++ Coroutines – a negative overhead abstraction
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
 

Similar to Let's talks about string operations in C++17

CGI.ppt
CGI.pptCGI.ppt
Cryptography and secure systems
Cryptography and secure systemsCryptography and secure systems
Cryptography and secure systems
Vsevolod Stakhov
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
Microsoft Tech Community
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
Microsoft Tech Community
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
apidays
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
PVS-Studio
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGit
Andrey Karpov
 
Static analysis and writing C/C++ of high quality code for embedded systems
Static analysis and writing C/C++ of high quality code for embedded systemsStatic analysis and writing C/C++ of high quality code for embedded systems
Static analysis and writing C/C++ of high quality code for embedded systems
Andrey Karpov
 
Writing Metasploit Plugins
Writing Metasploit PluginsWriting Metasploit Plugins
Writing Metasploit Plugins
amiable_indian
 
[1C2]webrtc 개발, 현재와 미래
[1C2]webrtc 개발, 현재와 미래[1C2]webrtc 개발, 현재와 미래
[1C2]webrtc 개발, 현재와 미래
NAVER D2
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
Lourens Naudé
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
corehard_by
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
Dongmin Yu
 
CompilersAndLibraries
CompilersAndLibrariesCompilersAndLibraries
CompilersAndLibraries
Staffan Tjernström
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
Yandex
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
Oleg Podsechin
 
Story of static code analyzer development
Story of static code analyzer developmentStory of static code analyzer development
Story of static code analyzer development
Andrey Karpov
 
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ..."Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
PVS-Studio
 
Bsides
BsidesBsides
Bsides
m j
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
PRADEEP
 

Similar to Let's talks about string operations in C++17 (20)

CGI.ppt
CGI.pptCGI.ppt
CGI.ppt
 
Cryptography and secure systems
Cryptography and secure systemsCryptography and secure systems
Cryptography and secure systems
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGit
 
Static analysis and writing C/C++ of high quality code for embedded systems
Static analysis and writing C/C++ of high quality code for embedded systemsStatic analysis and writing C/C++ of high quality code for embedded systems
Static analysis and writing C/C++ of high quality code for embedded systems
 
Writing Metasploit Plugins
Writing Metasploit PluginsWriting Metasploit Plugins
Writing Metasploit Plugins
 
[1C2]webrtc 개발, 현재와 미래
[1C2]webrtc 개발, 현재와 미래[1C2]webrtc 개발, 현재와 미래
[1C2]webrtc 개발, 현재와 미래
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
CompilersAndLibraries
CompilersAndLibrariesCompilersAndLibraries
CompilersAndLibraries
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Story of static code analyzer development
Story of static code analyzer developmentStory of static code analyzer development
Story of static code analyzer development
 
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ..."Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
 
Bsides
BsidesBsides
Bsides
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 

Recently uploaded

Hyperledger Besu 빨리 따라하기 (Private Networks)
Hyperledger Besu 빨리 따라하기 (Private Networks)Hyperledger Besu 빨리 따라하기 (Private Networks)
Hyperledger Besu 빨리 따라하기 (Private Networks)
wonyong hwang
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
mohitd6
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
Paul Brebner
 
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
manji sharman06
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
jrodriguezq3110
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
OnePlan Solutions
 
Hands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion StepsHands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion Steps
servicesNitor
 
Folding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a seriesFolding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a series
Philip Schwarz
 
Folding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a seriesFolding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a series
Philip Schwarz
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
vaishalijagtap12
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
What is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdfWhat is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdf
kalichargn70th171
 
What’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 UpdateWhat’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 Update
VictoriaMetrics
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 
Refactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contextsRefactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contexts
Michał Kurzeja
 
Trailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptxTrailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptx
ImtiazBinMohiuddin
 
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery FleetStork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
Vince Scalabrino
 

Recently uploaded (20)

Hyperledger Besu 빨리 따라하기 (Private Networks)
Hyperledger Besu 빨리 따라하기 (Private Networks)Hyperledger Besu 빨리 따라하기 (Private Networks)
Hyperledger Besu 빨리 따라하기 (Private Networks)
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
 
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
Call Girls Bangalore🔥7023059433🔥Best Profile Escorts in Bangalore Available 24/7
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
 
Hands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion StepsHands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion Steps
 
Folding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a seriesFolding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a series
 
Folding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a seriesFolding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a series
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
What is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdfWhat is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdf
 
What’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 UpdateWhat’s new in VictoriaMetrics - Q2 2024 Update
What’s new in VictoriaMetrics - Q2 2024 Update
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 
Refactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contextsRefactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contexts
 
Trailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptxTrailhead Talks_ Journey of an All-Star Ranger .pptx
Trailhead Talks_ Journey of an All-Star Ranger .pptx
 
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery FleetStork Product Overview: An AI-Powered Autonomous Delivery Fleet
Stork Product Overview: An AI-Powered Autonomous Delivery Fleet
 

Let's talks about string operations in C++17

  • 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
  • 3. The plan  string_view  Elementary conversion routines  Searchers  Summary
  • 4. string_view H E L L O W O R L D Owning String Non-Owning String-View [start_ptr, length] 0
  • 5. string_view template<class CharT, class Traits = std::char_traits<CharT>> class basic_string_view; std::string_view std::basic_string_view<char> std::wstring_view std::basic_string_view<wchar_t> std::u16string_view std::basic_string_view<char16_t> std::u32string_view std::basic_string_view<char32_t>
  • 6. string_view creation const char* cstr = "Hello World"; std::string_view sv1 { cstr }; std::cout << sv1 << ", len: " << sv1.size() << 'n';std::string str = "Hello String"; std::string_view sv3 = str; std::cout << sv3 << ", len: " << sv3.size() << 'n'; Plus some more code… constexpr basic_string_view() noexcept; constexpr basic_string_view(const basic_string_view& other) noexcept = default; constexpr basic_string_view(const CharT* s, size_type count); constexpr basic_string_view(const CharT* s); // from string: operator std::basic_string_view<CharT, Traits>() const noexcept;
  • 7. string_view operations operator[] at front back data size/length max_size empty remove_prefix remove_suffix swap copy (not constexpr) substr - complexity O(1) and not O(n) as in std::string compare find rfind find_first_of find_last_of find_first_not_of find_last_not_of operators for lexicography compare: ==, !=, <=, >=, <, > operator << Bonus, C++20: starts_with ends_with
  • 8. How many string copies? std::string StartFromWordStr(const std::string& strArg, const std::string& word) { return strArg.substr(strArg.find(word)); } // call: std::string str {"Hello Amazing Programming Environment" }; auto subStr = StartFromWordStr(str, "Programming Environment"); std::cout << subStr << "n";
  • 9. How many copies? std::string_view StartFromWord(std::string_view str, std::string_view word) { return str.substr(str.find(word)); } // call: std::string str {"Hello Amazing Programming Environment"}; auto subView = StartFromWord(str, "Programming Environment"); std::cout << subView << 'n';
  • 11. More advanced example, string split std::vector<std::string_view> splitSVStd(std::string_view strv, std::string_view delims = " ") { std::vector<std::string_view> output; //output.reserve(strv.length() / 4); auto first = strv.begin(); while (first != strv.end()) { const auto second = std::find_first_of(first, std::cend(strv), std::cbegin(delims), std::cend(delims)); if (first != second) { output.emplace_back(strv.substr(std::distance(strv.begin(), first), std::distance(first, second))); } if (second == strv.end()) break; first = std::next(second); } return output; } https://www.bfilipek.com/2018/07/string-view-perf.html
  • 13. SSO  Currently, it’s 15 characters in MSVC (VS 2017)/GCC (8.1) or 22 characters in Clang (6.0).
  • 14. Risks!  Non null terminated strings  Temporary objects
  • 15. Risks – non null terminated strings std::string s = "Hello World"; std::cout << s.size() << 'n'; std::string_view sv = s; std::cout << sv.size() << 'n'; // 11 & 11 std::string s = "Hello World"; std::cout << s.size() << 'n'; std::string_view sv = s; auto sv2 = sv.substr(0, 5); std::cout << sv2.data() << 'n'; // again “Hello World” ! std::string number = "123.456"; std::string_view svNum { number.data(), 3 }; auto f = atof(svNum.data()); // should be 123, but is 123.456! std::cout << f << 'n'; std::string s { sv.data(), sv.size() };
  • 16. Risks – temporary objects! std::string_view StartFromWord(std::string_view str, std::string_view word) { return str.substr(str.find(word)); // substr creates only a new view } auto str = "My Super"s; auto sv = StartFromWord(str + " String", "Super");
  • 17. Risks – temporary objects! std::vector<int> GenerateVec() { return std::vector<int>(5, 1); } const std::vector<int>& refv = GenerateVec(); for (auto &elem : GenerateVec()) { // ... } std::string func() { std::string s; // build s... return s; } std::string_view sv = func(); // no temp lifetime extension! std::vector<int> CreateVector() { ... } std::string GetString() { return "Hello"; } auto &x = CreateVector()[10]; // arbitrary element! auto pStr = GetString().c_str(); https://wg21.link/p0936
  • 18. string_view - summary  What do you think?
  • 19. Elementary Conversion Routines facility shortcomings sprintf format string, locale, buffer overrun snprintf format string, locale sscanf format string, locale atol locale, does not signal errors strtol locale, ignores whitespace and 0x prefix strstream locale, ignores whitespace stringstream locale, ignores whitespace, memory allocation num_put / num_get facets locale, virtual function to_string locale, memory allocation stoi etc. locale, memory allocation, ignores whitespace and 0x prefix, exception on error https://wg21.link/p0067r5
  • 20. Elementary Conversion Routines 10,000,000 (coliru) 10,000,000 (Laptop1) 50,000,000 (Laptop1) 50,000,000 (Lenovo) 50,000,000 (Laptop1 x64) 50,000,000 (Laptop2) atol() 616 546 2,994 4,202 3,311 4,068 strtoul() 459 454 2,421 2,560 2,660 2,852 from_chars() 244 136 745 884 1,027 972 >> 1,484 7,299 37,590 47,072 31,351 48,116 stoul() 1,029 798 4,115 4,636 6,328 5,210 https://www.fluentcpp.com/2018/07/24/how-to-convert-a-string-to-an-int-in-c/ https://www.fluentcpp.com/2018/07/27/how-to-efficiently-convert-a-string-to-an-int-in-c/
  • 21. Elementary Conversion Routines  from_chars, to_chars  No locale  No memory allocation  C-style?
  • 22. Elementary Conversion Routines std::from_chars_result from_chars(const char* first, const char* last, int &value, int base = 10); std::from_chars_result from_chars(const char* first, const char* last, float& value, std::chars_format fmt = std::chars_format::general); struct from_chars_result { const char* ptr; std::errc ec; }; struct to_chars_result { char* ptr; std::errc ec; }; std::to_chars_result to_chars(char* first, char* last, int value, int base = 10); std::to_chars_result to_chars(char* first, char* last, float value, std::chars_format fmt, int precision);
  • 23. Result int value; const auto res = std::from_chars(str.data(), str.data() + str.size(), value); if (res.ec == std::errc::invalid_argument) { std::cout << "invalid argument!, res.p distance: " << 'n'; } else if (res.ec == std::errc::result_out_of_range) { std::cout << "out of range! res.p distance: " << 'n'; } else { std::cout << "value: " << value << 'n'; }
  • 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!

Editor's Notes

  1. 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¹.
  2. What else?
  3. When matching and finding a mismatch we know that “u” doesn’t occur in “word” so we can skip the next two positions.
  4. When matching and finding a mismatch we know that “u” doesn’t occur in “word” so we can skip the next two positions.