regex.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**************************************************************************/
  2. /* regex.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef REGEX_H
  31. #define REGEX_H
  32. #include "core/object/ref_counted.h"
  33. #include "core/string/ustring.h"
  34. #include "core/templates/hash_map.h"
  35. #include "core/templates/vector.h"
  36. #include "core/variant/array.h"
  37. #include "core/variant/dictionary.h"
  38. #include "core/variant/typed_array.h"
  39. class RegExMatch : public RefCounted {
  40. GDCLASS(RegExMatch, RefCounted);
  41. struct Range {
  42. int start = 0;
  43. int end = 0;
  44. };
  45. String subject;
  46. Vector<Range> data;
  47. HashMap<String, int> names;
  48. friend class RegEx;
  49. protected:
  50. static void _bind_methods();
  51. int _find(const Variant &p_name) const;
  52. public:
  53. String get_subject() const;
  54. int get_group_count() const;
  55. Dictionary get_names() const;
  56. PackedStringArray get_strings() const;
  57. String get_string(const Variant &p_name) const;
  58. int get_start(const Variant &p_name) const;
  59. int get_end(const Variant &p_name) const;
  60. };
  61. class RegEx : public RefCounted {
  62. GDCLASS(RegEx, RefCounted);
  63. void *general_ctx = nullptr;
  64. void *code = nullptr;
  65. String pattern;
  66. void _pattern_info(uint32_t what, void *where) const;
  67. protected:
  68. static void _bind_methods();
  69. #ifndef DISABLE_DEPRECATED
  70. static Ref<RegEx> _create_from_string_bind_compat_95212(const String &p_pattern);
  71. Error _compile_bind_compat_95212(const String &p_pattern);
  72. static void _bind_compatibility_methods();
  73. #endif
  74. public:
  75. static Ref<RegEx> create_from_string(const String &p_pattern, bool p_show_error = true);
  76. void clear();
  77. Error compile(const String &p_pattern, bool p_show_error = true);
  78. Ref<RegExMatch> search(const String &p_subject, int p_offset = 0, int p_end = -1) const;
  79. TypedArray<RegExMatch> search_all(const String &p_subject, int p_offset = 0, int p_end = -1) const;
  80. String sub(const String &p_subject, const String &p_replacement, bool p_all = false, int p_offset = 0, int p_end = -1) const;
  81. bool is_valid() const;
  82. String get_pattern() const;
  83. int get_group_count() const;
  84. PackedStringArray get_names() const;
  85. RegEx();
  86. RegEx(const String &p_pattern);
  87. ~RegEx();
  88. };
  89. #endif // REGEX_H