| 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 | |
| 31 | #ifndef REGEX_H |
| 32 | #define REGEX_H |
| 33 | |
| 34 | #include "core/object/ref_counted.h" |
| 35 | #include "core/string/ustring.h" |
| 36 | #include "core/templates/hash_map.h" |
| 37 | #include "core/templates/vector.h" |
| 38 | #include "core/variant/array.h" |
| 39 | #include "core/variant/dictionary.h" |
| 40 | #include "core/variant/typed_array.h" |
| 41 | |
| 42 | class RegExMatch : public RefCounted { |
| 43 | GDCLASS(RegExMatch, RefCounted); |
| 44 | |
| 45 | struct Range { |
| 46 | int start = 0; |
| 47 | int end = 0; |
| 48 | }; |
| 49 | |
| 50 | String subject; |
| 51 | Vector<Range> data; |
| 52 | HashMap<String, int> names; |
| 53 | |
| 54 | friend class RegEx; |
| 55 | |
| 56 | protected: |
| 57 | static void _bind_methods(); |
| 58 | |
| 59 | int _find(const Variant &p_name) const; |
| 60 | |
| 61 | public: |
| 62 | String get_subject() const; |
| 63 | int get_group_count() const; |
| 64 | Dictionary get_names() const; |
| 65 | |
| 66 | PackedStringArray get_strings() const; |
| 67 | String get_string(const Variant &p_name) const; |
| 68 | int get_start(const Variant &p_name) const; |
| 69 | int get_end(const Variant &p_name) const; |
| 70 | }; |
| 71 | |
| 72 | class RegEx : public RefCounted { |
| 73 | GDCLASS(RegEx, RefCounted); |
| 74 | |
| 75 | void *general_ctx = nullptr; |
| 76 | void *code = nullptr; |
| 77 | String pattern; |
| 78 | |
| 79 | void _pattern_info(uint32_t what, void *where) const; |
| 80 | |
| 81 | protected: |
| 82 | static void _bind_methods(); |
| 83 | |
| 84 | public: |
| 85 | static Ref<RegEx> create_from_string(const String &p_pattern); |
| 86 | |
| 87 | void clear(); |
| 88 | Error compile(const String &p_pattern); |
| 89 | |
| 90 | Ref<RegExMatch> search(const String &p_subject, int p_offset = 0, int p_end = -1) const; |
| 91 | TypedArray<RegExMatch> search_all(const String &p_subject, int p_offset = 0, int p_end = -1) const; |
| 92 | String sub(const String &p_subject, const String &p_replacement, bool p_all = false, int p_offset = 0, int p_end = -1) const; |
| 93 | |
| 94 | bool is_valid() const; |
| 95 | String get_pattern() const; |
| 96 | int get_group_count() const; |
| 97 | PackedStringArray get_names() const; |
| 98 | |
| 99 | RegEx(); |
| 100 | RegEx(const String &p_pattern); |
| 101 | ~RegEx(); |
| 102 | }; |
| 103 | |
| 104 | #endif // REGEX_H |
| 105 | |