{"id":10290,"date":"2025-01-13T11:42:00","date_gmt":"2025-01-13T11:42:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10290"},"modified":"2025-07-03T14:20:50","modified_gmt":"2025-07-03T14:20:50","slug":"c26-library-string-and-string_view-processing","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c26-library-string-and-string_view-processing\/","title":{"rendered":"C++26 Library: string and string_view Processing"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">C++26 offers many small improvements around <code>string<\/code>s and <code>string_view<\/code>s<code>.<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1023\" height=\"510\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/StringandStringviews-1.png\" alt=\"\" class=\"wp-image-10297\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/StringandStringviews-1.png 1023w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/StringandStringviews-1-300x150.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/StringandStringviews-1-768x383.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/StringandStringviews-1-705x351.png 705w\" sizes=\"auto, (max-width: 1023px) 100vw, 1023px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"> First of all: What is a <code>string_view?<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">std::string_view<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>std::string_view <\/code>is a non-owning reference to a <code>string<\/code>. It represents a view of a sequence of characters. This sequence of characters can be a C++ string or a C-string. In a typical way, C++17 offers four type synonyms for the underlying character types.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">std<span style=\"color: #555555\">::<\/span>string_view      std<span style=\"color: #555555\">::<\/span>basic_string_view<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">char<\/span><span style=\"color: #555555\">&gt;<\/span>\nstd<span style=\"color: #555555\">::<\/span>wstring_view     std<span style=\"color: #555555\">::<\/span>basic_string_view<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">wchar_t<\/span><span style=\"color: #555555\">&gt;<\/span>\nstd<span style=\"color: #555555\">::<\/span>u16string_view   std<span style=\"color: #555555\">::<\/span>basic_string_view<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">char16_t<\/span><span style=\"color: #555555\">&gt;<\/span>\nstd<span style=\"color: #555555\">::<\/span>u32string_view   std<span style=\"color: #555555\">::<\/span>basic_string_view<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">char32_t<\/span><span style=\"color: #555555\">&gt;<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">One question remains. Why do we need a<code> std::string_view<\/code>? Why had Google, LLVM, and Bloomberg already an implementation of a string view? The answer is easy. It\u2019s pretty cheap to copy a <code>std::string_view. <\/code>A <code>std::string_view<\/code> only needs two pieces of information: the pointer to the character sequence and their length. As you may assume, the <code>std::string_view<\/code> and its three siblings consist mainly of reading operations that follow the interface of <code>std::string.<\/code> Mainly because it gets the new methods <code>remove_prefix <\/code>and<code> remove_suffix<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing for Success or Failure of &lt;<code>charconv<\/code>&gt; Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The functions<code> std::to_chars<\/code> and&nbsp;<code> std::from_chars<\/code> was inconvenient to test: <code>if(res.ec == std::errc{})<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Here&#8217;s a simplified program from <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/to_chars\">https:\/\/en.cppreference.com\/w\/cpp\/utility\/to_chars<\/a>.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ charconv.cpp <\/span>\n \n<span style=\"color: #009999\">#include &lt;charconv&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iomanip&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string_view&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;system_error&gt;<\/span>\n \n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T<span style=\"color: #555555\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold\">void<\/span> show_to_chars(T value) {\n    <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">size_t<\/span> buf_size <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">5<\/span>;\n    <span style=\"color: #007788; font-weight: bold\">char<\/span> buf[buf_size];\n    std<span style=\"color: #555555\">::<\/span>to_chars_result result <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>to_chars(buf, buf <span style=\"color: #555555\">+<\/span> buf_size, value);\n \n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (result.ec <span style=\"color: #555555\">!=<\/span> std<span style=\"color: #555555\">::<\/span>errc{})\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>make_error_code(result.ec).message() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">else<\/span>{\n        std<span style=\"color: #555555\">::<\/span>string_view str(buf, result.ptr <span style=\"color: #555555\">-<\/span> buf);\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>quoted(str) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n}\n \n<span style=\"color: #007788; font-weight: bold\">int<\/span> main() {\n    show_to_chars(<span style=\"color: #FF6600\">42<\/span>);\n    show_to_chars(<span style=\"color: #FF6600\">1234567<\/span>);  \n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The program includes the headers <code>&lt;charconv&gt;<\/code> for character conversion, <code>&lt;iomanip&gt;<\/code> for input\/output manipulators,  <code>&lt;string_view&gt;<\/code> for handling <code>string_view<\/code>s, and <code>&lt;system_error&gt;<\/code> for error handling.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>show_to_chars <\/code>function template takes a value of any type <code>T<\/code> and tries to convert it to a character sequence. Inside the function, a buffer of size 5 is declared to hold the resulting character sequence. The <code>std::to_chars<\/code> function is then called to perform the conversion, storing the result in a <code>std::to_chars_result<\/code> object named <code>result<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>result<\/code> object contains a pointer to the end of the converted character sequence and an error code. If the error code is not equal to <code>std::errc{}<\/code>, indicating an error occurred during conversion, an error message is printed to the console using <code>std::make_error_code(result.ec).message()<\/code>. Otherwise, a <code>std::string_view<\/code> object is created to represent the converted character sequence, and the sequence is printed to the console using <code>std::quoted<\/code> to ensure it is displayed within double quotes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>main <\/code>function, the <code>show_to_chars <\/code>function is called twice: first with the value <code>42<\/code> and then with the value <code>1234567<\/code>. The first call successfully converts the value <code>42<\/code> to a character sequence and prints it. However, the second call attempts to convert the value <code>1234567<\/code>, which exceeds the buffer size of 5, resulting in an error message being printed to the console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, here comes the output of the program:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"436\" height=\"62\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/charconv.png\" alt=\"\" class=\"wp-image-10306\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/charconv.png 436w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/charconv-300x43.png 300w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks to C++26, the function <code>std::to_chars <\/code>retuns a boolean and the function <code>show_to_chars <\/code>can be simplified:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T<span style=\"color: #555555\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold\">void<\/span> show_to_chars(T value) {\n    std<span style=\"color: #555555\">::<\/span>array<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">char<\/span>, <span style=\"color: #FF6600\">5<\/span><span style=\"color: #555555\">&gt;<\/span> str;\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> result <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>to_chars(str.data(), str.data() <span style=\"color: #555555\">+<\/span> str.size(), value)) {\n            std<span style=\"color: #555555\">::<\/span>string_view strView(str.data(), result.ptr);\n            std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>quoted(strView) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n        }\n    <span style=\"color: #006699; font-weight: bold\">else<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>make_error_code(result.ec).message() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Inside the function, a <code>std::array<\/code> of characters with a fixed size of 5 is declared to hold the resulting character sequence. The <code>std::to_chars<\/code> function is then called to perform the conversion. This function attempts to convert the numeric value <code>value<\/code> into a character sequence and store it in the <code>str<\/code> array. The <code>std::to_chars<\/code> function returns a <code>std::to_chars_result<\/code> object, which contains a pointer to the end of the converted character sequence and an error code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The function uses an <code>if<\/code> statement to check the result of the <code>std::to_chars<\/code> call. If the conversion is successful, the <code>result<\/code> object will be implicitly convertible to <code>true<\/code>, and the function will proceed to print the converted character sequence. This is done by creating a <code>std::string_view<\/code> object named <code>strView<\/code> that represents the character sequence from the beginning of the <code>str<\/code> array up to the <code>result.ptr<\/code> pointer. The <code>std::cout<\/code> stream is then used to print this string view to the console, with <code>std::quoted<\/code> ensuring the output is displayed within double quotes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the conversion fails, the <code>result <\/code>bject will be implicitly convertible to <code>false<\/code>, and the function will print an error message instead. The error message is obtained by calling <code>std::make_error_code(result.ec).message()<\/code>, which converts the error code to a human-readable string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> They are further <code>string <\/code>and <code>string_view<\/code> processing functions in C++26:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Interfacing<code> stringstream<\/code>s with <code>std::string_view<\/code><\/li>\n\n\n\n<li>Concatenation of <code>string<\/code>s and<code> string_view<\/code>s<\/li>\n\n\n\n<li>Arithmetic overloads of <code>std::to_string<\/code> and use <code>std::format<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"> I already presented them in my previous post: &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/an-overview-of-c26-the-library\/\">An Overview of C++26: The Library<\/a>&#8220;. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The C++26 library has more to offer. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++26 offers many small improvements around strings and string_views. First of all: What is a string_view? std::string_view A std::string_view is a non-owning reference to a string. It represents a view of a sequence of characters. This sequence of characters can be a C++ string or a C-string. In a typical way, C++17 offers four type [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10294,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[463],"class_list":["post-10290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog","tag-string"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=10290"}],"version-history":[{"count":19,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10290\/revisions"}],"predecessor-version":[{"id":10551,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10290\/revisions\/10551"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10294"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}