[PHP-DEV] Re: [PHP-CVS] cvs: php4 /main spprintf.c

2003-02-11 Thread Sascha Schumann
Markus,

here is a patch against the current CVS which

- trims +100 lines of code from spprintf.c
- introduces an overflow detection in STR_TO_DEC
- eliminates dead code (e.g. assert(foo); if (foo) {..})
- removes unused macros from the original code
- simplifies code (e.g. cc was completely dropped)
- improves run-time performance

  The max_len feature is never used in our code base.
  Nevertheless, cpu cycles were spent on each string
  operation to check the current length against max_len which
  is quite inefficient.  Thus, I've moved the check to
  vspprintf where it is applied only once per call.

- Sascha
Index: spprintf.c
===
RCS file: /repository/php4/main/spprintf.c,v
retrieving revision 1.12
diff -u -r1.12 spprintf.c
--- spprintf.c  11 Feb 2003 20:30:37 -  1.12
+++ spprintf.c  11 Feb 2003 22:43:10 -
@@ -89,6 +89,8 @@
 #define FLOAT_DIGITS6
 #define EXPONENT_LENGTH 10
 
+#include ext/standard/php_smart_str.h
+
 /*
  * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
  *
@@ -96,134 +98,48 @@
  */
 #define NUM_BUF_SIZE512
 
-
-/*
- * Size for realloc operations
- */
-#define SPPRINTF_BLOCK_SIZE 128
-
-/*
- * Descriptor for buffer area
- */
-struct xbuf_area {
-   char*buf;  /* pointer to buffer */
-   size_t  size;
-   size_t  max_len;
-   char*buf_end;  /* pointer to buffer end or ~0 */
-   char*nextb;/* pointer to next byte to read/write   */
-};
-
-typedef struct xbuf_area xbuffy;
-
-/* Resize xbuf so that add bytes can be added. Reallocation is done
- * in defined block size to minimize calls to realloc.
- */
-static void xbuf_resize(xbuffy *xbuf, size_t add) 
-{
-   char *buf;
-   size_t size, offset;
-
-   if (xbuf-buf) {
-   offset = xbuf-nextb - xbuf-buf;
-   if (offset+add  xbuf-size) {
-   return; /* do not change size if not necessary */
-   }
-   } else {
-   offset = 0;
-   }
-   if (addSPPRINTF_BLOCK_SIZE) {
-   size = xbuf-size + SPPRINTF_BLOCK_SIZE;
-   } else {
-   size = xbuf-size + add;
-   }
-   if (xbuf-max_len  size  xbuf-max_len) {
-   size = xbuf-max_len;
-   }
-
-   buf = erealloc(xbuf-buf, size+1); /* alloc space for NUL */
-   
-   if (buf) {
-   xbuf-buf = buf;
-   xbuf-buf_end = xbuf-max_len ? buf[size] : (char *) ~0;
-   xbuf-nextb = buf+offset;
-   xbuf-size = size;
-   }
-}
-
-/* Initialise xbuffy with size spprintf_BLOCK_SIZE
- */
-static char * xbuf_init(xbuffy *xbuf, size_t max_len) 
-{
-   xbuf-buf = NULL;
-   xbuf-size = 0;
-   xbuf-max_len = max_len;
-   xbuf_resize(xbuf, 0); /* NOT max_len */
-   return xbuf-buf;
-}
-
 /*
- * The INS_CHAR macro inserts a character in the buffer and writes
- * the buffer back to disk if necessary
- * It uses the char pointers sp and bep:
- *  sp points to the next available character in the buffer
- *  bep points to the end-of-buffer+1
- * While using this macro, note that the nextb pointer is NOT updated.
+ * The INS_CHAR macro inserts a character in the buffer.
  *
- * NOTE: Evaluation of the c argument should not have any side-effects
+ * NOTE: Evaluation of the ch argument should not have any side-effects
  */
-#define INS_CHAR_NR(xbuf, ch, cc)   \
-   if (xbuf-nextb  xbuf-buf_end) {  \
-   *(xbuf-nextb++) = ch;  \
-   cc++;   \
-   }
-
-#define INS_STRING(xbuf, s, slen, cc)   \
-   xbuf_resize(xbuf, s_len);   \
-   if (xbuf-nextb+slen  xbuf-buf_end) { \
-   memcpy(xbuf-nextb, s, slen);   \
-   xbuf-nextb += slen;\
-   cc += slen; \
-   s += slen;  \
-   } else {\
-   for (i = s_len; i != 0; i--) {  \
-   INS_CHAR_NR(xbuf, *s, cc);  \
-   s++;\
-   }   \
-   }
-
-#define INS_CHAR(xbuf, ch, cc)  \
-   xbuf_resize(xbuf, 1);   \
-   INS_CHAR_NR(xbuf, ch, cc)
+#define INS_CHAR_NR(xbuf, ch) do { \
+   smart_str_appendc(xbuf, ch);\
+} while (0)
+
+#define INS_STRING(xbuf, s, slen) do { \
+   smart_str_appendl(xbuf, s, slen);   \
+} while (0)
+   
+#define INS_CHAR(xbuf, ch)  \
+   INS_CHAR_NR(xbuf, ch)
 
 /*
  * Macro that does padding. The padding is done by printing
  * the character ch.
  */
-#define PAD(xbuf, width, len, ch, cc)   \
-   if (width  len) {   

[PHP-DEV] Re: [PHP-CVS] cvs: php4 /main spprintf.c

2003-02-11 Thread Marcus Börger
At 23:44 11.02.2003, Sascha Schumann wrote:

Markus,

here is a patch against the current CVS which

- trims +100 lines of code from spprintf.c
- introduces an overflow detection in STR_TO_DEC


Why then this comment? Did you forgot to remove it.
+/* XXX: Does not handle overflow. */
+#define STR_TO_DEC(str, num) do {  \



- eliminates dead code (e.g. assert(foo); if (foo) {..})
- removes unused macros from the original code
- simplifies code (e.g. cc was completely dropped)
- improves run-time performance

  The max_len feature is never used in our code base.
  Nevertheless, cpu cycles were spent on each string
  operation to check the current length against max_len which
  is quite inefficient.  Thus, I've moved the check to
  vspprintf where it is applied only once per call.

- Sascha


Hey cool i just thought about doing that, too.  You're really fast
Unfortunatley i haven't yet the time to try it out but it looks good.
Does it have any known problems?

marcus


p.s.: I asked about adding the cli manpage some days ago, can you help?


--
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DEV] Re: [PHP-CVS] cvs: php4 /main spprintf.c

2003-02-11 Thread Sascha Schumann
 Why then this comment? Did you forgot to remove it.

Yes.

 Hey cool i just thought about doing that, too.  You're really fast
 Unfortunatley i haven't yet the time to try it out but it looks good.
 Does it have any known problems?

No, but then there are no real test cases which could be
tested against.

 p.s.: I asked about adding the cli manpage some days ago, can you help?

$(mkinstalldirs) $(mandir)/man1
$(INSTALL_DATA) page.1 $(mandir)/man1/page.1

should be sufficient.

- Sascha

-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DEV] Re: [PHP-CVS] cvs: php4 /main spprintf.c

2003-02-11 Thread Marcus Börger
At 23:44 11.02.2003, Sascha Schumann wrote:

Markus,

here is a patch against the current CVS which


If you commit that stuff, you should apply the changes to our snprintf.c, too.

regards
marcus


--
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DEV] RE: [PHP-CVS] cvs: php4 /main spprintf.c spprintf.h

2002-04-10 Thread James Cox

Marcus,

I was wondering where exactly the mail was? I'd be interested to know why we
have more print functions.

James

-Original Message-
From: Marcus Bvrger [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 10, 2002 2:09 AM
To: [EMAIL PROTECTED]
Subject: [PHP-CVS] cvs: php4 /main spprintf.c spprintf.h


helly   Tue Apr  9 21:09:23 2002 EDT

  Added files:
/php4/main  spprintf.c spprintf.h
  Log:
  introducing spprintf and vspprintf
  #mail follows



-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP-DEV] RE: [PHP-CVS] cvs: php4 /main spprintf.c spprintf.h

2002-04-10 Thread James Cox

duh, found it in my HTML mail quarantine pile

-Original Message-
From: James Cox [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 10, 2002 8:30 AM
To: Marcus Bvrger
Cc: Php-Dev
Subject: [PHP-DEV] RE: [PHP-CVS] cvs: php4 /main spprintf.c spprintf.h


Marcus,

I was wondering where exactly the mail was? I'd be interested to know why we
have more print functions.

James

-Original Message-
From: Marcus Bvrger [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 10, 2002 2:09 AM
To: [EMAIL PROTECTED]
Subject: [PHP-CVS] cvs: php4 /main spprintf.c spprintf.h


helly   Tue Apr  9 21:09:23 2002 EDT

  Added files:
/php4/main  spprintf.c spprintf.h
  Log:
  introducing spprintf and vspprintf
  #mail follows



--
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DEV] Re: [PHP-CVS] cvs: php4 /main spprintf.c spprintf.h

2002-04-09 Thread Sebastian Bergmann

Marcus Börger wrote:
 helly   Tue Apr  9 21:09:23 2002 EDT

   Added files:
 /php4/main  spprintf.c spprintf.h
   Log:
   introducing spprintf and vspprintf
   #mail follows

  This breaks the Win32 build.

  The following patch is a start at fixing it

Index: php4dllts.dsp
===
RCS file: /repository/php4/win32/php4dllts.dsp,v
retrieving revision 1.58
diff -u -2 -b -w -B -r1.58 php4dllts.dsp
--- php4dllts.dsp   19 Mar 2002 19:15:40 -  1.58
+++ php4dllts.dsp   10 Apr 2002 04:14:32 -
 -247,4 +247,8 
 # Begin Source File

+SOURCE=..\main\spprintf.c
+# End Source File
+# Begin Source File
+
 SOURCE=..\main\user_streams.c
 # End Source File
 -332,4 +336,8 

 SOURCE=..\main\php_streams.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\main\spprintf.h
 # End Source File
 # Begin Source File

  The following errors remain

spprintf.obj: error LNK2001: Unresolved external symbol:
_ap_php_conv_fp

spprintf.obj: error LNK2001: Unresolved external symbol:
_ap_php_conv_p2

spprintf.obj: error LNK2001: Unresolved external symbol:
_ap_php_conv_10

-- 
  Sebastian Bergmann
  http://sebastian-bergmann.de/ http://phpOpenTracker.de/

  Did I help you? Consider a gift: http://wishlist.sebastian-bergmann.de/

-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php