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

[BUG] Standard library replace_regex function fails to replace slash characters #481

Closed
hdwalters opened this issue Sep 15, 2024 · 4 comments · Fixed by #487
Closed

[BUG] Standard library replace_regex function fails to replace slash characters #481

hdwalters opened this issue Sep 15, 2024 · 4 comments · Fixed by #487
Assignees
Labels
bug Something isn't working

Comments

@hdwalters
Copy link
Contributor

Describe the bug
The standard library replace_regex function calls sed internally, with command s/.../.../, but if the search or replace string itself contains /, this causes a sed syntax error.

To Reproduce
Reproduce with one liner command:

$ amber -e 'import * from "std/text"; echo replace_regex("/path/to/file.txt", "/", "#", true)'
sed: -e expression #1, char 0: no previous regular expression

Expected behavior
It should behave like this:

$ amber -e 'import * from "std/text"; echo replace_regex("/path/to/file.txt", "/", "#", true)'
#path#to#file.txt
@hdwalters hdwalters added the bug Something isn't working label Sep 15, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Sep 15, 2024
@hdwalters
Copy link
Contributor Author

I already have a workaround for this, which is to make replace_regex escape slash characters:

pub fun replace_regex(source: Text, search: Text, replace: Text, extended: Bool = false): Text {
    unsafe {
        search = replace(search, "/", "\/")
        replace = replace(replace, "/", "\/")
        ...

@hdwalters
Copy link
Contributor Author

Note, if slash characters are already escaped, this may have slightly unexpected behaviour:

$ amber -e 'import * from "std/text"; echo replace_regex("/path/to/file.txt", "\/", "#", true)'
/path/to/file.txt

@Mte90
Copy link
Member

Mte90 commented Sep 17, 2024

sed can use any symbol as separator so you can do s#something#any# and it will works.

@hdwalters
Copy link
Contributor Author

hdwalters commented Sep 19, 2024

sed can use any symbol as separator so you can do s#something#any# and it will works.

Yes I'm aware of that. Unfortunately we can't guarantee that callers of this stdlib function will not want to use "#" (or any other character we might pick) in the search or replace string; so it doesn't really give us anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants