Skip to content

Slicing operator #346

@thekid

Description

@thekid

Scope of Change

The concept of a slicing operator will be introduced to the framework and its libraries: Using $value('start:stop') will return a slice of the respective value from start until stop - 1, as an instance of the value.

Rationale

Unified syntax for accessing slices in lists and buffers.

Functionality

$value('start:stop')  # items start through stop-1
$value('start:')      # items start through the rest of the items in value
$value(':stop')       # items from the beginning through stop-1
$value(':')           # a copy of the whole value
  • Supports negative offsets for counting from the end of the underlying value instead of from the start.
  • Supports passing arrays - $value([0, 4])
  • Python's optional step (start:stop:step) syntax

Implementation

public function __invoke($slice) {
  list($start, $stop)= is_array($slice) ? $slice : explode(':', $slice, 2);
  $offset= (int)$start;
  $end= (int)$stop ?: $this->size;

  return /* ... implementation specific ... */;
}

This is implemented using the __invoke method, see e.g. xp-framework/core#349

Security considerations

n/a

Speed impact

n/a

Dependencies

n/a

Related documents

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions