On 2021-10-14 at 04:34:24 +1100,
Chris Angelico
On Thu, Oct 14, 2021 at 2:21 AM <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
On 2021-10-14 at 00:00:25 +0900, "Stephen J. Turnbull"
wrote: Chris Angelico writes:
+1, although it's debatable whether it should be remove suffix or remove all. I'd be happy with either.
If by "remove all" you mean "efefef" - "ef" == "", I think that's a footgun. Similarly for "efabcd" - "ef" == "abcdef" - "ef".
I don't know whether it qualifies as prior art, but in Erlang (a language emphatically *not* known for its string handling), strings are lists of codepoints, and the list subtraction operator¹ is spelled "--":
The list subtraction operator -- produces a list that is a copy of the first argument. The procedure is a follows: for each element in the second argument, the first occurrence of this element (if any) is removed.
Example:
2> [1,2,3,2,1,2]--[2,1,2]. [3,1,2]
And from my interactive prompt:
4> "abcdef" -- "ef". "abcd" 5> "abcdef" -- "ab". "cdef"
¹ http://erlang.org/doc/reference_manual/expressions.html#list-operations
It definitely counts as prior art. Another language that allows string subtraction is Pike:
"abcdef" - "ab"; (1) Result: "cdef"
I don't think it's quite such a foot-gun as you might think; but on the other hand, I'm also quite happy to see string subtraction defined in terms of removesuffix, since that IS a closer inverse to string addition. It'd end up being one of those cases where both behaviours are useful, and I'd just have to change gears when coding in multiple languages. (Which has to happen anyway. There's plenty of little differences, like modulo with negative numbers.)
The footgun is coding in multiple languages. You're in a maze of twisty little passages, all different. ;-) Just remember to use your clutch when you change gears. So aside from filename extensions, what are the real use cases for suffix removal? Plurals? No, too locale-dependent and too many exceptions. Whitespace left over from external data? No, there's already other functions for that (and regexen and actual parsers if they're not good enough). Directory traversal? No, that's what path instances and the os module are for.