[Python-3000-checkins] r64492 - in python/branches/py3k: Include/bytesobject.h Include/unicodeobject.h Objects/stringlib/formatter.h Objects/stringlib/localeutil.h Python/pystrtod.c

eric.smith python-3000-checkins at python.org
Tue Jun 24 03:06:47 CEST 2008


Author: eric.smith
Date: Tue Jun 24 03:06:47 2008
New Revision: 64492

Log:
Merged revisions 64491 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r64491 | eric.smith | 2008-06-23 20:42:10 -0400 (Mon, 23 Jun 2008) | 1 line
  
  Modified interface to _Py_[String|Unicode]InsertThousandsGrouping, in anticipation of fixing issue 3140.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Include/bytesobject.h
   python/branches/py3k/Include/unicodeobject.h
   python/branches/py3k/Objects/stringlib/formatter.h
   python/branches/py3k/Objects/stringlib/localeutil.h
   python/branches/py3k/Python/pystrtod.c

Modified: python/branches/py3k/Include/bytesobject.h
==============================================================================
--- python/branches/py3k/Include/bytesobject.h	(original)
+++ python/branches/py3k/Include/bytesobject.h	Tue Jun 24 03:06:47 2008
@@ -91,8 +91,8 @@
    see Objects/stringlib/localeutil.h */
 
 PyAPI_FUNC(int) _PyBytes_InsertThousandsGrouping(char *buffer,
-						  Py_ssize_t len,
-						  char *plast,
+						  Py_ssize_t n_buffer,
+						  Py_ssize_t n_digits,
 						  Py_ssize_t buf_size,
 						  Py_ssize_t *count,
 						  int append_zero_char);

Modified: python/branches/py3k/Include/unicodeobject.h
==============================================================================
--- python/branches/py3k/Include/unicodeobject.h	(original)
+++ python/branches/py3k/Include/unicodeobject.h	Tue Jun 24 03:06:47 2008
@@ -1458,8 +1458,8 @@
    see Objects/stringlib/localeutil.h */
 
 PyAPI_FUNC(int) _PyUnicode_InsertThousandsGrouping(Py_UNICODE *buffer,
-						  Py_ssize_t len,
-						  Py_UNICODE *plast,
+						  Py_ssize_t n_buffer,
+						  Py_ssize_t n_digits,
 						  Py_ssize_t buf_size,
 						  Py_ssize_t *count,
 						  int append_zero_char);

Modified: python/branches/py3k/Objects/stringlib/formatter.h
==============================================================================
--- python/branches/py3k/Objects/stringlib/formatter.h	(original)
+++ python/branches/py3k/Objects/stringlib/formatter.h	Tue Jun 24 03:06:47 2008
@@ -563,8 +563,7 @@
     if (format->type == 'n')
 	    /* Compute how many additional chars we need to allocate
 	       to hold the thousands grouping. */
-	    STRINGLIB_GROUPING(pnumeric_chars, n_digits,
-			       pnumeric_chars+n_digits,
+	    STRINGLIB_GROUPING(NULL, n_digits, n_digits,
 			       0, &n_grouping_chars, 0);
 
     /* Allocate a new string to hold the result */
@@ -592,8 +591,7 @@
 	    /* We know this can't fail, since we've already
 	       reserved enough space. */
 	    STRINGLIB_CHAR *pstart = p + n_leading_chars;
-	    int r = STRINGLIB_GROUPING(pstart, n_digits,
-				       pstart + n_digits,
+	    int r = STRINGLIB_GROUPING(pstart, n_digits, n_digits,
 				       spec.n_total+n_grouping_chars-n_leading_chars,
 				       NULL, 0);
 	    assert(r);

Modified: python/branches/py3k/Objects/stringlib/localeutil.h
==============================================================================
--- python/branches/py3k/Objects/stringlib/localeutil.h	(original)
+++ python/branches/py3k/Objects/stringlib/localeutil.h	Tue Jun 24 03:06:47 2008
@@ -8,10 +8,9 @@
 /**
  * _Py_InsertThousandsGrouping:
  * @buffer: A pointer to the start of a string.
- * @len: The length of the string.
- * @plast: A pointer to the end of of the digits in the string.  This
- *         may be before the end of the string (if the string contains
- *         decimals, for example).
+ * @n_buffer: The length of the string.
+ * @n_digits: The number of digits in the string, in which we want
+ *            to put the grouping chars.
  * @buf_size: The maximum size of the buffer pointed to by buffer.
  * @count: If non-NULL, points to a variable that will receive the
  *         number of characters we need to insert (and no formatting
@@ -21,10 +20,11 @@
  *         string.
  *
  * Inserts thousand grouping characters (as defined in the current
- *  locale) into the string between buffer and plast.  If count is
- *  non-NULL, don't do any formatting, just count the number of
- *  characters to insert.  This is used by the caller to appropriately
- *  resize the buffer, if needed.
+ *  locale) into the string between buffer and buffer+n_digits.  If
+ *  count is non-NULL, don't do any formatting, just count the number
+ *  of characters to insert.  This is used by the caller to
+ *  appropriately resize the buffer, if needed.  If count is non-NULL,
+ *  buffer can be NULL (it is not dereferenced at all in that case).
  *
  * Return value: 0 on error, else 1.  Note that no error can occur if
  *  count is non-NULL.
@@ -34,8 +34,8 @@
  **/
 int
 _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
-			    Py_ssize_t len,
-			    STRINGLIB_CHAR *plast,
+			    Py_ssize_t n_buffer,
+			    Py_ssize_t n_digits,
 			    Py_ssize_t buf_size,
 			    Py_ssize_t *count,
 			    int append_zero_char)
@@ -44,15 +44,22 @@
 	const char *grouping = locale_data->grouping;
 	const char *thousands_sep = locale_data->thousands_sep;
 	Py_ssize_t thousands_sep_len = strlen(thousands_sep);
-	STRINGLIB_CHAR *pend = buffer + len; /* current end of buffer */
-	STRINGLIB_CHAR *pmax = buffer + buf_size;       /* max of buffer */
+	STRINGLIB_CHAR *pend = NULL; /* current end of buffer */
+	STRINGLIB_CHAR *pmax = NULL; /* max of buffer */
 	char current_grouping;
+	Py_ssize_t remaining = n_digits; /* Number of chars remaining to
+					    be looked at */
 
 	/* Initialize the character count, if we're just counting. */
 	if (count)
 		*count = 0;
+	else {
+		/* We're not just counting, we're modifying buffer */
+		pend = buffer + n_buffer;
+		pmax = buffer + buf_size;
+	}
 
-	/* Starting at plast and working right-to-left, keep track of
+	/* Starting at the end and working right-to-left, keep track of
 	   what grouping needs to be added and insert that. */
 	current_grouping = *grouping++;
 
@@ -60,11 +67,11 @@
 	if (current_grouping == 0)
 		return 1;
 
-	while (plast - buffer > current_grouping) {
+	while (remaining > current_grouping) {
 		/* Always leave buffer and pend valid at the end of this
 		   loop, since we might leave with a return statement. */
 
-		plast -= current_grouping;
+		remaining -= current_grouping;
 		if (count) {
 			/* We're only counting, not touching the memory. */
 			*count += thousands_sep_len;
@@ -72,6 +79,8 @@
 		else {
 			/* Do the formatting. */
 
+			STRINGLIB_CHAR *plast = buffer + remaining;
+
 			/* Is there room to insert thousands_sep_len chars? */
 			if (pmax - pend < thousands_sep_len)
 				/* No room. */
@@ -111,7 +120,7 @@
 	if (append_zero_char) {
 		/* Append a zero character to mark the end of the string,
 		   if there's room. */
-		if (pend - plast < 1)
+		if (pend - (buffer + remaining) < 1)
 			/* No room, error. */
 			return 0;
 		*pend = 0;

Modified: python/branches/py3k/Python/pystrtod.c
==============================================================================
--- python/branches/py3k/Python/pystrtod.c	(original)
+++ python/branches/py3k/Python/pystrtod.c	Tue Jun 24 03:06:47 2008
@@ -364,8 +364,8 @@
 	/* At this point, p points just past the right-most character we
 	   want to format.  We need to add the grouping string for the
 	   characters between buffer and p. */
-	return _PyBytes_InsertThousandsGrouping(buffer, len, p,
-						 buf_size, NULL, 1);
+	return _PyBytes_InsertThousandsGrouping(buffer, len, p-buffer,
+						buf_size, NULL, 1);
 }
 
 /* see FORMATBUFLEN in unicodeobject.c */


More information about the Python-3000-checkins mailing list