SlideShare uma empresa Scribd logo
1 de 29
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!

Mais conteúdo relacionado

Mais procurados

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 - englishJen Yee Hong
 
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.17LogeekNightUkraine
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrencyxu liwei
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
Дмитрий Нестерук, Паттерны проектирования в 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 AutumnMoriyoshi Koizumi
 
Design Patterns in Modern C++
Design Patterns in Modern C++Design Patterns in Modern C++
Design Patterns in Modern C++Dmitri Nesteruk
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and deletePlatonov Sergey
 
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 3Linaro
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil Witecki
 
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 abstractionSergey 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 programmersJen Yee Hong
 

Mais procurados (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
 

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

Cryptography and secure systems
Cryptography and secure systemsCryptography and secure systems
Cryptography and secure systemsVsevolod 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++ CodeMicrosoft 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++ CodeMicrosoft 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 2PVS-Studio
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitAndrey 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 systemsAndrey Karpov
 
Writing Metasploit Plugins
Writing Metasploit PluginsWriting Metasploit Plugins
Writing Metasploit Pluginsamiable_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 VMsLourens 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 anatomyDongmin Yu
 
Степан Кольцов — 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 wayOleg Podsechin
 
Story of static code analyzer development
Story of static code analyzer developmentStory of static code analyzer development
Story of static code analyzer developmentAndrey 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
Bsidesm j
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 

Semelhante a 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
 

Último

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 

Último (20)

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 

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!

Notas do Editor

  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.