Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: support negative LEN in ${VAR:OFFSET:LEN}



On Mar 10, 12:10pm, Mikael Magnusson wrote:
} Subject: support negative LEN in ${VAR:OFFSET:LEN}
}
} +var(length) is treated directly as a length when it is positive.
} +When it is negative, it works as an offset just like var(offset). If
} +this results in a negative length, a diagnostic will be printed and
} +nothing will be substituted.

I don't object to adding the feature, but that documentation is a bit
confusing.  Also, when writing documentation, it's almost always better
to avoid passive phrasing like "x will be y".  Perhaps:

  When positive, var(length) counts from the var(offset) position
  toward the end of the scalar or array.  When negative, var(length)
  counts back from the end.  If this results in a position smaller
  than var(offset), a diagnostic is printed and nothing is substituted.

The equivalent $var[start,end] expression would not print a diagnostic.
Does bash really work that way?  That is, you have to know the length
of the string in order to safely count backwards from the end of it?

} +   foo="123456789"
} +   print ${foo:5:-6}
} +1:Regression test for total length < 0 in string
} +?(eval):2: substring expression < 0: -2

Is that what bash's diagnostic looks like?  If so we should borrow
consistently, but it'd be a lot clearer if it said

    substring expression: 3 < 5



Messages sorted by: Reverse Date, Date, Thread, Author