Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] regex cleanup #3627

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix #3615: Fix remove from dictionary with tuple as key
* Fix #3598: Using obj () now generated an empty dict instead of None
* Fix #3597: Do not translate .toString methods to str
* Fix #3610: Cleanup Python regex handling

## 4.6.0 - 2023-11-27

Expand Down
33 changes: 33 additions & 0 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5529,6 +5529,9 @@ let regex
| Some(ExprType(DeclaredTypeFullName Types.regexGroup)) -> true
| _ -> false

let createRegex r t args =
Helper.LibCall(com, "RegExp", "create", t, args, ?loc = r)

match i.CompiledName with
// TODO: Use RegexConst if no options have been passed?
| ".ctor" ->
Expand Down Expand Up @@ -5611,6 +5614,36 @@ let regex
| "get_Count" ->
Helper.GlobalCall("len", t, [ thisArg.Value ], [ t ], ?loc = r) |> Some
| "GetEnumerator" -> getEnumerator com r t thisArg.Value |> Some
| "IsMatch"
| "Match"
| "Matches" as meth ->
match thisArg, args with
| Some thisArg, args ->
if args.Length > 2 then
$"Regex.{meth} doesn't support more than 2 arguments"
|> addError com ctx.InlinePath r

thisArg :: args |> Some
| None, input :: pattern :: args ->
let reg = createRegex None Any (pattern :: args)

[
reg
input
]
|> Some
| _ -> None
|> Option.map (fun args ->
Helper.LibCall(
com,
"RegExp",
Naming.lowerFirst meth,
t,
args,
i.SignatureArgTypes,
?loc = r
)
)
| meth ->
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst

Expand Down
17 changes: 2 additions & 15 deletions src/fable-library-py/fable_library/reg_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,15 @@ def unescape(string: str) -> str:
return re.sub(r"\\(.)", r"\1", string)


def match(reg: Pattern[str] | str, input: str, start_at: int = 0) -> Match[str] | None:
if isinstance(reg, str):
flags = _options_to_flags(start_at)
return re.search(input, reg, flags)
def match(reg: Pattern[str], input: str, start_at: int = 0) -> Match[str] | None:
return reg.search(input, pos=start_at)


def matches(reg: Pattern[str], input: str, start_at: int = 0) -> list[Match[str]]:
if isinstance(reg, str):
flags = _options_to_flags(start_at)
input = input.replace("?<", "?P<")
return list(re.finditer(input, reg, flags=flags))

return list(reg.finditer(input, pos=start_at))


def is_match(reg: Pattern[str] | str, input: str, start_at: int = 0) -> bool:
if isinstance(reg, str):
# Note: input is the pattern here
flags = _options_to_flags(start_at)
return re.search(input, reg, flags=flags) is not None

def is_match(reg: Pattern[str], input: str, start_at: int = 0) -> bool:
return reg.search(input, pos=start_at) is not None


Expand Down
Loading