Re: Need help: Return reference slice

2014-10-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/29/14 3:50 PM, advibm wrote: Hello, I would like to create a D function that returns a slice of a string. This slice shall be a reference to a part of the string argument. Is this generally possible in D? This is the function: auto ref betweenTwoStrings(T)(inout T src, string start,

Re: Need help: Return reference slice

2014-10-30 Thread bearophile via Digitalmars-d-learn
Steven Schveighoffer: long a = src.countUntil(start); if (a 0) return src; // null a += start.length; long b = src[a..$].countUntil(end); I think there it's better to use auto instead of long. Bye, bearophile

Re: Need help: Return reference slice

2014-10-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/30/14 11:06 AM, bearophile wrote: Steven Schveighoffer: long a = src.countUntil(start); if (a 0) return src; // null a += start.length; long b = src[a..$].countUntil(end); I think there it's better to use auto instead of long. Sure, I didn't touch OP's function body,

Re: Need help: Return reference slice

2014-10-30 Thread advibm via Digitalmars-d-learn
Thank you very much for your additions. I am still new to D so I am glad to see solutions from experienced D programmers.

Need help: Return reference slice

2014-10-29 Thread advibm via Digitalmars-d-learn
Hello, I would like to create a D function that returns a slice of a string. This slice shall be a reference to a part of the string argument. Is this generally possible in D? This is the function: auto ref betweenTwoStrings(T)(inout T src, string start, string end) { long a =

Re: Need help: Return reference slice

2014-10-29 Thread bearophile via Digitalmars-d-learn
advibm: I would like to have something like that: char[] buf; // already filled array char[] partOfBuf = betweenTwoStrings(buf, START, END); partOfBuf[0] = 'a'; // THIS should also change the 'buf' variable assert(buf[0] == 'a'); Thanks for your help To do this you don't need to return a

Re: Need help: Return reference slice

2014-10-29 Thread advibm via Digitalmars-d-learn
On Wednesday, 29 October 2014 at 19:54:45 UTC, bearophile wrote: advibm: I would like to have something like that: char[] buf; // already filled array char[] partOfBuf = betweenTwoStrings(buf, START, END); partOfBuf[0] = 'a'; // THIS should also change the 'buf' variable assert(buf[0] ==

Re: Need help: Return reference slice

2014-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/29/2014 12:50 PM, advibm wrote: Hello, I would like to create a D function that returns a slice of a string. This slice shall be a reference to a part of the string argument. Is this generally possible in D? This is the function: auto ref betweenTwoStrings(T)(inout T src, string start,