Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slicing operator #346

Open
2 of 3 tasks
thekid opened this issue Feb 9, 2025 · 0 comments
Open
2 of 3 tasks

Slicing operator #346

thekid opened this issue Feb 9, 2025 · 0 comments

Comments

@thekid
Copy link
Member

thekid commented Feb 9, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant