From: Yuan Fu <casouri@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: mattiase@acm.org, emacs-devel@gnu.org
Subject: Re: Matching regex case-sensitively in C strings?
Date: Tue, 8 Nov 2022 12:59:12 -0800 [thread overview]
Message-ID: <2338A13C-A6E3-49B6-9E5C-A366022E4D29@gmail.com> (raw)
In-Reply-To: <838rklxmnm.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 842 bytes --]
> On Nov 8, 2022, at 11:37 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>
>> From: Yuan Fu <casouri@gmail.com>
>> Date: Tue, 8 Nov 2022 11:31:35 -0800
>> Cc: Eli Zaretskii <eliz@gnu.org>,
>> emacs-devel <emacs-devel@gnu.org>
>>
>> + Lisp_Object translate_table = ignore_case ? Vascii_canon_table : Qnil;
>
> Please don't call this "translate_table", as it isn't. The
> documentation elsewhere calls this "canonicalize_table" or just
> "canonicalize".
>
>> fast_c_string_match_ignore_case (Lisp_Object regexp,
>> const char *string, ptrdiff_t len)
>> {
>> + return fast_c_string_match (regexp, string, len, true);
>> +}
>
> I'm bothered that this function, which is supposed to be fast, will
> now be slower due to an extra function call.
How about this:
Now c_string_match functions mirror their string_match counterparts.
Yuan
[-- Attachment #2: c_string_match.diff --]
[-- Type: application/octet-stream, Size: 2934 bytes --]
diff --git a/src/lisp.h b/src/lisp.h
index 1e41e2064c9..9f497d92270 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -4757,6 +4757,8 @@ XMODULE_FUNCTION (Lisp_Object o)
extern void record_unwind_save_match_data (void);
extern ptrdiff_t fast_string_match_internal (Lisp_Object, Lisp_Object,
Lisp_Object);
+extern ptrdiff_t fast_c_string_match_internal (Lisp_Object, const char *,
+ ptrdiff_t, Lisp_Object);
INLINE ptrdiff_t
fast_string_match (Lisp_Object regexp, Lisp_Object string)
@@ -4770,8 +4772,21 @@ fast_string_match_ignore_case (Lisp_Object regexp, Lisp_Object string)
return fast_string_match_internal (regexp, string, Vascii_canon_table);
}
-extern ptrdiff_t fast_c_string_match_ignore_case (Lisp_Object, const char *,
- ptrdiff_t);
+INLINE ptrdiff_t
+fast_c_string_match (Lisp_Object regexp,
+ const char *string, ptrdiff_t len)
+{
+ return fast_c_string_match_internal (regexp, string, len, Qnil);
+}
+
+INLINE ptrdiff_t
+fast_c_string_match_ignore_case (Lisp_Object regexp,
+ const char *string, ptrdiff_t len)
+{
+ return fast_c_string_match_internal (regexp, string, len,
+ Vascii_canon_table);
+}
+
extern ptrdiff_t fast_looking_at (Lisp_Object, ptrdiff_t, ptrdiff_t,
ptrdiff_t, ptrdiff_t, Lisp_Object);
extern ptrdiff_t find_newline1 (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t,
diff --git a/src/search.c b/src/search.c
index b5d6a442c0f..f7a28202b23 100644
--- a/src/search.c
+++ b/src/search.c
@@ -496,19 +496,26 @@ fast_string_match_internal (Lisp_Object regexp, Lisp_Object string,
return val;
}
-/* Match REGEXP against STRING, searching all of STRING ignoring case,
- and return the index of the match, or negative on failure.
- This does not clobber the match data.
+/* Match REGEXP against STRING, searching all of STRING and return the
+ index of the match, or negative on failure. This does not clobber
+ the match data. Table is a canonicalize table.
+
We assume that STRING contains single-byte characters. */
ptrdiff_t
-fast_c_string_match_ignore_case (Lisp_Object regexp,
- const char *string, ptrdiff_t len)
+fast_c_string_match_internal (Lisp_Object regexp,
+ const char *string, ptrdiff_t len,
+ Lisp_Object table)
{
+ /* FIXME: This is expensive and not obviously correct when it makes
+ a difference. I.e., no longer "fast", and may hide bugs.
+ Something should be done about this. */
regexp = string_make_unibyte (regexp);
+ /* Record specpdl index because freeze_pattern pushes an
+ unwind-protect on the specpdl. */
specpdl_ref count = SPECPDL_INDEX ();
struct regexp_cache *cache_entry
- = compile_pattern (regexp, 0, Vascii_canon_table, 0, 0);
+ = compile_pattern (regexp, 0, table, 0, 0);
freeze_pattern (cache_entry);
re_match_object = Qt;
ptrdiff_t val = re_search (&cache_entry->buf, string, len, 0, len, 0);
next prev parent reply other threads:[~2022-11-08 20:59 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-07 5:41 Matching regex case-sensitively in C strings? Yuan Fu
2022-11-07 11:52 ` Eli Zaretskii
2022-11-07 20:35 ` Yuan Fu
2022-11-08 10:18 ` Mattias Engdegård
2022-11-08 12:29 ` Eli Zaretskii
2022-11-08 19:31 ` Yuan Fu
2022-11-08 19:37 ` Eli Zaretskii
2022-11-08 20:59 ` Yuan Fu [this message]
2022-11-09 10:53 ` Mattias Engdegård
2022-11-09 10:33 ` Mattias Engdegård
2022-11-09 13:06 ` Eli Zaretskii
2022-11-10 9:52 ` Mattias Engdegård
2022-11-10 11:18 ` Eli Zaretskii
2022-11-10 11:35 ` Robert Pluim
2022-11-10 14:20 ` Mattias Engdegård
2022-11-10 22:25 ` Yuan Fu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2338A13C-A6E3-49B6-9E5C-A366022E4D29@gmail.com \
--to=casouri@gmail.com \
--cc=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=mattiase@acm.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.