www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach over string

reply "Kagamin" <spam here.lot> writes:
foreach over string apparently iterates over chars by default 
instead of dchars. Didn't it prefer dchars?

string s="weiß";
int i;
foreach(c;s)i++;
assert(i==5);
May 24 2014
next sibling parent Etienne Cimon <etcimon gmail.com> writes:
On 2014-05-24 12:46, Kagamin wrote:
 foreach over string apparently iterates over chars by default instead of
 dchars. Didn't it prefer dchars?

 string s="weiß";
 int i;
 foreach(c;s)i++;
 assert(i==5);
A string is defined by: alias string = immutable(char)[]; It doesn't add anything to that type (unless you import a library like std.algorithm, which adds many "methods" thanks to UFCS and generic functions) I believe you are looking for dstring which is defined by: alias dstring = immutable(dchar)[]; dstring s="weiß"; int i; foreach(c;s)i++; assert(i==4);
May 24 2014
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/24/2014 09:46 AM, Kagamin wrote:
 foreach over string apparently iterates over chars by default instead of
 dchars. Didn't it prefer dchars?
I don't think so. The range algorithms iterate by dchar though.
 string s="weiß";
 int i;
 foreach(c;s)i++;
 assert(i==5);
Ali
May 24 2014
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Saturday, 24 May 2014 at 16:46:42 UTC, Kagamin wrote:
 foreach over string apparently iterates over chars by default 
 instead of dchars. Didn't it prefer dchars?

 string s="weiß";
 int i;
 foreach(c;s)i++;
 assert(i==5);
Nope. if you use foreach(dchar c; s) you will get the iteration by code-point you are looking for.
May 24 2014
parent "Kagamin" <spam here.lot> writes:
On Saturday, 24 May 2014 at 18:18:37 UTC, John Colvin wrote:
 if you use foreach(dchar c; s) you will get the iteration by 
 code-point you are looking for.
Actually I was trying to prevent decoding :) It just occurred to me it can be tricky in generic code.
May 24 2014
prev sibling parent "Kagamin" <spam here.lot> writes:
Ah, yeah, found this: 
http://forum.dlang.org/thread/mailman.266.1319139465.24802.digitalmars-d puremagic.com
May 24 2014