diff options
author | Benjamin Peterson <benjamin@python.org> | 2021-10-15 06:10:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-15 06:10:52 (GMT) |
commit | 9ce9cfe595d64e3081e69de7296042cc54bccf18 (patch) | |
tree | 56be8fe845fa854e4628652bc4c49c1f15475bbb /Objects/fileobject.c | |
parent | 160c38df7fc7ba22dc687879c387bf643ffc3398 (diff) | |
download | cpython-9ce9cfe595d64e3081e69de7296042cc54bccf18.zip cpython-9ce9cfe595d64e3081e69de7296042cc54bccf18.tar.gz cpython-9ce9cfe595d64e3081e69de7296042cc54bccf18.tar.bz2 |
bpo-45479: Futher simplify Py_UniversalNewlineFgets. (GH-28967)
Thank you to Eryk Sun for the suggestions in https://github.com/python/cpython/pull/28965#discussion_r729527143.
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r-- | Objects/fileobject.c | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 8eb6249..8ca56a8 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -248,7 +248,6 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) { char *p = buf; int c; - int skipnextlf = 0; if (fobj) { errno = ENXIO; /* What can you do... */ @@ -256,34 +255,21 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) } FLOCKFILE(stream); while (--n > 0 && (c = GETC(stream)) != EOF ) { - if (skipnextlf) { - skipnextlf = 0; - if (c == '\n') { - /* Seeing a \n here with skipnextlf true - ** means we saw a \r before. - */ - c = GETC(stream); - if (c == EOF) break; - } - } if (c == '\r') { - /* A \r is translated into a \n, and we skip - ** an adjacent \n, if any. We don't set the - ** newlinetypes flag until we've seen the next char. - */ - skipnextlf = 1; - c = '\n'; + // A \r is translated into a \n, and we skip an adjacent \n, if any. + c = GETC(stream); + if (c != '\n') { + ungetc(c, stream); + c = '\n'; + } } *p++ = c; - if (c == '\n') break; + if (c == '\n') { + break; + } } FUNLOCKFILE(stream); *p = '\0'; - if (skipnextlf) { - int c = GETC(stream); - if (c != '\n') - ungetc(c, stream); - } if (p == buf) return NULL; return buf; |