www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - .ends .starts .contains

reply Martin <Martin_member pathlink.com> writes:
I was just thinking, that it would be useful to have:

char []S;

if(S.ends(".com")){....

tha same with .starts .contains.
I need this quite often, I must say.

What you think?
Feb 09 2005
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Martin wrote:

 I was just thinking, that it would be useful to have:
 
 char []S;
 
 if(S.ends(".com")){....
 
 tha same with .starts .contains.
 I need this quite often, I must say.
 
 What you think?

Sounds like functions, not properties... Such as the already existing "std" ones, that could probably be used or modified ? import std.string;
 /*************************************
  * Find first occurrance of sub[] in string s[].
  * Return index in s[] where it is found.
  * Return -1 if not found.
  */
 
 int find(char[] s, char[] sub);

 /*************************************
  * Find last occurrance of sub in string s.
  * Return index in s where it is found.
  * Return -1 if not found.
  */
 
 int rfind(char[] s, char[] sub);

Strings in D are arrays, and not classes... This mean they use functions, not methods. As opposed to, for instance this class method: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#endsWith(java.lang.String) --anders
Feb 09 2005
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Anders F Björklund" <afb algonet.se> wrote in message
news:cud27p$2ac2$1 digitaldaemon.com...
 Martin wrote:

 I was just thinking, that it would be useful to have:

 char []S;

 if(S.ends(".com")){....

 tha same with .starts .contains.
 I need this quite often, I must say.

 What you think?

Sounds like functions, not properties... Such as the already existing "std" ones, that could probably be used or modified ? import std.string;
 /*************************************
  * Find first occurrance of sub[] in string s[].
  * Return index in s[] where it is found.
  * Return -1 if not found.
  */

 int find(char[] s, char[] sub);

 /*************************************
  * Find last occurrance of sub in string s.
  * Return index in s where it is found.
  * Return -1 if not found.
  */

 int rfind(char[] s, char[] sub);

Strings in D are arrays, and not classes... This mean they use functions, not methods.

But in D char[] can use functions as if it were a property. So we can use int ends(char[] str1, char[] str2) like this: str1.ends(".com");
 As opposed to, for instance this class method:

lang.String)
 --anders

Feb 09 2005
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Ivan Senji wrote:

Sounds like functions, not properties...

Strings in D are arrays, and not classes...
This mean they use functions, not methods.

But in D char[] can use functions as if it were a property. So we can use int ends(char[] str1, char[] str2) like this: str1.ends(".com");

Yeah, that is part of the "make-believe" features. Just like bool. Thanks for pointing it out, as it does make the code look nicer... --anders
Feb 09 2005
prev sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
 But in D char[] can use functions as if it were a property.
 So we can use int ends(char[] str1, char[] str2) like this:
 str1.ends(".com");

I don't think that's part of the language spec is it? It's an accident that it works and possibly even considered a bug that Walter hasn't fixed yet.
Feb 09 2005
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Ben Hinkle wrote:

But in D char[] can use functions as if it were a property.
So we can use int ends(char[] str1, char[] str2) like this:
str1.ends(".com");

I don't think that's part of the language spec is it? It's an accident that it works and possibly even considered a bug that Walter hasn't fixed yet.

Pretty sweet "bug", though? But even without the syntactic sugar, it is: ends(str1,".com"); Still no need for any extra properties. --anders
Feb 09 2005
prev sibling parent reply "Carlos Santander B." <csantander619 gmail.com> writes:
Ben Hinkle wrote:
But in D char[] can use functions as if it were a property.
So we can use int ends(char[] str1, char[] str2) like this:
str1.ends(".com");

I don't think that's part of the language spec is it? It's an accident that it works and possibly even considered a bug that Walter hasn't fixed yet.

Yeah, I wouldn't count on that "feature" either. _______________________ Carlos Santander Bernal
Feb 09 2005
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Carlos Santander B." <csantander619 gmail.com> wrote in message
news:cue6g8$g43$2 digitaldaemon.com...
 Ben Hinkle wrote:
But in D char[] can use functions as if it were a property.
So we can use int ends(char[] str1, char[] str2) like this:
str1.ends(".com");

I don't think that's part of the language spec is it? It's an accident


 it works and possibly even considered a bug that Walter hasn't fixed



Yeah, I wouldn't count on that "feature" either.

Walter please reply that this IS a feature and not a bug. And if it is a bug please don't remove it and add it to the spec. :)
 _______________________
 Carlos Santander Bernal

Feb 10 2005
prev sibling next sibling parent reply Nick <Nick_member pathlink.com> writes:
As Ivan mentioned, for arrays you can use functions directly as properties.

So, you can just define

# bool ends(char[] s, char[] p)
# {
#    return (p.length <= s.length) && (s[length-p.length..length] == p);
# }
#
# bool starts(char[] s, char[] p)
# {
#    return (p.length <= s.length) && (s[0..p.length] == p);
# }
#
# bool contains(char[] s, char[] p)
# {
#    return s.find(p) != -1;
# }

and you're done!

Nick

In article <cud0n0$28vh$1 digitaldaemon.com>, Martin says...
I was just thinking, that it would be useful to have:

char []S;

if(S.ends(".com")){....

tha same with .starts .contains.
I need this quite often, I must say.

What you think?

Feb 09 2005
parent reply "Charles" <no email.com> writes:
I think getting this for all types would be awesome what do ya'll think ?

Charlie

"Nick" <Nick_member pathlink.com> wrote in message
news:cuddbn$2nio$1 digitaldaemon.com...
 As Ivan mentioned, for arrays you can use functions directly as

 So, you can just define

 # bool ends(char[] s, char[] p)
 # {
 #    return (p.length <= s.length) && (s[length-p.length..length] == p);
 # }
 #
 # bool starts(char[] s, char[] p)
 # {
 #    return (p.length <= s.length) && (s[0..p.length] == p);
 # }
 #
 # bool contains(char[] s, char[] p)
 # {
 #    return s.find(p) != -1;
 # }

 and you're done!

 Nick

 In article <cud0n0$28vh$1 digitaldaemon.com>, Martin says...
I was just thinking, that it would be useful to have:

char []S;

if(S.ends(".com")){....

tha same with .starts .contains.
I need this quite often, I must say.

What you think?


Feb 09 2005
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Charles" <no email.com> wrote in message
news:cudidc$2sht$1 digitaldaemon.com...
 I think getting this for all types would be awesome what do ya'll think ?

It works for all array types. I had a strong feeling that it was documented by now, but can't seem to find it anywhere. And i don't think it is a bug. A long time ago i used this syntax with templates and something didn't work (don't remember what exactly, something with aliases) and Walter fixed that and didn't mention that this syntax is wrong. It would be nice if it was in the spec.
 Charlie

 "Nick" <Nick_member pathlink.com> wrote in message
 news:cuddbn$2nio$1 digitaldaemon.com...
 As Ivan mentioned, for arrays you can use functions directly as

 So, you can just define

 # bool ends(char[] s, char[] p)
 # {
 #    return (p.length <= s.length) && (s[length-p.length..length] == p);
 # }
 #
 # bool starts(char[] s, char[] p)
 # {
 #    return (p.length <= s.length) && (s[0..p.length] == p);
 # }
 #
 # bool contains(char[] s, char[] p)
 # {
 #    return s.find(p) != -1;
 # }

 and you're done!

 Nick

 In article <cud0n0$28vh$1 digitaldaemon.com>, Martin says...
I was just thinking, that it would be useful to have:

char []S;

if(S.ends(".com")){....

tha same with .starts .contains.
I need this quite often, I must say.

What you think?



Feb 09 2005
parent reply Nick <Nick_member pathlink.com> writes:
In article <cudsrs$58o$1 digitaldaemon.com>, Ivan Senji says...
"Charles" <no email.com> wrote in message
news:cudidc$2sht$1 digitaldaemon.com...
 I think getting this for all types would be awesome what do ya'll think ?

It works for all array types. I had a strong feeling that it was documented by now, but can't seem to find it anywhere. And i don't think it is a bug. A long time ago i used this syntax with templates and something didn't work (don't remember what exactly, something with aliases) and Walter fixed that and didn't mention that this syntax is wrong. It would be nice if it was in the spec.

If this becomes part of the spec, is there any good reason why it shouldn't work for other (non-array) types as well, such as ints and doubles? I can see that structs and classes would be a problem though. Nick
Feb 09 2005
parent reply "Charles" <no email.com> writes:
 If this becomes part of the spec, is there any good reason why it

 for other (non-array) types as well, such as ints and doubles?

Yes I think if we have it for arrays it should work for all types, I wonder what trouble ( if any ) this would cause for structs / classes etc ? Charlie "Nick" <Nick_member pathlink.com> wrote in message news:cue2bc$bgo$1 digitaldaemon.com...
 In article <cudsrs$58o$1 digitaldaemon.com>, Ivan Senji says...
"Charles" <no email.com> wrote in message
news:cudidc$2sht$1 digitaldaemon.com...
 I think getting this for all types would be awesome what do ya'll think



It works for all array types. I had a strong feeling that it was


by now,
but can't seem to find it anywhere. And i don't think it is a bug. A long
time ago
i used this syntax with templates and something didn't work (don't


what exactly, something with aliases) and Walter fixed that and didn't
mention
that this syntax is wrong.

It would be nice if it was in the spec.

If this becomes part of the spec, is there any good reason why it

 for other (non-array) types as well, such as ints and doubles? I can see

 structs and classes would be a problem though.

 Nick

Feb 09 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 9 Feb 2005 16:31:54 -0600, Charles <no email.com> wrote:
 If this becomes part of the spec, is there any good reason why it

 for other (non-array) types as well, such as ints and doubles?

Yes I think if we have it for arrays it should work for all types, I wonder what trouble ( if any ) this would cause for structs / classes etc ?

I think it's workable, after all it's only syntactic sugar. I can see possible collisions i.e. the class has a member 'foo' and a function 'foo' in this form exists. I believe the current method/function resolution system will pick the class method, this could cause bugs/confusion where the programmer meant the free function. I'm not sure it's possible to detect it and error without changing the resolution system, which seems unlikely to me. Regan
 Charlie


 "Nick" <Nick_member pathlink.com> wrote in message
 news:cue2bc$bgo$1 digitaldaemon.com...
 In article <cudsrs$58o$1 digitaldaemon.com>, Ivan Senji says...
"Charles" <no email.com> wrote in message
news:cudidc$2sht$1 digitaldaemon.com...
 I think getting this for all types would be awesome what do ya'll  



It works for all array types. I had a strong feeling that it was


by now,
but can't seem to find it anywhere. And i don't think it is a bug. A  

time ago
i used this syntax with templates and something didn't work (don't


what exactly, something with aliases) and Walter fixed that and didn't
mention
that this syntax is wrong.

It would be nice if it was in the spec.

If this becomes part of the spec, is there any good reason why it

 for other (non-array) types as well, such as ints and doubles? I can see

 structs and classes would be a problem though.

 Nick


Feb 09 2005
prev sibling next sibling parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Martin" <Martin_member pathlink.com> wrote in message 
news:cud0n0$28vh$1 digitaldaemon.com...
I was just thinking, that it would be useful to have:

 char []S;

 if(S.ends(".com")){....

 tha same with .starts .contains.
 I need this quite often, I must say.

 What you think?

Whether or not one likes C++, it's hard to deny the wisdom of B.S.'s philosphy that if something can be adequately incorporated in a library, it should not be a language feature.
Feb 09 2005
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
Hi, Martin,

Instead of
 char []S;
 if(S.ends(".com")){....

what about just this: if( isLike( S, "*.com" ) ) .... if( isLike( S, "http://*" ) ) .... Source of isLike below //| //| isLike - simple pattern match function for the D //| Supports '*' (any substring) and '?' (any one char) in patterns //| //| Andrew Fedoniouk Terra Informatica //| bit isLike(char[] text, char[] pattern) { const char AnySubString = '*'; const char AnyOneChar = '?'; char *str = text; char *str_end = str + text.length; char *pat = pattern; char *pat_end = pat + pattern.length; char *wildcard = null; char *str_pos = null; while(true) { if (*pat == AnySubString) { wildcard = ++pat; str_pos = str; } else if (str == str_end) { return pat == pat_end; } else if (pat < pat_end && (*pat == *str || *pat == AnyOneChar)) { ++str; ++pat; } else if (wildcard) { str = ++str_pos; pat = wildcard; } else { return false; } } }
Feb 09 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
//|
//| like - simple pattern match function for the D
//| Supports '*' (any substring) and '?' (any one char) in patterns
//|
//| Andrew Fedoniouk   Terra Informatica
//|

bit like(char[] text, char[] pattern)
{
    const char AnySubString = '*';
    const char AnyOneChar = '?';

    char *str = text;
    char *str_end = str + text.length;
    char *pat = pattern;
    char *pat_end = pat + pattern.length;

    char *wildcard = null;
    char *str_pos = null;

    while(true)
    {
        if (*pat == AnySubString) {
            wildcard = ++pat;
            str_pos = str;
        } else if (str == str_end) {
            return pat == pat_end;
        } else if (pat < pat_end && (*pat == *str || *pat == AnyOneChar)) {
            ++str;
            ++pat;
        } else if (wildcard) {
            str = ++str_pos;
            pat = wildcard;
        } else {
            return false;
        }
    }
}

unittest
{
     debug(string) printf("std.string.like.unittest\n");
     assert(like("foobar", "foo*"));
     assert(like("foobar", "*bar"));
     assert( !like("", "foo*"));
     assert( !like("", "*bar"));
     assert( !like("", "?"));
     assert( like("f", "?"));
     assert( like("f", "*"));
     assert( like("foo", "?oo"));
     assert( !like("foobar", "?oo"));
     assert( like("foobar", "?oo*"));
     assert( like("foobar", "*oo*b*"));
     assert( like("foobar", "*oo*ar"));
     assert( like("terrainformatica.com", "*.com"));
}
Feb 09 2005