Skip to content

Commit 3c251ea

Browse files
committed
Shorten comments
1 parent fe2e3b6 commit 3c251ea

2 files changed

Lines changed: 14 additions & 26 deletions

File tree

‎Lib/test/test_io/test_textio.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,8 @@ def test_newlines_input(self):
460460
self.assertEqual(txt.read(), "".join(expected))
461461

462462
def test_newlines_input_long_lines(self):
463-
# Lines from a few characters to many machine words long, with and
464-
# without control or non-ASCII characters in them, read with and
465-
# without newline translation.
463+
# Lines shorter and longer than a machine word, some with control
464+
# or non-ASCII characters.
466465
lengths = [*range(20), *range(248, 272), 600]
467466
for end in ("\n", "\r", "\r\n"):
468467
for ctrl in ("", "\x00", "\t", "\x0c", "\x0e", "\xe9"):

‎Modules/_io/textio.c‎

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -349,35 +349,20 @@ ctz(size_t v)
349349
# define HAVE_CTZ 0
350350
#endif
351351

352-
/* Skip a size_t word at a time towards the first '\r' or '\n' in the
353-
NUL-terminated UCS1 buffer [s, end). Return a pointer to it, or to an
354-
earlier character from which the caller's own loop finds it.
355-
356-
If out is not NULL, also copy the skipped characters to out. out must
357-
be no further into its buffer than s is into the input, and its buffer
358-
must have room for the whole input. Whole words are copied, so up to
359-
sizeof(size_t) bytes past the skipped characters may also be written;
360-
the caller overwrites them or leaves them past the end of its result.
361-
362-
Lines are often short, and the caller's loop is fastest for them, so if
363-
either of the first two characters is '\r' or below, return a pointer
364-
to it at once. Most words of text have no byte below 0x0E and cost one
365-
cheap test; the rest are checked for '\r' and '\n' exactly, so tabs and
366-
other control characters do not stop the search.
367-
368-
(x - ones * n) & ~x & high flags the bytes of x below n. The cheap test
369-
uses n = 0x0E; the exact test uses n = 1 on x = w ^ (ones * '\n') and
370-
x = w ^ (ones * '\r'), whose zero bytes are the matches. Borrows can
371-
flag extra bytes, but only above a byte below n, so the lowest flagged
372-
byte is always correct: on little-endian machines, ctz() gives the
373-
first match; elsewhere, the caller finds it in the word. */
352+
/* Skip a word at a time through the NUL-terminated UCS1 string [s, end).
353+
Return a pointer to the first '\r' or '\n', or to an earlier character
354+
from which the caller's loop finds it. If out is not NULL, also copy
355+
the skipped characters to out, which must be no further along than s
356+
and have room for the whole input; up to sizeof(size_t) extra bytes
357+
may be written past them. */
374358
static inline const char *
375359
skip_to_newline_ucs1(const char *s, const char *end, char *out)
376360
{
377361
const size_t ones = (size_t)-1 / 0xFF; /* 0x01 in every byte */
378362
const size_t high = ones * 0x80;
379363

380-
/* s[1] is only read when s[0] is not the terminating NUL. */
364+
/* Leave short lines to the caller's loop. s[1] is only read when
365+
s[0] is not the terminating NUL. */
381366
if ((unsigned char)s[0] <= '\r') {
382367
return s;
383368
}
@@ -394,7 +379,11 @@ skip_to_newline_ucs1(const char *s, const char *end, char *out)
394379
memcpy(out, &w, sizeof(w));
395380
out += sizeof(w);
396381
}
382+
/* (x - ones * n) & ~x & high is nonzero if a byte of x is below n.
383+
Most words have no byte below 0x0E. */
397384
if ((w - ones * 0x0E) & ~w & high) {
385+
/* Flag the bytes equal to '\n' or '\r'. Borrows can flag extra
386+
bytes, but only above the first match. */
398387
size_t lf = w ^ (ones * '\n');
399388
size_t cr = w ^ (ones * '\r');
400389
size_t m = (((lf - ones) & ~lf) | ((cr - ones) & ~cr)) & high;

0 commit comments

Comments
 (0)