root/ext/standard/php_smart_string.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP license,      |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Author: Sascha Schumann <sascha@schumann.cx>                         |
  16    |         Xinchen Hui <laruence@php.net>                               |
  17    +----------------------------------------------------------------------+
  18  */
  19 
  20 /* $Id$ */
  21 
  22 #ifndef PHP_SMART_STRING_H
  23 #define PHP_SMART_STRING_H
  24 
  25 #include "php_smart_string_public.h"
  26 
  27 #include <stdlib.h>
  28 #ifndef SMART_STR_USE_REALLOC
  29 #include <zend.h>
  30 #endif
  31 
  32 #define smart_string_0(x) do {                                                                          \
  33         if ((x)->c) {                                                                                                   \
  34                 (x)->c[(x)->len] = '\0';                                                                        \
  35         }                                                                                                                               \
  36 } while (0)
  37 
  38 #ifndef SMART_STRING_PREALLOC
  39 #define SMART_STRING_PREALLOC 128
  40 #endif
  41 
  42 #ifndef SMART_STRING_START_SIZE
  43 #define SMART_STRING_START_SIZE 78
  44 #endif
  45 
  46 #ifdef SMART_STRING_USE_REALLOC
  47 #define SMART_STRING_REALLOC(a,b,c) realloc((a),(b))
  48 #else
  49 #define SMART_STRING_REALLOC(a,b,c) perealloc((a),(b),(c))
  50 #endif
  51 
  52 #define SMART_STRING_DO_REALLOC(d, what) \
  53         (d)->c = SMART_STRING_REALLOC((d)->c, (d)->a + 1, (what))
  54 
  55 #define smart_string_alloc4(d, n, what, newlen) do {                                    \
  56         if (!(d)->c) {                                                                                                  \
  57                 (d)->len = 0;                                                                                           \
  58                 newlen = (n);                                                                                           \
  59                 (d)->a = newlen < SMART_STRING_START_SIZE                                               \
  60                                 ? SMART_STRING_START_SIZE                                                               \
  61                                 : newlen + SMART_STRING_PREALLOC;                                               \
  62                 SMART_STRING_DO_REALLOC(d, what);                                                               \
  63         } else {                                                                                                                \
  64                 newlen = (d)->len + (n);                                                                        \
  65                 if (newlen >= (d)->a) {                                                                         \
  66                         (d)->a = newlen + SMART_STRING_PREALLOC;                                        \
  67                         SMART_STRING_DO_REALLOC(d, what);                                                       \
  68                 }                                                                                                                       \
  69         }                                                                                                                               \
  70 } while (0)
  71 
  72 #define smart_string_alloc(d, n, what) \
  73         smart_string_alloc4((d), (n), (what), newlen)
  74 
  75 /* wrapper */
  76 
  77 #define smart_string_appends_ex(dest, src, what) \
  78         smart_string_appendl_ex((dest), (src), strlen(src), (what))
  79 #define smart_string_appends(dest, src) \
  80         smart_string_appendl((dest), (src), strlen(src))
  81 
  82 #define smart_string_appendc(dest, c) \
  83         smart_string_appendc_ex((dest), (c), 0)
  84 #define smart_string_free(s) \
  85         smart_string_free_ex((s), 0)
  86 #define smart_string_appendl(dest, src, len) \
  87         smart_string_appendl_ex((dest), (src), (len), 0)
  88 #define smart_string_append(dest, src) \
  89         smart_string_append_ex((dest), (src), 0)
  90 #define smart_string_append_long(dest, val) \
  91         smart_string_append_long_ex((dest), (val), 0)
  92 #define smart_string_append_unsigned(dest, val) \
  93         smart_string_append_unsigned_ex((dest), (val), 0)
  94 
  95 #define smart_string_appendc_ex(dest, ch, what) do {                                    \
  96         register size_t __nl;                                                                                   \
  97         smart_string_alloc4((dest), 1, (what), __nl);                                           \
  98         (dest)->len = __nl;                                                                                             \
  99         ((unsigned char *) (dest)->c)[(dest)->len - 1] = (ch);                  \
 100 } while (0)
 101 
 102 #define smart_string_free_ex(s, what) do {                                                              \
 103         smart_string *__s = (smart_string *) (s);                                                               \
 104         if (__s->c) {                                                                                                   \
 105                 pefree(__s->c, what);                                                                           \
 106                 __s->c = NULL;                                                                                          \
 107         }                                                                                                                               \
 108         __s->a = __s->len = 0;                                                                                  \
 109 } while (0)
 110 
 111 #define smart_string_appendl_ex(dest, src, nlen, what) do {                     \
 112         register size_t __nl;                                                                                   \
 113         smart_string *__dest = (smart_string *) (dest);                                         \
 114                                                                                                                                         \
 115         smart_string_alloc4(__dest, (nlen), (what), __nl);                                      \
 116         memcpy(__dest->c + __dest->len, (src), (nlen));                                 \
 117         __dest->len = __nl;                                                                                             \
 118 } while (0)
 119 
 120 #define smart_string_append_generic_ex(dest, num, type, vartype, func) do {     \
 121         char __b[32];                                                                                                                   \
 122         char *__t = zend_print##func##_to_buf(__b + sizeof(__b) - 1, (num));    \
 123         smart_string_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type));      \
 124 } while (0)
 125 
 126 #define smart_string_append_unsigned_ex(dest, num, type) \
 127         smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _ulong)
 128 
 129 #define smart_string_append_long_ex(dest, num, type) \
 130         smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _long)
 131 
 132 #define smart_string_append_ex(dest, src, what) \
 133         smart_string_appendl_ex((dest), ((smart_string *)(src))->c, \
 134                 ((smart_string *)(src))->len, (what));
 135 
 136 
 137 #define smart_string_setl(dest, src, nlen) do {                                         \
 138         (dest)->len = (nlen);                                                                                   \
 139         (dest)->a = (nlen) + 1;                                                                                 \
 140         (dest)->c = (char *) (src);                                                                             \
 141 } while (0)
 142 
 143 #define smart_string_sets(dest, src) \
 144         smart_string_setl((dest), (src), strlen(src));
 145 
 146 #endif

/* [<][>][^][v][top][bottom][index][help] */