Commit a10d4a6f authored by Fredrik Lundh's avatar Fredrik Lundh

changed \x to consume exactly two hex digits. implements PEP-223

for 8-bit strings.
parent 07f40f5c
...@@ -873,10 +873,11 @@ parsestr(char *s) ...@@ -873,10 +873,11 @@ parsestr(char *s)
return PyUnicode_DecodeUnicodeEscape( return PyUnicode_DecodeUnicodeEscape(
s, len, NULL); s, len, NULL);
} }
else if (rawmode || strchr(s, '\\') == NULL) { if (rawmode || strchr(s, '\\') == NULL)
return PyString_FromStringAndSize(s, len); return PyString_FromStringAndSize(s, len);
}
v = PyString_FromStringAndSize((char *)NULL, len); v = PyString_FromStringAndSize((char *)NULL, len);
if (v == NULL)
return NULL;
p = buf = PyString_AsString(v); p = buf = PyString_AsString(v);
end = s + len; end = s + len;
while (s < end) { while (s < end) {
...@@ -909,24 +910,35 @@ parsestr(char *s) ...@@ -909,24 +910,35 @@ parsestr(char *s)
*p++ = c; *p++ = c;
break; break;
case 'x': case 'x':
if (isxdigit(Py_CHARMASK(*s))) { if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) {
unsigned int x = 0; unsigned int x = 0;
do { c = Py_CHARMASK(*s);
c = Py_CHARMASK(*s); s++;
s++; if (isdigit(c))
x = (x<<4) & ~0xF; x = c - '0';
if (isdigit(c)) else if (islower(c))
x += c - '0'; x = 10 + c - 'a';
else if (islower(c)) else
x += 10 + c - 'a'; x = 10 + c - 'A';
else x = x << 4;
x += 10 + c - 'A'; c = Py_CHARMASK(*s);
} while (isxdigit(Py_CHARMASK(*s))); s++;
if (isdigit(c))
x += c - '0';
else if (islower(c))
x += 10 + c - 'a';
else
x += 10 + c - 'A';
*p++ = x; *p++ = x;
break; break;
} }
/* FALLTHROUGH */ PyErr_SetString(PyExc_ValueError, "invalid \\x escape");
default: *p++ = '\\'; *p++ = s[-1]; break; Py_DECREF(v);
return NULL;
default:
*p++ = '\\';
*p++ = s[-1];
break;
} }
} }
_PyString_Resize(&v, (int)(p - buf)); _PyString_Resize(&v, (int)(p - buf));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment