How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.
import std;
void main(){
writeln("Hello World!"[0..this.indexOf("o")]);
}
Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 13, 2021 How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.
|
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Friday, 13 August 2021 at 21:05:22 UTC, Marcone wrote: >How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.
this does not exist (and see few reason for) but algo + ufcs allows this easily, e.g
bonus: both can throw bound error |
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to user1234 | On Friday, 13 August 2021 at 21:14:29 UTC, user1234 wrote: >On Friday, 13 August 2021 at 21:05:22 UTC, Marcone wrote: >How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.
this does not exist (and see few reason for) but algo + ufcs allows this easily, e.g
bonus: both can throw bound error My example was just an example. I don't want this solution. I want to have the power to handle the string inside the square brackets. |
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On 8/13/21 5:05 PM, Marcone wrote: >How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you. import std; void main(){ There is no string class to extend.
-Steve |
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 13 August 2021 at 21:47:22 UTC, Steven Schveighoffer wrote: >On 8/13/21 5:05 PM, Marcone wrote: >How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you. import std; void main(){ There is no string class to extend.
-Steve Isn't there some unario operator template that I can use with lambda to handle a string literal? |
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote: >Isn't there some unario operator template that I can use with lambda to handle a string literal? So, something other than an exact "lit"[0..this.xx(..)] syntax is fine? What didn't you like about What is a real example of something you want to do? |
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to jfondren | On 8/13/21 4:08 PM, jfondren wrote: > On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote: >> >> Isn't there some unario operator template that I can use with lambda to handle a string literal? > > So, something other than an exact "lit"[0..this.xx(..)] syntax is fine? > > What didn't you like about `"Hello World!".findSplit("o")[0].writeln;` then? > > What is a real example of something you want to do? And I started writing the following but stopped because the semantics are not clear. I first called it 'between' but then should the 'o' that was searched be a part of the output? Should "from 'o' to 'o'" produce an empty string, should it include a single 'o' or should it go all the way to the next 'o'? What about the last line which says "from 'd' to 'a'"? Is that an entirely empty range or just 'd' or 'd' till the end? I don't think the programming language can decide one way or the other. import std.algorithm; import std.range; import std.stdio; auto inclusive(R, E)(R range, E fromNeedle, E toNeedle) { auto found = range.find(fromNeedle); return chain(found.front.only, found.drop(1).findSplitAfter(only(toNeedle))[0]); } void main() { const s = "Hello World!"; auto r = s.inclusive('o', 'o'); writeln(r); writeln("abcdef".inclusive('d', 'a')); } Ali |
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to jfondren | On Friday, 13 August 2021 at 23:08:07 UTC, jfondren wrote: >On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote: >Isn't there some unario operator template that I can use with lambda to handle a string literal? So, something other than an exact "lit"[0..this.xx(..)] syntax is fine? What didn't you like about What is a real example of something you want to do? writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]); indexOf()is just a simple example, not the goal. I want handle literal inside [] like it bellow, but in literal: string x = "Hello World!"; |
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Friday, 13 August 2021 at 23:23:55 UTC, Marcone wrote: >On Friday, 13 August 2021 at 23:08:07 UTC, jfondren wrote: >On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote: >Isn't there some unario operator template that I can use with lambda to handle a string literal? So, something other than an exact "lit"[0..this.xx(..)] syntax is fine? What didn't you like about What is a real example of something you want to do? writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]); indexOf()is just a simple example, not the goal. I want handle literal inside [] like it bellow, but in literal: string x = "Hello World!";
|
August 13, 2021 Re: How to extend the string class to return this inside the square bracket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Friday, 13 August 2021 at 23:23:55 UTC, Marcone wrote: >writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]); indexOf()is just a simple example, not the goal. I want handle literal inside [] like it bellow, but in literal: string x = "Hello World!"; You can use the
|