January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | He made a mistake then. Use s = ""; instead of s.length = 0; Then AssertError will be thrown. "Ilya Minkov" <minkov@cs.tum.edu> wrote in message news:bu6ur8$f8s$1@digitaldaemon.com... > yaneurao wrote: > > > No. All three are not equivalent in D. > > > > first case , he uses '=' for '==' by mistake. > > True. But the compiler would not allow it. > > > second case , s would not be empty but null. > > STOP TALKING SHIT! > > > eg. > > char[] s = "ABC"; > > s.length = 0; > > if ( !s ) { ... } // s is empty but not null > > int main() { > char[] s = "ABC"; > s.length = 0; > assert(!s); > return 0; > } > > Compiles and runs without failure. > > > so , 'if ( !s )' is wrong for checking whether s is empty or not. > You are a fool man! YOU DIDN'T EVEN CHECK! > |
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | It seems s.length = 0 is equivalent s = null, since char[] s = new char[256]; s.length = 0; printf("%d %p\n", s); outputs 0 00000000. But, for effectiveness, I sometimes want to allocate memory block in advance. For example, char[] str = new char[2048]; str.length = 0; // It assumes that the pointer is kept. str~= "foo"; ... It's effective when it is expected that str.length almost always reaches to near or above 2048. But at present, it becomes waste because str.length = 0 is equivalent str = null. "Walter" <walter@digitalmars.com> wrote in message news:bu5n0g$1eju$2@digitaldaemon.com... > > "yaneurao" <yaneurao_member@pathlink.com> wrote in message news:bu5gub$13kh$1@digitaldaemon.com... > > but someone might mistake to write > > if ( s.length == 0 ) { .. } > > if ( !s ) { .. } > > if ( s == "" ) { .. } > > All three are equivalent in D. > > |
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert | "Robert" <no@spam.ne.jp> wrote in message news:bu7plm$1tfs$1@digitaldaemon.com... > It seems s.length = 0 is equivalent s = null, since > > char[] s = new char[256]; > s.length = 0; > printf("%d %p\n", s); > > outputs 0 00000000. > Maybe thats what it prints but after char[] s = new char[256]; shouldnt s.length = 256 ? even if the indexes are empty. makes more sense to me. Phill. > But, for effectiveness, I sometimes want to allocate memory block in > advance. > For example, > > char[] str = new char[2048]; > str.length = 0; // It assumes that the pointer is kept. > str~= "foo"; > ... > > It's effective when it is expected that str.length almost always reaches to > near or above 2048. > But at present, it becomes waste because str.length = 0 is equivalent str = > null. > > > > "Walter" <walter@digitalmars.com> wrote in message news:bu5n0g$1eju$2@digitaldaemon.com... > > > > "yaneurao" <yaneurao_member@pathlink.com> wrote in message news:bu5gub$13kh$1@digitaldaemon.com... > > > but someone might mistake to write > > > if ( s.length == 0 ) { .. } > > > if ( !s ) { .. } > > > if ( s == "" ) { .. } > > > > All three are equivalent in D. > > > > > |
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill | char[] s = new char[256]; and char[] s; s.length = 256; are almost equivalent. You can use which you like. And, indeed char[] s; s.length = 4; printf("[%.*s]", s); outputs "[]". However, char[] s; s.length = 4; s ~= "A"; printf("[%.*s]", s); outputs "[]", too; i.e. s is "\0\0\0\0A" and s.length == 5. This is not what I want. What I want is "A\0\0\0" with s.length == 1. "Phill" <phill@pacific.net.au> wrote in message news:bu7r5m$20dl$1@digitaldaemon.com... > "Robert" <no@spam.ne.jp> wrote in message news:bu7plm$1tfs$1@digitaldaemon.com... > > It seems s.length = 0 is equivalent s = null, since > > > > char[] s = new char[256]; > > s.length = 0; > > printf("%d %p\n", s); > > > > outputs 0 00000000. > > > Maybe thats what it prints but after > > char[] s = new char[256]; > > shouldnt > > s.length = 256 ? > > even if the indexes are empty. > > makes more sense to me. > > Phill. > > > But, for effectiveness, I sometimes want to allocate memory block in > > advance. > > For example, > > > > char[] str = new char[2048]; > > str.length = 0; // It assumes that the pointer is kept. > > str~= "foo"; > > ... > > > > It's effective when it is expected that str.length almost always reaches > to > > near or above 2048. > > But at present, it becomes waste because str.length = 0 is equivalent str > = > > null. > > > > > > > > "Walter" <walter@digitalmars.com> wrote in message news:bu5n0g$1eju$2@digitaldaemon.com... > > > > > > "yaneurao" <yaneurao_member@pathlink.com> wrote in message news:bu5gub$13kh$1@digitaldaemon.com... > > > > but someone might mistake to write > > > > if ( s.length == 0 ) { .. } > > > > if ( !s ) { .. } > > > > if ( s == "" ) { .. } > > > > > > All three are equivalent in D. > > > > > > > > > > |
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | "Ben Hinkle" <bhinkle4@juno.com> wrote in message news:bu65i1$24l6$1@digitaldaemon.com... > > > if ( s.length = 0 ) { .. } > > I assume that should be == yes. |
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | "yaneurao" <yaneurao_member@pathlink.com> wrote in message news:bu68jk$29oc$1@digitaldaemon.com... > In article <bu5n0g$1eju$2@digitaldaemon.com>, Walter says... > >> but someone might mistake to write > >> if ( s.length = 0 ) { .. } > >> if ( !s ) { .. } > >> if ( s == "" ) { .. } > >All three are equivalent in D. > No. All three are not equivalent in D. > > first case , he uses '=' for '==' by mistake. I assumed == was meant. > second case , s would not be empty but null. > > eg. > char[] s = "ABC"; > s.length = 0; > if ( !s ) { ... } // s is empty but not null It's the same thing. It's a compiler bug that it doesn't give the same result. > so , 'if ( !s )' is wrong for checking whether s is empty or not. > > third case , even if s is null , it goes well. > > eg. > char[] s = "ABC"; > s.length = 0; > if ( s=="" ) { ... } > > but third case , compiler generates string comparing code. it's slower than: > if ( s.length == 0 ) Slower, but equivalent. A future compiler should optimize this better. > > so , 'if ( s.length == 0 )' should be used , I think. > but everybody doesn't understand this. > that's why I want '.empty' property for char[] or array. I'd rather fix the compiler problems <g>. |
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <bu8c3r$2ssk$2@digitaldaemon.com>, Walter says... >> eg. >> char[] s = "ABC"; >> s.length = 0; >> if ( !s ) { ... } // s is empty but not null > >It's the same thing. It's a compiler bug that it doesn't give the same result. I see! I wrote some situation about this in this article : http://www.digitalmars.com/drn-bin/wwwnews?D/21871 eg.1 char[] s = ""; // s set to not null though s.length == 0 eg.2 char[] s2 = "A"; char[] s1 = s2[0..0]; // s1 set to not null though s1.length == 0 it seems to be compiler's bug. yaneurao. |
January 16, 2004 String Puzzle (was Re: how to check char[ ] is empty) | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | In article <bu7l1d$1lk1$1@digitaldaemon.com>, yaneurao says... > >but Mr.Walter said that 'if (s.length == 0)' is equivalent to 'if (!s)'. >if D is designed such , following examples seem to be compiler's bug. Hmm. This made me write a puzzle for everyone interested: <code> char[] w = "kkkkskkskksksk"; so.printf("w='%.*s' w.length=%d\n",w,w.length); // I've removed this line. Figure out what this line was! char[] v = w; so.printf("v='%.*s' v.length=%d\n",v,v.length); </code> <output> w='kkkkskkskksksk' w.length=14 v='kkkkskkskksksk' v.length=55 </output> |
January 16, 2004 Re: String Puzzle (was Re: how to check char[ ] is empty) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | Georg Wrede wrote: >In article <bu7l1d$1lk1$1@digitaldaemon.com>, yaneurao says... > > >>but Mr.Walter said that 'if (s.length == 0)' is equivalent to 'if (!s)'. >>if D is designed such , following examples seem to be compiler's bug. >> >> > >Hmm. This made me write a puzzle for everyone interested: > ><code> >char[] w = "kkkkskkskksksk"; >so.printf("w='%.*s' w.length=%d\n",w,w.length); > >// I've removed this line. Figure out what this line was! > > Can we answer here? It's really very simple. >char[] v = w; >so.printf("v='%.*s' v.length=%d\n",v,v.length); ></code> ><output> >w='kkkkskkskksksk' w.length=14 >v='kkkkskkskksksk' v.length=55 ></output> > > |
January 16, 2004 Re: String Puzzle (was Re: how to check char[ ] is empty) | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | In article <bu8pmf$i6a$1@digitaldaemon.com>, J Anderson says... > >Georg Wrede wrote: > >> Hmm. This made me write a puzzle for everyone interested: > Can we answer here? It's really very simple. Well, I wish nobody would publish the answer for a couple of days. Let people think about this at home during the weekend, and then, on Monday we could have some educated opinions about what the implications of this are! And whether we should change something in D. |
Copyright © 1999-2021 by the D Language Foundation