auto decoding rant

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 15 08:55:44 PDT 2017


On Thursday, June 15, 2017 15:47:54 Jonathan Shamir via Digitalmars-d wrote:
> I see this is a recurring rant (I apologize if this is a
> repeating topic, I'm new to the forums). Here's an example of
> something that should be simple in D but isn't:
>
> enum string PATTERN = "abcd";
> immutable char[10] repeatingPattern =
> PATTERN.cycle().takeExactly(10).array();
>
> The fact that front(string) returns a dchar makes some really
> simple (and common) use-cases practically impossible.
>
> Also note I can't cast to char[] in compile time? What's the
> reason for that?
>
> I would recommend adding something like this to phobos (and in
> all fairness, it should have been the other way around):

Yes, it should be the other way around, but changing it at this point
without massive code breakage is very difficult if not impossible.

> auto noDecode(T)(T[] str) if (isNarrowString!(T[])) {
>      static struct NoDecode {
>          private T[] str;
>
>          @property ref T front() {
>              assert (!this.empty);
>              return str[0];
>          }
>          @property bool empty() {
>              return (str.empty);
>          }
>          void popFront() {
>              if (!this.empty) {
>                  str = str[1 .. $];
>              }
>          }
>          NoDecode save() {
>              return this;
>          }
>      }
>      return NoDecode(str);
> }

Already there:

http://dlang.org/phobos/std_utf.html#byCodeUnit

- Jonathan M Davis



More information about the Digitalmars-d mailing list