zend_smart_string.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifndef PHP_SMART_STRING_H
  20. #define PHP_SMART_STRING_H
  21. #include "zend_smart_string_public.h"
  22. #include <stdlib.h>
  23. #include <zend.h>
  24. /* wrapper */
  25. #define smart_string_appends_ex(str, src, what) \
  26. smart_string_appendl_ex((str), (src), strlen(src), (what))
  27. #define smart_string_appends(str, src) \
  28. smart_string_appendl((str), (src), strlen(src))
  29. #define smart_string_append_ex(str, src, what) \
  30. smart_string_appendl_ex((str), ((smart_string *)(src))->c, \
  31. ((smart_string *)(src))->len, (what));
  32. #define smart_string_sets(str, src) \
  33. smart_string_setl((str), (src), strlen(src));
  34. #define smart_string_appendc(str, c) \
  35. smart_string_appendc_ex((str), (c), 0)
  36. #define smart_string_free(s) \
  37. smart_string_free_ex((s), 0)
  38. #define smart_string_appendl(str, src, len) \
  39. smart_string_appendl_ex((str), (src), (len), 0)
  40. #define smart_string_append(str, src) \
  41. smart_string_append_ex((str), (src), 0)
  42. #define smart_string_append_long(str, val) \
  43. smart_string_append_long_ex((str), (val), 0)
  44. #define smart_string_append_unsigned(str, val) \
  45. smart_string_append_unsigned_ex((str), (val), 0)
  46. ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len);
  47. ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len);
  48. static zend_always_inline size_t smart_string_alloc(smart_string *str, size_t len, zend_bool persistent) {
  49. if (UNEXPECTED(!str->c) || UNEXPECTED(len >= str->a - str->len)) {
  50. if (persistent) {
  51. _smart_string_alloc_persistent(str, len);
  52. } else {
  53. _smart_string_alloc(str, len);
  54. }
  55. }
  56. return str->len + len;
  57. }
  58. static zend_always_inline void smart_string_free_ex(smart_string *str, zend_bool persistent) {
  59. if (str->c) {
  60. pefree(str->c, persistent);
  61. str->c = NULL;
  62. }
  63. str->a = str->len = 0;
  64. }
  65. static zend_always_inline void smart_string_0(smart_string *str) {
  66. if (str->c) {
  67. str->c[str->len] = '\0';
  68. }
  69. }
  70. static zend_always_inline void smart_string_appendc_ex(smart_string *dest, char ch, zend_bool persistent) {
  71. dest->len = smart_string_alloc(dest, 1, persistent);
  72. dest->c[dest->len - 1] = ch;
  73. }
  74. static zend_always_inline void smart_string_appendl_ex(smart_string *dest, const char *str, size_t len, zend_bool persistent) {
  75. size_t new_len = smart_string_alloc(dest, len, persistent);
  76. memcpy(dest->c + dest->len, str, len);
  77. dest->len = new_len;
  78. }
  79. static zend_always_inline void smart_string_append_long_ex(smart_string *dest, zend_long num, zend_bool persistent) {
  80. char buf[32];
  81. char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
  82. smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
  83. }
  84. static zend_always_inline void smart_string_append_unsigned_ex(smart_string *dest, zend_ulong num, zend_bool persistent) {
  85. char buf[32];
  86. char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
  87. smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
  88. }
  89. static zend_always_inline void smart_string_setl(smart_string *dest, char *src, size_t len) {
  90. dest->len = len;
  91. dest->a = len + 1;
  92. dest->c = src;
  93. }
  94. static zend_always_inline void smart_string_reset(smart_string *str) {
  95. str->len = 0;
  96. }
  97. #endif
  98. /*
  99. * Local variables:
  100. * tab-width: 4
  101. * c-basic-offset: 4
  102. * indent-tabs-mode: t
  103. * End:
  104. * vim600: sw=4 ts=4 fdm=marker
  105. * vim<600: sw=4 ts=4
  106. */