[Python-checkins] r54147 - sandbox/trunk/pep3101/unicodeformat.c

patrick.maupin python-checkins at python.org
Tue Mar 6 06:04:39 CET 2007


Author: patrick.maupin
Date: Tue Mar  6 06:04:37 2007
New Revision: 54147

Modified:
   sandbox/trunk/pep3101/unicodeformat.c
Log:
Broke output_allocate into two functions

Modified: sandbox/trunk/pep3101/unicodeformat.c
==============================================================================
--- sandbox/trunk/pep3101/unicodeformat.c	(original)
+++ sandbox/trunk/pep3101/unicodeformat.c	Tue Mar  6 06:04:37 2007
@@ -424,6 +424,30 @@
 }
 
 /*
+    output_extend reallocates the output string buffer.
+    It returns a status:  0 for a failed reallocation,
+    1 for success.
+*/
+
+static int
+output_extend(FmtState *fs, Py_ssize_t count)
+{
+    CH_TYPE *startptr;
+    Py_ssize_t curlen, maxlen;
+    startptr = STROBJ_AS_PTR(fs->outstr.obj);
+    curlen = fs->outstr.ptr - startptr;
+    maxlen = curlen + count + fs->size_increment;
+    if (STROBJ_RESIZE(&fs->outstr.obj, maxlen) < 0)
+        return 0;
+    startptr = STROBJ_AS_PTR(fs->outstr.obj);
+    fs->outstr.ptr = startptr + curlen;
+    fs->outstr.end = startptr + maxlen;
+    if (fs->size_increment < MAX_SIZE_INCREMENT)
+        fs->size_increment *= SIZE_MULTIPLIER;
+    return 1;
+}
+
+/*
     output_allocate reserves space in our output string buffer
 
     In some cases, it has to reallocate the string.
@@ -437,21 +461,9 @@
 static int
 output_allocate(FmtState *fs, Py_ssize_t count, CH_TYPE **ptr)
 {
-    Py_ssize_t room = fs->outstr.end - fs->outstr.ptr;
-    if (count > room) {
-        CH_TYPE *startptr;
-        Py_ssize_t curlen, maxlen;
-        startptr = STROBJ_AS_PTR(fs->outstr.obj);
-        curlen = fs->outstr.ptr - startptr;
-        maxlen = curlen + count + fs->size_increment;
-        if (STROBJ_RESIZE(&fs->outstr.obj, maxlen) < 0)
-            return 0;
-        startptr = STROBJ_AS_PTR(fs->outstr.obj);
-        fs->outstr.ptr = startptr + curlen;
-        fs->outstr.end = startptr + maxlen;
-        if (fs->size_increment < MAX_SIZE_INCREMENT)
-            fs->size_increment *= SIZE_MULTIPLIER;
-    }
+    if ((count > fs->outstr.end - fs->outstr.ptr) &&
+            !output_extend(fs, count))
+        return 0;
     *ptr = fs->outstr.ptr;
     fs->outstr.ptr += count;
     return 1;
@@ -469,20 +481,11 @@
 static int
 output_data(FmtState *fs, const CH_TYPE *s, Py_ssize_t count)
 {
-    CH_TYPE *dst;
-
-    /* there is some duplicate code here with output_allocate,
-       which is here to avoid a function call if there's already
-       enough allocated space */
-    Py_ssize_t room = fs->outstr.end - fs->outstr.ptr;
-    if (count > room) {
-        if (output_allocate(fs, count, &dst) == 0)
-            return 0;
-    } else {
-        dst = fs->outstr.ptr;
-        fs->outstr.ptr += count;
-    }
-    memcpy(dst, s, count * sizeof(CH_TYPE));
+    if ((count > fs->outstr.end - fs->outstr.ptr) &&
+            !output_extend(fs, count))
+        return 0;
+    memcpy(fs->outstr.ptr, s, count * sizeof(CH_TYPE));
+    fs->outstr.ptr += count;
     return 1;
 }
 


More information about the Python-checkins mailing list