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

[breaking][debatable] change treatment of slices in strutils.delete & sequtils.delete #19127

Closed
wants to merge 1 commit into from

Conversation

a-mr
Copy link
Contributor

@a-mr a-mr commented Nov 11, 2021

For bound checks in s.delete:

  1. (relaxing) allow 2 slices like [0 .. -1] and [s.len .. s.len - 1] which deletes nothing
  2. (more strict, breaking) forbid slices like [2..0]

Generally, new rule for slice [a .. b] is: a <= 0 <= s.len and b - a >= -1.

Motivation:

  • closer correspondence to the current getter [] behavior which allows indexing like s[0 .. -1], s[1 .. 0] etc (returns empty string/seq) but does not allow if b - a < -1.
  • seems logical, e.g. slice [2 .. 2] deletes only element 2, slice [2..1] is an empty slice and deletes nothing, and [2..0] is illegal
  • error-resistant and efficient for most typical algorithm like:
    var a = b
    while someCondition(a): dec a  # can skip!
    inc a
    s.delete(a .. b)
    See such an example in strutils.trimZeros in the changes.

@Araq
Copy link
Member

Araq commented Nov 12, 2021

Sorry, but (1) and (2) are the presented alternatives or what do you mean?

@a-mr
Copy link
Contributor Author

a-mr commented Nov 12, 2021

No, they are implemented together in this PR.

They could have been seen as 2 independent changes but I'm trying to propose a uniform treatment of slices here in get [] and delete. BTW I think that slice indexing [] should also add requirement 0<=a<=s.len (then b will be -1<=b<=s.len-1), currently it's legal to call e.g. s[-100 .. -101], which does not make sense to me. In that case delete and [] semantics would perfectly match.

@arnetheduck
Copy link
Contributor

Relevant ticket: #16492

@stale
Copy link

stale bot commented Nov 22, 2022

This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Nov 22, 2022
@stale stale bot closed this Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants