mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
rewrite translate_newlines for clarity
This commit is contained in:
parent
4f38317d5a
commit
42d63847c3
1 changed files with 11 additions and 12 deletions
|
@ -591,20 +591,20 @@ translate_into_utf8(const char* str, const char* enc) {
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
|
translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
|
||||||
int skip_next_lf = 0, length = strlen(s), final_length;
|
int skip_next_lf = 0, needed_length = strlen(s) + 2, final_length;
|
||||||
char *buf, *current;
|
char *buf, *current;
|
||||||
char c;
|
char c = '\0';
|
||||||
buf = PyMem_MALLOC(length + 2);
|
buf = PyMem_MALLOC(needed_length);
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
tok->done = E_NOMEM;
|
tok->done = E_NOMEM;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
for (current = buf; (c = *s++);) {
|
for (current = buf; *s; s++, current++) {
|
||||||
|
c = *s;
|
||||||
if (skip_next_lf) {
|
if (skip_next_lf) {
|
||||||
skip_next_lf = 0;
|
skip_next_lf = 0;
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
c = *s;
|
c = *++s;
|
||||||
s++;
|
|
||||||
if (!c)
|
if (!c)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -614,19 +614,18 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
|
||||||
c = '\n';
|
c = '\n';
|
||||||
}
|
}
|
||||||
*current = c;
|
*current = c;
|
||||||
current++;
|
|
||||||
}
|
}
|
||||||
/* If this is exec input, add a newline to the end of the file if
|
/* If this is exec input, add a newline to the end of the string if
|
||||||
there isn't one already. */
|
there isn't one already. */
|
||||||
if (exec_input && *current != '\n') {
|
if (exec_input && c != '\n') {
|
||||||
*current = '\n';
|
*current = '\n';
|
||||||
current++;
|
current++;
|
||||||
}
|
}
|
||||||
*current = '\0';
|
*current = '\0';
|
||||||
final_length = current - buf;
|
final_length = current - buf + 1;
|
||||||
if (final_length < length && final_length)
|
if (final_length < needed_length && final_length)
|
||||||
/* should never fail */
|
/* should never fail */
|
||||||
buf = PyMem_REALLOC(buf, final_length + 1);
|
buf = PyMem_REALLOC(buf, final_length);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue