Thread overview | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 15, 2004 how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
I want empty property in char[]. C# style: string s; if ( !s || s.length == 0) { .. } // or if ( !s !! s!=String.Empty) { .. } // Note in C# when we access length property for nullReffernce , // throw NullReferenceException. D style: char[] s; if ( !s || s.length == 0) { .. } // or simplifying this : if ( s.length == 0 ) { .. } but someone might mistake to write if ( s.length = 0 ) { .. } if ( !s ) { .. } if ( s == "" ) { .. } // this is right. but _adeq is called , so not good code , I think. So , char [] had better have empty property like this: if ( s.empty ) { .. } It should be equivalent to : if ( s.length == 0 ) { .. } yaneurao. |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | "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 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | > > if ( s.length = 0 ) { .. }
I assume that should be ==
|
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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. second case , s would not be empty but null. eg. char[] s = "ABC"; s.length = 0; if ( !s ) { ... } // s is empty but not null 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 ) 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. yaneurao. |
January 15, 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. How about making s.length = 0; set the data pointer to null? That way the three will be equivalent since it would be impossible to have a non-null pointer and 0 length. The performance hit would just be a single 0 test when changing lengths. > > first case , he uses '=' for '==' by mistake. > second case , s would not be empty but null. > > eg. > char[] s = "ABC"; > s.length = 0; > if ( !s ) { ... } // s is empty but not null > > 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 ) > > 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. > > yaneurao. > > |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | > How about making > s.length = 0; > set the data pointer to null? That way the three will be equivalent since it > would be impossible to have a non-null pointer and 0 length. The performance > hit would just be a single 0 test when changing lengths. looking at the source file src/phobos/internal/gc/gc.d the function _d_arraysetlength already does this so apparently I'm confused about why those three tests are different (modulo the part about the first test having = instead of ==). -Ben |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | So Walter has kindly given you _three_ ways to do the same thing and you still want _more_ ways? If sharability of code is a major goal in modern programming, then there should be _fewer_ ways of doing a particular thing (while still allowing expressiveness). How usefull is a language that allows so much obfuscation? ... Wait, don't answer that. In article <bu68jk$29oc$1@digitaldaemon.com>, yaneurao says... > >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. >second case , s would not be empty but null. > >eg. >char[] s = "ABC"; >s.length = 0; >if ( !s ) { ... } // s is empty but not null > >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 ) > >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. > >yaneurao. > > |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to The Lone Haranguer | In article <bu6jnn$2sdc$1@digitaldaemon.com>, The Lone Haranguer says... >So Walter has kindly given you _three_ ways to do the same thing and you still want _more_ ways? please read http://www.digitalmars.com/drn-bin/wwwnews?D/21817 three ways are not the same. yaneurao. |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | In article <bu6cj5$2gh8$1@digitaldaemon.com>, Ben Hinkle says... >looking at the source file src/phobos/internal/gc/gc.d the function _d_arraysetlength already does this so apparently I'm confused about why those three tests are different (modulo the part about the first test having = instead of ==). char[](or array) could be not null and empty easily like this: eg. char s = "ABC"; s = ""; // s is not null but empty(length==0) so null-checking is not equal to empty-checking. by this reason , if you want to check whether char[] is empty , you should type 'if ( s.length == 0 )' , not 'if ( !s )'. thus , it is necessary for char[](or array) to have a '.empty' property. yaneurao. |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | In article <bu6c41$2fpc$1@digitaldaemon.com>, Ben Hinkle says... >How about making > s.length = 0; >set the data pointer to null? I guess it is not realistic. when copying array , assigning array or some changing array , GC needs to detect whether length == 0 or not ? eg. char [] s1 = "ABC"; char [] s2 = s1[1..1]; // here , s2 is empty though not null. s2 could be not null and empty(length == 0) easily. I don't know where it is a bug or not. but every time changing array , should GC detect length == 0 and set the data pointer to null? it is not realistic implementation, I think. yaneurao. |
Copyright © 1999-2021 by the D Language Foundation