Skip to content

Commit

Permalink
fix: 🐛 match any character between func name and ( (#495)
Browse files Browse the repository at this point in the history
* fix: 🐛 match any character between func name and `(`

* refactor: ♻️ use non-capturing group

* fix: 🐛 match white space before `(`

* fix: Ignore comments

* refactor: 🔥 removed redundant whitespace token

moved the regex string to the top and added a description

* fix: 🐛 match space before `(`

and word boundary for the method name

* refactor: ♻️ removed `(?<!#)`

It only handled one specific case, `#func`. Something like `# ` was still matched. To avoid giving a false impression, I removed it, and this case is now also handled by `is_top_level_func()`.

* fix: 🐛 more word boundaries
  • Loading branch information
KANAjetzt authored Jan 24, 2025
1 parent 64bcd83 commit 0e5dae7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion addons/mod_loader/internal/mod_hook_preprocessor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const HASH_COLLISION_ERROR := \
const MOD_LOADER_HOOKS_START_STRING := \
"\n# ModLoader Hooks - The following code has been automatically added by the Godot Mod Loader."

## \\bfunc\\b\\s+ -> Match the word 'func' and one or more whitespace characters
## \\b%s\\b -> the function name
## (?:.*\\n*)*?\\s*\\( -> Match any character between zero and unlimited times, but be lazy
## and only do this until a '(' is found.
const REGEX_MATCH_FUNC_WITH_WHITESPACE := "\\bfunc\\b\\s+\\b%s\\b(?:.*\\n*)*?\\s*\\("

## finds function names used as setters and getters (excluding inline definitions)
## group 2 and 4 contain the xetter names
Expand Down Expand Up @@ -338,7 +343,7 @@ func match_method_body(method_name: String, func_body_start_index: int, text: St

static func match_func_with_whitespace(method_name: String, text: String, offset := 0) -> RegExMatch:
# Dynamically create the new regex for that specific name
var func_with_whitespace := RegEx.create_from_string("func\\s+%s[\\\\\\s]*\\(" % method_name)
var func_with_whitespace := RegEx.create_from_string(REGEX_MATCH_FUNC_WITH_WHITESPACE % method_name)
return func_with_whitespace.search(text, offset)


Expand Down

0 comments on commit 0e5dae7

Please sign in to comment.