unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: master 87e422f: Beef up the Emacs string utility set a bit
       [not found] ` <20201221175347.9F1B820B76@vcs0.savannah.gnu.org>
@ 2020-12-21 18:24   ` Stefan Kangas
  2020-12-21 18:32     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 22+ messages in thread
From: Stefan Kangas @ 2020-12-21 18:24 UTC (permalink / raw)
  To: Lars Ingebrigtsen, emacs-devel

larsi@gnus.org (Lars Ingebrigtsen) writes:

> branch: master
> commit 87e422f1044068a4d27e5e4bfdbc664d9e4bbc43
> Author: Lars Ingebrigtsen <larsi@gnus.org>
> Commit: Lars Ingebrigtsen <larsi@gnus.org>
>
>     Beef up the Emacs string utility set a bit

Thanks, this looks very good.

> +@defun slice-string string regexp

But how about calling this `string-slice' instead?  I personally find it
quite useful to have related functions under one prefix, not least for
discoverability.

(FWIW, I would also be in favour of renaming `split-string' to
`string-split'.)



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 18:24   ` master 87e422f: Beef up the Emacs string utility set a bit Stefan Kangas
@ 2020-12-21 18:32     ` Lars Ingebrigtsen
  2020-12-21 18:44       ` Lars Ingebrigtsen
  2020-12-21 19:05       ` Stefan Monnier
  0 siblings, 2 replies; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 18:32 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: emacs-devel

Stefan Kangas <stefankangas@gmail.com> writes:

>> +@defun slice-string string regexp
>
> But how about calling this `string-slice' instead?  I personally find it
> quite useful to have related functions under one prefix, not least for
> discoverability.
>
> (FWIW, I would also be in favour of renaming `split-string' to
> `string-split'.)

Since it's an analogue of split-string, I thought keeping the same
naming structure would be nice.  But I don't have a strong opinion
here -- if flipping the name makes things better, then I don't object.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 18:32     ` Lars Ingebrigtsen
@ 2020-12-21 18:44       ` Lars Ingebrigtsen
  2020-12-21 19:05         ` Stefan Kangas
                           ` (3 more replies)
  2020-12-21 19:05       ` Stefan Monnier
  1 sibling, 4 replies; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 18:44 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: emacs-devel

I've been going through

https://github.com/magnars/s.el

to see whether there's anything there that seems both 1) useful and 2)
Emacs doesn't have already, but there was less than I remembered,
really.  With the five functions I added, the rest are mostly just
aliases for stuff we have in Emacs 28.

Things I wasn't sure about:

s-chomp (s)

Is this really useful?  Emacs is so un-line-oriented that I can't recall
having removed newlines from a string ever...

s-center (len s)

I can squint a bit and almost see how this would be useful, but not
really?  I can only remember padding either to the left or right.

s-pad-left (len padding s)
s-pad-right (len padding s)

Hm, I thought Emacs had these, but now I can't find them...  OK, I'll
add them.

s-shared-start (s1 s2)
s-shared-end (s1 s2)

I don't see the utility.

s-repeat (num s)

This neither.

There's also a bunch of "titleize" functions that just seem to quirky.
And the camelCase and SnakeCase functions?  Dunno, I think they're best
left in the s.el for people that want that.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 18:44       ` Lars Ingebrigtsen
@ 2020-12-21 19:05         ` Stefan Kangas
  2020-12-21 20:03           ` Lars Ingebrigtsen
  2020-12-21 20:18         ` Basil L. Contovounesios
                           ` (2 subsequent siblings)
  3 siblings, 1 reply; 22+ messages in thread
From: Stefan Kangas @ 2020-12-21 19:05 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> I've been going through
>
> https://github.com/magnars/s.el
>
> to see whether there's anything there that seems both 1) useful and 2)
> Emacs doesn't have already, but there was less than I remembered,
> really.  With the five functions I added, the rest are mostly just
> aliases for stuff we have in Emacs 28.

I had a look at GitHub to get some data, in case that helps.

> Things I wasn't sure about:
>
> s-chomp (s)
>
> Is this really useful?  Emacs is so un-line-oriented that I can't recall
> having removed newlines from a string ever...

It seems to have some real world usage, at least:

    https://github.com/search?l=Emacs+Lisp&q=s-chomp&type=Code

> s-center (len s)
>
> I can squint a bit and almost see how this would be useful, but not
> really?  I can only remember padding either to the left or right.

Not used a lot:

    https://github.com/search?l=Emacs+Lisp&p=2&q=s-center&type=Code

> s-shared-start (s1 s2)
> s-shared-end (s1 s2)
>
> I don't see the utility.

Not used a lot:

    https://github.com/search?l=Emacs+Lisp&p=2&q=s-shared-start&type=Code

> s-repeat (num s)
>
> This neither.

Seems to see some use, but not a lot:

    https://github.com/search?l=Emacs+Lisp&q=s-repeat&type=Code

> There's also a bunch of "titleize" functions that just seem to quirky.
> And the camelCase and SnakeCase functions?  Dunno, I think they're best
> left in the s.el for people that want that.

Unsure about titleize, but here we have:

    https://github.com/search?l=Emacs+Lisp&q=s-titleize&type=Code



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 18:32     ` Lars Ingebrigtsen
  2020-12-21 18:44       ` Lars Ingebrigtsen
@ 2020-12-21 19:05       ` Stefan Monnier
  2020-12-21 19:19         ` Lars Ingebrigtsen
  1 sibling, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2020-12-21 19:05 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Stefan Kangas, emacs-devel

> Since it's an analogue of split-string, I thought keeping the same
> naming structure would be nice.  But I don't have a strong opinion
> here -- if flipping the name makes things better, then I don't object.

FWIW, I'm in favor of striving to use the same prefix (see also the
https://elpa.gnu.org/devel/prefixed-core.html)


        Stefan




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 19:05       ` Stefan Monnier
@ 2020-12-21 19:19         ` Lars Ingebrigtsen
  0 siblings, 0 replies; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 19:19 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Stefan Kangas, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Since it's an analogue of split-string, I thought keeping the same
>> naming structure would be nice.  But I don't have a strong opinion
>> here -- if flipping the name makes things better, then I don't object.
>
> FWIW, I'm in favor of striving to use the same prefix (see also the
> https://elpa.gnu.org/devel/prefixed-core.html)

OK; flipped.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 19:05         ` Stefan Kangas
@ 2020-12-21 20:03           ` Lars Ingebrigtsen
  0 siblings, 0 replies; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 20:03 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: emacs-devel

Stefan Kangas <stefankangas@gmail.com> writes:

> I had a look at GitHub to get some data, in case that helps.

Yes, thanks.

> It seems to have some real world usage, at least:
>
>     https://github.com/search?l=Emacs+Lisp&q=s-chomp&type=Code

Hm...  is there a way to filter out the code people are including from
s.el?  Many of those are just the example code from there:

(defexamples s-chomp
  (s-chomp "no newlines\n") => "no newlines"
  (s-chomp "no newlines\r\n") => "no newlines"
  (s-chomp "some newlines\n\n") => "some newlines\n")

Let's see...

https://github.com/search?q=s-chomp+-defexample+-newlines&type=Code

This probably filters a bit too much?  But it got rid of most of the
example code.

What would we call it in Emacs Lisp?  I guess most people would
understand `string-chomp', but it's a bit un-Elispy.

>> s-repeat (num s)
>>
>> This neither.
>
> Seems to see some use, but not a lot:
>
>     https://github.com/search?l=Emacs+Lisp&q=s-repeat&type=Code

4K of those were from

	 (s-repeat (org-habit-scheduled-repeat habit))

With

https://github.com/search?q=s-repeat+-org&type=Code

we're down to more reasonable numbers.  Of the first few pagefuls of
hits, more than nine tenths of them use s-repeat at a synonym for
make-string.  (s-repeat count "-") instead of (make-string count ?-).

So I'm leaning towards "not".

>> There's also a bunch of "titleize" functions that just seem to quirky.
>> And the camelCase and SnakeCase functions?  Dunno, I think they're best
>> left in the s.el for people that want that.
>
> Unsure about titleize, but here we have:
>
>     https://github.com/search?l=Emacs+Lisp&q=s-titleize&type=Code

Oh, I forgot again that "titleize" here was the same as `capitalize' in
Emacs Lisp.  OK, then the possibly useful function is

s-capitalize (s)

which upcases the first character and downcases the rest.  And that
seems to have a fair number of usages:

https://github.com/search?q=s-capitalize&type=Code

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 18:44       ` Lars Ingebrigtsen
  2020-12-21 19:05         ` Stefan Kangas
@ 2020-12-21 20:18         ` Basil L. Contovounesios
  2020-12-21 20:38           ` Clément Pit-Claudel
  2020-12-21 20:20         ` Clément Pit-Claudel
  2020-12-22 10:41         ` Mattias Engdegård
  3 siblings, 1 reply; 22+ messages in thread
From: Basil L. Contovounesios @ 2020-12-21 20:18 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Stefan Kangas, emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> s-shared-start (s1 s2)
> s-shared-end (s1 s2)
>
> I don't see the utility.

s-shared-start is already covered by try-completion, right?  Which has
various uses, e.g. in project--read-file-cpd-relative.

-- 
Basil



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 18:44       ` Lars Ingebrigtsen
  2020-12-21 19:05         ` Stefan Kangas
  2020-12-21 20:18         ` Basil L. Contovounesios
@ 2020-12-21 20:20         ` Clément Pit-Claudel
  2020-12-21 20:34           ` Lars Ingebrigtsen
  2020-12-22 10:41         ` Mattias Engdegård
  3 siblings, 1 reply; 22+ messages in thread
From: Clément Pit-Claudel @ 2020-12-21 20:20 UTC (permalink / raw)
  To: emacs-devel

On 12/21/20 1:44 PM, Lars Ingebrigtsen wrote:
> I've been going through
> 
> https://github.com/magnars/s.el
> 
> to see whether there's anything there that seems both 1) useful and 2)
> Emacs doesn't have already, but there was less than I remembered,
> really.  With the five functions I added, the rest are mostly just
> aliases for stuff we have in Emacs 28.
> 
> Things I wasn't sure about:
> 
> s-chomp (s)
> 
> Is this really useful?  Emacs is so un-line-oriented that I can't recall
> having removed newlines from a string ever...

Look for (replace-regexp-in-string "\n$" "" and variants, or string-trim-right.  One common use case is in conjunction with shell-command-to-string.  Even if it was in Emacs, though, I'm not sure I'd find it and use it instead of string-trim-end.

> s-shared-start (s1 s2)
> s-shared-end (s1 s2)

It's useful for computing relative paths (as done by file-relative-name) and for abbreviating entries in a list (finding the common prefix of many entries and abbreviating them all that way).  It's a trivial extension of compare-strings, but discoverability isn't great.  The implementation of s-shared-start in s.el is inefficient, but my patch (https://github.com/magnars/s.el/pull/134/files) was never merged.

An efficient implementation of s-shared-end would be nice, but I think it would require generalizing compare-strings.



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 20:20         ` Clément Pit-Claudel
@ 2020-12-21 20:34           ` Lars Ingebrigtsen
  2020-12-21 20:44             ` Clément Pit-Claudel
  0 siblings, 1 reply; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 20:34 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

Clément Pit-Claudel <cpitclaudel@gmail.com> writes:

> Look for (replace-regexp-in-string "\n$" "" and variants, or
> string-trim-right.  One common use case is in conjunction with
> shell-command-to-string.  Even if it was in Emacs, though, I'm not
> sure I'd find it and use it instead of string-trim-end.

I see one single instance:

~/src/emacs/trunk/lisp/allout.el:6071:            (replace-regexp-in-string "\n$" ""

Hm...  should that be "\n\\'"?  Of which we have two:

~/src/emacs/trunk/lisp/org/ox.el:4623:	 (code (replace-regexp-in-string
~/src/emacs/trunk/lisp/cus-start.el:292:				 (setq tmp (replace-regexp-in-string


-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 20:18         ` Basil L. Contovounesios
@ 2020-12-21 20:38           ` Clément Pit-Claudel
  2020-12-21 21:11             ` Lars Ingebrigtsen
  0 siblings, 1 reply; 22+ messages in thread
From: Clément Pit-Claudel @ 2020-12-21 20:38 UTC (permalink / raw)
  To: emacs-devel

On 12/21/20 3:18 PM, Basil L. Contovounesios wrote:
> Lars Ingebrigtsen <larsi@gnus.org> writes:
> 
>> s-shared-start (s1 s2)
>> s-shared-end (s1 s2)
>>
>> I don't see the utility.
> 
> s-shared-start is already covered by try-completion, right?  Which has
> various uses, e.g. in project--read-file-cpd-relative.

It's also covered by compare-strings, but both need a bit of setup to do the same as s-shared-start, so a wrapper can be useful (for example, it's easy to forget to reset completion-regexp-list). Additionally, discovering the right function to call can be hard (I hadn't realized that try-completion could be used for this, and neither had the authors or s.el).



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 20:34           ` Lars Ingebrigtsen
@ 2020-12-21 20:44             ` Clément Pit-Claudel
  2020-12-21 21:06               ` Lars Ingebrigtsen
  0 siblings, 1 reply; 22+ messages in thread
From: Clément Pit-Claudel @ 2020-12-21 20:44 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

On 12/21/20 3:34 PM, Lars Ingebrigtsen wrote:
> Clément Pit-Claudel <cpitclaudel@gmail.com> writes:
> 
>> Look for (replace-regexp-in-string "\n$" "" and variants, or
>> string-trim-right.  One common use case is in conjunction with
>> shell-command-to-string.  Even if it was in Emacs, though, I'm not
>> sure I'd find it and use it instead of string-trim-end.
> 
> I see one single instance:
> 
> ~/src/emacs/trunk/lisp/allout.el:6071:            (replace-regexp-in-string "\n$" ""
> 
> Hm...  should that be "\n\\'"? 
Yup, probably.

There's another one in sml.el; I don't have a good way to check across ELPA:

                      ;; Strip the newline, to avoid adding a prompt.
                      (if (string-match "\n\\'" eol)
                          (replace-match " " t t eol) eol)




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 20:44             ` Clément Pit-Claudel
@ 2020-12-21 21:06               ` Lars Ingebrigtsen
  2020-12-21 22:13                 ` Basil L. Contovounesios
  0 siblings, 1 reply; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 21:06 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

Clément Pit-Claudel <cpitclaudel@gmail.com> writes:

>                       ;; Strip the newline, to avoid adding a prompt.
>                       (if (string-match "\n\\'" eol)
>                           (replace-match " " t t eol) eol)

But that one replaced it with a space...

Anyway, I've now added this as `string-chop-newline' to keep some of the
flavour of "chomp".  Feel free to change it to something else.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 20:38           ` Clément Pit-Claudel
@ 2020-12-21 21:11             ` Lars Ingebrigtsen
  2020-12-21 21:29               ` Clément Pit-Claudel
  0 siblings, 1 reply; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 21:11 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

Clément Pit-Claudel <cpitclaudel@gmail.com> writes:

>> s-shared-start is already covered by try-completion, right?  Which has
>> various uses, e.g. in project--read-file-cpd-relative.
>
> It's also covered by compare-strings, but both need a bit of setup to
> do the same as s-shared-start, so a wrapper can be useful (for
> example, it's easy to forget to reset
> completion-regexp-list). Additionally, discovering the right function
> to call can be hard (I hadn't realized that try-completion could be
> used for this, and neither had the authors or s.el).

Perhaps adding something like shared-start would be a good addition for
seq?  The s.el semantics seem a bit unclear -- do "Foobar" and "foozot"
share a prefix?  Adding to seq (with a predicate to compare characters)
would be nice...

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 21:11             ` Lars Ingebrigtsen
@ 2020-12-21 21:29               ` Clément Pit-Claudel
  2020-12-21 21:32                 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 22+ messages in thread
From: Clément Pit-Claudel @ 2020-12-21 21:29 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

On 12/21/20 4:11 PM, Lars Ingebrigtsen wrote:
> Clément Pit-Claudel <cpitclaudel@gmail.com> writes:
> 
>>> s-shared-start is already covered by try-completion, right?  Which has
>>> various uses, e.g. in project--read-file-cpd-relative.
>>
>> It's also covered by compare-strings, but both need a bit of setup to
>> do the same as s-shared-start, so a wrapper can be useful (for
>> example, it's easy to forget to reset
>> completion-regexp-list). Additionally, discovering the right function
>> to call can be hard (I hadn't realized that try-completion could be
>> used for this, and neither had the authors or s.el).
> 
> Perhaps adding something like shared-start would be a good addition for
> seq?  The s.el semantics seem a bit unclear -- do "Foobar" and "foozot"
> share a prefix?  Adding to seq (with a predicate to compare characters)
> would be nice...

But slow, right?  Or maybe it would have a fast path for specific predicates? 



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 21:29               ` Clément Pit-Claudel
@ 2020-12-21 21:32                 ` Lars Ingebrigtsen
  0 siblings, 0 replies; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 21:32 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

Clément Pit-Claudel <cpitclaudel@gmail.com> writes:

> But slow, right?  Or maybe it would have a fast path for specific predicates? 

Yes, perhaps?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 21:06               ` Lars Ingebrigtsen
@ 2020-12-21 22:13                 ` Basil L. Contovounesios
  2020-12-21 22:19                   ` Lars Ingebrigtsen
  0 siblings, 1 reply; 22+ messages in thread
From: Basil L. Contovounesios @ 2020-12-21 22:13 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Clément Pit-Claudel, emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Anyway, I've now added this as `string-chop-newline' to keep some of the
> flavour of "chomp".  Feel free to change it to something else.

string-chop-newline is written more efficiently as

  (string-remove-suffix "\n" string)

which makes me wonder why we need it at all.

-- 
Basil



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 22:13                 ` Basil L. Contovounesios
@ 2020-12-21 22:19                   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-21 22:19 UTC (permalink / raw)
  To: Basil L. Contovounesios; +Cc: Clément Pit-Claudel, emacs-devel

"Basil L. Contovounesios" <contovob@tcd.ie> writes:

> Lars Ingebrigtsen <larsi@gnus.org> writes:
>
>> Anyway, I've now added this as `string-chop-newline' to keep some of the
>> flavour of "chomp".  Feel free to change it to something else.
>
> string-chop-newline is written more efficiently as
>
>   (string-remove-suffix "\n" string)

Now updated.

> which makes me wonder why we need it at all.

The github search seems to imply that it's something that people find
useful -- perhaps because it conveys intent?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-21 18:44       ` Lars Ingebrigtsen
                           ` (2 preceding siblings ...)
  2020-12-21 20:20         ` Clément Pit-Claudel
@ 2020-12-22 10:41         ` Mattias Engdegård
  2020-12-22 14:48           ` Lars Ingebrigtsen
  2020-12-22 15:24           ` Alfred M. Szmidt
  3 siblings, 2 replies; 22+ messages in thread
From: Mattias Engdegård @ 2020-12-22 10:41 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Stefan Kangas, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 785 bytes --]

Thanks for the good work! I suggest that 'string-limit' be replaced with two separate functions, 'string-prefix' and 'string-suffix', because these are two different operations and it makes no sense to multiplex them onto a single function with an argument saying which one we are going to use.

Suggested patch attached. The names can be discussed; for example, 'string-left' and 'string-right' would do just as well.

If someone prefers saturating semantics for negative length arguments, then that's an option too. Ie, in case we want

 (string-prefix "abc" -2)

to yield the empty string instead of an error. I think an error is more useful.

The new functions are now inline (defsubst) in keeping with the spirit of subr-x.el. (We really should inline these better!)


[-- Attachment #2: 0001-Replace-string-limit-with-string-prefix-and-string-s.patch --]
[-- Type: application/octet-stream, Size: 6399 bytes --]

From 799aa76f2aa2fbbb2d1264a9bfdb6208edfbff65 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Tue, 22 Dec 2020 11:23:39 +0100
Subject: [PATCH] Replace string-limit with string-prefix and string-suffix

Have separate functions for distinct operations instead of
multiplexing them both onto a single function.

* doc/lispref/strings.texi (Creating Strings):
* etc/NEWS (https):
* lisp/emacs-lisp/shortdoc.el (string):
Update documentation and announcement.
* lisp/emacs-lisp/subr-x.el (string-limit): Remove.
(string-prefix, string-suffix): New.
* test/lisp/emacs-lisp/subr-x-tests.el (subr-string-limit): Remove.
(subr-string-prefix, subr-string-suffix): New tests.
---
 doc/lispref/strings.texi             | 12 ++++++++----
 etc/NEWS                             |  2 +-
 lisp/emacs-lisp/shortdoc.el          | 10 ++++++----
 lisp/emacs-lisp/subr-x.el            | 29 ++++++++++++++++------------
 test/lisp/emacs-lisp/subr-x-tests.el | 22 ++++++++++++++-------
 5 files changed, 47 insertions(+), 28 deletions(-)

diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index ef848ac510..b71279d5d6 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -405,12 +405,16 @@ Creating Strings
 will not be shortened.
 @end defun
 
-@defun string-limit string length &optional end
+@defun string-prefix string length
 If @var{string} is shorter than @var{length}, @var{string} is returned
 as is.  Otherwise, return a substring of @var{string} consisting of
-the first @var{length} characters.  If the optional @var{end}
-parameter is given, return a string of the @var{length} last
-characters instead.
+the first @var{length} characters.
+@end defun
+
+@defun string-suffix string length
+If @var{string} is shorter than @var{length}, @var{string} is returned
+as is.  Otherwise, return a substring of @var{string} consisting of
+the last @var{length} characters.
 @end defun
 
 @defun string-lines string &optional omit-nulls
diff --git a/etc/NEWS b/etc/NEWS
index 46b8435a14..6ad95f7ca4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1442,7 +1442,7 @@ that makes it a valid button.
 
 +++
 *** A number of new string manipulation functions have been added.
-'string-clean-whitespace', 'string-fill', 'string-limit',
+'string-clean-whitespace', 'string-fill', 'string-prefix', 'string-suffx',
 'string-lines', 'string-pad', 'string-chop-newline' and 'string-slice'.
 
 +++
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index e9e1be1d55..0603046bf6 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -143,10 +143,12 @@ string
   (substring
    :eval (substring "foobar" 0 3)
    :eval (substring "foobar" 3))
-  (string-limit
-   :eval (string-limit "foobar" 3)
-   :eval (string-limit "foobar" 3 t)
-   :eval (string-limit "foobar" 10))
+  (string-prefix
+   :eval (string-prefix "abcde" 3)
+   :eval (string-prefix "abcde" 10))
+  (string-suffix
+   :eval (string-suffix "abcde" 3)
+   :eval (string-suffix "abcde" 10))
   (split-string
    :eval (split-string "foo bar")
    :eval (split-string "|foo|bar|" "|")
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 09c4649817..b775989a88 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -286,20 +286,25 @@ string-fill
       (fill-region (point-min) (point-max)))
     (buffer-string)))
 
-(defun string-limit (string length &optional end)
-  "Return (up to) a LENGTH substring of STRING.
-If STRING is shorter than or equal to LENGTH, the entire string
-is returned unchanged.
-
-If STRING is longer than LENGTH, return a substring consisting of
-the first LENGTH characters of STRING.  If END is non-nil, return
-the last LENGTH characters instead."
+(defsubst string-prefix (string length)
+  "Return the first LENGTH characters of STRING.
+If LENGTH is greater than the length of STRING, return STRING."
+  (declare (side-effect-free t) (pure t))
   (unless (natnump length)
     (signal 'wrong-type-argument (list 'natnump length)))
-  (cond
-   ((<= (length string) length) string)
-   (end (substring string (- (length string) length)))
-   (t (substring string 0 length))))
+  (if (< length (length string))
+      (substring string 0 length)
+    string))
+
+(defsubst string-suffix (string length)
+  "Return the last LENGTH characters of STRING.
+If LENGTH is greater than the length of STRING, return STRING."
+  (declare (side-effect-free t) (pure t))
+  (unless (natnump length)
+    (signal 'wrong-type-argument (list 'natnump length)))
+  (if (< length (length string))
+        (substring string (- (length string) length))
+      string))
 
 (defun string-lines (string &optional omit-nulls)
   "Split STRING into a list of lines.
diff --git a/test/lisp/emacs-lisp/subr-x-tests.el b/test/lisp/emacs-lisp/subr-x-tests.el
index 854d61ed28..88b763fa40 100644
--- a/test/lisp/emacs-lisp/subr-x-tests.el
+++ b/test/lisp/emacs-lisp/subr-x-tests.el
@@ -592,13 +592,21 @@ subr-string-fill
   (should (equal (string-fill "foo bar zot" 5) "foo\nbar\nzot"))
   (should (equal (string-fill "foo bar zot" 7) "foo bar\nzot")))
 
-(ert-deftest subr-string-limit ()
-  (should (equal (string-limit "foo" 10) "foo"))
-  (should (equal (string-limit "foo" 2) "fo"))
-  (should (equal (string-limit "foo" 2 t) "oo"))
-  (should (equal (string-limit "abc" 10 t) "abc"))
-  (should (equal (string-limit "foo" 0) ""))
-  (should-error (string-limit "foo" -1)))
+(ert-deftest subr-string-prefix ()
+  (should (equal (string-prefix "abc" 4) "abc"))
+  (should (equal (string-prefix "abc" 3) "abc"))
+  (should (equal (string-prefix "abc" 2) "ab"))
+  (should (equal (string-prefix "abc" 1) "a"))
+  (should (equal (string-prefix "abc" 0) ""))
+  (should-error (string-prefix "abc" -1)))
+
+(ert-deftest subr-string-suffix ()
+  (should (equal (string-suffix "abc" 4) "abc"))
+  (should (equal (string-suffix "abc" 3) "abc"))
+  (should (equal (string-suffix "abc" 2) "bc"))
+  (should (equal (string-suffix "abc" 1) "c"))
+  (should (equal (string-suffix "abc" 0) ""))
+  (should-error (string-suffix "abc" -1)))
 
 (ert-deftest subr-string-lines ()
   (should (equal (string-lines "foo") '("foo")))
-- 
2.21.1 (Apple Git-122.3)


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-22 10:41         ` Mattias Engdegård
@ 2020-12-22 14:48           ` Lars Ingebrigtsen
  2020-12-22 15:03             ` Mattias Engdegård
  2020-12-22 15:24           ` Alfred M. Szmidt
  1 sibling, 1 reply; 22+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-22 14:48 UTC (permalink / raw)
  To: Mattias Engdegård; +Cc: Stefan Kangas, emacs-devel

Mattias Engdegård <mattiase@acm.org> writes:

> Thanks for the good work! I suggest that 'string-limit' be replaced
> with two separate functions, 'string-prefix' and 'string-suffix',
> because these are two different operations and it makes no sense to
> multiplex them onto a single function with an argument saying which
> one we are going to use.
>
> Suggested patch attached. The names can be discussed; for example,
> 'string-left' and 'string-right' would do just as well.

I'm not really enthusiastic about any of these names.  When we're
talking about suffixes, we're usually talking about file name suffixes
like ".png", and this has nothing to do with that.  These are functions
for limiting the length of a string, and would be called
`string-truncate' if it hadn't been for `string-truncate-left' already
existing and doing something else, and `truncate-string-to-width' doing
something else yet again.

That is, these functions are not for picking out a part of a string, but
for limiting the length, which makes `string-prefix' misleading as for
what the intention is.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-22 14:48           ` Lars Ingebrigtsen
@ 2020-12-22 15:03             ` Mattias Engdegård
  0 siblings, 0 replies; 22+ messages in thread
From: Mattias Engdegård @ 2020-12-22 15:03 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Stefan Kangas, emacs-devel

22 dec. 2020 kl. 15.48 skrev Lars Ingebrigtsen <larsi@gnus.org>:

> I'm not really enthusiastic about any of these names.  When we're
> talking about suffixes, we're usually talking about file name suffixes
> like ".png", and this has nothing to do with that.

Suffix and prefix are (in computing and mathematics) generic terms for the trailing and leading parts of a sequence such as a string. There's nothing in the words that ties them to file names.

>  These are functions
> for limiting the length of a string, and would be called
> `string-truncate' if it hadn't been for `string-truncate-left' already
> existing and doing something else, and `truncate-string-to-width' doing
> something else yet again.
> 
> That is, these functions are not for picking out a part of a string, but
> for limiting the length, which makes `string-prefix' misleading as for
> what the intention is.

Naming pure functions after what they return is standard practice. How is that misleading?
(And who are we to say what the user's intention is?)





^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: master 87e422f: Beef up the Emacs string utility set a bit
  2020-12-22 10:41         ` Mattias Engdegård
  2020-12-22 14:48           ` Lars Ingebrigtsen
@ 2020-12-22 15:24           ` Alfred M. Szmidt
  1 sibling, 0 replies; 22+ messages in thread
From: Alfred M. Szmidt @ 2020-12-22 15:24 UTC (permalink / raw)
  Cc: larsi, stefankangas, emacs-devel

   -@defun string-limit string length &optional end
   +@defun string-prefix string length

This doesn't map very well to string-prefix-p.  The previous names are
better, but still not good I think.  Since this is so very much
similar to substring, why not substring-suffix/prefix or
substring-last/start?




^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2020-12-22 15:24 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20201221175345.27592.89597@vcs0.savannah.gnu.org>
     [not found] ` <20201221175347.9F1B820B76@vcs0.savannah.gnu.org>
2020-12-21 18:24   ` master 87e422f: Beef up the Emacs string utility set a bit Stefan Kangas
2020-12-21 18:32     ` Lars Ingebrigtsen
2020-12-21 18:44       ` Lars Ingebrigtsen
2020-12-21 19:05         ` Stefan Kangas
2020-12-21 20:03           ` Lars Ingebrigtsen
2020-12-21 20:18         ` Basil L. Contovounesios
2020-12-21 20:38           ` Clément Pit-Claudel
2020-12-21 21:11             ` Lars Ingebrigtsen
2020-12-21 21:29               ` Clément Pit-Claudel
2020-12-21 21:32                 ` Lars Ingebrigtsen
2020-12-21 20:20         ` Clément Pit-Claudel
2020-12-21 20:34           ` Lars Ingebrigtsen
2020-12-21 20:44             ` Clément Pit-Claudel
2020-12-21 21:06               ` Lars Ingebrigtsen
2020-12-21 22:13                 ` Basil L. Contovounesios
2020-12-21 22:19                   ` Lars Ingebrigtsen
2020-12-22 10:41         ` Mattias Engdegård
2020-12-22 14:48           ` Lars Ingebrigtsen
2020-12-22 15:03             ` Mattias Engdegård
2020-12-22 15:24           ` Alfred M. Szmidt
2020-12-21 19:05       ` Stefan Monnier
2020-12-21 19:19         ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).