mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-13 07:56:17 +01:00
[MacOS] Fallback to CFStringGetCSTring if CFStringGetCStringPtr fails.
This commit is contained in:
parent
cd97f93bf7
commit
c60226e0a1
@ -175,16 +175,46 @@ static gchar *
|
|||||||
create_cstr_from_cfstring (CFStringRef str)
|
create_cstr_from_cfstring (CFStringRef str)
|
||||||
{
|
{
|
||||||
const gchar *cstr;
|
const gchar *cstr;
|
||||||
|
CFIndex length = CFStringGetLength (str);
|
||||||
|
char *buffer = NULL;
|
||||||
|
|
||||||
if (str == NULL)
|
if (str == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
|
cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
|
||||||
|
/* CFStringGetCStringPtr returns "NULL if the internal storage of
|
||||||
|
* theString does not allow [a pointer] to be returned efficiently".
|
||||||
|
* (Apple's docs don't say what that means). In that case we must
|
||||||
|
* use CFStringGetCString as a fallback.
|
||||||
|
*/
|
||||||
|
if (cstr != NULL)
|
||||||
|
{
|
||||||
CFRelease (str);
|
CFRelease (str);
|
||||||
|
|
||||||
return g_strdup (cstr);
|
return g_strdup (cstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer = g_malloc0 (length + 1);
|
||||||
|
/* Start off with a buffer size sufficient for the most likely case
|
||||||
|
* that the CFString is ASCII so the UTF8 representation will be 1
|
||||||
|
* byte per code point. Keep trying up to 4 bytes per code point,
|
||||||
|
* the max allowed by RFC3629.
|
||||||
|
*/
|
||||||
|
for (int i = 1; i <= 4; ++i)
|
||||||
|
{
|
||||||
|
if (CFStringGetCString (str, buffer, length, kCFStringEncodingUTF8))
|
||||||
|
{
|
||||||
|
CFRelease (str);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
length += length;
|
||||||
|
buffer = g_realloc (buffer, length + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_free (buffer);
|
||||||
|
CFRelease (str);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
url_escape_hostname (const char *url)
|
url_escape_hostname (const char *url)
|
||||||
{
|
{
|
||||||
|
@ -53,16 +53,46 @@ static gchar *
|
|||||||
create_cstr_from_cfstring (CFStringRef str)
|
create_cstr_from_cfstring (CFStringRef str)
|
||||||
{
|
{
|
||||||
const gchar *cstr;
|
const gchar *cstr;
|
||||||
|
CFIndex length = CFStringGetLength (str);
|
||||||
|
char *buffer = NULL;
|
||||||
|
|
||||||
if (str == NULL)
|
if (str == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
|
cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
|
||||||
|
/* CFStringGetCStringPtr returns "NULL if the internal storage of
|
||||||
|
* theString does not allow [a pointer] to be returned efficiently".
|
||||||
|
* (Apple's docs don't say what that means). In that case we must
|
||||||
|
* use CFStringGetCString as a fallback.
|
||||||
|
*/
|
||||||
|
if (cstr != NULL)
|
||||||
|
{
|
||||||
CFRelease (str);
|
CFRelease (str);
|
||||||
|
|
||||||
return g_strdup (cstr);
|
return g_strdup (cstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer = g_malloc0 (length + 1);
|
||||||
|
/* Start off with a buffer size sufficient for the most likely case
|
||||||
|
* that the CFString is ASCII so the UTF8 representation will be 1
|
||||||
|
* byte per code point. Keep trying up to 4 bytes per code point,
|
||||||
|
* the max allowed by RFC3629.
|
||||||
|
*/
|
||||||
|
for (int i = 1; i <= 4; ++i)
|
||||||
|
{
|
||||||
|
if (CFStringGetCString (str, buffer, length, kCFStringEncodingUTF8))
|
||||||
|
{
|
||||||
|
CFRelease (str);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
length += length;
|
||||||
|
buffer = g_realloc (buffer, length + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_free (buffer);
|
||||||
|
CFRelease (str);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/*< internal >
|
/*< internal >
|
||||||
* create_cstr_from_cfstring_with_fallback:
|
* create_cstr_from_cfstring_with_fallback:
|
||||||
* @str: a #CFStringRef
|
* @str: a #CFStringRef
|
||||||
|
Loading…
Reference in New Issue
Block a user