-
Notifications
You must be signed in to change notification settings - Fork 182
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
Replace substring in character/string_type #366
Comments
This has also been requested a few times before. @zbeekman wrote in #69 (comment):
@jacobwilliams suggested it also in j3-fortran/fortran_proposals#96 (comment):
|
There is also a proposal from the string thread:
Originally proposed by @zbeekman in #69 (comment) |
We have to be a bit careful with the string related patches, because there are only a few modules and many independent features. This will likely result in many conflicting patches and will require a lot of rebasing along the way. The module to put those functions in would be |
What would we expect the function to return if the input is Option 2 is inspired from the fact that the second |
features I would like would be to
|
I also support the options suggested by @urbanjost:
but I would prefer to split them into two functions. |
Function introduced by @urbanjost can work as an all in one pack for any kind of "replace" operation that a user wants to execute. But I have a doubt, in deciding the Nth occurrence.
|
Python uses a non-overlapping count: >>> "abababababa".replace("aba","zzz")
'zzzbzzzbzzz'
>>> "abababababa".replace("aba","zzz",1)
'zzzbabababa'
>>> "abababababa".replace("aba","zzz",2)
'zzzbzzzbaba' which corresponds to your second bullet point. Edit: The StringiFor library also provides a |
If everyone is fine with the idea. |
Feel free to start working on a patch, @ChetanKarwa . You can either start from the default branch and we will figure out rebasing the patch later, or you could base your feature branch on the patch in #343, which already creates the Let me know if you need any help with this. |
I wanted to ask these questions:
|
I suggest you omit
The first one. (For the second version you would need first a string class (#333) which is not available yet. Moreover, the second version would likely just call the first version.) More importantly I think If KMP is not too challenging I think that would be a great start. I guess in practice there will be some trade-offs (e.g. depending on the string sizes, cache size, etc.). |
I meant to mention not to consider the "cmd" option. I consider the other functionality desirable (ignore case of the old string, replace right to left as well as left to right, some type of selection of which occurrence(s) to replace). The replace() function mentioned has a lot of history, and was originally part of a line editor so "cmd" preceded having an old A KMP implementation would potentially be reusable for other functions ( I would be curious to time it against INDEX(3f) in different compilers too) as well, and seems worth implementing. I would be curious if anyone has a known use case for processing large amounts of data with replace(3f) or for searching for words efficiently in general in other proposed stdlib procedures. I would implement a request to search from right to left with a logical called BACK to be consistent with intrinsics such as INDEX(3f), VERIFY(3f), and SCAN(3f) for consistency instead of using the sign of the occurrence. |
An implementation of "replace all" should be careful with overlapping strings. Consider the following example with @aman-godara 's option 2: If the replacing string is In addition to the above examples from other languages, I would like to point out the command
I find it very convenient in my Tcl programs and I could probably use it in Fortran as well. |
I didn't get this part, would anyone mind explaining with a proper example of the use of function replace |
I did not mean to suggest that the current round should include a Fortran
version of this functionality ;). Just wanted to add it to the wishlist.
Op wo 31 mrt. 2021 om 14:12 schreef ChetanKarwa ***@***.***>:
… In addition to the above examples from other languages, I would like to
point out the command string map from Tcl:
set a "A B C A"
string map {"A" "AA" "B" "BB"} $a results in AABBCAA and care is taken to
avoid overlapping substitutions.
I didn't get this part, would anyone mind explaining with a proper example
of the use of function replace
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#366 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN6YRYVLQN5DJBRTB2WFLDTGMGSNANCNFSM4Z5JHY4A>
.
|
You can find a complete explanation in the Tcler wiki: https://wiki.tcl-lang.org/page/string+map Translated to Fortran it might look like
It is a more powerful version of replace which allows to map multiple substrings in one pass. The rule to prevent overlap issues is
But this should go to a future PR. |
although TCL calls them keys and values think of the map list as just a set of old1,new1 old2,new2, old3,new3 so that would be |
I wish to work on this issue. Please assign this to me. Also, we have intrinsic |
Maybe there is a better name like |
Since we always put a low-level functionality forward first for a new functionality that we wish to add to But tcl's Taking example from @urbanjost's comment. |
I just tried your example and the answer is option 2.
Op di 15 jun. 2021 om 21:27 schreef Aman Godara ***@***.***>:
… Since we always put a low-level functionality forward first for a new
functionality that we wish to add to stdlib. I decided to implement
replace_all.
I personally try to design these low-level functionality such that all
possible high-level operations can still be done using a combination of
many low-level functionalities. For eg: I decided to not add pos argument
at this moment because the same operation can be done using a combination
of slice (already added to stdlib) and find (under review).
But I added replace_overlapping feature to replace_all because this
cannot be achieved in any other way.
But *tcl*'s replace_map functionality is still not added in this
replace_all.
I wonder what would the output be for *Tcl*'s replace_map function if the
input string is "wababw" and the input map is {"abab" -> "pqrs", "ba" ->
"ds"}
will it be
Option 1). "wpqrsdsw"
Option 2). "wpqrsw"?
Taking example from @urbanjost <https://github.com/urbanjost>'s comment
<#366 (comment)>
.
I think one possible hacky way of replacing "AB" using the map {"A" ->
"B", "B" -> "XXX"} would be to replace "A" with a dummy character let's say
"$" and "B" with let's say "#" and do the normal replace_all call twice
i.e. once for each of the two replacements. This will give us "$#". Now
replace "$" with "B" and then "#" with "XXX", again by calling replace_all
twice. So at the end we will have "BXXX". In short, hacky solution first
takes the input to an intermediate stage and from this intermediate stage
it takes to the final solution.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#366 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN6YR2KCZH6HYNDOPPTOKTTS6SR5ANCNFSM4Z5JHY4A>
.
|
I tried an online compiler for Tcl (by tutorials point) to understand more about the behaviour of the function. Here is the code: set a "wababw"
set a [string map {"abab" "ppp" "abw" "tt"} $a]
puts $a output is: It confirmed that overlapping substitutions are avoided. Also, one thing to note is when I tried set a "a b c a"
set a [string map {"a" "aa" "b" "bb"} $a]
puts $a Output retained the spaces: Conclusion: But IMO there can be other ways to deliver |
I also propose to add a few other functions that can be commonly used.
Replace function, this can be implemented in several ways:
We already have an operator to Concat a string with another but I feel we should also implement a function for the same.
The community can have a discussion over the need for such functions and if needed also suggest some other implementation of Replace function.
Originally posted by @ChetanKarwa in #364 (comment)
The text was updated successfully, but these errors were encountered: