Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That would depend on whether you want to iterate over code units or code points. The language itself iterates over arrays by array elements, and strings are arrays of code units, so if you simply use <code>foreach</code> with type inference, then with</p> <pre><code>foreach(c; "La Verité") writeln(c); </code></pre> <p>the last two characters printed would be gibberish, because <code>é</code> is a code point made up of two UTF-8 code units, and you're printing out individual code units (since <code>char</code> is a UTF-8 code unit). Whereas, if you do</p> <pre><code>foreach(dchar c; "La Verité") writeln(c); </code></pre> <p>then the runtime will decode the code units to code points, and <code>é</code> will be printed as the last character. But none of this is really operating on strings as ranges. <code>foreach</code> operates on arrays natively without having to use the input range API. However, for all string types, the range API looks like</p> <pre><code>@property bool empty(); @property dchar front(); void popFront(); </code></pre> <p>It operates on strings as ranges of <code>dchar</code> - <em>not</em> their code unit type. This avoids issues with functions like <code>std.algorithm.filter</code> operating on individual code units, since that would make no sense. Operating on code points isn't 100% correct either, since Unicode gets very complicated with regards to combining code points and graphemes and whatnot, but operating on code points is far closer to being correct (and I believe there's work being done on adding range support for graphemes into the standard library for the cases where you need that and are willing to pay the performance hit). So, having the range API for strings operate on them as ranges of <code>dchar</code> is far more correct, and if you did something like</p> <pre><code>foreach(c; filter!"true"("La Verité")) writeln(c); </code></pre> <p>you would be iterating over <code>dchar</code>, and <code>é</code> would print correctly. The downside to all of this of course is the fact that <code>foreach</code> on strings operates on the code unit level by default whereas the range API for strings operate on them as code points, so you have to be careful when mixing array operations and range-based operations on strings. That's also why <code>string</code> and <code>wstring</code> are not considered random-access ranges - just bidirectional ranges. You can't do random access in O(1) on code points when they're made up of varying numbers of code units (whereas <code>dstring</code> <em>is</em> a random-access range, because with UTF-32, every code unit is a code point).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload