January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | "yaneurao" <yaneurao_member@pathlink.com> wrote in message news:bu6mgl$3131$1@digitaldaemon.com... > 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. Hmm. I see. I wonder if it would make life easier to make "" have 0 length and null pointer. I guess that would be un-C-like since there "" is a pointer to a 0 byte and that would cause problems with things like printf(""); I'm leaning towards keeping things the way they are and just making sure to use .length to test for empty strings since there are different ways of being empty. (deep, eh?) If your code needs to cast to a non-null char* then use the pointer test. Which one to use depends on the context. -Ben |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | Well, 'if ( !s )' is poor style anyway and should be avoided. Use 'if ( s == null )' it's clearer. Plus, even though D still doesn't strongly-type boolean, one should code as if it were. In article <bu6mgl$3131$1@digitaldaemon.com>, yaneurao says... > >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 yaneurao | 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 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | yaneurao wrote:
> http://www.digitalmars.com/drn-bin/wwwnews?D/21817
>
> three ways are not the same.
>
> yaneurao.
Then WHY THE HELL does this run?
int main() {
char[] s = "ABC";
s.length = 0;
assert(!s);
return 0;
}
-eye
|
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | In article <bu6usn$f8s$2@digitaldaemon.com>, Ilya Minkov says... >Then WHY THE HELL does this run? >int main() { > char[] s = "ABC"; > s.length = 0; > assert(!s); > return 0; >} I mistook it. try this code. int main() { char[] s = ""; assert(s); assert(s.length == 0); return 0; } s could be null and length == 0 at the same time. yaneurao. |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaneurao | In article <bu71mq$k8b$1@digitaldaemon.com>, yaneurao says... >int main() { >char[] s = ""; >assert(s); >assert(s.length == 0); >return 0; >} >s could be null and length == 0 at the same time. sorry i mistook to write. s cound be *not* null and length == 0 at the same time. eg. 1) char[] s = ""; 2) char[] s2 = "ABC"; char[] s = s2[0..0]; and so on. my english is very poor , sorry... yaneurao. |
January 15, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | Ilya Just tone it down a bit, will you? There's no need to be that aggressive. "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 yaneurao | >s cound be *not* null and length == 0 at the same time.
.. aaaand your point issss.... ??
|
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to The Lone Haranguer | In article <bu7hri$1gir$1@digitaldaemon.com>, The Lone Haranguer says... >>s cound be *not* null and length == 0 at the same time. >.. aaaand your point issss.... ?? at first I thought the emptiness of char[] should be checked by 'if (s.length == 0)' , not ' if (!s)' because I knew the situation s.length == 0 and not null. 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. eg.1 char[] s = ""; eg.2 char[] s2 = "A"; char[] s1 = s2[0..0]; yaneurao. |
January 16, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to The Lone Haranguer | Walter said there are three ways to check a string is empty. (1) s.length == 0 (2) s == null (3) s == "" But, (2) is wrong in such a case as: import std.c.stdio; class A { private char[] m_str; this(char[] str) { m_str = str; } char[] sub(int i, int j) { return m_str[i..j]; } } char[] getSub(A a) { int begin, length; scanf("%d%d", &begin, &length); return a.sub(begin, begin + length); } int main() { A a = new A("foo"); char[] sub = getSub(a); if(sub == null) { printf("nothing"); } else { printf("substring: %.*s\n", sub); } return 0; } In this case, else branch is always performed (i.e. sub == null is always false), even if sub is empty string. OTOH (3) is a stupid way to check empty. Because it is not effective. In C, It is as: strcnmp(s, "", s_length) == 0 It's waste. Furthermore, this way cannot be used in case of int[]. So, (1) is the best way to check whether string is empty in D at present. It is effecient, works always correctly, and can be used for int[]. But, he says that .empty property is better than (1), as STL. .empty property helps for us *not* to use any *magic numbers* even 0 and we can grasp the meaning easily. I think so, too. "The Lone Haranguer" <The_member@pathlink.com> wrote in message news:bu7hri$1gir$1@digitaldaemon.com... > >s cound be *not* null and length == 0 at the same time. > > .. aaaand your point issss.... ?? > > |
Copyright © 1999-2021 by the D Language Foundation