zend_smart_string.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sascha Schumann <sascha@schumann.cx> |
  14. | Xinchen Hui <laruence@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifndef PHP_SMART_STRING_H
  18. #define PHP_SMART_STRING_H
  19. #include "zend_smart_string_public.h"
  20. #include <stdlib.h>
  21. #include <zend.h>
  22. /* wrapper */
  23. #define smart_string_appends_ex(str, src, what) \
  24. smart_string_appendl_ex((str), (src), strlen(src), (what))
  25. #define smart_string_appends(str, src) \
  26. smart_string_appendl((str), (src), strlen(src))
  27. #define smart_string_append_ex(str, src, what) \
  28. smart_string_appendl_ex((str), ((smart_string *)(src))->c, \
  29. ((smart_string *)(src))->len, (what));
  30. #define smart_string_sets(str, src) \
  31. smart_string_setl((str), (src), strlen(src));
  32. #define smart_string_appendc(str, c) \
  33. smart_string_appendc_ex((str), (c), 0)
  34. #define smart_string_free(s) \
  35. smart_string_free_ex((s), 0)
  36. #define smart_string_appendl(str, src, len) \
  37. smart_string_appendl_ex((str), (src), (len), 0)
  38. #define smart_string_append(str, src) \
  39. smart_string_append_ex((str), (src), 0)
  40. #define smart_string_append_long(str, val) \
  41. smart_string_append_long_ex((str), (val), 0)
  42. #define smart_string_append_unsigned(str, val) \
  43. smart_string_append_unsigned_ex((str), (val), 0)
  44. ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len);
  45. ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len);
  46. static zend_always_inline size_t smart_string_alloc(smart_string *str, size_t len, bool persistent) {
  47. if (UNEXPECTED(!str->c) || UNEXPECTED(len >= str->a - str->len)) {
  48. if (persistent) {
  49. _smart_string_alloc_persistent(str, len);
  50. } else {
  51. _smart_string_alloc(str, len);
  52. }
  53. }
  54. return str->len + len;
  55. }
  56. static zend_always_inline void smart_string_free_ex(smart_string *str, bool persistent) {
  57. if (str->c) {
  58. pefree(str->c, persistent);
  59. str->c = NULL;
  60. }
  61. str->a = str->len = 0;
  62. }
  63. static zend_always_inline void smart_string_0(smart_string *str) {
  64. if (str->c) {
  65. str->c[str->len] = '\0';
  66. }
  67. }
  68. static zend_always_inline void smart_string_appendc_ex(smart_string *dest, char ch, bool persistent) {
  69. dest->len = smart_string_alloc(dest, 1, persistent);
  70. dest->c[dest->len - 1] = ch;
  71. }
  72. static zend_always_inline void smart_string_appendl_ex(smart_string *dest, const char *str, size_t len, bool persistent) {
  73. size_t new_len = smart_string_alloc(dest, len, persistent);
  74. memcpy(dest->c + dest->len, str, len);
  75. dest->len = new_len;
  76. }
  77. static zend_always_inline void smart_string_append_long_ex(smart_string *dest, zend_long num, bool persistent) {
  78. char buf[32];
  79. char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
  80. smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
  81. }
  82. static zend_always_inline void smart_string_append_unsigned_ex(smart_string *dest, zend_ulong num, bool persistent) {
  83. char buf[32];
  84. char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
  85. smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
  86. }
  87. static zend_always_inline void smart_string_setl(smart_string *dest, char *src, size_t len) {
  88. dest->len = len;
  89. dest->a = len + 1;
  90. dest->c = src;
  91. }
  92. static zend_always_inline void smart_string_reset(smart_string *str) {
  93. str->len = 0;
  94. }
  95. #endif