www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - auto decoding rant

reply Jonathan Shamir <jonathan weka.io> writes:
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):


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);
}
Jun 15 2017
next sibling parent reply Seb <seb wilzba.ch> writes:
On Thursday, 15 June 2017 at 15:47:54 UTC, Jonathan Shamir wrote:
 Also note I can't cast to char[] in compile time? What's the 
 reason for that?
Have a look at: https://dlang.org/phobos/std_utf.html#byCodeUnit https://dlang.org/phobos/std_utf.html#byChar https://dlang.org/phobos/std_string.html#.representation
Jun 15 2017
parent Jonathan Shamir <jonathan weka.io> writes:
On Thursday, 15 June 2017 at 15:50:42 UTC, Seb wrote:
 On Thursday, 15 June 2017 at 15:47:54 UTC, Jonathan Shamir 
 wrote:
 Also note I can't cast to char[] in compile time? What's the 
 reason for that?
Have a look at: https://dlang.org/phobos/std_utf.html#byCodeUnit https://dlang.org/phobos/std_utf.html#byChar https://dlang.org/phobos/std_string.html#.representation
Perfect, thanks!
Jun 15 2017
prev sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
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
Jun 15 2017