summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorKa-Ping Yee <ping@zesty.ca>2001-01-24 17:19:08 (GMT)
committerKa-Ping Yee <ping@zesty.ca>2001-01-24 17:19:08 (GMT)
commitfa004ad36c86f4af406cfa3a84ec8b6cb391e6dd (patch)
tree61544d6bc2f118d4187cfc4887711f728d0fb7cf /Objects
parent726b78ecb8660278399abaf36f98dec56ecf1271 (diff)
downloadcpython-fa004ad36c86f4af406cfa3a84ec8b6cb391e6dd.zip
cpython-fa004ad36c86f4af406cfa3a84ec8b6cb391e6dd.tar.gz
cpython-fa004ad36c86f4af406cfa3a84ec8b6cb391e6dd.tar.bz2
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Switch from octal escapes to hex escapes for other nonprintable characters.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/stringobject.c23
-rw-r--r--Objects/unicodeobject.c25
2 files changed, 36 insertions, 12 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index df3ab49..9cf64ba 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -335,8 +335,14 @@ string_print(PyStringObject *op, FILE *fp, int flags)
c = op->ob_sval[i];
if (c == quote || c == '\\')
fprintf(fp, "\\%c", c);
- else if (c < ' ' || c >= 0177)
- fprintf(fp, "\\%03o", c & 0377);
+ else if (c == '\t')
+ fprintf(fp, "\\t");
+ else if (c == '\n')
+ fprintf(fp, "\\n");
+ else if (c == '\r')
+ fprintf(fp, "\\r");
+ else if (c < ' ' || c >= 0x7f)
+ fprintf(fp, "\\x%02x", c & 0xff);
else
fputc(c, fp);
}
@@ -374,10 +380,15 @@ string_repr(register PyStringObject *op)
c = op->ob_sval[i];
if (c == quote || c == '\\')
*p++ = '\\', *p++ = c;
- else if (c < ' ' || c >= 0177) {
- sprintf(p, "\\%03o", c & 0377);
- while (*p != '\0')
- p++;
+ else if (c == '\t')
+ *p++ = '\\', *p++ = 't';
+ else if (c == '\n')
+ *p++ = '\\', *p++ = 'n';
+ else if (c == '\r')
+ *p++ = '\\', *p++ = 'r';
+ else if (c < ' ' || c >= 0x7f) {
+ sprintf(p, "\\x%02x", c & 0xff);
+ p += 4;
}
else
*p++ = c;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 39ea071..5c193dd 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1343,7 +1343,7 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
char *p;
char *q;
- static const char *hexdigit = "0123456789ABCDEF";
+ static const char *hexdigit = "0123456789abcdef";
repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1);
if (repr == NULL)
@@ -1372,12 +1372,25 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
*p++ = hexdigit[(ch >> 4) & 0xf];
*p++ = hexdigit[ch & 15];
}
- /* Map non-printable US ASCII to '\ooo' */
+ /* Map special whitespace to '\t', \n', '\r' */
+ else if (ch == '\t') {
+ *p++ = '\\';
+ *p++ = 't';
+ }
+ else if (ch == '\n') {
+ *p++ = '\\';
+ *p++ = 'n';
+ }
+ else if (ch == '\r') {
+ *p++ = '\\';
+ *p++ = 'r';
+ }
+ /* Map non-printable US ASCII to '\xhh' */
else if (ch < ' ' || ch >= 128) {
*p++ = '\\';
- *p++ = hexdigit[(ch >> 6) & 7];
- *p++ = hexdigit[(ch >> 3) & 7];
- *p++ = hexdigit[ch & 7];
+ *p++ = 'x';
+ *p++ = hexdigit[(ch >> 4) & 0xf];
+ *p++ = hexdigit[ch & 15];
}
/* Copy everything else as-is */
else
@@ -1498,7 +1511,7 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
char *p;
char *q;
- static const char *hexdigit = "0123456789ABCDEF";
+ static const char *hexdigit = "0123456789abcdef";
repr = PyString_FromStringAndSize(NULL, 6 * size);
if (repr == NULL)