unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* What's the best way to do "string-memq"?
@ 2008-11-10 18:14 Alan Mackenzie
  2008-11-10 18:17 ` Drew Adams
  2008-11-11  4:05 ` Kevin Rodgers
  0 siblings, 2 replies; 5+ messages in thread
From: Alan Mackenzie @ 2008-11-10 18:14 UTC (permalink / raw)
  To: help-gnu-emacs

Hi, everybody,

I need a predicate which I'd ideally like to write as

    (string-memq (char-after) skip-chars)

, where skip-chars is a string like "^;{}?:", and the predicate should
return t when (char-after) is one of (?^ ?\; ?\{ ?\} ?\? ?\:).

I can't see a convenient way to code this (no, I haven't looked into CL,
and don't want to).  Isn't there some elisp function something like
C's strchr?  Or must I dissect the string into its component characters
for a memq, or (almost as bad), regexp-quote the character from the
buffer and do `string-match' with that?

-- 
Alan Mackenzie (Nuremberg, Germany).




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

* RE: What's the best way to do "string-memq"?
  2008-11-10 18:14 What's the best way to do "string-memq"? Alan Mackenzie
@ 2008-11-10 18:17 ` Drew Adams
  2008-11-10 19:02   ` Alan Mackenzie
  2008-11-11  4:05 ` Kevin Rodgers
  1 sibling, 1 reply; 5+ messages in thread
From: Drew Adams @ 2008-11-10 18:17 UTC (permalink / raw)
  To: 'Alan Mackenzie', help-gnu-emacs

> I need a predicate which I'd ideally like to write as
>     (string-memq (char-after) skip-chars)
> , where skip-chars is a string like "^;{}?:", and the predicate should
> return t when (char-after) is one of (?^ ?\; ?\{ ?\} ?\? ?\:).

Here are a couple of ways:

(string-match (regexp-quote
               (char-to-string (char-after)))
              skip-chars)

(member (char-after) (string-to-list skip-chars))

(They return non-nil, not t, but you can fix that.) Someone else will probably
show you a better way. And you're right that cl offers this out of the box.





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

* RE: What's the best way to do "string-memq"?
  2008-11-10 19:02   ` Alan Mackenzie
@ 2008-11-10 19:00     ` Drew Adams
  0 siblings, 0 replies; 5+ messages in thread
From: Drew Adams @ 2008-11-10 19:00 UTC (permalink / raw)
  To: 'Alan Mackenzie'; +Cc: help-gnu-emacs

> > (string-match (regexp-quote
> >                (char-to-string (char-after)))
> >               skip-chars)
> 
> > (member (char-after) (string-to-list skip-chars))
> 
> The non-nil isn't a problem at all.
> 
> But thanks for the tip!  I probably won't want to use 
> `string-to-list', since it's in mule, and mule wasn't
> included as standard in some previous Emacs, (Was it 20 or 21?).

`string-to-list' is available in emacs -Q for both Emacs 20 and 21.

That said, you might well not want to create a list here.

> But string-to-list is nothing more than:
> 
>     (defsubst string-to-list (string)
>       "Return a list of characters in STRING."
>       (append string nil))
> 
> , so I think I can use `append' directly.  But what a kludge it is!
> 
>     (append "asdf" "1234!)  => (?a ?s ?d ?f "1234)
> 
> .  It's almost embarrassing.  ;-(
> 
> Thanks again!  This list always comes up with the goods.

`append' with a nil second arg is a typical way to convert sequences (such as
strings and vectors) to lists.





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

* Re: What's the best way to do "string-memq"?
  2008-11-10 18:17 ` Drew Adams
@ 2008-11-10 19:02   ` Alan Mackenzie
  2008-11-10 19:00     ` Drew Adams
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Mackenzie @ 2008-11-10 19:02 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

Hi, Drew!

On Mon, Nov 10, 2008 at 10:17:54AM -0800, Drew Adams wrote:
> > I need a predicate which I'd ideally like to write as
> >     (string-memq (char-after) skip-chars)
> > , where skip-chars is a string like "^;{}?:", and the predicate should
> > return t when (char-after) is one of (?^ ?\; ?\{ ?\} ?\? ?\:).

> Here are a couple of ways:

> (string-match (regexp-quote
>                (char-to-string (char-after)))
>               skip-chars)

> (member (char-after) (string-to-list skip-chars))

> (They return non-nil, not t, but you can fix that.) Someone else will
> probably show you a better way. And you're right that cl offers this
> out of the box.

The non-nil isn't a problem at all.

But thanks for the tip!  I probably won't want to use `string-to-list',
since it's in mule, and mule wasn't included as standard in some previous
Emacs, (Was it 20 or 21?).

But string-to-list is nothing more than:

    (defsubst string-to-list (string)
      "Return a list of characters in STRING."
      (append string nil))

, so I think I can use `append' directly.  But what a kludge it is!

    (append "asdf" "1234!)  => (?a ?s ?d ?f "1234)

.  It's almost embarrassing.  ;-(

Thanks again!  This list always comes up with the goods.

-- 
Alan Mackenzie (Nuremberg, Germany).




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

* Re: What's the best way to do "string-memq"?
  2008-11-10 18:14 What's the best way to do "string-memq"? Alan Mackenzie
  2008-11-10 18:17 ` Drew Adams
@ 2008-11-11  4:05 ` Kevin Rodgers
  1 sibling, 0 replies; 5+ messages in thread
From: Kevin Rodgers @ 2008-11-11  4:05 UTC (permalink / raw)
  To: help-gnu-emacs

Alan Mackenzie wrote:
> Hi, everybody,
> 
> I need a predicate which I'd ideally like to write as
> 
>     (string-memq (char-after) skip-chars)
> 
> , where skip-chars is a string like "^;{}?:", and the predicate should
> return t when (char-after) is one of (?^ ?\; ?\{ ?\} ?\? ?\:).
> 
> I can't see a convenient way to code this (no, I haven't looked into CL,
> and don't want to).  Isn't there some elisp function something like
> C's strchr?  Or must I dissect the string into its component characters
> for a memq, or (almost as bad), regexp-quote the character from the
> buffer and do `string-match' with that?

How about: (looking-at (format "[%s]" skip-chars))

The only problem is that '^' is special within "[...]", as are ']' and
'-' (see the Regexp Special node of the Emacs Lisp manual).

Or perhaps:

(save-excursion
   (> (skip-chars-forward skip-chars) 0))

In skip-chars-forward (and -backward) `^' is still special within
"[...]", but it can be quoted with `\'.

-- 
Kevin Rodgers
Denver, Colorado, USA





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

end of thread, other threads:[~2008-11-11  4:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-10 18:14 What's the best way to do "string-memq"? Alan Mackenzie
2008-11-10 18:17 ` Drew Adams
2008-11-10 19:02   ` Alan Mackenzie
2008-11-10 19:00     ` Drew Adams
2008-11-11  4:05 ` Kevin Rodgers

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).