2000-06-21 18:11:21 +02:00
|
|
|
/* gutf8.c - Operations on UTF-8 strings.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Tom Tromey
|
|
|
|
* Copyright (C) 2000 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 13:02:02 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2000-06-21 18:11:21 +02:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 13:02:02 +02:00
|
|
|
* Lesser General Public License for more details.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2000-07-26 13:02:02 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2000-06-21 18:11:21 +02:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2000-07-04 00:01:58 +02:00
|
|
|
#ifdef HAVE_CODESET
|
2000-06-21 18:11:21 +02:00
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "glib.h"
|
|
|
|
|
2001-03-09 22:31:21 +01:00
|
|
|
#ifdef G_PLATFORM_WIN32
|
2001-01-25 22:16:46 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#define STRICT
|
2000-12-17 19:43:57 +01:00
|
|
|
#include <windows.h>
|
2001-03-09 22:31:21 +01:00
|
|
|
#undef STRICT
|
2000-12-17 19:43:57 +01:00
|
|
|
#endif
|
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
#include "libcharset/libcharset.h"
|
|
|
|
|
2001-01-16 03:24:24 +01:00
|
|
|
#include "glibintl.h"
|
2001-01-05 22:22:47 +01:00
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
#define UTF8_COMPUTE(Char, Mask, Len) \
|
|
|
|
if (Char < 128) \
|
|
|
|
{ \
|
|
|
|
Len = 1; \
|
|
|
|
Mask = 0x7f; \
|
|
|
|
} \
|
|
|
|
else if ((Char & 0xe0) == 0xc0) \
|
|
|
|
{ \
|
|
|
|
Len = 2; \
|
|
|
|
Mask = 0x1f; \
|
|
|
|
} \
|
|
|
|
else if ((Char & 0xf0) == 0xe0) \
|
|
|
|
{ \
|
|
|
|
Len = 3; \
|
|
|
|
Mask = 0x0f; \
|
|
|
|
} \
|
|
|
|
else if ((Char & 0xf8) == 0xf0) \
|
|
|
|
{ \
|
|
|
|
Len = 4; \
|
|
|
|
Mask = 0x07; \
|
|
|
|
} \
|
|
|
|
else if ((Char & 0xfc) == 0xf8) \
|
|
|
|
{ \
|
|
|
|
Len = 5; \
|
|
|
|
Mask = 0x03; \
|
|
|
|
} \
|
|
|
|
else if ((Char & 0xfe) == 0xfc) \
|
|
|
|
{ \
|
|
|
|
Len = 6; \
|
|
|
|
Mask = 0x01; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
Len = -1;
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
#define UTF8_LENGTH(Char) \
|
|
|
|
((Char) < 0x80 ? 1 : \
|
|
|
|
((Char) < 0x800 ? 2 : \
|
|
|
|
((Char) < 0x10000 ? 3 : \
|
|
|
|
((Char) < 0x200000 ? 4 : \
|
|
|
|
((Char) < 0x4000000 ? 5 : 6)))))
|
|
|
|
|
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
#define UTF8_GET(Result, Chars, Count, Mask, Len) \
|
|
|
|
(Result) = (Chars)[0] & (Mask); \
|
|
|
|
for ((Count) = 1; (Count) < (Len); ++(Count)) \
|
|
|
|
{ \
|
|
|
|
if (((Chars)[(Count)] & 0xc0) != 0x80) \
|
|
|
|
{ \
|
|
|
|
(Result) = -1; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
(Result) <<= 6; \
|
|
|
|
(Result) |= ((Chars)[(Count)] & 0x3f); \
|
|
|
|
}
|
2001-01-05 22:22:47 +01:00
|
|
|
|
|
|
|
#define UNICODE_VALID(Char) \
|
|
|
|
((Char) < 0x110000 && \
|
|
|
|
((Char) < 0xD800 || (Char) >= 0xE000) && \
|
|
|
|
(Char) != 0xFFFE && (Char) != 0xFFFF)
|
|
|
|
|
|
|
|
|
2001-09-02 19:14:23 +02:00
|
|
|
static const gchar utf8_skip_data[256] = {
|
2000-06-21 18:11:21 +02:00
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
2001-11-28 22:23:32 +01:00
|
|
|
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
|
2000-06-21 18:11:21 +02:00
|
|
|
};
|
|
|
|
|
2001-09-02 19:14:23 +02:00
|
|
|
const gchar * const g_utf8_skip = utf8_skip_data;
|
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
/**
|
|
|
|
* g_utf8_find_prev_char:
|
2001-09-24 23:28:57 +02:00
|
|
|
* @str: pointer to the beginning of a UTF-8 encoded string
|
2000-06-21 18:11:21 +02:00
|
|
|
* @p: pointer to some position within @str
|
|
|
|
*
|
|
|
|
* Given a position @p with a UTF-8 encoded string @str, find the start
|
|
|
|
* of the previous UTF-8 character starting before @p. Returns %NULL if no
|
|
|
|
* UTF-8 characters are present in @p before @str.
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* @p does not have to be at the beginning of a UTF-8 character. No check
|
2000-06-21 18:11:21 +02:00
|
|
|
* is made to see if the character found is actually valid other than
|
|
|
|
* it starts with an appropriate byte.
|
|
|
|
*
|
|
|
|
* Return value: a pointer to the found character or %NULL.
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_utf8_find_prev_char (const char *str,
|
|
|
|
const char *p)
|
|
|
|
{
|
2001-08-03 07:52:28 +02:00
|
|
|
for (--p; p >= str; --p)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
|
|
|
if ((*p & 0xc0) != 0x80)
|
|
|
|
return (gchar *)p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_find_next_char:
|
|
|
|
* @p: a pointer to a position within a UTF-8 encoded string
|
|
|
|
* @end: a pointer to the end of the string, or %NULL to indicate
|
2001-09-24 23:28:57 +02:00
|
|
|
* that the string is nul-terminated, in which case
|
2000-06-21 18:11:21 +02:00
|
|
|
* the returned value will be
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Finds the start of the next UTF-8 character in the string after @p.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* @p does not have to be at the beginning of a UTF-8 character. No check
|
2000-06-21 18:11:21 +02:00
|
|
|
* is made to see if the character found is actually valid other than
|
|
|
|
* it starts with an appropriate byte.
|
|
|
|
*
|
|
|
|
* Return value: a pointer to the found character or %NULL
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_utf8_find_next_char (const gchar *p,
|
|
|
|
const gchar *end)
|
|
|
|
{
|
|
|
|
if (*p)
|
|
|
|
{
|
|
|
|
if (end)
|
|
|
|
for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
for (++p; (*p & 0xc0) == 0x80; ++p)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return (p == end) ? NULL : (gchar *)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_prev_char:
|
|
|
|
* @p: a pointer to a position within a UTF-8 encoded string
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Finds the previous UTF-8 character in the string before @p.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
|
|
|
* @p does not have to be at the beginning of a UTF-8 character. No check
|
|
|
|
* is made to see if the character found is actually valid other than
|
|
|
|
* it starts with an appropriate byte. If @p might be the first
|
2001-09-24 23:28:57 +02:00
|
|
|
* character of the string, you must use g_utf8_find_prev_char() instead.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
|
|
|
* Return value: a pointer to the found character.
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_utf8_prev_char (const gchar *p)
|
|
|
|
{
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
p--;
|
|
|
|
if ((*p & 0xc0) != 0x80)
|
|
|
|
return (gchar *)p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_strlen:
|
2001-09-24 23:28:57 +02:00
|
|
|
* @p: pointer to the start of a UTF-8 encoded string.
|
2000-06-21 18:11:21 +02:00
|
|
|
* @max: the maximum number of bytes to examine. If @max
|
|
|
|
* is less than 0, then the string is assumed to be
|
|
|
|
* nul-terminated.
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Returns the length of the string in characters.
|
|
|
|
*
|
2000-06-21 18:11:21 +02:00
|
|
|
* Return value: the length of the string in characters
|
2001-04-16 22:05:25 +02:00
|
|
|
**/
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong
|
2001-05-19 07:32:50 +02:00
|
|
|
g_utf8_strlen (const gchar *p,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gssize max)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len = 0;
|
2000-06-21 18:11:21 +02:00
|
|
|
const gchar *start = p;
|
2001-03-20 22:30:40 +01:00
|
|
|
|
|
|
|
if (max < 0)
|
|
|
|
{
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
p = g_utf8_next_char (p);
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
2001-03-20 22:30:40 +01:00
|
|
|
if (max == 0 || !*p)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = g_utf8_next_char (p);
|
|
|
|
|
|
|
|
while (p - start < max && *p)
|
|
|
|
{
|
|
|
|
++len;
|
|
|
|
p = g_utf8_next_char (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only do the last len increment if we got a complete
|
|
|
|
* char (don't count partial chars)
|
|
|
|
*/
|
|
|
|
if (p - start == max)
|
|
|
|
++len;
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
2001-03-20 22:30:40 +01:00
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_get_char:
|
2001-09-24 23:28:57 +02:00
|
|
|
* @p: a pointer to Unicode character encoded as UTF-8
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
|
2001-05-15 21:18:08 +02:00
|
|
|
* If @p does not point to a valid UTF-8 encoded character, results are
|
2001-07-19 16:35:48 +02:00
|
|
|
* undefined. If you are not sure that the bytes are complete
|
2001-09-24 23:28:57 +02:00
|
|
|
* valid Unicode characters, you should use g_utf8_get_char_validated()
|
2001-07-19 16:35:48 +02:00
|
|
|
* instead.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-05-15 21:18:08 +02:00
|
|
|
* Return value: the resulting character
|
2000-06-21 18:11:21 +02:00
|
|
|
**/
|
|
|
|
gunichar
|
|
|
|
g_utf8_get_char (const gchar *p)
|
|
|
|
{
|
|
|
|
int i, mask = 0, len;
|
|
|
|
gunichar result;
|
|
|
|
unsigned char c = (unsigned char) *p;
|
|
|
|
|
|
|
|
UTF8_COMPUTE (c, mask, len);
|
|
|
|
if (len == -1)
|
|
|
|
return (gunichar)-1;
|
|
|
|
UTF8_GET (result, p, i, mask, len);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_offset_to_pointer:
|
|
|
|
* @str: a UTF-8 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @offset: a character offset within @str
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
|
|
|
* Converts from an integer character offset to a pointer to a position
|
|
|
|
* within the string.
|
|
|
|
*
|
|
|
|
* Return value: the resulting pointer
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_utf8_offset_to_pointer (const gchar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong offset)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
|
|
|
const gchar *s = str;
|
|
|
|
while (offset--)
|
|
|
|
s = g_utf8_next_char (s);
|
|
|
|
|
|
|
|
return (gchar *)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_pointer_to_offset:
|
|
|
|
* @str: a UTF-8 encoded string
|
|
|
|
* @pos: a pointer to a position within @str
|
|
|
|
*
|
|
|
|
* Converts from a pointer to position within a string to a integer
|
2001-09-24 23:28:57 +02:00
|
|
|
* character offset.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
|
|
|
* Return value: the resulting character offset
|
|
|
|
**/
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong
|
2000-06-21 18:11:21 +02:00
|
|
|
g_utf8_pointer_to_offset (const gchar *str,
|
|
|
|
const gchar *pos)
|
|
|
|
{
|
|
|
|
const gchar *s = str;
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong offset = 0;
|
2000-06-21 18:11:21 +02:00
|
|
|
|
|
|
|
while (s < pos)
|
|
|
|
{
|
|
|
|
s = g_utf8_next_char (s);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-16 22:05:25 +02:00
|
|
|
/**
|
|
|
|
* g_utf8_strncpy:
|
|
|
|
* @dest: buffer to fill with characters from @src
|
2001-09-24 23:28:57 +02:00
|
|
|
* @src: UTF-8 encoded string
|
2001-04-16 22:05:25 +02:00
|
|
|
* @n: character count
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Like the standard C <function>strncpy()</function> function, but
|
|
|
|
* copies a given number of characters instead of a given number of
|
|
|
|
* bytes. The @src string must be valid UTF-8 encoded text.
|
|
|
|
* (Use g_utf8_validate() on all text before trying to use UTF-8
|
|
|
|
* utility functions with it.)
|
2001-04-16 22:05:25 +02:00
|
|
|
*
|
|
|
|
* Return value: @dest
|
|
|
|
**/
|
2000-06-21 18:11:21 +02:00
|
|
|
gchar *
|
2001-07-20 20:12:10 +02:00
|
|
|
g_utf8_strncpy (gchar *dest,
|
|
|
|
const gchar *src,
|
|
|
|
gsize n)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
|
|
|
const gchar *s = src;
|
|
|
|
while (n && *s)
|
|
|
|
{
|
|
|
|
s = g_utf8_next_char(s);
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
strncpy(dest, src, s - src);
|
|
|
|
dest[s - src] = 0;
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
G_LOCK_DEFINE_STATIC (aliases);
|
2000-06-21 18:11:21 +02:00
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
static GHashTable *
|
|
|
|
get_alias_hash (void)
|
|
|
|
{
|
|
|
|
static GHashTable *alias_hash = NULL;
|
|
|
|
const char *aliases;
|
2000-06-21 18:11:21 +02:00
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
G_LOCK (aliases);
|
2000-06-21 18:11:21 +02:00
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
if (!alias_hash)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
2001-09-27 04:49:05 +02:00
|
|
|
alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
|
|
|
|
aliases = _g_locale_get_charset_aliases ();
|
|
|
|
while (*aliases != '\0')
|
|
|
|
{
|
|
|
|
const char *canonical;
|
|
|
|
const char *alias;
|
|
|
|
const char **alias_array;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
alias = aliases;
|
|
|
|
aliases += strlen (aliases) + 1;
|
|
|
|
canonical = aliases;
|
|
|
|
aliases += strlen (aliases) + 1;
|
|
|
|
|
|
|
|
alias_array = g_hash_table_lookup (alias_hash, canonical);
|
|
|
|
if (alias_array)
|
|
|
|
{
|
|
|
|
while (alias_array[count])
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
alias_array = g_renew (const char *, alias_array, count + 2);
|
|
|
|
alias_array[count] = alias;
|
|
|
|
alias_array[count + 1] = NULL;
|
|
|
|
|
|
|
|
g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
|
|
|
|
}
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
2001-09-27 04:49:05 +02:00
|
|
|
|
|
|
|
G_UNLOCK (aliases);
|
|
|
|
|
|
|
|
return alias_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* As an abuse of the alias table, the following routines gets
|
|
|
|
* the charsets that are aliases for the canonical name.
|
|
|
|
*/
|
|
|
|
const char **
|
|
|
|
_g_charset_get_aliases (const char *canonical_name)
|
|
|
|
{
|
|
|
|
GHashTable *alias_hash = get_alias_hash ();
|
|
|
|
|
|
|
|
return g_hash_table_lookup (alias_hash, canonical_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2002-12-15 02:35:07 +01:00
|
|
|
g_utf8_get_charset_internal (const char *raw_data,
|
|
|
|
const char **a)
|
2001-09-27 04:49:05 +02:00
|
|
|
{
|
|
|
|
const char *charset = getenv("CHARSET");
|
|
|
|
|
|
|
|
if (charset && *charset)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
2001-09-27 04:49:05 +02:00
|
|
|
*a = charset;
|
|
|
|
|
|
|
|
if (charset && strstr (charset, "UTF-8"))
|
2000-06-21 18:11:21 +02:00
|
|
|
return TRUE;
|
2001-09-27 04:49:05 +02:00
|
|
|
else
|
|
|
|
return FALSE;
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
/* The libcharset code tries to be thread-safe without
|
|
|
|
* a lock, but has a memory leak and a missing memory
|
|
|
|
* barrier, so we lock for it
|
|
|
|
*/
|
|
|
|
G_LOCK (aliases);
|
2002-12-15 02:35:07 +01:00
|
|
|
charset = _g_locale_charset_unalias (raw_data);
|
2001-09-27 04:49:05 +02:00
|
|
|
G_UNLOCK (aliases);
|
|
|
|
|
|
|
|
if (charset && *charset)
|
2000-12-17 19:43:57 +01:00
|
|
|
{
|
2001-09-27 04:49:05 +02:00
|
|
|
*a = charset;
|
2000-12-17 19:43:57 +01:00
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
if (charset && strstr (charset, "UTF-8"))
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
2000-12-17 19:43:57 +01:00
|
|
|
}
|
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
/* Assume this for compatibility at present. */
|
2001-09-27 04:49:05 +02:00
|
|
|
*a = "US-ASCII";
|
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-12-15 02:35:07 +01:00
|
|
|
typedef struct _GCharsetCache GCharsetCache;
|
|
|
|
|
|
|
|
struct _GCharsetCache {
|
|
|
|
gboolean is_utf8;
|
|
|
|
gchar *raw;
|
|
|
|
gchar *charset;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
charset_cache_free (gpointer data)
|
|
|
|
{
|
|
|
|
GCharsetCache *cache = data;
|
|
|
|
g_free (cache->raw);
|
|
|
|
g_free (cache->charset);
|
|
|
|
g_free (cache);
|
|
|
|
}
|
2000-06-21 18:11:21 +02:00
|
|
|
|
2001-04-16 22:05:25 +02:00
|
|
|
/**
|
|
|
|
* g_get_charset:
|
|
|
|
* @charset: return location for character set name
|
|
|
|
*
|
|
|
|
* Obtains the character set for the current locale; you might use
|
|
|
|
* this character set as an argument to g_convert(), to convert from
|
|
|
|
* the current locale's encoding to some other encoding. (Frequently
|
|
|
|
* g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
|
|
|
|
* though.)
|
|
|
|
*
|
|
|
|
* The return value is %TRUE if the locale's encoding is UTF-8, in that
|
|
|
|
* case you can perhaps avoid calling g_convert().
|
|
|
|
*
|
|
|
|
* The string returned in @charset is not allocated, and should not be
|
|
|
|
* freed.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if the returned charset is UTF-8
|
|
|
|
**/
|
2000-06-21 18:11:21 +02:00
|
|
|
gboolean
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
g_get_charset (G_CONST_RETURN char **charset)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
2002-12-15 02:35:07 +01:00
|
|
|
static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
|
|
|
|
GCharsetCache *cache = g_static_private_get (&cache_private);
|
|
|
|
const gchar *raw;
|
|
|
|
|
|
|
|
if (!cache)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
2002-12-15 02:35:07 +01:00
|
|
|
cache = g_new0 (GCharsetCache, 1);
|
|
|
|
g_static_private_set (&cache_private, cache, charset_cache_free);
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
2002-12-15 02:35:07 +01:00
|
|
|
|
|
|
|
raw = _g_locale_charset_raw ();
|
|
|
|
|
|
|
|
if (!(cache->raw && strcmp (cache->raw, raw) == 0))
|
|
|
|
{
|
|
|
|
const gchar *new_charset;
|
|
|
|
|
|
|
|
g_free (cache->raw);
|
|
|
|
g_free (cache->charset);
|
|
|
|
cache->raw = g_strdup (raw);
|
|
|
|
cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
|
|
|
|
cache->charset = g_strdup (new_charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (charset)
|
|
|
|
*charset = cache->charset;
|
|
|
|
|
|
|
|
return cache->is_utf8;
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unicode_strchr */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unichar_to_utf8:
|
2000-09-06 16:42:13 +02:00
|
|
|
* @c: a ISO10646 character code
|
|
|
|
* @outbuf: output buffer, must have at least 6 bytes of space.
|
2000-07-31 20:52:11 +02:00
|
|
|
* If %NULL, the length will be computed and returned
|
2001-09-24 23:28:57 +02:00
|
|
|
* and nothing will be written to @outbuf.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Converts a single character to UTF-8.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
|
|
|
* Return value: number of bytes written
|
|
|
|
**/
|
|
|
|
int
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
g_unichar_to_utf8 (gunichar c,
|
|
|
|
gchar *outbuf)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
guint len = 0;
|
2000-06-21 18:11:21 +02:00
|
|
|
int first;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (c < 0x80)
|
|
|
|
{
|
|
|
|
first = 0;
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
else if (c < 0x800)
|
|
|
|
{
|
|
|
|
first = 0xc0;
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
else if (c < 0x10000)
|
|
|
|
{
|
|
|
|
first = 0xe0;
|
|
|
|
len = 3;
|
|
|
|
}
|
|
|
|
else if (c < 0x200000)
|
|
|
|
{
|
|
|
|
first = 0xf0;
|
|
|
|
len = 4;
|
|
|
|
}
|
|
|
|
else if (c < 0x4000000)
|
|
|
|
{
|
|
|
|
first = 0xf8;
|
|
|
|
len = 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
first = 0xfc;
|
|
|
|
len = 6;
|
|
|
|
}
|
|
|
|
|
2000-07-31 20:52:11 +02:00
|
|
|
if (outbuf)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
2000-07-31 20:52:11 +02:00
|
|
|
for (i = len - 1; i > 0; --i)
|
|
|
|
{
|
|
|
|
outbuf[i] = (c & 0x3f) | 0x80;
|
|
|
|
c >>= 6;
|
|
|
|
}
|
|
|
|
outbuf[0] = c | first;
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_strchr:
|
2001-09-24 23:28:57 +02:00
|
|
|
* @p: a nul-terminated UTF-8 encoded string
|
|
|
|
* @len: the maximum length of @p
|
|
|
|
* @c: a ISO10646 character
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Finds the leftmost occurrence of the given ISO10646 character
|
|
|
|
* in a UTF-8 encoded string, while limiting the search to @len bytes.
|
|
|
|
* If @len is -1, allow unbounded search.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Return value: %NULL if the string does not contain the character,
|
|
|
|
* otherwise, a pointer to the start of the leftmost occurrence of
|
|
|
|
* the character in the string.
|
2000-06-21 18:11:21 +02:00
|
|
|
**/
|
|
|
|
gchar *
|
2001-06-09 01:14:03 +02:00
|
|
|
g_utf8_strchr (const char *p,
|
2001-09-10 17:50:26 +02:00
|
|
|
gssize len,
|
2001-06-09 01:14:03 +02:00
|
|
|
gunichar c)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
|
|
|
gchar ch[10];
|
|
|
|
|
2001-09-10 17:50:26 +02:00
|
|
|
gint charlen = g_unichar_to_utf8 (c, ch);
|
|
|
|
ch[charlen] = '\0';
|
2000-06-21 18:11:21 +02:00
|
|
|
|
2001-09-10 17:50:26 +02:00
|
|
|
return g_strstr_len (p, len, ch);
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
2001-06-09 01:14:03 +02:00
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
/**
|
|
|
|
* g_utf8_strrchr:
|
2001-09-24 23:28:57 +02:00
|
|
|
* @p: a nul-terminated UTF-8 encoded string
|
|
|
|
* @len: the maximum length of @p
|
|
|
|
* @c: a ISO10646 character
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Find the rightmost occurrence of the given ISO10646 character
|
|
|
|
* in a UTF-8 encoded string, while limiting the search to @len bytes.
|
|
|
|
* If @len is -1, allow unbounded search.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Return value: %NULL if the string does not contain the character,
|
|
|
|
* otherwise, a pointer to the start of the rightmost occurrence of the
|
|
|
|
* character in the string.
|
2000-06-21 18:11:21 +02:00
|
|
|
**/
|
|
|
|
gchar *
|
2001-06-09 01:14:03 +02:00
|
|
|
g_utf8_strrchr (const char *p,
|
2001-09-10 17:50:26 +02:00
|
|
|
gssize len,
|
2001-06-09 01:14:03 +02:00
|
|
|
gunichar c)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
|
|
|
gchar ch[10];
|
|
|
|
|
2001-09-10 17:50:26 +02:00
|
|
|
gint charlen = g_unichar_to_utf8 (c, ch);
|
|
|
|
ch[charlen] = '\0';
|
2000-06-21 18:11:21 +02:00
|
|
|
|
2001-09-10 17:50:26 +02:00
|
|
|
return g_strrstr_len (p, len, ch);
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
/* Like g_utf8_get_char, but take a maximum length
|
|
|
|
* and return (gunichar)-2 on incomplete trailing character
|
|
|
|
*/
|
|
|
|
static inline gunichar
|
2001-07-19 16:35:48 +02:00
|
|
|
g_utf8_get_char_extended (const gchar *p,
|
|
|
|
gssize max_len)
|
2001-01-05 22:22:47 +01:00
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
guint i, len;
|
2001-01-05 22:22:47 +01:00
|
|
|
gunichar wc = (guchar) *p;
|
|
|
|
|
|
|
|
if (wc < 0x80)
|
|
|
|
{
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
else if (wc < 0xc0)
|
|
|
|
{
|
|
|
|
return (gunichar)-1;
|
|
|
|
}
|
|
|
|
else if (wc < 0xe0)
|
|
|
|
{
|
|
|
|
len = 2;
|
|
|
|
wc &= 0x1f;
|
|
|
|
}
|
|
|
|
else if (wc < 0xf0)
|
|
|
|
{
|
|
|
|
len = 3;
|
|
|
|
wc &= 0x0f;
|
|
|
|
}
|
|
|
|
else if (wc < 0xf8)
|
|
|
|
{
|
|
|
|
len = 4;
|
|
|
|
wc &= 0x07;
|
|
|
|
}
|
|
|
|
else if (wc < 0xfc)
|
|
|
|
{
|
|
|
|
len = 5;
|
|
|
|
wc &= 0x03;
|
|
|
|
}
|
|
|
|
else if (wc < 0xfe)
|
|
|
|
{
|
|
|
|
len = 6;
|
|
|
|
wc &= 0x01;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (gunichar)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_len >= 0 && len > max_len)
|
|
|
|
{
|
|
|
|
for (i = 1; i < max_len; i++)
|
|
|
|
{
|
|
|
|
if ((((guchar *)p)[i] & 0xc0) != 0x80)
|
|
|
|
return (gunichar)-1;
|
|
|
|
}
|
|
|
|
return (gunichar)-2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < len; ++i)
|
|
|
|
{
|
|
|
|
gunichar ch = ((guchar *)p)[i];
|
|
|
|
|
|
|
|
if ((ch & 0xc0) != 0x80)
|
|
|
|
{
|
|
|
|
if (ch)
|
|
|
|
return (gunichar)-1;
|
|
|
|
else
|
|
|
|
return (gunichar)-2;
|
|
|
|
}
|
|
|
|
|
|
|
|
wc <<= 6;
|
|
|
|
wc |= (ch & 0x3f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UTF8_LENGTH(wc) != len)
|
|
|
|
return (gunichar)-1;
|
|
|
|
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
|
2001-07-19 16:35:48 +02:00
|
|
|
/**
|
|
|
|
* g_utf8_get_char_validated:
|
2001-09-24 23:28:57 +02:00
|
|
|
* @p: a pointer to Unicode character encoded as UTF-8
|
2001-07-19 16:35:48 +02:00
|
|
|
* @max_len: the maximum number of bytes to read, or -1, for no maximum.
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
|
2001-07-19 16:35:48 +02:00
|
|
|
* This function checks for incomplete characters, for invalid characters
|
|
|
|
* such as characters that are out of the range of Unicode, and for
|
|
|
|
* overlong encodings of valid characters.
|
|
|
|
*
|
|
|
|
* Return value: the resulting character. If @p points to a partial
|
|
|
|
* sequence at the end of a string that could begin a valid character,
|
|
|
|
* returns (gunichar)-2; otherwise, if @p does not point to a valid
|
2001-09-24 23:28:57 +02:00
|
|
|
* UTF-8 encoded Unicode character, returns (gunichar)-1.
|
2001-07-19 16:35:48 +02:00
|
|
|
**/
|
|
|
|
gunichar
|
|
|
|
g_utf8_get_char_validated (const gchar *p,
|
|
|
|
gssize max_len)
|
|
|
|
{
|
|
|
|
gunichar result = g_utf8_get_char_extended (p, max_len);
|
|
|
|
|
|
|
|
if (result & 0x80000000)
|
|
|
|
return result;
|
|
|
|
else if (!UNICODE_VALID (result))
|
|
|
|
return (gunichar)-1;
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-06-21 18:11:21 +02:00
|
|
|
/**
|
2001-01-05 22:22:47 +01:00
|
|
|
* g_utf8_to_ucs4_fast:
|
|
|
|
* @str: a UTF-8 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
|
|
|
* the string is nul-terminated.
|
2001-01-05 22:22:47 +01:00
|
|
|
* @items_written: location to store the number of characters in the
|
|
|
|
* result, or %NULL.
|
|
|
|
*
|
2000-06-21 18:11:21 +02:00
|
|
|
* Convert a string from UTF-8 to a 32-bit fixed width
|
2001-01-05 22:22:47 +01:00
|
|
|
* representation as UCS-4, assuming valid UTF-8 input.
|
|
|
|
* This function is roughly twice as fast as g_utf8_to_ucs4()
|
|
|
|
* but does no error checking on the input.
|
2000-06-21 18:11:21 +02:00
|
|
|
*
|
|
|
|
* Return value: a pointer to a newly allocated UCS-4 string.
|
2001-09-24 23:28:57 +02:00
|
|
|
* This value must be freed with g_free().
|
2000-06-21 18:11:21 +02:00
|
|
|
**/
|
|
|
|
gunichar *
|
2001-01-05 22:22:47 +01:00
|
|
|
g_utf8_to_ucs4_fast (const gchar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len,
|
|
|
|
glong *items_written)
|
2000-06-21 18:11:21 +02:00
|
|
|
{
|
2001-01-05 22:22:47 +01:00
|
|
|
gint j, charlen;
|
2000-06-21 18:11:21 +02:00
|
|
|
gunichar *result;
|
|
|
|
gint n_chars, i;
|
|
|
|
const gchar *p;
|
2001-01-05 22:22:47 +01:00
|
|
|
|
|
|
|
g_return_val_if_fail (str != NULL, NULL);
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
n_chars = 0;
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
p = g_utf8_next_char (p);
|
|
|
|
++n_chars;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-05-02 17:19:55 +02:00
|
|
|
while (p < str + len && *p)
|
2001-01-05 22:22:47 +01:00
|
|
|
{
|
|
|
|
p = g_utf8_next_char (p);
|
|
|
|
++n_chars;
|
|
|
|
}
|
|
|
|
}
|
2000-06-21 18:11:21 +02:00
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
result = g_new (gunichar, n_chars + 1);
|
2000-06-21 18:11:21 +02:00
|
|
|
|
|
|
|
p = str;
|
|
|
|
for (i=0; i < n_chars; i++)
|
|
|
|
{
|
2001-01-05 22:22:47 +01:00
|
|
|
gunichar wc = ((unsigned char *)p)[0];
|
|
|
|
|
|
|
|
if (wc < 0x80)
|
|
|
|
{
|
|
|
|
result[i] = wc;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (wc < 0xe0)
|
|
|
|
{
|
|
|
|
charlen = 2;
|
|
|
|
wc &= 0x1f;
|
|
|
|
}
|
|
|
|
else if (wc < 0xf0)
|
|
|
|
{
|
|
|
|
charlen = 3;
|
|
|
|
wc &= 0x0f;
|
|
|
|
}
|
|
|
|
else if (wc < 0xf8)
|
|
|
|
{
|
|
|
|
charlen = 4;
|
|
|
|
wc &= 0x07;
|
|
|
|
}
|
|
|
|
else if (wc < 0xfc)
|
|
|
|
{
|
|
|
|
charlen = 5;
|
|
|
|
wc &= 0x03;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
charlen = 6;
|
|
|
|
wc &= 0x01;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 1; j < charlen; j++)
|
|
|
|
{
|
|
|
|
wc <<= 6;
|
|
|
|
wc |= ((unsigned char *)p)[j] & 0x3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = wc;
|
|
|
|
p += charlen;
|
|
|
|
}
|
2000-06-21 18:11:21 +02:00
|
|
|
}
|
2001-01-05 22:22:47 +01:00
|
|
|
result[i] = 0;
|
|
|
|
|
|
|
|
if (items_written)
|
|
|
|
*items_written = i;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_to_ucs4:
|
|
|
|
* @str: a UTF-8 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
|
|
|
* the string is nul-terminated.
|
2001-01-05 22:22:47 +01:00
|
|
|
* @items_read: location to store number of bytes read, or %NULL.
|
|
|
|
* If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
|
|
|
|
* returned in case @str contains a trailing partial
|
|
|
|
* character. If an error occurs then the index of the
|
|
|
|
* invalid input is stored here.
|
|
|
|
* @items_written: location to store number of characters written or %NULL.
|
|
|
|
* The value here stored does not include the trailing 0
|
|
|
|
* character.
|
|
|
|
* @error: location to store the error occuring, or %NULL to ignore
|
|
|
|
* errors. Any of the errors in #GConvertError other than
|
|
|
|
* %G_CONVERT_ERROR_NO_CONVERSION may occur.
|
|
|
|
*
|
|
|
|
* Convert a string from UTF-8 to a 32-bit fixed width
|
|
|
|
* representation as UCS-4. A trailing 0 will be added to the
|
|
|
|
* string after the converted text.
|
|
|
|
*
|
|
|
|
* Return value: a pointer to a newly allocated UCS-4 string.
|
|
|
|
* This value must be freed with g_free(). If an
|
|
|
|
* error occurs, %NULL will be returned and
|
|
|
|
* @error set.
|
|
|
|
**/
|
|
|
|
gunichar *
|
|
|
|
g_utf8_to_ucs4 (const gchar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len,
|
|
|
|
glong *items_read,
|
|
|
|
glong *items_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gunichar *result = NULL;
|
|
|
|
gint n_chars, i;
|
|
|
|
const gchar *in;
|
|
|
|
|
|
|
|
in = str;
|
|
|
|
n_chars = 0;
|
|
|
|
while ((len < 0 || str + len - in > 0) && *in)
|
|
|
|
{
|
|
|
|
gunichar wc = g_utf8_get_char_extended (in, str + len - in);
|
|
|
|
if (wc & 0x80000000)
|
|
|
|
{
|
|
|
|
if (wc == (gunichar)-2)
|
|
|
|
{
|
|
|
|
if (items_read)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
|
|
|
|
_("Partial character sequence at end of input"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid byte sequence in conversion input"));
|
|
|
|
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
n_chars++;
|
|
|
|
|
|
|
|
in = g_utf8_next_char (in);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = g_new (gunichar, n_chars + 1);
|
|
|
|
|
|
|
|
in = str;
|
|
|
|
for (i=0; i < n_chars; i++)
|
|
|
|
{
|
|
|
|
result[i] = g_utf8_get_char (in);
|
|
|
|
in = g_utf8_next_char (in);
|
|
|
|
}
|
|
|
|
result[i] = 0;
|
|
|
|
|
|
|
|
if (items_written)
|
|
|
|
*items_written = n_chars;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (items_read)
|
|
|
|
*items_read = in - str;
|
2000-06-21 18:11:21 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-09-18 16:55:24 +02:00
|
|
|
/**
|
|
|
|
* g_ucs4_to_utf8:
|
|
|
|
* @str: a UCS-4 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
|
|
|
* the string is terminated with a 0 character.
|
2001-01-05 22:22:47 +01:00
|
|
|
* @items_read: location to store number of characters read read, or %NULL.
|
|
|
|
* @items_written: location to store number of bytes written or %NULL.
|
|
|
|
* The value here stored does not include the trailing 0
|
|
|
|
* byte.
|
|
|
|
* @error: location to store the error occuring, or %NULL to ignore
|
|
|
|
* errors. Any of the errors in #GConvertError other than
|
|
|
|
* %G_CONVERT_ERROR_NO_CONVERSION may occur.
|
|
|
|
*
|
2000-09-18 16:55:24 +02:00
|
|
|
* Convert a string from a 32-bit fixed width representation as UCS-4.
|
2001-01-05 22:22:47 +01:00
|
|
|
* to UTF-8. The result will be terminated with a 0 byte.
|
2000-09-18 16:55:24 +02:00
|
|
|
*
|
|
|
|
* Return value: a pointer to a newly allocated UTF-8 string.
|
2001-01-05 22:22:47 +01:00
|
|
|
* This value must be freed with g_free(). If an
|
|
|
|
* error occurs, %NULL will be returned and
|
|
|
|
* @error set.
|
2000-09-18 16:55:24 +02:00
|
|
|
**/
|
|
|
|
gchar *
|
2001-01-05 22:22:47 +01:00
|
|
|
g_ucs4_to_utf8 (const gunichar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len,
|
|
|
|
glong *items_read,
|
|
|
|
glong *items_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
2000-09-18 16:55:24 +02:00
|
|
|
{
|
|
|
|
gint result_length;
|
2001-01-05 22:22:47 +01:00
|
|
|
gchar *result = NULL;
|
|
|
|
gchar *p;
|
2000-09-18 16:55:24 +02:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
result_length = 0;
|
2001-01-05 22:22:47 +01:00
|
|
|
for (i = 0; len < 0 || i < len ; i++)
|
|
|
|
{
|
|
|
|
if (!str[i])
|
|
|
|
break;
|
2000-09-18 16:55:24 +02:00
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
if (str[i] >= 0x80000000)
|
|
|
|
{
|
|
|
|
if (items_read)
|
|
|
|
*items_read = i;
|
|
|
|
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Character out of range for UTF-8"));
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
result_length += UTF8_LENGTH (str[i]);
|
|
|
|
}
|
2000-09-18 16:55:24 +02:00
|
|
|
|
|
|
|
result = g_malloc (result_length + 1);
|
|
|
|
p = result;
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
i = 0;
|
|
|
|
while (p < result + result_length)
|
|
|
|
p += g_unichar_to_utf8 (str[i++], p);
|
2000-09-18 16:55:24 +02:00
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
if (items_written)
|
|
|
|
*items_written = p - result;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (items_read)
|
|
|
|
*items_read = i;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf16_to_utf8:
|
|
|
|
* @str: a UTF-16 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
2001-01-05 22:22:47 +01:00
|
|
|
* the string is terminated with a 0 character.
|
|
|
|
* @items_read: location to store number of words read, or %NULL.
|
|
|
|
* If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
|
|
|
|
* returned in case @str contains a trailing partial
|
|
|
|
* character. If an error occurs then the index of the
|
|
|
|
* invalid input is stored here.
|
|
|
|
* @items_written: location to store number of bytes written, or %NULL.
|
|
|
|
* The value stored here does not include the trailing
|
|
|
|
* 0 byte.
|
|
|
|
* @error: location to store the error occuring, or %NULL to ignore
|
|
|
|
* errors. Any of the errors in #GConvertError other than
|
|
|
|
* %G_CONVERT_ERROR_NO_CONVERSION may occur.
|
|
|
|
*
|
|
|
|
* Convert a string from UTF-16 to UTF-8. The result will be
|
|
|
|
* terminated with a 0 byte.
|
|
|
|
*
|
|
|
|
* Return value: a pointer to a newly allocated UTF-8 string.
|
|
|
|
* This value must be freed with g_free(). If an
|
|
|
|
* error occurs, %NULL will be returned and
|
|
|
|
* @error set.
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_utf16_to_utf8 (const gunichar2 *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len,
|
|
|
|
glong *items_read,
|
|
|
|
glong *items_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
/* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
|
|
|
|
* are marked.
|
|
|
|
*/
|
|
|
|
const gunichar2 *in;
|
|
|
|
gchar *out;
|
|
|
|
gchar *result = NULL;
|
|
|
|
gint n_bytes;
|
|
|
|
gunichar high_surrogate;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str != 0, NULL);
|
|
|
|
|
|
|
|
n_bytes = 0;
|
|
|
|
in = str;
|
|
|
|
high_surrogate = 0;
|
|
|
|
while ((len < 0 || in - str < len) && *in)
|
|
|
|
{
|
|
|
|
gunichar2 c = *in;
|
|
|
|
gunichar wc;
|
|
|
|
|
|
|
|
if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
|
|
|
|
{
|
|
|
|
if (high_surrogate)
|
|
|
|
{
|
|
|
|
wc = SURROGATE_VALUE (high_surrogate, c);
|
|
|
|
high_surrogate = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid sequence in conversion input"));
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (high_surrogate)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid sequence in conversion input"));
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
|
|
|
|
{
|
|
|
|
high_surrogate = c;
|
|
|
|
goto next1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wc = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
n_bytes += UTF8_LENGTH (wc);
|
|
|
|
|
|
|
|
next1:
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (high_surrogate && !items_read)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
|
|
|
|
_("Partial character sequence at end of input"));
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point, everything is valid, and we just need to convert
|
|
|
|
*/
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
result = g_malloc (n_bytes + 1);
|
|
|
|
|
|
|
|
high_surrogate = 0;
|
|
|
|
out = result;
|
|
|
|
in = str;
|
|
|
|
while (out < result + n_bytes)
|
|
|
|
{
|
|
|
|
gunichar2 c = *in;
|
|
|
|
gunichar wc;
|
|
|
|
|
|
|
|
if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
|
|
|
|
{
|
|
|
|
wc = SURROGATE_VALUE (high_surrogate, c);
|
|
|
|
high_surrogate = 0;
|
|
|
|
}
|
|
|
|
else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
|
|
|
|
{
|
|
|
|
high_surrogate = c;
|
|
|
|
goto next2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wc = c;
|
|
|
|
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
out += g_unichar_to_utf8 (wc, out);
|
|
|
|
|
|
|
|
next2:
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
*out = '\0';
|
|
|
|
|
|
|
|
if (items_written)
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
*items_written = out - result;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (items_read)
|
|
|
|
*items_read = in - str;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf16_to_ucs4:
|
|
|
|
* @str: a UTF-16 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
2001-01-05 22:22:47 +01:00
|
|
|
* the string is terminated with a 0 character.
|
|
|
|
* @items_read: location to store number of words read, or %NULL.
|
|
|
|
* If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
|
|
|
|
* returned in case @str contains a trailing partial
|
|
|
|
* character. If an error occurs then the index of the
|
|
|
|
* invalid input is stored here.
|
|
|
|
* @items_written: location to store number of characters written, or %NULL.
|
|
|
|
* The value stored here does not include the trailing
|
|
|
|
* 0 character.
|
|
|
|
* @error: location to store the error occuring, or %NULL to ignore
|
|
|
|
* errors. Any of the errors in #GConvertError other than
|
|
|
|
* %G_CONVERT_ERROR_NO_CONVERSION may occur.
|
|
|
|
*
|
|
|
|
* Convert a string from UTF-16 to UCS-4. The result will be
|
|
|
|
* terminated with a 0 character.
|
|
|
|
*
|
|
|
|
* Return value: a pointer to a newly allocated UCS-4 string.
|
|
|
|
* This value must be freed with g_free(). If an
|
|
|
|
* error occurs, %NULL will be returned and
|
|
|
|
* @error set.
|
|
|
|
**/
|
|
|
|
gunichar *
|
|
|
|
g_utf16_to_ucs4 (const gunichar2 *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len,
|
|
|
|
glong *items_read,
|
|
|
|
glong *items_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
const gunichar2 *in;
|
|
|
|
gchar *out;
|
|
|
|
gchar *result = NULL;
|
|
|
|
gint n_bytes;
|
|
|
|
gunichar high_surrogate;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str != 0, NULL);
|
|
|
|
|
|
|
|
n_bytes = 0;
|
|
|
|
in = str;
|
|
|
|
high_surrogate = 0;
|
|
|
|
while ((len < 0 || in - str < len) && *in)
|
|
|
|
{
|
|
|
|
gunichar2 c = *in;
|
|
|
|
gunichar wc;
|
|
|
|
|
|
|
|
if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
|
|
|
|
{
|
|
|
|
if (high_surrogate)
|
|
|
|
{
|
|
|
|
wc = SURROGATE_VALUE (high_surrogate, c);
|
|
|
|
high_surrogate = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid sequence in conversion input"));
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (high_surrogate)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid sequence in conversion input"));
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
|
|
|
|
{
|
|
|
|
high_surrogate = c;
|
|
|
|
goto next1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wc = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
n_bytes += sizeof (gunichar);
|
|
|
|
|
|
|
|
next1:
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (high_surrogate && !items_read)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
|
|
|
|
_("Partial character sequence at end of input"));
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point, everything is valid, and we just need to convert
|
|
|
|
*/
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
result = g_malloc (n_bytes + 4);
|
|
|
|
|
|
|
|
high_surrogate = 0;
|
|
|
|
out = result;
|
|
|
|
in = str;
|
|
|
|
while (out < result + n_bytes)
|
|
|
|
{
|
|
|
|
gunichar2 c = *in;
|
|
|
|
gunichar wc;
|
|
|
|
|
|
|
|
if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
|
|
|
|
{
|
|
|
|
wc = SURROGATE_VALUE (high_surrogate, c);
|
|
|
|
high_surrogate = 0;
|
|
|
|
}
|
|
|
|
else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
|
|
|
|
{
|
|
|
|
high_surrogate = c;
|
|
|
|
goto next2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wc = c;
|
|
|
|
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
*(gunichar *)out = wc;
|
|
|
|
out += sizeof (gunichar);
|
|
|
|
|
|
|
|
next2:
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
*(gunichar *)out = 0;
|
|
|
|
|
|
|
|
if (items_written)
|
|
|
|
/********** DIFFERENT for UTF8/UCS4 **********/
|
|
|
|
*items_written = (out - result) / sizeof (gunichar);
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (items_read)
|
|
|
|
*items_read = in - str;
|
|
|
|
|
|
|
|
return (gunichar *)result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_to_utf16:
|
|
|
|
* @str: a UTF-8 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
|
|
|
* the string is nul-terminated.
|
2001-01-05 22:22:47 +01:00
|
|
|
* @items_read: location to store number of bytes read, or %NULL.
|
|
|
|
* If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
|
|
|
|
* returned in case @str contains a trailing partial
|
|
|
|
* character. If an error occurs then the index of the
|
|
|
|
* invalid input is stored here.
|
|
|
|
* @items_written: location to store number of words written, or %NULL.
|
|
|
|
* The value stored here does not include the trailing
|
|
|
|
* 0 word.
|
|
|
|
* @error: location to store the error occuring, or %NULL to ignore
|
|
|
|
* errors. Any of the errors in #GConvertError other than
|
|
|
|
* %G_CONVERT_ERROR_NO_CONVERSION may occur.
|
|
|
|
*
|
|
|
|
* Convert a string from UTF-8 to UTF-16. A 0 word will be
|
|
|
|
* added to the result after the converted text.
|
|
|
|
*
|
|
|
|
* Return value: a pointer to a newly allocated UTF-16 string.
|
|
|
|
* This value must be freed with g_free(). If an
|
|
|
|
* error occurs, %NULL will be returned and
|
|
|
|
* @error set.
|
|
|
|
**/
|
|
|
|
gunichar2 *
|
|
|
|
g_utf8_to_utf16 (const gchar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len,
|
|
|
|
glong *items_read,
|
|
|
|
glong *items_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gunichar2 *result = NULL;
|
|
|
|
gint n16;
|
|
|
|
const gchar *in;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str != NULL, NULL);
|
|
|
|
|
|
|
|
in = str;
|
|
|
|
n16 = 0;
|
|
|
|
while ((len < 0 || str + len - in > 0) && *in)
|
|
|
|
{
|
|
|
|
gunichar wc = g_utf8_get_char_extended (in, str + len - in);
|
|
|
|
if (wc & 0x80000000)
|
|
|
|
{
|
|
|
|
if (wc == (gunichar)-2)
|
|
|
|
{
|
|
|
|
if (items_read)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
|
|
|
|
_("Partial character sequence at end of input"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid byte sequence in conversion input"));
|
|
|
|
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wc < 0xd800)
|
|
|
|
n16 += 1;
|
|
|
|
else if (wc < 0xe000)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid sequence in conversion input"));
|
|
|
|
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
else if (wc < 0x10000)
|
|
|
|
n16 += 1;
|
|
|
|
else if (wc < 0x110000)
|
|
|
|
n16 += 2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Character out of range for UTF-16"));
|
|
|
|
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
in = g_utf8_next_char (in);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = g_new (gunichar2, n16 + 1);
|
|
|
|
|
|
|
|
in = str;
|
|
|
|
for (i = 0; i < n16;)
|
|
|
|
{
|
|
|
|
gunichar wc = g_utf8_get_char (in);
|
|
|
|
|
|
|
|
if (wc < 0x10000)
|
|
|
|
{
|
|
|
|
result[i++] = wc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
|
|
|
|
result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
|
|
|
|
}
|
|
|
|
|
|
|
|
in = g_utf8_next_char (in);
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = 0;
|
|
|
|
|
|
|
|
if (items_written)
|
|
|
|
*items_written = n16;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (items_read)
|
|
|
|
*items_read = in - str;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_ucs4_to_utf16:
|
|
|
|
* @str: a UCS-4 encoded string
|
2001-09-24 23:28:57 +02:00
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
|
|
|
* the string is terminated with a 0 character.
|
2001-01-05 22:22:47 +01:00
|
|
|
* @items_read: location to store number of bytes read, or %NULL.
|
|
|
|
* If an error occurs then the index of the invalid input
|
|
|
|
* is stored here.
|
|
|
|
* @items_written: location to store number of words written, or %NULL.
|
|
|
|
* The value stored here does not include the trailing
|
|
|
|
* 0 word.
|
|
|
|
* @error: location to store the error occuring, or %NULL to ignore
|
|
|
|
* errors. Any of the errors in #GConvertError other than
|
|
|
|
* %G_CONVERT_ERROR_NO_CONVERSION may occur.
|
|
|
|
*
|
|
|
|
* Convert a string from UCS-4 to UTF-16. A 0 word will be
|
|
|
|
* added to the result after the converted text.
|
|
|
|
*
|
|
|
|
* Return value: a pointer to a newly allocated UTF-16 string.
|
|
|
|
* This value must be freed with g_free(). If an
|
|
|
|
* error occurs, %NULL will be returned and
|
|
|
|
* @error set.
|
|
|
|
**/
|
|
|
|
gunichar2 *
|
|
|
|
g_ucs4_to_utf16 (const gunichar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
glong len,
|
|
|
|
glong *items_read,
|
|
|
|
glong *items_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gunichar2 *result = NULL;
|
|
|
|
gint n16;
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
n16 = 0;
|
|
|
|
i = 0;
|
|
|
|
while ((len < 0 || i < len) && str[i])
|
|
|
|
{
|
|
|
|
gunichar wc = str[i];
|
|
|
|
|
|
|
|
if (wc < 0xd800)
|
|
|
|
n16 += 1;
|
|
|
|
else if (wc < 0xe000)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid sequence in conversion input"));
|
|
|
|
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
else if (wc < 0x10000)
|
|
|
|
n16 += 1;
|
|
|
|
else if (wc < 0x110000)
|
|
|
|
n16 += 2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Character out of range for UTF-16"));
|
|
|
|
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = g_new (gunichar2, n16 + 1);
|
|
|
|
|
|
|
|
for (i = 0, j = 0; j < n16; i++)
|
|
|
|
{
|
|
|
|
gunichar wc = str[i];
|
|
|
|
|
|
|
|
if (wc < 0x10000)
|
|
|
|
{
|
|
|
|
result[j++] = wc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
|
|
|
|
result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result[j] = 0;
|
|
|
|
|
|
|
|
if (items_written)
|
|
|
|
*items_written = n16;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (items_read)
|
|
|
|
*items_read = i;
|
|
|
|
|
2000-09-18 16:55:24 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-09-11 02:09:31 +02:00
|
|
|
/**
|
|
|
|
* g_utf8_validate:
|
|
|
|
* @str: a pointer to character data
|
|
|
|
* @max_len: max bytes to validate, or -1 to go until nul
|
|
|
|
* @end: return location for end of valid data
|
|
|
|
*
|
|
|
|
* Validates UTF-8 encoded text. @str is the text to validate;
|
|
|
|
* if @str is nul-terminated, then @max_len can be -1, otherwise
|
|
|
|
* @max_len should be the number of bytes to validate.
|
2001-05-15 21:18:08 +02:00
|
|
|
* If @end is non-%NULL, then the end of the valid range
|
2000-09-11 02:09:31 +02:00
|
|
|
* will be stored there (i.e. the address of the first invalid byte
|
|
|
|
* if some bytes were invalid, or the end of the text being validated
|
|
|
|
* otherwise).
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Returns %TRUE if all of @str was valid. Many GLib and GTK+
|
|
|
|
* routines <emphasis>require</emphasis> valid UTF-8 as input;
|
2000-09-11 02:09:31 +02:00
|
|
|
* so data read from a file or the network should be checked
|
|
|
|
* with g_utf8_validate() before doing anything else with it.
|
|
|
|
*
|
2001-09-24 23:28:57 +02:00
|
|
|
* Return value: %TRUE if the text was valid UTF-8
|
2000-09-11 02:09:31 +02:00
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_utf8_validate (const gchar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gssize max_len,
|
2000-09-11 02:09:31 +02:00
|
|
|
const gchar **end)
|
|
|
|
{
|
|
|
|
|
|
|
|
const gchar *p;
|
2001-01-05 22:22:47 +01:00
|
|
|
|
|
|
|
g_return_val_if_fail (str != NULL, FALSE);
|
2000-09-11 02:09:31 +02:00
|
|
|
|
|
|
|
if (end)
|
|
|
|
*end = str;
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
|
|
|
|
while ((max_len < 0 || (p - str) < max_len) && *p)
|
|
|
|
{
|
|
|
|
int i, mask = 0, len;
|
|
|
|
gunichar result;
|
|
|
|
unsigned char c = (unsigned char) *p;
|
|
|
|
|
|
|
|
UTF8_COMPUTE (c, mask, len);
|
|
|
|
|
|
|
|
if (len == -1)
|
2000-12-14 00:54:51 +01:00
|
|
|
break;
|
2000-09-11 02:09:31 +02:00
|
|
|
|
|
|
|
/* check that the expected number of bytes exists in str */
|
|
|
|
if (max_len >= 0 &&
|
|
|
|
((max_len - (p - str)) < len))
|
2000-12-14 00:54:51 +01:00
|
|
|
break;
|
2000-09-11 02:09:31 +02:00
|
|
|
|
|
|
|
UTF8_GET (result, p, i, mask, len);
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
|
|
|
|
break;
|
|
|
|
|
2000-09-11 02:09:31 +02:00
|
|
|
if (result == (gunichar)-1)
|
2000-12-14 00:54:51 +01:00
|
|
|
break;
|
2001-01-05 22:22:47 +01:00
|
|
|
|
|
|
|
if (!UNICODE_VALID (result))
|
|
|
|
break;
|
2000-09-11 02:09:31 +02:00
|
|
|
|
|
|
|
p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end)
|
|
|
|
*end = p;
|
2000-12-14 00:54:51 +01:00
|
|
|
|
|
|
|
/* See that we covered the entire length if a length was
|
|
|
|
* passed in, or that we ended on a nul if not
|
|
|
|
*/
|
|
|
|
if (max_len >= 0 &&
|
|
|
|
p != (str + max_len))
|
|
|
|
return FALSE;
|
|
|
|
else if (max_len < 0 &&
|
|
|
|
*p != '\0')
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
2000-09-11 02:09:31 +02:00
|
|
|
}
|
|
|
|
|
2001-03-20 01:55:44 +01:00
|
|
|
/**
|
|
|
|
* g_unichar_validate:
|
|
|
|
* @ch: a Unicode character
|
|
|
|
*
|
|
|
|
* Checks whether @ch is a valid Unicode character. Some possible
|
|
|
|
* integer values of @ch will not be valid. 0 is considered a valid
|
|
|
|
* character, though it's normally a string terminator.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if @ch is a valid Unicode character
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_unichar_validate (gunichar ch)
|
|
|
|
{
|
|
|
|
return UNICODE_VALID (ch);
|
|
|
|
}
|
2002-07-26 21:48:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_utf8_strreverse:
|
|
|
|
* @str: a UTF-8 encoded string
|
|
|
|
* @len: the maximum length of @str to use. If @len < 0, then
|
|
|
|
* the string is nul-terminated.
|
|
|
|
*
|
|
|
|
* Reverses a UTF-8 string. @str must be valid UTF-8 encoded text.
|
|
|
|
* (Use g_utf8_validate() on all text before trying to use UTF-8
|
|
|
|
* utility functions with it.)
|
|
|
|
*
|
|
|
|
* Note that unlike g_strreverse(), this function returns
|
|
|
|
* newly-allocated memory, which should be freed with g_free() when
|
|
|
|
* no longer needed.
|
|
|
|
*
|
|
|
|
* Returns: a newly-allocated string which is the reverse of @str.
|
2002-11-28 01:15:45 +01:00
|
|
|
*
|
|
|
|
* Since: 2.2
|
2002-07-26 21:48:02 +02:00
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
g_utf8_strreverse (const gchar *str,
|
|
|
|
gssize len)
|
|
|
|
{
|
|
|
|
gchar *result;
|
|
|
|
const gchar *p;
|
|
|
|
gchar *m, *r, skip;
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen (str);
|
|
|
|
|
|
|
|
result = g_new (gchar, len + 1);
|
|
|
|
r = result + len;
|
|
|
|
p = str;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
skip = g_utf8_skip[*(guchar*)p];
|
|
|
|
r -= skip;
|
|
|
|
for (m = r; skip; skip--)
|
|
|
|
*m++ = *p++;
|
|
|
|
}
|
|
|
|
result[len] = 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|