Jump to page: 1 27  
Page
Thread overview
how to check char[ ] is empty
Jan 15, 2004
yaneurao
Jan 15, 2004
Walter
Jan 15, 2004
Ben Hinkle
Jan 16, 2004
Walter
Jan 15, 2004
yaneurao
Jan 15, 2004
Ben Hinkle
Jan 15, 2004
Ben Hinkle
Jan 15, 2004
yaneurao
Jan 15, 2004
Ben Hinkle
Jan 15, 2004
The Lone Haranguer
Jan 21, 2004
Sean L. Palmer
Jan 21, 2004
The Lone Haranguer
Jan 23, 2004
Sean L. Palmer
Jan 23, 2004
Matthew
Jan 23, 2004
The Lone Haranguer
Jan 23, 2004
Sean L. Palmer
Jan 23, 2004
Georg Wrede
Jan 21, 2004
davepermen
Jan 15, 2004
yaneurao
Jan 15, 2004
The Lone Haranguer
Jan 15, 2004
yaneurao
Jan 15, 2004
Ilya Minkov
Jan 15, 2004
yaneurao
Jan 15, 2004
yaneurao
Jan 16, 2004
The Lone Haranguer
Jan 16, 2004
yaneurao
String Puzzle (was Re: how to check char[ ] is empty)
Jan 16, 2004
Georg Wrede
Jan 16, 2004
J Anderson
Jan 16, 2004
Georg Wrede
Jan 16, 2004
Ant
Jan 16, 2004
Georg Wrede
Jan 16, 2004
The Lone Haranguer
Jan 17, 2004
Matthew
Jan 17, 2004
The Lone Haranguer
Jan 21, 2004
Sean L. Palmer
Jan 21, 2004
J Anderson
Jan 21, 2004
Georg Wrede
Re: String Puzzle - OT
Jan 21, 2004
Ant
Jan 21, 2004
Georg Wrede
Jan 21, 2004
Georg Wrede
Jan 16, 2004
Robert
Jan 21, 2004
Sean L. Palmer
Jan 15, 2004
Ilya Minkov
Jan 15, 2004
Matthew
Jan 16, 2004
Robert
[BUG] Re: how to check char[ ] is empty
Jan 20, 2004
Ilya Minkov
Jan 21, 2004
Georg Wrede
Jan 21, 2004
Juan C.
Jan 21, 2004
Vathix
Jan 21, 2004
Georg Wrede
Jun 04, 2004
Walter
Jun 04, 2004
Matthew
Jan 21, 2004
Andy Friesen
Jan 21, 2004
Sean L. Palmer
Jan 23, 2004
Ilya Minkov
Jan 16, 2004
Walter
Jan 16, 2004
yaneurao
Jan 21, 2004
Sean L. Palmer
Jan 16, 2004
Robert
Jan 16, 2004
Phill
Jan 16, 2004
Robert
January 15, 2004
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
"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
> > if ( s.length = 0 ) { .. }

I assume that should be ==


January 15, 2004
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
"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
> 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
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
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
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
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.


« First   ‹ Prev
1 2 3 4 5 6 7