| |
 | Posted by Andrew Fedoniouk in reply to Vathix | Permalink Reply |
|
Andrew Fedoniouk 
Posted in reply to Vathix
|
"Vathix" <chris@dprogramming.com> wrote in message news:op.sudzzol2l2lsvj@esi...
> The .. operator could return a Range type:
>
> struct Range
> {
> size_t start, end;
> }
>
>
> opSlice() could then be removed in favor of opIndex(Range) and other functions could take range parameters.
>
>
> void select(HWND hwEditBox, Range sel)
> {
> // ...
> }
>
> select(myhwnd, 3 .. 9);
>
>
> Might want to name it Slice or something instead, since Range is pretty popular (and in DTL?).
FYI:
Here is the range as it is declared in Harmonia. harmonia.gx.geometry.d
/**
* diapason
* please pay attention that both l and h belong to the range - [l,h]
*/
struct range
{
int l,h; // low and high
static range opCall( int low, int high ) { range r; r.l = low; r.h =
high; return r; }
static range opCall( int lowandhigh ) { range r; r.l = lowandhigh; r.h =
lowandhigh; return r; }
bool isEmpty() { return (l > h); }
int length() { return (l <= h)? h - l + 1 : 0; }
bool overlap(range r) { return (max!(int)(l,r.l)) <=
(min!(int)(h,r.h)); }
bool opEquals(range b) { return (l == b.l) && (h == b.h); }
// check if 'i' is inside the range
bool contains(int i) { return (i >= l) && (i <= h); }
// intersection of two ranges r3 = r1 & r2;
range opAnd(range r) { return opCall( max!(int)(l,r.l),
min!(int)(h,r.h)); }
// union of two ranges r3 = r1 | r2;
range opOr(range r) { return opCall( min!(int)(l,r.l),
max!(int)(h,r.h)); }
range opAddAssign(int i) { return l += i, h += i, *this; }
range opSubAssign(int i) { return l -= i, h -= i, *this; }
// inplace intersection r1 &= r2;
range opAndAssign(range a) { return l = max!(int)(l,a.l), h =
min!(int)(h,a.h), *this; }
// inplace union r1 |= r2;
range opOrAssign(range b) { if(isEmpty()) *this = b;
else if(!b.isEmpty()) { l =
min!(int)(l,b.l); h = max!(int)(h,b.h); }
return *this; }
static range empty() { range r; r.l = 0; r.h = -1; return r; }
};
|