regex.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*************************************************************************/
  2. /* regex.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef REGEX_H
  30. #define REGEX_H
  31. #include "core/vector.h"
  32. #include "core/ustring.h"
  33. #include "core/dictionary.h"
  34. #include "core/reference.h"
  35. #include "core/resource.h"
  36. class RegExNode;
  37. class RegExMatch : public Reference {
  38. OBJ_TYPE(RegExMatch, Reference);
  39. struct Group {
  40. Variant name;
  41. int start;
  42. int length;
  43. };
  44. Vector<Group> captures;
  45. String string;
  46. friend class RegEx;
  47. friend class RegExSearch;
  48. friend class RegExNodeCapturing;
  49. friend class RegExNodeBackReference;
  50. protected:
  51. static void _bind_methods();
  52. public:
  53. String expand(const String& p_template) const;
  54. int get_group_count() const;
  55. Array get_group_array() const;
  56. Array get_names() const;
  57. Dictionary get_name_dict() const;
  58. String get_string(const Variant& p_name) const;
  59. int get_start(const Variant& p_name) const;
  60. int get_end(const Variant& p_name) const;
  61. RegExMatch();
  62. };
  63. class RegEx : public Resource {
  64. OBJ_TYPE(RegEx, Resource);
  65. RegExNode* root;
  66. Vector<Variant> group_names;
  67. String pattern;
  68. int lookahead_depth;
  69. protected:
  70. static void _bind_methods();
  71. public:
  72. void clear();
  73. Error compile(const String& p_pattern);
  74. Ref<RegExMatch> search(const String& p_text, int p_start = 0, int p_end = -1) const;
  75. String sub(const String& p_text, const String& p_replacement, bool p_all = false, int p_start = 0, int p_end = -1) const;
  76. bool is_valid() const;
  77. String get_pattern() const;
  78. int get_group_count() const;
  79. Array get_names() const;
  80. RegEx();
  81. RegEx(const String& p_pattern);
  82. ~RegEx();
  83. };
  84. #endif // REGEX_H