Method `[..]()


Method `[..]

mixed `[..](object arg, mixed start, int start_type, mixed end, int end_type)
string `[..](string arg, int start, int start_type, int end, int end_type)
array `[..](array arg, int start, int start_type, int end, int end_type)

Description

Extracts a subrange.

This is the function form of expressions with the [..] operator. arg is the thing from which the subrange is to be extracted. start is the lower bound of the subrange and end the upper bound.

start_type and end_type specifies how the start and end indices, respectively, are to be interpreted. The types are either Pike.INDEX_FROM_BEG, Pike.INDEX_FROM_END or Pike.OPEN_BOUND. In the last case, the index value is insignificant.

The relation between [..] expressions and this function is therefore as follows:

a[i..j]    <=>	`[..] (a, i, Pike.INDEX_FROM_BEG, j, Pike.INDEX_FROM_BEG)
    a[i..<j]   <=>	`[..] (a, i, Pike.INDEX_FROM_BEG, j, Pike.INDEX_FROM_END)
    a[i..]     <=>	`[..] (a, i, Pike.INDEX_FROM_BEG, 0, Pike.OPEN_BOUND)
    a[<i..j]   <=>	`[..] (a, i, Pike.INDEX_FROM_END, j, Pike.INDEX_FROM_BEG)
    a[<i..<j]  <=>	`[..] (a, i, Pike.INDEX_FROM_END, j, Pike.INDEX_FROM_END)
    a[<i..]    <=>	`[..] (a, i, Pike.INDEX_FROM_END, 0, Pike.OPEN_BOUND)
    a[..j]     <=>	`[..] (a, 0, Pike.OPEN_BOUND, j, Pike.INDEX_FROM_BEG)
    a[..<j]    <=>	`[..] (a, 0, Pike.OPEN_BOUND, j, Pike.INDEX_FROM_END)
    a[..]      <=>	`[..] (a, 0, Pike.OPEN_BOUND, 0, Pike.OPEN_BOUND)

The subrange is specified as follows by the two bounds:

  • If the lower bound refers to an index before the lowest allowable (typically zero) then it's taken as an open bound which starts at the first index (without any error).

  • Correspondingly, if the upper bound refers to an index past the last allowable then it's taken as an open bound which ends at the last index (without any error).

  • If the lower bound is less than or equal to the upper bound, then the subrange is the inclusive range between them, i.e. from and including the element at the lower bound and up to and including the element at the upper bound.

  • If the lower bound is greater than the upper bound then the result is an empty subrange (without any error).

Returns

The returned value depends on the type of arg:

arg can have any of the following types:
string

A string with the characters in the range is returned.

array

An array with the elements in the range is returned.

object

If the object implements lfun::`[..], that function is called with the four remaining arguments.

As a compatibility measure, if the object does not implement lfun::`[..] but lfun::`[] then the latter is called with the bounds transformed to normal from-the-beginning indices in array-like fashion:

`[..] (a, i, Pike.INDEX_FROM_BEG, j, Pike.INDEX_FROM_BEG)

Calls a->`[] (i, j)

`[..] (a, i, Pike.INDEX_FROM_BEG, j, Pike.INDEX_FROM_END)

Calls a->`[] (i, a->_sizeof()-1-j)

`[..] (a, i, Pike.INDEX_FROM_BEG, 0, Pike.OPEN_BOUND)

Calls a->`[] (i, Int.NATIVE_MAX)

`[..] (a, i, Pike.INDEX_FROM_END, j, Pike.INDEX_FROM_BEG)

Calls a->`[] (a->_sizeof()-1-i, j)

`[..] (a, i, Pike.INDEX_FROM_END, j, Pike.INDEX_FROM_END)

Calls a->`[] (a->_sizeof()-1-i, a->_sizeof()-1-j), except that a->_sizeof() is called only once.

`[..] (a, i, Pike.INDEX_FROM_END, 0, Pike.OPEN_BOUND)

Calls a->`[] (a->_sizeof()-1-i, Int.NATIVE_MAX)

`[..] (a, 0, Pike.OPEN_BOUND, j, Pike.INDEX_FROM_BEG)

Calls a->`[] (0, j)

`[..] (a, 0, Pike.OPEN_BOUND, j, Pike.INDEX_FROM_END)

Calls a->`[] (0, a->_sizeof()-1-j)

`[..] (a, 0, Pike.OPEN_BOUND, 0, Pike.OPEN_BOUND)

Calls a->`[] (0, Int.NATIVE_MAX)

Note that Int.NATIVE_MAX might be replaced with an even larger integer in the future.

See also

lfun::`[..], `[]