sed: skip a c32rtomb1 call in y/a/b/

* bootstrap.conf (gnulib_modules): Add strnlen.
* lib/.gitignore, m4/.gitignore: Adjust.
* sed/compile.c (compile_program):
* sed/debug.c (debug_print_translation):
* sed/execute.c (translate_mb):
Adjust to data structure change.
* sed/sed.h (struct sed_cmd.translate.a.pair):
Keep the destination in multibyte rather than character form,
as this avoids a c32rtomb1 call during execution.
This commit is contained in:
Paul Eggert committed 2026-08-17 01:17:23 -07:00
1 parent e236a2345e
commit f7cc306958
7 files changed
+35 -26

No files matched your search

+1
View File
@@ -65,6 +65,7 @@ ssize_t
stdckdint-h
streq
strerror
strnlen
strtoimax
strverscmp
unlocked-io
+1
View File
@@ -249,6 +249,7 @@
/string.in.h
/stripslash.c
/strncpy.c
/strnlen.c
/strnul.c
/strtoimax.c
/strtol.c
+1 -1
View File
@@ -1,4 +1,3 @@
/lock.m4
/00gnulib.m4
/__inline.m4
/absolute-header.m4
@@ -222,6 +221,7 @@
/strerror_r.m4
/string_h.m4
/strncpy.m4
/strnlen.m4
/strtoimax.m4
/strtoll.m4
/strverscmp.m4
+7 -5
View File
@@ -1195,9 +1195,9 @@ compile_program (struct vector *vector)
/* trans_pairs = {src(0), dest(0), src(1), dest(1), ... }
src(i) : i-th source character.
dest(i) : i-th destination character. */
int *trans_pairs = xnmalloc (src_char_num,
2 * sizeof *trans_pairs);
cur_cmd->x.translate.a.pair = trans_pairs;
struct trans_pair *trans_pair = xnmalloc (src_char_num,
sizeof *trans_pair);
cur_cmd->x.translate.a.pair = trans_pair;
for (idx_t i = 0; i < src_char_num; i++)
{
if (idx >= dest_len)
@@ -1205,7 +1205,7 @@ compile_program (struct vector *vector)
/* Set the i-th source character. */
mcel_t s = mcel_scan (src_buf, src_buf + len);
trans_pairs[2 * i] = s.err ? -s.err : s.ch;
trans_pair[i].from = s.err ? -s.err : s.ch;
src_buf += s.len; /* Forward to next character. */
len -= s.len;
@@ -1213,7 +1213,9 @@ compile_program (struct vector *vector)
mcel_t d = mcel_scan (dest_buf + idx, dest_buf + dest_len);
/* Set the i-th destination character. */
trans_pairs[2 * i + 1] = d.err ? -d.err : d.ch;
memcpy (trans_pair[i].to, dest_buf + idx, d.len);
if (d.len < sizeof trans_pair[i].to)
trans_pair[i].to[d.len] = '\0';
idx += d.len; /* Forward to next character. */
}
if (idx != dest_len)
+5 -2
View File
@@ -249,10 +249,13 @@ debug_print_translation (const struct sed_cmd *sc)
/* multibyte translation */
putchar ('/');
for (idx_t i = 0; i < npairs; i++)
debug_print_mb (sc->x.translate.a.pair[2 * i]);
debug_print_mb (sc->x.translate.a.pair[i].from);
putchar ('/');
for (idx_t i = 0; i < npairs; i++)
debug_print_mb (sc->x.translate.a.pair[2 * i + 1]);
fwrite (sc->x.translate.a.pair[i].to, 1,
1 + strnlen (sc->x.translate.a.pair[i].to + 1,
sizeof sc->x.translate.a.pair[i].to - 1),
stdout);
putchar ('/');
}
else
+5 -13
View File
@@ -1102,7 +1102,7 @@ do_subst (struct subst *sub)
/* Translate the global input LINE via TRANS.
This function handles the multi-byte case. */
static void
translate_mb (idx_t npairs, int *pair)
translate_mb (idx_t npairs, struct trans_pair *pair)
{
mcel_t g;
for (idx_t idx = 0; idx < line.length; idx += g.len)
@@ -1113,19 +1113,11 @@ translate_mb (idx_t npairs, int *pair)
/* 'i' indicate i-th translate pair. */
for (idx_t i = 0; i < npairs; i++)
{
if (pair[2 * i] == ch)
if (pair[i].from == ch)
{
bool move_remain_buffer = false;
int tr = pair[2 * i + 1];
char mbuf[MCEL_LEN_MAX];
idx_t trans_len;
if (tr < 0)
{
mbuf[0] = -tr;
trans_len = 1;
}
else
trans_len = c32rtomb1 (mbuf, tr);
char *tr = pair[i].to;
idx_t trans_len = 1 + strnlen (tr + 1, sizeof pair[i].to - 1);
if (g.len < trans_len)
{
@@ -1151,7 +1143,7 @@ translate_mb (idx_t npairs, int *pair)
line.length += move_offset;
idx += move_offset;
}
memcpy (line.active + prev_idx, mbuf, trans_len);
memcpy (line.active + prev_idx, tr, trans_len);
break;
}
}
+15 -5
View File
@@ -197,11 +197,21 @@ struct sed_cmd {
sb[(unsigned char) {I}] is the translation of I. */
char *sb;
/* An array used for multibyte translation. For 0 <= I < 2 * npairs,
the translation of pair[I] is pair[I + 1] when I is even,
and a negative pair[I] stands for the encoding error byte -pair[I]
instead of for the usual char32_t character pair[I]. */
int *pair;
/* An array used for multibyte translation. For 0 <= I < npairs,
the translation of pair[I].from is pair[I].to. */
struct trans_pair
{
/* The character or encding-error byte translated from.
A negative value represents a negated encoding error byte
instead of the usual char32_t character value. */
int from;
/* The multibyte representation of the translation.
It is always at least one byte long; after that is null
terminated unless it is exactly MCEL_LEN_MAX bytes.
A null byte is therefore represented by !to[0] && !to[1]. */
char to[MCEL_LEN_MAX];
} *pair;
} a;
} translate;