diff Python/bltinmodule.c @ 42283:18e5ae91bd29

Make chr() and ord() return/accept surrogate pairs in narrow builds. The domain of chr() and the range of ord() are now always [0 ... 0x10FFFF].
author Guido van Rossum <guido@python.org>
date Sun, 15 Jul 2007 13:00:05 +0000
parents a53e48be6df9
children 3772103200ec
line wrap: on
line diff
--- a/Python/bltinmodule.c	Sat Jul 14 18:51:36 2007 +0000
+++ b/Python/bltinmodule.c	Sun Jul 15 13:00:05 2007 +0000
@@ -317,7 +317,11 @@
 PyDoc_STRVAR(chr_doc,
 "chr(i) -> Unicode character\n\
 \n\
-Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
+Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."
+#ifndef Py_UNICODE_WIDE
+"\nIf 0x10000 <= i, a surrogate pair is returned."
+#endif
+);
 
 
 static PyObject *
@@ -1179,6 +1183,19 @@
 			ord = (long)*PyUnicode_AS_UNICODE(obj);
 			return PyInt_FromLong(ord);
 		}
+#ifndef Py_UNICODE_WIDE
+		if (size == 2) {
+			/* Decode a valid surrogate pair */
+			int c0 = PyUnicode_AS_UNICODE(obj)[0];
+			int c1 = PyUnicode_AS_UNICODE(obj)[1];
+			if (0xD800 <= c0 && c0 <= 0xDBFF &&
+			    0xDC00 <= c1 && c1 <= 0xDFFF) {
+				ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) +
+				       0x00010000);
+				return PyInt_FromLong(ord);
+			}
+		}
+#endif
 	}
 	else if (PyBytes_Check(obj)) {
 		/* XXX Hopefully this is temporary */
@@ -1205,7 +1222,11 @@
 PyDoc_STRVAR(ord_doc,
 "ord(c) -> integer\n\
 \n\
-Return the integer ordinal of a one-character string.");
+Return the integer ordinal of a one-character string."
+#ifndef Py_UNICODE_WIDE
+"\nA valid surrogate pair is also accepted."
+#endif
+);
 
 
 static PyObject *