-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathRegExpGroup.gd
62 lines (47 loc) · 1.46 KB
/
RegExpGroup.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Utility class that provides utility to quickly create regex objects and
# replace text with multiple regular expressions.
class_name RegExpGroup
extends Reference
static func compile(pattern: String) -> RegEx:
var regex := RegEx.new()
regex.compile(pattern)
return regex
static func collection(patterns: Dictionary) -> RegExCollection:
return RegExCollection.new(patterns)
class RegExCollection:
var _regexes := {}
var _current_index := 0
var _current_array := []
func _init(regexes: Dictionary) -> void:
for pattern in regexes:
var replacement: String = regexes[pattern]
var regex := RegEx.new()
regex.compile(pattern)
_regexes[regex] = replacement
_current_array = _regexes.keys()
func replace(text: String) -> String:
for regex in _regexes:
var replacement: String = _regexes[regex]
text = regex.sub(text, replacement, true)
return text
func _iterator_is_valid() -> bool:
return _current_index < _current_array.size()
func _iter_init(_arg) -> bool:
_current_index = 0
_current_array = _regexes.keys()
return _iterator_is_valid()
func _iter_next(_arg) -> bool:
_current_index += 1
return _iterator_is_valid()
func _iter_get(_arg):
return current()
func size() -> int:
return _regexes.size()
func keys() -> Array:
return _regexes.keys()
func values() -> Array:
return _regexes.values()
func current() -> RegEx:
return _current_array[_current_index]
func currentReplacement() -> String:
return _regexes[current()]