all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* idiomatic case-insensitive string comparison?
@ 2022-07-18 17:47 Sam Steingold
  2022-07-18 19:41 ` Sam Steingold
  0 siblings, 1 reply; 2+ messages in thread
From: Sam Steingold @ 2022-07-18 17:47 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,
Suppose I need to compare strings in a case-insensitive manner.

Common Lisp provides `string-equal', `string-lessp' and `string-greaterp'
(http://clhs.lisp.se/Body/f_stgeq_.htm).

However, in Emacs Lisp `string=' and `string-equal' are the same
(and are not affected by `case-fold-search').

I suppose I can define

--8<---------------cut here---------------start------------->8---
(defun string=-case-fold (a b)
  "case-insensitive string equality test"
  (string= (downcase a) (downcase b)))
--8<---------------cut here---------------end--------------->8---

but this has at least two problems:

1. It can break down for some non-ASCII characters (e.g., Eszett)

2. It allocates fresh memory unnecessarily

So, what is the Emacs Lisp idiom for case-insensitive string equality?

PS. Some languages offer strcasecmp()
(https://en.wikipedia.org/wiki/C_string_handling) others do not
(https://stackoverflow.com/q/319426/850781).

-- 
Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.2113
http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com
https://iris.org.il https://www.memritv.org https://www.dhimmitude.org
recursion, n: See recursion.



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

* Re: idiomatic case-insensitive string comparison?
  2022-07-18 17:47 idiomatic case-insensitive string comparison? Sam Steingold
@ 2022-07-18 19:41 ` Sam Steingold
  0 siblings, 0 replies; 2+ messages in thread
From: Sam Steingold @ 2022-07-18 19:41 UTC (permalink / raw)
  To: help-gnu-emacs

Apparently, I need to do

--8<---------------cut here---------------start------------->8---
(defun string=-ic (a b)
  "case-insensitive string equality test"
  (eq t (compare-strings a nil nil b nil nil t)))
--8<---------------cut here---------------end--------------->8---

I discovered `compare-strings' by noticing that `string-prefix-p'
accepts an IGNORE-CASE argument and looking at its source code.

-- 
Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.2113
http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com
https://www.peaceandtolerance.org/ https://camera.org https://ffii.org
Between grand theft and a legal fee, there only stands a law degree.



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

end of thread, other threads:[~2022-07-18 19:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-18 17:47 idiomatic case-insensitive string comparison? Sam Steingold
2022-07-18 19:41 ` Sam Steingold

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.