Trying to alias this a grapheme range + making it a forward range

aliak something at something.com
Mon Jul 8 21:55:13 UTC 2019


Problem 1:

I'm trying to get a string to behave as a .byGrapheme range by 
default, but I can't figure out Grapheme. I'm trying to replicate 
this behavior:

foreach (g; "hello".byGrapheme) {
     write(g[]);
}

In a custom type:

struct ustring {
     string data;
     this(string data) {
     	this.data = data;
     }
     auto get() {
         static struct Range {
             typeof(string.init.byGrapheme) source;
             bool empty() { return source.empty; }
             void popFront() { source.popFront; }
             auto front() { return source.front[]; }
             auto save() { return this; };
         }
         return Range(this.data.byGrapheme);
     }
     alias get this;
}

But I keep on ending up with a UTFException: "Encoding an invalid 
code point in UTF-8" with code like:

writeln("hello".ustring);

Problem 2:

How can I get the aliased ustring type to behave as a 
ForwardRange? If I add the save method to the voldermort range 
type, the isForwardRange!ustring fails because the requirement on 
isForwardRange checks to see if save returns the same type it's 
called on. Which is not the case here since typeof(ustring.save) 
== ustring.get.Range). But nontheless does have a save method.

Cheers,
- Ali



More information about the Digitalmars-d-learn mailing list