Skip to content

Commit d6b260d

Browse files
committed
PoC: precompute string lengths
1 parent 1309240 commit d6b260d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/subshell/common.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,11 @@ create_cd_command (const char *s)
14031403
escape_fmt = "\\0%03o";
14041404
}
14051405

1406+
const int quote_cmd_start_len = strlen (quote_cmd_start);
1407+
const int before_wrap_len = strlen (before_wrap);
1408+
const int after_wrap_len = strlen (after_wrap);
1409+
const int quote_cmd_end_len = strlen (quote_cmd_end);
1410+
14061411
/* Measure the length of an escaped byte. In the unlikely case that it won't be uniform in some
14071412
* future shell, have an upper estimate by measuring the largest byte. */
14081413
const int escaped_char_len = sprintf (buf, escape_fmt, 0xFF);
@@ -1414,13 +1419,12 @@ create_cd_command (const char *s)
14141419
g_string_append (ret, "./");
14151420

14161421
// Copy the beginning of the command to the buffer
1417-
g_string_append (ret, quote_cmd_start);
1422+
g_string_append_len (ret, quote_cmd_start, quote_cmd_start_len);
14181423

14191424
/* Sending physical lines over a certain small limit causes problems on some platforms,
14201425
* see ticket #4480. Make sure to wrap in time. See how large we can grow so that an
14211426
* additional line wrapping or closing string still fits. */
1422-
const int max_length =
1423-
COOKED_MODE_BUFFER_SIZE - MAX (strlen (before_wrap), strlen (quote_cmd_end));
1427+
const int max_length = COOKED_MODE_BUFFER_SIZE - MAX (before_wrap_len, quote_cmd_end_len);
14241428
g_assert (max_length >= 64); // Make sure we have enough room to breathe.
14251429

14261430
line_length = ret->len;
@@ -1436,10 +1440,10 @@ create_cd_command (const char *s)
14361440
if (line_length + (n - su) > max_length)
14371441
{
14381442
// wrap to next physical line
1439-
g_string_append (ret, before_wrap);
1443+
g_string_append_len (ret, before_wrap, before_wrap_len);
14401444
g_string_append_c (ret, '\n');
1441-
g_string_append (ret, after_wrap);
1442-
line_length = strlen (after_wrap);
1445+
g_string_append_len (ret, after_wrap, after_wrap_len);
1446+
line_length = after_wrap_len;
14431447
}
14441448
// append character
14451449
g_string_append_len (ret, su, (size_t) (n - su));
@@ -1451,18 +1455,18 @@ create_cd_command (const char *s)
14511455
if (line_length + escaped_char_len > max_length)
14521456
{
14531457
// wrap to next physical line
1454-
g_string_append (ret, before_wrap);
1458+
g_string_append_len (ret, before_wrap, before_wrap_len);
14551459
g_string_append_c (ret, '\n');
1456-
g_string_append (ret, after_wrap);
1457-
line_length = strlen (after_wrap);
1460+
g_string_append_len (ret, after_wrap, after_wrap_len);
1461+
line_length = after_wrap_len;
14581462
}
14591463
// append escaped byte
14601464
g_string_append_printf (ret, escape_fmt, (unsigned char) su[c]);
14611465
line_length += escaped_char_len;
14621466
}
14631467
}
14641468

1465-
g_string_append (ret, quote_cmd_end);
1469+
g_string_append_len (ret, quote_cmd_end, quote_cmd_end_len);
14661470

14671471
return ret;
14681472
}

0 commit comments

Comments
 (0)