[Python-checkins] cpython: and back to the "magic" formula (with a comment) it is

benjamin.peterson python-checkins at python.org
Wed Nov 23 17:45:04 CET 2011


http://hg.python.org/cpython/rev/14c979556d43
changeset:   73728:14c979556d43
user:        Benjamin Peterson <benjamin at python.org>
date:        Wed Nov 23 10:44:52 2011 -0600
summary:
  and back to the "magic" formula (with a comment) it is

files:
  Objects/unicodeobject.c |  18 +++---------------
  1 files changed, 3 insertions(+), 15 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -6164,21 +6164,9 @@
     kind = PyUnicode_KIND(unicode);
     data = PyUnicode_DATA(unicode);
     len = PyUnicode_GET_LENGTH(unicode);
-    expandsize = 0;
-    switch (kind) {
-    case PyUnicode_1BYTE_KIND:
-        expandsize = 4;
-        break;
-    case PyUnicode_2BYTE_KIND:
-        expandsize = 6;
-        break;
-    case PyUnicode_4BYTE_KIND:
-        expandsize = 10;
-        break;
-    default:
-        assert(0);
-        break;
-    }
+    /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6
+       bytes, and 1 byte characters 4. */
+    expandsize = kind * 2 + 2;
 
     if (len > PY_SSIZE_T_MAX / expandsize)
         return PyErr_NoMemory();

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list