regex.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*************************************************************************/
  2. /* regex.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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/array.h"
  33. #include "core/dictionary.h"
  34. #include "core/map.h"
  35. #include "core/reference.h"
  36. #include "core/ustring.h"
  37. #include "core/vector.h"
  38. class RegExMatch : public Reference {
  39. GDCLASS(RegExMatch, Reference);
  40. struct Range {
  41. int start;
  42. int end;
  43. };
  44. String subject;
  45. Vector<Range> data;
  46. Map<String, int> names;
  47. friend class RegEx;
  48. protected:
  49. static void _bind_methods();
  50. int _find(const Variant &p_name) const;
  51. public:
  52. String get_subject() const;
  53. int get_group_count() const;
  54. Dictionary get_names() const;
  55. Array get_strings() const;
  56. String get_string(const Variant &p_name) const;
  57. int get_start(const Variant &p_name) const;
  58. int get_end(const Variant &p_name) const;
  59. };
  60. class RegEx : public Reference {
  61. GDCLASS(RegEx, Reference);
  62. void *general_ctx;
  63. void *code;
  64. String pattern;
  65. void _pattern_info(uint32_t what, void *where) const;
  66. protected:
  67. static void _bind_methods();
  68. public:
  69. void clear();
  70. Error compile(const String &p_pattern);
  71. void _init(const String &p_pattern = "");
  72. Ref<RegExMatch> search(const String &p_subject, int p_offset = 0, int p_end = -1) const;
  73. Array search_all(const String &p_subject, int p_offset = 0, int p_end = -1) const;
  74. String sub(const String &p_subject, const String &p_replacement, bool p_all = false, int p_offset = 0, int p_end = -1) const;
  75. bool is_valid() const;
  76. String get_pattern() const;
  77. int get_group_count() const;
  78. Array get_names() const;
  79. RegEx();
  80. RegEx(const String &p_pattern);
  81. ~RegEx();
  82. };
  83. #endif // REGEX_H