January 21, 2004 Re: [BUG] Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | In article <bukk58$1ip1$1@digitaldaemon.com>, Vathix says... > >Georg Wrede wrote: > >> This would let us equate the null pointer with the empty string. >> Append (~) null to foo should give foo. Length of cast(string)null >> should be zero. > >Why is a null character so special? it's just another character. It was special in C for a reason, that's how C strings are. Sorry for ambiguous text. I meant that appending the null pointer should be a null-op. Example: dString foo, bar, baz; foo = bar ~ baz; where baz happens to be unassigned, then foo should just simply get the value of bar. (I didn't mean that foo should be appended a \0.) |
January 21, 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... > 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. In STL, they have container::empty() in order to allow containers that have a bad (linear) time for obtaining size(), can provide a "cheap" test for empty(). You can implement empty() even if you do not go and obtain the size. To see the difference, understand how stdin.empty() would return false immediately unless it's at eof, and stdin.length() would not return until you hit Ctrl-Z on the keyboard or whatever. Yes, D arrays are almost mandated to store the length somewhere easily accessible, but it wouldn't hurt to consider other types of data structures when designing the standard D container interface. In other words, I agree with you. ;) Sean |
January 21, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to The Lone Haranguer | Bah. Usability in a boolean sense is a desirable property for some objects, especially for basic types in a C-derived language. I prefer if (!s) because it's shorter, which means we all can read it quicker (fewer characters == fewer potential bugs, as whitespace and comments cannot possibly be bugs), thus all else being equal between two pieces of code, less code is always better. Coding defensively can be good, up to a point, I guess. But not if it gets in my way too much. There's little enough time in the day without typing 3 times as much as you have to. Sean "The Lone Haranguer" <The_member@pathlink.com> wrote in message news:bu6u6e$e5n$1@digitaldaemon.com... > 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. |
January 21, 2004 Re: String Puzzle (was Re: how to check char[ ] is empty) | ||||
---|---|---|---|---|
| ||||
Posted in reply to The Lone Haranguer | Ok, nobody ever answered this. "The Lone Haranguer" <The_member@pathlink.com> wrote in message news:bua90q$30ub$1@digitaldaemon.com... > Hopefully there will be a bit of fondling on Sat and sun nights, yes. |
January 21, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert | Anyone want to wager on whether (s == "") will, in the *next* version of DMD, generate the same code as A) (!strcmp(s,"")) or B) s.length == 0 ? My money is on B ;) Sean "Robert" <no@spam.ne.jp> wrote in message news:bu7mo8$1orm$1@digitaldaemon.com... > (3) s == "" > > 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[]. But what if we could do this: int[] x; if (x) { ... } or even: if (x == {}) { ... } or if (x == int[]()) { ... } or if (x == int[0]) { ... } hehehe > 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. Good point. Sean |
January 21, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | I hope I never have to read your code. In article <bul827$2i19$1@digitaldaemon.com>, Sean L. Palmer says... > >Bah. > >Usability in a boolean sense is a desirable property for some objects, especially for basic types in a C-derived language. I prefer if (!s) because it's shorter, which means we all can read it quicker (fewer characters == fewer potential bugs, as whitespace and comments cannot possibly be bugs), thus all else being equal between two pieces of code, less code is always better. > >Coding defensively can be good, up to a point, I guess. But not if it gets in my way too much. There's little enough time in the day without typing 3 times as much as you have to. > >Sean > >"The Lone Haranguer" <The_member@pathlink.com> wrote in message news:bu6u6e$e5n$1@digitaldaemon.com... >> 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. > > |
January 21, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | Calm down, Ilya! "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 21, 2004 Re: how to check char[ ] is empty | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | > fewer potential bugs, as whitespace and comments cannot possibly be bugs
uhm, except outdated comments that state invalid stuff.
you should learn perl. there you can really write your way (the shorter, the
better).
|
January 21, 2004 Re: String Puzzle (was Re: how to check char[ ] is empty) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | Sean L. Palmer wrote:
>Ok, nobody ever answered this.
>
>"The Lone Haranguer" <The_member@pathlink.com> wrote in message
>news:bua90q$30ub$1@digitaldaemon.com...
>
>
>>Hopefully there will be a bit of fondling on Sat and sun nights, yes.
>>
>>
<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!
w.length = 55;
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 21, 2004 Re: String Puzzle (was Re: how to check char[ ] is empty) | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | In article <bulc6l$2opi$1@digitaldaemon.com>, J Anderson says... > >Sean L. Palmer wrote: > >>Ok, nobody ever answered this. >> >>"The Lone Haranguer" <The_member@pathlink.com> wrote in message news:bua90q$30ub$1@digitaldaemon.com... >> >> >>>Hopefully there will be a bit of fondling on Sat and sun nights, yes. >>> >>> ><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! > w.length = 55; > >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> Yes, exactly. More in http://www.digitalmars.com/drn-bin/wwwnews?D/22137 |
Copyright © 1999-2021 by the D Language Foundation