GLib substr function

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

GLib substr function

b0unc3
Hi all,

there is any implementation of a substr function in GLib ?

I mean :
string = "hello world"
g_*substr*(string,2,6)
output = llo w


_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Christian Dywan-2
Am Fri, 10 Apr 2009 12:45:39 +0200
schrieb b0unc3 <[hidden email]>:

> Hi all,
>
> there is any implementation of a substr function in GLib ?
>
> I mean :
> string = "hello world"
> g_*substr*(string,2,6)
> output = llo w

Hey b0unc3,

no, there isn't. Either you use a higher level language which has it or
you use low level functions in C.

For the sake of demonstration, it took me 2 minutes to write a simple
substring function in C that does what you want, have a look how it
works. :)

Yours,
    Christian
_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list

substr.c (746 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

efa@iol.it
In reply to this post by b0unc3
b0unc3 wrote:
> there is any implementation of a substr function in GLib ?
> I mean :
> string = "hello world"
> g_*substr*(string,2,6)
> output = llo w

probably not, maybe because is something too simple like:

g_*strndup* (string+start, end-start+1);

Valerio
_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Tim-Philipp Müller
In reply to this post by b0unc3
On Fri, 2009-04-10 at 12:45 +0200, b0unc3 wrote:

> there is any implementation of a substr function in GLib ?
>
> I mean :
> string = "hello world"
> g_*substr*(string,2,6)
> output = llo w

substr = g_strndup (string + offset, len);
...
g_free (substr);

should do the trick (assuming that offset < strlen).

Cheers
 -Tim


_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Nelson Benítez León
In reply to this post by b0unc3
2009/4/10 b0unc3 <[hidden email]>:
> Hi all,
>
> there is any implementation of a substr function in GLib ?
>
> I mean :
> string = "hello world"
> g_*substr*(string,2,6)
> output = llo w

Another way,

substring (GString *str, int index, int len)
{
  return g_string_new_len (str->str, index, MIN (str->len - index, len));
}

taken from http://bugzilla.gnome.org/show_bug.cgi?id=109286#c2 .


I personally would like that glib provide those small but useful
string functions (like other high level languages do), for example,
glib doesn't provide a simple function to replace strings, like this
one written by Tim in
http://bugzilla.gnome.org/show_bug.cgi?id=65987#c2 ,

gchar *
g_strreplace (const gchar *string,
              const gchar *search,
              const gchar *replacement)


Instead you currently have to use gregex to replace some simple
strings (where no regex are involved).
_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

b0unc3
2009/4/10 Nelson Benítez León <[hidden email]>
2009/4/10 b0unc3 <[hidden email]>:
> Hi all,
>
> there is any implementation of a substr function in GLib ?
>
> I mean :
> string = "hello world"
> g_*substr*(string,2,6)
> output = llo w

Another way,

substring (GString *str, int index, int len)
{
 return g_string_new_len (str->str, index, MIN (str->len - index, len));
}

taken from http://bugzilla.gnome.org/show_bug.cgi?id=109286#c2 .


I personally would like that glib provide those small but useful
string functions (like other high level languages do), for example,
glib doesn't provide a simple function to replace strings, like this
one written by Tim in
http://bugzilla.gnome.org/show_bug.cgi?id=65987#c2 ,

gchar *
g_strreplace (const gchar *string,
             const gchar *search,
             const gchar *replacement)


Instead you currently have to use gregex to replace some simple
strings (where no regex are involved).

First of all thanks to everyone who replayed.
The implementation using g_srtndup looks ok.

I was wondering why not to add a so simple example in the official docs (maybe in the g_strndup explanation).


_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Christian Dywan-2
Am Fri, 10 Apr 2009 16:49:43 +0200
schrieb b0unc3 <[hidden email]>:

> 2009/4/10 Nelson Benítez León <[hidden email]>
>
> > 2009/4/10 b0unc3 <[hidden email]>:
> > > Hi all,
> > >
> > > there is any implementation of a substr function in GLib ?
> > >
> > > I mean :
> > > string = "hello world"
> > > g_*substr*(string,2,6)
> > > output = llo w
> >
> > Another way,
> >
> > substring (GString *str, int index, int len)
> > {
> >  return g_string_new_len (str->str, index, MIN (str->len - index,
> > len)); }
> >
> > taken from http://bugzilla.gnome.org/show_bug.cgi?id=109286#c2 .
> >
> >
> > I personally would like that glib provide those small but useful
> > string functions (like other high level languages do), for example,
> > glib doesn't provide a simple function to replace strings, like this
> > one written by Tim in
> > http://bugzilla.gnome.org/show_bug.cgi?id=65987#c2 ,
> >
> > gchar *
> > g_strreplace (const gchar *string,
> >              const gchar *search,
> >              const gchar *replacement)
> >
> >
> > Instead you currently have to use gregex to replace some simple
> > strings (where no regex are involved).
>
>
> First of all thanks to everyone who replayed.
> The implementation using g_srtndup looks ok.
>
> I was wondering why not to add a so simple example in the official
> docs (maybe in the g_strndup explanation).

Hey,

the reason is simple. This is entirely up to the programming language
and not at all Glib specific. If you want to learn more about C you
should look for a good C (online) book.

Regards,
    Christian
_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Butrus Damaskus
On Fri, Apr 10, 2009 at 6:34 PM, Christian Dywan <[hidden email]> wrote:

> Am Fri, 10 Apr 2009 16:49:43 +0200
> schrieb b0unc3 <[hidden email]>:
>
>> 2009/4/10 Nelson Benítez León <[hidden email]>
>>
>> > 2009/4/10 b0unc3 <[hidden email]>:
>> > > Hi all,
>> > >
>> > > there is any implementation of a substr function in GLib ?
>> > >
>> > > I mean :
>> > > string = "hello world"
>> > > g_*substr*(string,2,6)
>> > > output = llo w
>> >
>> > Another way,
>> >
>> > substring (GString *str, int index, int len)
>> > {
>> >  return g_string_new_len (str->str, index, MIN (str->len - index,
>> > len)); }
>> >
>> > taken from http://bugzilla.gnome.org/show_bug.cgi?id=109286#c2 .
>> >
>> >
>> > I personally would like that glib provide those small but useful
>> > string functions (like other high level languages do), for example,
>> > glib doesn't provide a simple function to replace strings, like this
>> > one written by Tim in
>> > http://bugzilla.gnome.org/show_bug.cgi?id=65987#c2 ,
>> >
>> > gchar *
>> > g_strreplace (const gchar *string,
>> >              const gchar *search,
>> >              const gchar *replacement)
>> >
>> >
>> > Instead you currently have to use gregex to replace some simple
>> > strings (where no regex are involved).
>>
>>
>> First of all thanks to everyone who replayed.
>> The implementation using g_srtndup looks ok.
>>
>> I was wondering why not to add a so simple example in the official
>> docs (maybe in the g_strndup explanation).
>
> Hey,
>
> the reason is simple. This is entirely up to the programming language
> and not at all Glib specific. If you want to learn more about C you
> should look for a good C (online) book.
>

That's ridiculous! You have many things in glib (like ghash, gstring
etc.) which can be done better with specific language constructs in
some languages (associative arrays, operators on strings etc.). Yet,
as glib is primarily foused on C, it's a good idea to implement those
features, which are (in plain C) missing...

P.
_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Stef Walter
In reply to this post by b0unc3
b0unc3 wrote:
> 2009/4/10 Nelson Benítez León <[hidden email]>
>> Another way,
>>
>> substring (GString *str, int index, int len)
>> {
>>  return g_string_new_len (str->str, index, MIN (str->len - index, len));
>> }
>>

<snip>

>
> First of all thanks to everyone who replayed.
> The implementation using g_srtndup looks ok.
>
> I was wondering why not to add a so simple example in the official docs
> (maybe in the g_strndup explanation).

Maybe because it's completely wrong for UTF-8 strings?

Cheers,

Stef

_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Freddie Unpenstein
In reply to this post by b0unc3
From: "Stef Walter", Date: 11/04/2009 03:45, Wrote:

>>> substring (GString *str, int index, int len)
>>> {
>>> return g_string_new_len (str->str, index, MIN (str->len - index, len));
>>> }
>> I was wondering why not to add a so simple example in the official docs
>> (maybe in the g_strndup explanation).
> Maybe because it's completely wrong for UTF-8 strings?

Why, then, doesn't glib come with a completely right implementation?

Basic string operations like sub-string and string replace, should be part of glib, and for every string format that glib supports (including plain ASCII). Especially when it's so obvious that a lot of people still do it incorrectly.


Fredderic

   Paralegal Scholarship
Save on Paralegal Training with a Scholarship - Click Now!
Click Here For More Information
 

_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Daniel Elstner
In reply to this post by Christian Dywan-2
Am Freitag, den 10.04.2009, 14:08 +0200 schrieb Christian Dywan:

> For the sake of demonstration, it took me 2 minutes to write a simple
> substring function in C that does what you want, have a look how it
> works. :)

It doesn't.  Your function allocates memory using a byte count but then
uses the same value for the number of characters to copy.

--Daniel


_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Christian Dywan-2
Am Fri, 26 Jun 2009 18:52:10 +0200
schrieb Daniel Elstner <[hidden email]>:

> Am Freitag, den 10.04.2009, 14:08 +0200 schrieb Christian Dywan:
>
> > For the sake of demonstration, it took me 2 minutes to write a
> > simple substring function in C that does what you want, have a look
> > how it works. :)
>
> It doesn't.  Your function allocates memory using a byte count but
> then uses the same value for the number of characters to copy.
>
> --Daniel

Oh well,

so you dug up a two and a half month old message of mine to point out
that 2 minutes are enough to make a stupid mistake. :)

You're right, it's not correct for UTF8 strings. I suppose it's good
you pointed it out in case somebody else actually used the example as-is
without noticing my mistake.

Regards,
    Christian
_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GLib substr function

Christian Dywan-3
In reply to this post by Daniel Elstner
Am Fri, 26 Jun 2009 18:52:10 +0200
schrieb Daniel Elstner <[hidden email]>:

> Am Freitag, den 10.04.2009, 14:08 +0200 schrieb Christian Dywan:
>
> > For the sake of demonstration, it took me 2 minutes to write a
> > simple substring function in C that does what you want, have a look
> > how it works. :)
>
> It doesn't.  Your function allocates memory using a byte count but
> then uses the same value for the number of characters to copy.
>
> --Daniel

Oh well,

so you dug up a two and a half month old message of mine to point out
that 2 minutes are enough to make a stupid mistake. :)

You're right, it's not correct for UTF8 strings. I suppose it's good
you pointed it out in case somebody else actually used the example as-is
without noticing my mistake.

Regards,
    Christian
_______________________________________________
gtk-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-devel-list