digitalmars.D - .ends .starts .contains
- Martin (6/6) Feb 09 2005 I was just thinking, that it would be useful to have:
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (10/34) Feb 09 2005 Sounds like functions, not properties...
- Ivan Senji (7/40) Feb 09 2005 But in D char[] can use functions as if it were a property.
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (4/12) Feb 09 2005 Yeah, that is part of the "make-believe" features. Just like bool.
- Ben Hinkle (2/5) Feb 09 2005 I don't think that's part of the language spec is it? It's an accident t...
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (4/10) Feb 09 2005 Pretty sweet "bug", though? But even without the syntactic sugar,
- Carlos Santander B. (4/13) Feb 09 2005 Yeah, I wouldn't count on that "feature" either.
- Ivan Senji (7/20) Feb 10 2005 that
- Nick (19/25) Feb 09 2005 As Ivan mentioned, for arrays you can use functions directly as properti...
- Charles (5/35) Feb 09 2005 I think getting this for all types would be awesome what do ya'll think ...
- Ivan Senji (11/53) Feb 09 2005 It works for all array types. I had a strong feeling that it was documen...
- Nick (5/17) Feb 09 2005 If this becomes part of the spec, is there any good reason why it should...
- Charles (11/34) Feb 09 2005 Yes I think if we have it for arrays it should work for all types, I won...
- Regan Heath (9/50) Feb 09 2005 I think it's workable, after all it's only syntactic sugar.
- Matthew (5/11) Feb 09 2005 Whether or not one likes C++, it's hard to deny the wisdom of B.S.'s
- Andrew Fedoniouk (44/46) Feb 09 2005 what about just this:
- Andrew Fedoniouk (51/51) Feb 09 2005 //|
- kkk (1/1) Apr 26 2009 but it lose out Error: undefined identifier SysAllocString,where is the ...
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
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
"Anders F Björklund" <afb algonet.se> wrote in message news:cud27p$2ac2$1 digitaldaemon.com...Martin 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 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
Ivan Senji wrote: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... --andersSounds 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");
Feb 09 2005
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
Ben Hinkle wrote:Pretty sweet "bug", though? But even without the syntactic sugar, it is: ends(str1,".com"); Still no need for any extra properties. --andersBut 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
Ben Hinkle wrote:Yeah, I wouldn't count on that "feature" either. _______________________ Carlos Santander BernalBut 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
"Carlos Santander B." <csantander619 gmail.com> wrote in message news:cue6g8$g43$2 digitaldaemon.com...Ben Hinkle wrote:thatBut 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 accidentyet.it works and possibly even considered a bug that Walter hasn't fixedWalter 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. :)Yeah, I wouldn't count on that "feature" either._______________________ Carlos Santander Bernal
Feb 10 2005
As Ivan mentioned, for arrays you can use functions directly as properties. So, you can just define 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
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 asproperties.So, you can just define 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
"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 asproperties.So, you can just define 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
In article <cudsrs$58o$1 digitaldaemon.com>, Ivan Senji says..."Charles" <no email.com> wrote in message news:cudidc$2sht$1 digitaldaemon.com...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. NickI 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.
Feb 09 2005
If this becomes part of the spec, is there any good reason why itshouldn't workfor 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 thinkdocumentedIt works for all array types. I had a strong feeling that it wasrememberby 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'tshouldn't workwhat 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 itfor other (non-array) types as well, such as ints and doubles? I can seethatstructs and classes would be a problem though. Nick
Feb 09 2005
On Wed, 9 Feb 2005 16:31:54 -0600, Charles <no email.com> wrote: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. ReganIf this becomes part of the spec, is there any good reason why itshouldn't workfor 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...thinkI think getting this for all types would be awesome what do ya'lldocumentedIt works for all array types. I had a strong feeling that it wasrememberby now, but can't seem to find it anywhere. And i don't think it is a bug. Alongtime ago i used this syntax with templates and something didn't work (don'tshouldn't workwhat 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 itfor other (non-array) types as well, such as ints and doubles? I can seethatstructs and classes would be a problem though. Nick
Feb 09 2005
"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
Hi, Martin, Instead ofchar []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
//| //| 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
but it lose out Error: undefined identifier SysAllocString,where is the SysAllocString?
Apr 26 2009