-
Notifications
You must be signed in to change notification settings - Fork 90
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
Escape backslashes in search and replacement #592
base: staging
Are you sure you want to change the base?
Conversation
Wow! Tested and this works. Thanks! 🙌 |
@lens0021 the tests doesn't work. Can you check what is happening or you need help? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests does't work
This comment was marked as resolved.
This comment was marked as resolved.
So I think that the bash generated is not very ok.
This is an example of the bash generated for the This amber code: |
@lens0021 @Mte90 It seems that only the
It suddenly works. |
But still these two tests fail:
I can't investigate this problem further now. Maybe later today or tomorrow I can play around it |
the tests are still failing |
There are some changes on bash 5.2 $ docker run --rm -v $PWD:/tmp bash:5.1.16 bash /tmp/replace_once.sh
Succinctly\
$ docker run --rm -v $PWD:/tmp bash:5.2.0 bash /tmp/replace_once.sh
Succeeded I will try to write a compatible code... |
src/std/text.ab
Outdated
// TODO: Revisit after https://github.com/amber-lang/amber/issues/632 | ||
const bash_major = trust $ echo \"\$\{BASH_VERSINFO[0]}\" $ as Num | ||
const bash_minor = trust $ echo \"\$\{BASH_VERSINFO[1]}\" $ as Num | ||
if bash_major > 5 or (bash_major == 5 and bash_minor > 1) { | ||
replace = trust $ echo \$\{{nameof replace}//\\\\/\\\\\\\\} $ | ||
} else { | ||
replace = trust $ echo \$\{{nameof replace}//\\\\/\\\\} $ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is dirty. When #622 is resolved, this could be improved.
Now the both versions are supported. $ cargo run -- build src/tests/stdlib/replace_once.ab src/tests/stdlib/replace_once.sh
Compiling amber v0.3.5-alpha (/usr/local/git/amber)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.52s
Running `target/debug/amber build src/tests/stdlib/replace_once.ab src/tests/stdlib/rep
lace_once.sh`
$ docker run --rm -v $PWD:/tmp bash:5.1.16 bash /tmp/src/tests/stdlib/replace_once.sh
Succeeded
$ docker run --rm -v $PWD:/tmp bash:5.2.0 bash /tmp/src/tests/stdlib/replace_once.sh
Succeeded |
|
||
main { | ||
echo replace("banana banana", "banana", "apple") | ||
echo replace("\\", "\\", "apple") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To confirm that replace
replaces multiple instances, may I suggest testing multiple occurrences of the search text:
echo replace("\\ \\", "\\", "apple")
echo replace("\\banana\\", "\\", "\\\\")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might also consider cloning the modified test, and overwriting replace_once.ab, with adjusted expected output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests for replace_once and edited text.ab to make the test pass, though I did not know why it was broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not entirely happy with these tests:
- I had in mind that both test files would contain calls to
replace
andreplace_once
, with identical parameters, to show the differences in behaviour. - Each function call should test a specific use case; they don't all have to contain backslashes!
- Each function call should contain multiple copies of the search text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example:
import * from "std/text"
// Output
// TWO TWO TWO
// a\\b\\c\\d
// first..second..third..fourth
main {
echo replace("one one one", "one", "TWO")
echo replace("a\\b\\c\\d", "\\", "\\\\")
echo replace("first\nsecond\nthird\nfourth", "\n", "..")
}
Co-authored-by: Huw Walters <[email protected]>
|
pub fun replace(source, search, replace) { | ||
return "\$\{source//{search}/{replace}}" | ||
search = trust $ echo \$\{{nameof search}//\\\\/\\\\\\\\} $ | ||
// TODO: Revisit after https://github.com/amber-lang/amber/issues/622 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this comment would be better placed above the bash_version
function definition, as it's defined once but called multiple times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, now you've moved the BASH_VERSINFO
test to a helper function, you no longer need the allow_absurd_cast
annotation here.
|
||
main { | ||
echo replace("banana banana", "banana", "apple") | ||
echo replace("\\", "\\", "apple") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not entirely happy with these tests:
- I had in mind that both test files would contain calls to
replace
andreplace_once
, with identical parameters, to show the differences in behaviour. - Each function call should test a specific use case; they don't all have to contain backslashes!
- Each function call should contain multiple copies of the search text.
|
||
main { | ||
echo replace("banana banana", "banana", "apple") | ||
echo replace("\\", "\\", "apple") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example:
import * from "std/text"
// Output
// TWO TWO TWO
// a\\b\\c\\d
// first..second..third..fourth
main {
echo replace("one one one", "one", "TWO")
echo replace("a\\b\\c\\d", "\\", "\\\\")
echo replace("first\nsecond\nthird\nfourth", "\n", "..")
}
main { | ||
echo replace_once("Succinctly", "inctly", "eeded") | ||
echo replace_once("Succinctly\\", "inctly\\", "eeded") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example:
import * from "std/text"
// Output
// TWO one one
// a\\b\c\d
// first..second
// third
// fourth
main {
echo replace_once("one one one", "one", "TWO")
echo replace_once("a\\b\\c\\d", "\\", "\\\\")
echo replace_once("first\nsecond\nthird\nfourth", "\n", "..")
}
Fixes #589.