Best way to make Until! into string

Jonathan M Davis jmdavisProg at gmail.com
Tue Jun 22 11:26:58 PDT 2010


div0 wrote:

> On 22/06/2010 07:29, Jonathan M Davis wrote:
>> Okay. If you call until like so
>>
>> str.until('\"')
>>
>> you get a Until!(pred,string,char). I want to turn that into a string.
>> array() doesn't seem to do the trick right now. It used to work, but now
>> it gives me
>>
>> main.d(47): Error: template std.array.array(Range) if (isForwardRange!
>> (Range)) does not match any function template declaration
>> main.d(47): Error: template std.array.array(Range) if (isForwardRange!
>> (Range)) cannot deduce template function from argument types !()(Until!
>> (pred,string,char))
>>
>> to!string just converts it into a string with the Until! stuff being
>> included in the string rather than giving me the actual result, so that
>> doesn't work.
>>
>> So, what is the correct and preferred way to convert the result of Until!
>> to as string when you were searching on a string in the first place? The
>> std.algorithm functions are definitely nice, but they have tendancy to
>> return hard-to-use types.
>>
>> - Jonathan M Davis
> 
> Could be wrong, but strings aren't (conceptually) arrays any more.

As I understand it, they're definitely arrays. It's just that they because 
they're arrays of char (well immutable(char)) but are read as unicode code 
points, the type of the array isn't necessarily a full character and code 
that needs to read code points has to treat them as a range of code points 
rather than an array of char. So, whether you treat them as an array depends 
a bit on what you're doing with them. As long as you're not actually trying 
to intrepret them as code points, however, they're the same as any other 
array.

> 
> They are bidirectional ranges which is why the array call doesn't work.
> Though how you actually get a string back I don't know.
> 

I wasn't clear enough. I was basically doing this:

to!string(array(str.until('\"')));

As I understand it, that forces the Until type into an array of whatever 
type (probably char[]) and then to!string would convert it to 
immutable(char)[]. It's the cleanest way that I found (well, actually, the 
only way I think) to convert the result of until() to string in spite of the 
fact that it was called with a string in the first place. It's one of the 
prices of flexibility, I guess.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list