]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add lambda example to case transformation docs
authorJonathan Wakely <jwakely@redhat.com>
Wed, 12 Mar 2025 11:30:04 +0000 (11:30 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 12 Mar 2025 11:32:55 +0000 (11:32 +0000)
libstdc++-v3/ChangeLog:

* doc/xml/manual/strings.xml: Tweak formatting. Add example
using lambda.
* doc/html/manual/strings.html: Regenerate.

libstdc++-v3/doc/html/manual/strings.html
libstdc++-v3/doc/xml/manual/strings.xml

index 34a34dfa980c84973bc5cb749b09e7f4b9449504..5344d0e892382f7ecded0a6780e40821cde44909 100644 (file)
    </pre><p>
      <span class="emphasis"><em>Note</em></span> that these calls all
       involve the global C locale through the use of the C functions
-      <code class="code">toupper/tolower</code>.  This is absolutely guaranteed to work --
-      but <span class="emphasis"><em>only</em></span> if the string contains <span class="emphasis"><em>only</em></span> characters
-      from the basic source character set, and there are <span class="emphasis"><em>only</em></span>
-      96 of those.  Which means that not even all English text can be
+      <code class="code">toupper</code>/<code class="code">tolower</code>.
+      This is absolutely guaranteed to work
+      -- but <span class="emphasis"><em>only</em></span> if the string contains
+      <span class="emphasis"><em>only</em></span> characters from the basic source character set,
+      and there are <span class="emphasis"><em>only</em></span> 96 of those.
+      Which means that not even all English text can be
       represented (certain British spellings, proper names, and so forth).
       So, if all your input forevermore consists of only those 96
       characters (hahahahahaha), then you're done.
       // std::tolower(c) is undefined if c &lt; 0 so cast to unsigned char.
       return std::tolower((unsigned char)c);
    } </pre><p>(Thanks to James Kanze for assistance and suggestions on all of this.)
-   </p><p>Another common operation is trimming off excess whitespace.  Much
+   </p><p>
+     Since C++11 the wrapper can be replaced with a lambda expression,
+     which can perform the conversion to <code class="code">unsigned char</code> and
+     also ensure the single-argument form of <code class="code">std::lower</code> is used:
+   </p><pre class="programlisting">
+     std::transform (s.begin(), s.end(), capital_s.begin(),
+                     [](unsigned char c) { return std::tolower(c); });
+   </pre><p>Another common operation is trimming off excess whitespace.  Much
       like transformations, this task is trivial with the use of string's
       <code class="code">find</code> family.  These examples are broken into multiple
       statements for readability:
index 4a63dd964771ff85799e0f5e5147dc65139e7128..58a78d01d239c21d038d6a42b8865454d1635d1e 100644 (file)
    <para>
      <emphasis>Note</emphasis> that these calls all
       involve the global C locale through the use of the C functions
-      <code>toupper/tolower</code>.  This is absolutely guaranteed to work --
-      but <emphasis>only</emphasis> if the string contains <emphasis>only</emphasis> characters
-      from the basic source character set, and there are <emphasis>only</emphasis>
-      96 of those.  Which means that not even all English text can be
+      <code>toupper</code>/<code>tolower</code>.
+      This is absolutely guaranteed to work
+      -- but <emphasis>only</emphasis> if the string contains
+      <emphasis>only</emphasis> characters from the basic source character set,
+      and there are <emphasis>only</emphasis> 96 of those.
+      Which means that not even all English text can be
       represented (certain British spellings, proper names, and so forth).
       So, if all your input forevermore consists of only those 96
       characters (hahahahahaha), then you're done.
    } </programlisting>
    <para>(Thanks to James Kanze for assistance and suggestions on all of this.)
    </para>
+   <para>
+     Since C++11 the wrapper can be replaced with a lambda expression,
+     which can perform the conversion to <code>unsigned char</code> and
+     also ensure the single-argument form of <code>std::lower</code> is used:
+   </para>
+   <programlisting>
+     std::transform (s.begin(), s.end(), capital_s.begin(),
+                     [](unsigned char c) { return std::tolower(c); });
+   </programlisting>
    <para>Another common operation is trimming off excess whitespace.  Much
       like transformations, this task is trivial with the use of string's
       <code>find</code> family.  These examples are broken into multiple