Re: [Vala] How to check if string is empty

2017-01-17 Thread Ulink
Am 2017-01-17 um 12:09 schrieb rastersoft:

> That's true for the first one, but the second is as fast as str[0] == 0,
> because as soon as one byte doesn't fit, the function ends.

You are right, because str=="" indeed calls g_strcmp0(), but
str[0]=='\0' calls string_get() which seems not faster that g_strcmp0().

So the fastest (C-like) solution would be:

bool is_null_or_empty3 (string? str)
{
   return str == null || *(char*)str == '\0';
}

But it's ugly ;-)


___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] How to check if string is empty

2017-01-17 Thread rastersoft
Hi:

El 17/01/17 a las 10:24, Ulink escribió:
> But I think str.length==0 and str=="" are no high performance solutions,
> especially for big/long strings.
That's true for the first one, but the second is as fast as str[0] == 0,
because as soon as one byte doesn't fit, the function ends.

-- 
Nos leemos
 RASTER(Linux user #228804)
ras...@rastersoft.com  http://www.rastersoft.com

___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] How to check if string is empty

2017-01-17 Thread Ulink
As Guillaume wrote, you have to use string? str and check string==null
FIRST.

But I think str.length==0 and str=="" are no high performance solutions,
especially for big/long strings.

Maybe this is better?

bool is_null_or_empty (string? str)
{
   return str == null || str[0] == '\0';
}

___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] How to check if string is empty

2017-01-17 Thread Dmitry Golovin
My mistake. Thanks for correcting me.

17.01.2017, 10:55, "Jens Georg" :
>>  In Vala you can simply just check for str != "", this is enough.
>
> No, if str is nullable it's semantically different, because str != ""
> will be generated as
>
> if (g_strcmp0 (_tmp1_, "") != 0) {
>    ..
> }
>
> This will be TRUE for _tmp1_ being NULL because NULL isn't "" ( and
> g_strcmp0 will return -1 because NULL is < everything)
> so if str is nullable, str != "" means NULL or not empty.
> ___
> vala-list mailing list
> vala-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/vala-list
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] How to check if string is empty

2017-01-17 Thread Jens Georg


In Vala you can simply just check for str != "", this is enough.


No, if str is nullable it's semantically different, because str != "" 
will be generated as


if (g_strcmp0 (_tmp1_, "") != 0) {
  ..
}

This will be TRUE for _tmp1_ being NULL because NULL isn't "" ( and 
g_strcmp0 will return -1 because NULL is < everything)

so if str is nullable, str != "" means NULL or not empty.
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list