Follow-up Comment #5, bug #60802 (project groff): Hi Branden,
I agree that changing existing expected behavior should be avoided. With 20-20 hindsight, it might have been better to make .substring take OFFSET LENGTH parameters instead of N1 N2 (similar to Perl's "substr"). But that water reached the ocean long ago, and .substring has been documented and presumably in use in the wild for a while. Moving forward, the cases where I ran into trouble were trying to extract or remove characters which might not exist, e.g. chop off the last char, if there are any chars, with .substring STR 0 -2 or delete the first char, if they are any chars, with .substring STR 2 I think this could work compatibly with existing semantics by allowing N1 and N2 to "refer" to positions before or after the existing string without error; if the range is negative, i.e. N2 points before N1, then the substring is empty and STR is set to "". If the range is positive, then the substring is whatever characters exist within the range. I will attach a new regression script for this revised proposal, including use of negative indicies which count backwards from the end (like currently, except it is allowed to refer to before the start of the string without error). Here is revised documentation: -- Request: .substring str n1 [n2] Replace the string named STR with the substring defined by the indices N1 and N2. The first character in the string has index 0. If N1 or N2 is negative, it is counted from the end of the string, going backwards: The last character has index -1, the character before the last character has index -2, etc. If N2 is omitted then -1 is used implicitly, indicating the last character, if any. If N2 refers to a position before N1 then the substring is empty, and the string named STR is set to contain "". It is allowed for N1 or N2 to point to positions before the start or after the end of the existing string; the substring includes only characters at valid positions within the indicated range, if any. .ds xxx abcdefgh .substring xxx 1 -4 \*[xxx] => bcde .substring xxx 2 \*[xxx] => de .substring xxx -2 \*[xxx] => de .substring xxx 1 \*[xxx] => e .substring xxx 1 \*[xxx] => (empty) .substring xxx 1 \*[xxx] => (empty) (file #51649) _______________________________________________________ Additional Item Attachment: File name: substring_test_rev3.bash Size:4 KB <https://file.savannah.gnu.org/file/substring_test_rev3.bash?file_id=51649> _______________________________________________________ Reply to this item at: <https://savannah.gnu.org/bugs/?60802> _______________________________________________ Message sent via Savannah https://savannah.gnu.org/