5

How to read a line as a range in D?

I know there is ranges in D, but I just wondered how to simply iterate over each character of a string using this concept?

To show what I'm after, the similar code in Go is:

for _, someChar := range someString {
    // Do something
}
2
  • 1
    ddili.org/ders/d.en/ranges.html
    – sigod
    Commented May 16, 2013 at 16:02
  • @sigod, yea, should have checked Ali's book! It's definitely the go to resource for reading up on D stuff right now. Commented May 16, 2013 at 21:29

2 Answers 2

12

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 foreach with type inference, then with

foreach(c; "La Verité")
    writeln(c);

the last two characters printed would be gibberish, because é is a code point made up of two UTF-8 code units, and you're printing out individual code units (since char is a UTF-8 code unit). Whereas, if you do

foreach(dchar c; "La Verité")
    writeln(c);

then the runtime will decode the code units to code points, and é will be printed as the last character. But none of this is really operating on strings as ranges. foreach operates on arrays natively without having to use the input range API. However, for all string types, the range API looks like

@property bool empty();
@property dchar front();
void popFront();

It operates on strings as ranges of dchar - not their code unit type. This avoids issues with functions like std.algorithm.filter 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 dchar is far more correct, and if you did something like

foreach(c; filter!"true"("La Verité"))
    writeln(c);

you would be iterating over dchar, and é would print correctly. The downside to all of this of course is the fact that foreach 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 string and wstring 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 dstring is a random-access range, because with UTF-32, every code unit is a code point).

3
  • What happens if I import std.array which provides the needed Range-functions on UFCS level, will iterating over a string still be the same?
    – dav1d
    Commented May 18, 2013 at 10:57
  • @dav1d foreach on array will only ever use the array API. For arrays, the range-based functions must be used explicitly. And actually, as annoying as the inconsistency with foreach is, making its behavior depend on whether std.array was imported or not would be very bug-prone, since simply adding or removing that import could drastically change the behavior of your code depending on what it was doing. Commented May 18, 2013 at 22:27
  • Yeah, I was really hoping it wouldn't change the code. But on the other hand UFCS allows iterating over objects which do not implement the opApply (might be wrong here) or range interface. Thanks for the clarification.
    – dav1d
    Commented May 19, 2013 at 11:41
1
foreach(ch; str)
    do_something(ch);

A string is an InputRange. An InputRange implements three things:

  • empty; is it empty?
  • front; give me the next item.
  • popFront; advance the range, otherwise front will return the same.

foreach "understands" how to work with ranges, so it "just works".

But I don't speak Go, so I'm not entirely sure we're speaking the same language.

4
  • Yes, of course, Ugh, don't know how I could forget this one, has used it a few times already, so I should know it ... gotta blame the lack of sleep last night :) Commented May 16, 2013 at 15:07
  • I hear you, I'm not entirely lucid myself at the moment.
    – 0b1100110
    Commented May 16, 2013 at 15:11
  • 2
    Watch out for type inference here though. That code is iterating over code units, not code points, so it's unlikely to operate on Unicode correctly. Commented May 16, 2013 at 17:26
  • @JonathanMDavis I'm pretty sure I couldn't handle it if it did operate on unicode correctly. =P But you have a good point.
    – 0b1100110
    Commented May 17, 2013 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.