Commit 4411d2ba authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Move UTF32 conversion to more appropriate file

parent 48827db4
...@@ -5,8 +5,7 @@ ...@@ -5,8 +5,7 @@
* Copyright (C) 2003-2005 the VideoLAN team * Copyright (C) 2003-2005 the VideoLAN team
* $Id$ * $Id$
* *
* Authors: Derk-Jan Hartman <thedj at users.sf.net> * Author: Derk-Jan Hartman <thedj at users.sf.net>
* Rémi Denis-Courmont <rem at videolan.org>
* *
* vlc_current_charset() an adaption of mp_locale_charset(): * vlc_current_charset() an adaption of mp_locale_charset():
* *
...@@ -375,69 +374,3 @@ char *__vlc_fix_readdir_charset( vlc_object_t *p_this, const char *psz_string ) ...@@ -375,69 +374,3 @@ char *__vlc_fix_readdir_charset( vlc_object_t *p_this, const char *psz_string )
return strdup( psz_string ); return strdup( psz_string );
} }
/**********************************************************************
* UTF32toUTF8: converts UTF-32 to UTF-8
*********************************************************************/
char *UTF32toUTF8( const wchar_t *src, size_t len, size_t *newlen )
{
char *res, *out;
/* allocate memory */
out = res = (char *)malloc( 6 * len );
if( res == NULL )
return NULL;
while( len > 0 )
{
uint32_t uv = *src++;
len--;
if( uv < 0x80 )
{
*out++ = uv;
continue;
}
else
if( uv < 0x800 )
{
*out++ = (( uv >> 6) | 0xc0);
*out++ = (( uv & 0x3f) | 0x80);
continue;
}
else
if( uv < 0x10000 )
{
*out++ = (( uv >> 12) | 0xe0);
*out++ = (((uv >> 6) & 0x3f) | 0x80);
*out++ = (( uv & 0x3f) | 0x80);
continue;
}
else
{
*out++ = (( uv >> 18) | 0xf0);
*out++ = (((uv >> 12) & 0x3f) | 0x80);
*out++ = (((uv >> 6) & 0x3f) | 0x80);
*out++ = (( uv & 0x3f) | 0x80);
continue;
}
}
len = out - res;
res = realloc( res, len );
if( newlen != NULL )
*newlen = len;
return res;
}
char *FromUTF32( const wchar_t *src )
{
size_t len;
const wchar_t *in;
/* determine the size of the string */
for( len = 1, in = src; GetWBE( in ); len++ )
in++;
return UTF32toUTF8( src, len, NULL );
}
...@@ -174,3 +174,73 @@ char *EnsureUTF8( char *str ) ...@@ -174,3 +174,73 @@ char *EnsureUTF8( char *str )
return str; return str;
} }
/**********************************************************************
* UTF32toUTF8: converts an array from UTF-32 to UTF-8
*********************************************************************/
char *UTF32toUTF8( const wchar_t *src, size_t len, size_t *newlen )
{
char *res, *out;
/* allocate memory */
out = res = (char *)malloc( 6 * len );
if( res == NULL )
return NULL;
while( len > 0 )
{
uint32_t uv = *src++;
len--;
if( uv < 0x80 )
{
*out++ = uv;
continue;
}
else
if( uv < 0x800 )
{
*out++ = (( uv >> 6) | 0xc0);
*out++ = (( uv & 0x3f) | 0x80);
continue;
}
else
if( uv < 0x10000 )
{
*out++ = (( uv >> 12) | 0xe0);
*out++ = (((uv >> 6) & 0x3f) | 0x80);
*out++ = (( uv & 0x3f) | 0x80);
continue;
}
else
{
*out++ = (( uv >> 18) | 0xf0);
*out++ = (((uv >> 12) & 0x3f) | 0x80);
*out++ = (((uv >> 6) & 0x3f) | 0x80);
*out++ = (( uv & 0x3f) | 0x80);
continue;
}
}
len = out - res;
res = realloc( res, len );
if( newlen != NULL )
*newlen = len;
return res;
}
/**********************************************************************
* FromUTF32: converts an UTF-32 string to UTF-8
**********************************************************************
* The result must be free()'d. NULL on error.
*********************************************************************/
char *FromUTF32( const wchar_t *src )
{
size_t len;
const wchar_t *in;
/* determine the size of the string */
for( len = 1, in = src; GetWBE( in ); len++ )
in++;
return UTF32toUTF8( src, len, NULL );
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment