Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 15, 2017 auto decoding rant | ||||
---|---|---|---|---|
| ||||
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); } |
June 15, 2017 Re: auto decoding rant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Shamir | 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 |
June 15, 2017 Re: auto decoding rant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | 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!
|
June 15, 2017 Re: auto decoding rant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Shamir | 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 |
Copyright © 1999-2021 by the D Language Foundation