Thanks for the proposal. I believe we don't support all patterns because it will be inefficient, especially the suffix/trailing functions. It is not necessarily trivial to implement it for regexes either (and regexes have their own mechanism to represent prefixes/suffixes).
The reason we don't have "trim_prefix" and "trim_suffix" is because there is no reasonable implementation of trim_prefix/1 and trim_suffix/1 - where no pattern to trim on is given (for example, it it to the behaviour of trim_leading/1 and trim_trailing/1). So we could add trim_prefix/2 and trim_suffix/2, but then it is inconsistent with other trim* functions. So unless there is a strong reason for adding this, rather than consistency, I would not go this route. On Wed, Mar 2, 2022 at 2:25 PM 'eksperimental' via elixir-lang-core < elixir-lang-core@googlegroups.com> wrote: > When starting to work in this implementation was wondering what should > be the return value of > > String.trim_leading("--__--abc", [" ", "-", "_"]) > > > and I realized that it should mimic String.replace_leading/3 > Therefore it should returning "abc" > > Which leads me to think we should introduce String.trim_prefix/2, in > order to be consistent with String.replace_prefix/3 > where the same input as above should behave like this: > > iex> String.trim_prefix("--__--abc", [" ", "-", "_"]) > "__--abc" > > Which also makes me think that String.replace_prefix should also > behave like String.replace/3 where the second argument (match) is a > pattern, not a string (ie. a string, list of strings, or a compiled > search pattern). By introducing this last consistency change, the > implementation of the consistency changes for the other functions > mentioned above is straight-forward. > > So in short this consistency proposals suggests some functions to > support t:String.pattern/0 which could be a string, a list of strings, > or a compiled search pattern. > > 1. `String.replace_{leading, prefix, suffix, trailing}/3` to support in > its second argument (match) t:String.pattern/0. Current > String.replace/4 supports t:String.pattern/0 | t:Regex.t/0, but for > the sake of simplifying this proposal I would leave out > Regexes out of the equation for now. > > 2. Introduce String.trim_{prefix, suffix}/2 which will support > t:String.pattern/0 as a its second arguments. These functions should > mimic String.replace_{prefix, suffix}/2 > > 3. Add support for t:String.pattern/0 for String.{trim, trim_leading, > trim_trailing}/2 to support t:String.pattern/0 as its second argument. > These functions should mimic String.{replace, replace_leading, > replace_trailing}/3 > > Please let me know what you think. > > -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to elixir-lang-core+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/621f703a.1c69fb81.2b06.fa97SMTPIN_ADDED_MISSING%40gmr-mx.google.com > . > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JVuiDhxBsmcR43A6Pq-xUdFO%2BBbDaX9foCnQKs4-%2Bwyg%40mail.gmail.com.