summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2002-10-11 05:37:59 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2002-10-11 05:37:59 (GMT)
commita5f0907d790c889b1b7ea8ce3f79ff6b519ff361 (patch)
treef5e46a699b6d2796e2c31945f5eae48ba8bd00af /Objects
parent049cd6b563892b64edd606cfdf36c5d0f30b7b16 (diff)
downloadcpython-a5f0907d790c889b1b7ea8ce3f79ff6b519ff361.zip
cpython-a5f0907d790c889b1b7ea8ce3f79ff6b519ff361.tar.gz
cpython-a5f0907d790c889b1b7ea8ce3f79ff6b519ff361.tar.bz2
Back out #479898.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/stringobject.c84
1 files changed, 15 insertions, 69 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 5c5b6ae..35b4b47 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -26,20 +26,6 @@ static PyStringObject *nullstring;
static PyObject *interned;
-#if defined(HAVE_MBTOWC) && defined(HAVE_WCHAR_H) && defined(HAVE_WCTYPE_H)
-# define PRINT_MULTIBYTE_STRING
-# include <locale.h>
-# include <wchar.h>
-# include <wctype.h>
-# if defined(HAVE_ISWPRINT)
-# define _isprint iswprint
-# else
-# define _isprint isprint
-# endif
-#endif
-
-static const char *hexchars = "0123456789abcdef";
-
/*
For both PyString_FromString() and PyString_FromStringAndSize(), the
parameter `size' denotes number of characters to allocate, not counting any
@@ -763,14 +749,8 @@ PyString_AsStringAndSize(register PyObject *obj,
static int
string_print(PyStringObject *op, FILE *fp, int flags)
{
-#ifndef PRINT_MULTIBYTE_STRING
int i;
char c;
-#else
- char *scur, *send;
- wchar_t c;
- int cr;
-#endif
int quote;
/* XXX Ought to check for interrupts when writing long strings */
@@ -796,36 +776,20 @@ string_print(PyStringObject *op, FILE *fp, int flags)
quote = '"';
fputc(quote, fp);
-#ifndef PRINT_MULTIBYTE_STRING
for (i = 0; i < op->ob_size; i++) {
c = op->ob_sval[i];
-#else
- for (scur = op->ob_sval, send = op->ob_sval + op->ob_size;
- scur < send; scur += cr) {
- if ((cr = mbtowc(&c, scur, send - scur)) <= 0)
- goto non_printable;
-#endif
if (c == quote || c == '\\')
- fputc('\\', fp), fputc(c, fp);
+ fprintf(fp, "\\%c", c);
else if (c == '\t')
- fputs("\\t", fp);
+ fprintf(fp, "\\t");
else if (c == '\n')
- fputs("\\n", fp);
+ fprintf(fp, "\\n");
else if (c == '\r')
- fputs("\\r", fp);
-#ifndef PRINT_MULTIBYTE_STRING
- else if (' ' <= c && c < 0x7f)
- fputc(c, fp);
+ fprintf(fp, "\\r");
+ else if (c < ' ' || c >= 0x7f)
+ fprintf(fp, "\\x%02x", c & 0xff);
else
- fprintf(fp, "\\x%02x", c & 0xff);
-#else
- else if (_isprint(c))
- fwrite(scur, cr, 1, fp);
- else {
-non_printable: cr = 1; /* unit to move cursor */
- fprintf(fp, "\\x%02x", *scur & 0xff);
- }
-#endif
+ fputc(c, fp);
}
fputc(quote, fp);
return 0;
@@ -846,14 +810,8 @@ PyString_Repr(PyObject *obj, int smartquotes)
return NULL;
}
else {
-#ifndef PRINT_MULTIBYTE_STRING
register int i;
register char c;
-#else
- register char *scur, *send;
- wchar_t c;
- int cr;
-#endif
register char *p;
int quote;
@@ -866,18 +824,11 @@ PyString_Repr(PyObject *obj, int smartquotes)
p = PyString_AS_STRING(v);
*p++ = quote;
-#ifndef PRINT_MULTIBYTE_STRING
for (i = 0; i < op->ob_size; i++) {
/* There's at least enough room for a hex escape
and a closing quote. */
assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
c = op->ob_sval[i];
-#else
- for (scur = op->ob_sval, send = op->ob_sval + op->ob_size;
- scur < send; scur += cr) {
- if ((cr = mbtowc(&c, scur, send - scur)) <= 0)
- goto non_printable;
-#endif
if (c == quote || c == '\\')
*p++ = '\\', *p++ = c;
else if (c == '\t')
@@ -886,20 +837,15 @@ PyString_Repr(PyObject *obj, int smartquotes)
*p++ = '\\', *p++ = 'n';
else if (c == '\r')
*p++ = '\\', *p++ = 'r';
-#ifndef PRINT_MULTIBYTE_STRING
- else if (' ' <= c && c < 0x7f)
- *p++ = c;
- else {
-#else
- else if (_isprint(c))
- memcpy(p, scur, cr), p += cr;
- else {
-non_printable: cr = 1; c = *scur;
-#endif
- *p++ = '\\'; *p++ = 'x';
- *p++ = hexchars[(c >> 4) & 0x0f];
- *p++ = hexchars[c & 0x0f];
+ else if (c < ' ' || c >= 0x7f) {
+ /* For performance, we don't want to call
+ PyOS_snprintf here (extra layers of
+ function call). */
+ sprintf(p, "\\x%02x", c & 0xff);
+ p += 4;
}
+ else
+ *p++ = c;
}
assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
*p++ = quote;