Thread overview
[phobos] substring for std.metastrings
Feb 26, 2010
Igor Lesik
Feb 28, 2010
Philippe Sigaud
Feb 28, 2010
Igor Lesik
Mar 01, 2010
Philippe Sigaud
Mar 02, 2010
Igor Lesik
Mar 05, 2010
Igor Lesik
Mar 07, 2010
Igor Lesik
Mar 08, 2010
Sean Kelly
February 26, 2010
I believe, I have a function that is good candidate to be in std.metastrings. Feedback is appreciated.

/**
?* Returns:
?*???????? .found = true if string t found in s,
?*???????? .found = false if string t not found in s
?*/
template substring(string s, string t)
{
??? static if (s == null || t == null || t.length == 0 || s.length == 0) {
??????? const match = false;
??????? const found = false;
??? }
??? else static if (s[0] == t[0])
??? {
??????? const match = true;
??????? static if (t.length == 1)
??????????? const found = true;
??????? else static if (s.length == 1)
??????????? const found = false;
??????? else static if (substring!(s[1..$],t[1..$]).match)
??????????? const found = substring!(s[1..$],t[1..$]).found;
??????? else
??????????? const found = substring!(s[1..$],t[0..$]).found;
??? }
??? else
??? {
??????? const match = false;
??????? const found = substring!(s[1..$],t[0..$]).found;
??? }
}

unittest
{
??? assert(!substring!(null,"fun").found);
??? assert(!substring!("","fun").found);
??? assert(!substring!("fun",null).found);
??? assert(!substring!("fun","").found);
??? assert(!substring!("","").found);
??? assert(substring!("fun with gun","fun").found);
??? assert(substring!("fun with gun","gun").found);
??? assert(!substring!("fun with gun","peace").found);
}



February 28, 2010
(hmm, first time post there. Hello, I'm mostly human and mostly harmless).

On Fri, Feb 26, 2010 at 11:45, Igor Lesik <curoles at yahoo.com> wrote:

> I believe, I have a function that is good candidate to be in
> std.metastrings.
> Feedback is appreciated.
>
>
Hi Igor,

Some remarks:

1- I guess that  when t is the empty string you should return true, not false: the empty string is present in any string. So your first static if should be cut in two.

2- the .found necessary to get the result could be hidden with a two-levels template:

template substring(string s, string t)
{
    enum bool substring = substringImpl!(s,t).found;
}

and rename your substring into substringImpl. Then you can do:

auto s = substring!("fun with gun", "run");

I don't know if you can put substringImpl as a private template inside substring?

3- Maybe you could do a (string s, char c) version which casts the char into a string and look for it.


Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100228/4454f7ab/attachment.htm>
February 28, 2010
Hi Philippe,

thanks for the remarks

>1- I guess that? when t is the empty string you should return true, not false: the empty string is present in any string.
That is true, empty string is present in any string. But usually when I look for an empty substring it happens by mistake. On other hand, returning true is?probably what most people would expect, so I tend to agree with you.

>2- the .found necessary to get the result could be hidden with a two-levels template: auto s = substring!("fun with gun", "run");
I try to comply to the style of std.metastrigns, they do not do it there.
And when I see "auto s = substring!("fun with gun", "run");" I presume s is substring, not
boolean.

>3- Maybe you could do a (string s, char c) version which casts the char into a string and look for it.
Agree.

I would like to hear if people think that substring?deserves to be in std.metastrings. Otherwise all this discussion is pointless.

Thanks,
Igor


?

________________________________
From: Philippe Sigaud <philippe.sigaud at gmail.com>
To: Discuss the phobos library for D <phobos at puremagic.com>
Sent: Sun, February 28, 2010 9:59:50 AM
Subject: Re: [phobos] substring for std.metastrings

(hmm, first time post there. Hello, I'm mostly human and mostly harmless).

On Fri, Feb 26, 2010 at 11:45, Igor Lesik <curoles at yahoo.com> wrote:

I believe, I have a function that is good candidate to be in std.metastrings.
>Feedback is appreciated.
>
>

Hi Igor,

Some remarks:

1- I guess that? when t is the empty string you should return true, not false: the empty string is present in any string. So your first static if should be cut in two.

2- the .found necessary to get the result could be hidden with a two-levels template:

template substring(string s, string t)
{
??? enum bool substring = substringImpl!(s,t).found;
}

and rename your substring into substringImpl. Then you can do:

auto s = substring!("fun with gun", "run");

I don't know if you can put substringImpl as a private template inside substring?

3- Maybe you could do a (string s, char c) version which casts the char into a string and look for it.


Philippe



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100228/307548c1/attachment.htm>


March 01, 2010
> >2- the .found necessary to get the result could be hidden with a
> two-levels template:
> >auto s = substring!("fun with gun", "run");
> I try to comply to the style of std.metastrigns, they do not do it there.
>

They do that for parsing, because you have to 'return' both the value and
the rest and you can't do both the eponymous trick and declare another
'.something' at the same time.
Format becomes the formatted string without an intermediary '.format'
For substring, I think a boolean is the only thing you want. But you may
want to return the rest also. ie, your template consumes the string until it
founds the substring.


> And when I see "auto s = substring!("fun with gun", "run");" I presume s is
> substring, not
> boolean.
>

Yeah me too at first sight, but then you already know the substring: it's
"run"...
You could call it "contains":   auto c = contains!(bigstring, smallstring);


>
> I would like to hear if people think that substring deserves to be in std.metastrings. Otherwise all this discussion is pointless.
>
> You may not have many answers there: they are all rushing to finish D and
the most important librairies like std.concurrency.
You'll have to wait 1-2 months to have them look elsewhere.

Anyway, what usage do you see for substring?

  Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100301/5867d677/attachment.htm>
March 01, 2010
>Anyway, what usage do you see for substring?
I used it to parse mixin strings.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100301/295cced8/attachment.htm>
March 04, 2010
What does substring do?

Andrei

Philippe Sigaud wrote:
> (hmm, first time post there. Hello, I'm mostly human and mostly harmless).
> 
> On Fri, Feb 26, 2010 at 11:45, Igor Lesik <curoles at yahoo.com <mailto:curoles at yahoo.com>> wrote:
> 
>     I believe, I have a function that is good candidate to be in
>     std.metastrings.
>     Feedback is appreciated.
> 
> 
> Hi Igor,
> 
> Some remarks:
> 
> 1- I guess that  when t is the empty string you should return true, not false: the empty string is present in any string. So your first static if should be cut in two.
> 
> 2- the .found necessary to get the result could be hidden with a two-levels template:
> 
> template substring(string s, string t)
> {
>     enum bool substring = substringImpl!(s,t).found;
> }
> 
> and rename your substring into substringImpl. Then you can do:
> 
> auto s = substring!("fun with gun", "run");
> 
> I don't know if you can put substringImpl as a private template inside substring?
> 
> 3- Maybe you could do a (string s, char c) version which casts the char into a string and look for it.
> 
> 
> Philippe
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
March 04, 2010
>What does substring do?

"returns" true if string A found in string B

Igor




March 05, 2010
To me "substring" means "get me the substring from i to j". How about "contains"?

Andrei

Igor Lesik wrote:
>> What does substring do?
> 
> "returns" true if string A found in string B
> 
> Igor
> 
> 
> 
> 
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
March 07, 2010
>>> What does substring do?
>> 
>> "returns" true if string A found in string B

Andrei wrote:
>To me "substring" means "get me the substring from i to j". How about "contains"?


Agree, "contains" is better. (I am afraid "strstr" burned too deeply in my brain structures).

But the main question is still unanswered. Do people think that such a function belongs to std.metastrings?

Thanks,
Igor



March 07, 2010
+1.  It's what I chose for Tango anyway:

http://dsource.org/projects/tango/docs/stable/tango.core.Array.html#contains

On Mar 4, 2010, at 11:11 PM, Andrei Alexandrescu wrote:

> To me "substring" means "get me the substring from i to j". How about "contains"?
> 
> Andrei
> 
> Igor Lesik wrote:
>>> What does substring do?
>> "returns" true if string A found in string B
>> Igor
>>      _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos