Re: revised gstring



From: Havoc Pennington <hp@redhat.com>
> [patch against gstring.c] 

Hi Havoc (and other GTK+ members, of course)

I do not understand why glib does not propose
any kind of support for wide char strings - I mean
that GString only handles char * based strings
(since gchar is typedefed from char).

Not a real matter for those who don't need it
but since it is not a coding problem, this should
probably help sometimes...

Basically, this would lead to something like :

/* in glib.h */

typedef gschar char;
typedef gwchar wchar_t;

#define G_STRING_SMALL          1
#define G_STRING_WIDE           2

/* the typedef for these two stuff */
typedef struct _GStringSmall GStringSmall;
typedef struct _GStringWide GStringWide;

struct _GStringSmall
{
    gschar       *str;
    guint        len;
    guint        type;
};

struct _GStringWide
{
    gwchar       *str;
    guint        len;
    guint        type;
};

#if (USE_WCHAR == 1)
#define GString GStringWide
#define gchar gwchar
#define g_string_prepend g_string_prepend_small
#else
#define gchar gschar
#define GString GStringSmall
#define g_string_prepend g_string_prepend_wide
#endif

GString     *g_string_prepend(GString *string, 
                    const gchar *val);

/* 
** gstring.c should be splitted in two files : 
** gstringsmall.c
** gstringwide.c 
*/

/* in gstringsmall.c */
GStringSmall *
g_string_prepend_small(GStringSmall     *string,
                       gschar           *val)
{
    /* ... */
    g_return_val_if_failed (string != NULL, NULL);
    g_return_val_if_failed (string->type != G_STRING_SMALL, NULL);
    /* ... rest of the code ... */
} 

/* the same in gstringwide.c */
GStringWide *
g_string_prepend_wide(GStringWide     *gstring,
                      gwchar          *val)
{
    /* ... */
    g_return_val_if_failed (string != NULL, NULL);
    g_return_val_if_failed (string->type != G_STRING_WIDE, NULL);
    /* ... rest of the code ... */
}

/* end of code example */

Since the _wide and the _small versions will be compiled
in the libglib.so library, (at least if the system supports 
wchar_t, which is the almost allways case), this will allow
the user to choose between normal or wide string when 
compiling its program. 

Hope this is not a stupid idea :)

Emmanuel




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]