/hphp/runtime/base/preg.h

https://gitlab.com/iranjith4/hhvm · C Header · 149 lines · 85 code · 30 blank · 34 comment · 0 complexity · d2afe0550d501ddabb41effc955b86c3 MD5 · raw file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2016 Facebook, Inc. (http://www.facebook.com) |
  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. */
  16. #ifndef incl_HPHP_PREG_H_
  17. #define incl_HPHP_PREG_H_
  18. #include "hphp/runtime/base/req-containers.h"
  19. #include "hphp/runtime/base/type-string.h"
  20. #include <cstdint>
  21. #include <cstddef>
  22. #include <pcre.h>
  23. #define PREG_PATTERN_ORDER 1
  24. #define PREG_SET_ORDER 2
  25. #define PREG_OFFSET_CAPTURE (1<<8)
  26. #define PREG_SPLIT_NO_EMPTY (1<<0)
  27. #define PREG_SPLIT_DELIM_CAPTURE (1<<1)
  28. #define PREG_SPLIT_OFFSET_CAPTURE (1<<2)
  29. #define PREG_REPLACE_EVAL (1<<0)
  30. #define PREG_GREP_INVERT (1<<0)
  31. enum {
  32. PHP_PCRE_NO_ERROR = 0,
  33. PHP_PCRE_INTERNAL_ERROR,
  34. PHP_PCRE_BACKTRACK_LIMIT_ERROR,
  35. PHP_PCRE_RECURSION_LIMIT_ERROR,
  36. PHP_PCRE_BAD_UTF8_ERROR,
  37. PHP_PCRE_BAD_UTF8_OFFSET_ERROR
  38. };
  39. namespace HPHP {
  40. ///////////////////////////////////////////////////////////////////////////////
  41. struct Array;
  42. struct Variant;
  43. struct pcre_cache_entry {
  44. pcre_cache_entry() = default;
  45. ~pcre_cache_entry();
  46. pcre_cache_entry(const pcre_cache_entry&) = delete;
  47. pcre_cache_entry& operator=(const pcre_cache_entry&) = delete;
  48. pcre* re;
  49. pcre_extra* extra; // Holds results of studying
  50. int preg_options:1;
  51. int compile_options:31;
  52. int num_subpats;
  53. mutable std::atomic<char**> subpat_names{nullptr};
  54. };
  55. struct PCREglobals {
  56. PCREglobals();
  57. ~PCREglobals();
  58. // pcre ini_settings
  59. int64_t preg_backtrace_limit;
  60. int64_t preg_recursion_limit;
  61. pcre_jit_stack* jit_stack;
  62. };
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // Cache management
  65. /*
  66. * Initialize PCRE cache.
  67. */
  68. void pcre_init();
  69. /*
  70. * Clear PCRE cache. Not thread safe - call only after parsing options.
  71. */
  72. void pcre_reinit();
  73. /*
  74. * Clean up thread-local PCREs.
  75. */
  76. void pcre_session_exit();
  77. /*
  78. * Dump the contents of the PCRE cache to filename.
  79. */
  80. void pcre_dump_cache(const std::string& filename);
  81. ///////////////////////////////////////////////////////////////////////////////
  82. // PHP API
  83. Variant preg_grep(const String& pattern, const Array& input, int flags = 0);
  84. Variant preg_match(const String& pattern, const String& subject,
  85. Variant* matches = nullptr,
  86. int flags = 0, int offset = 0);
  87. Variant preg_match_all(const String& pattern, const String& subject,
  88. Variant* matches = nullptr,
  89. int flags = 0, int offset = 0);
  90. Variant preg_replace_impl(const Variant& pattern, const Variant& replacement,
  91. const Variant& subject, int limit, Variant* count,
  92. bool is_callable, bool is_filter);
  93. int preg_replace(Variant& result,
  94. const Variant& pattern,
  95. const Variant& replacement,
  96. const Variant& subject,
  97. int limit = -1);
  98. int preg_replace_callback(Variant& result,
  99. const Variant& pattern,
  100. const Variant& callback,
  101. const Variant& subject,
  102. int limit = -1);
  103. int preg_filter(Variant& result,
  104. const Variant& pattern,
  105. const Variant& replacement,
  106. const Variant& subject,
  107. int limit = -1);
  108. Variant preg_split(const String& pattern,
  109. const String& subject,
  110. int limit = -1,
  111. int flags = 0);
  112. String preg_quote(const String& str, const String& delimiter = null_string);
  113. Variant php_split(const String& spliton, const String& str, int count,
  114. bool icase);
  115. int preg_last_error();
  116. size_t preg_pcre_cache_size();
  117. ///////////////////////////////////////////////////////////////////////////////
  118. }
  119. #endif // incl_HPHP_PREG_H__