Text editing [Was: Re: #line decoder]

bearophile bearophileHUGS at lycos.com
Sun Sep 28 15:49:40 PDT 2008


Sergey Gromov:
> Unfortunately this doesn't address the issue I've faced: readLine 
> returns a slice of an internal buffer and there's no way to notice that 
> except by RTFM or debugging.

You are right.
This is how I have mostly solved your problem in my libs. This is a lazy generator of all the combinations of the given items, taken at groups of length k:

class Xcombinations(TyItem) {
    this(TyItem[] items, int k, bool copy=true) {
    ...
    }
}

The copy argument is true by default, it means that all the arrays it yields are actual copies. So by default it acts safely but it's slower. In the not so common situations you want to go faster and you know what you are doing you can set copy to false, so the dupping isn't performed, and the code goes ten or more times faster. You have to put that third argument to false, so while you program you know what does it means.

A similar strategy may help avoid some problems with the readLine and other functions/generators. If no performance loss can be accepted then 'copy' may even become a compile-time constant, so you can use it with "static if":

class Xcombinations(TyItem, bool copy=true) {
    this(TyItem[] items, int k) {
    ...
    }
}

Bye,
bearophile


More information about the Digitalmars-d-announce mailing list