Pastey gunicode.e

--http://developer.gnome.org/glib/2.31/glib-Unicode-Manipulation.html#g-utf8-to-ucs4-fast
 
include std/dll.e 
include std/memory.e 
include std/machine.e 
 
constant 
glib_ = open_dll("libglib-2.0.so.0"), 
g_utf8_to_ucs4_fast_ = define_c_func(glib_, "g_utf8_to_ucs4_fast", {C_POINTER, C_LONG, C_LONG}, C_POINTER), 
g_ucs4_to_utf8_ = define_c_func(glib_, "g_ucs4_to_utf8", {C_POINTER, C_LONG, C_POINTER, C_POINTER, C_POINTER}, C_POINTER), 
g_free_ = define_c_proc(glib_, "g_free", {C_POINTER}) 
 
public function g_ucs4_to_utf8(sequence ucs4) 
	sequence str = "" 
	atom l 
	atom len_written = allocate(4) 
	if len_written = NULL then 
		return str 
	end if 
	atom ucs4p = allocate(4 * length(ucs4)) 
	if ucs4p = NULL then 
		free(len_written) 
		return str 
	end if 
	poke4(ucs4p, ucs4) 
	atom ret = c_func(g_ucs4_to_utf8_, {ucs4p, length(ucs4), NULL, len_written, NULL}) 
	if ret != NULL then 
		l = peek4u(len_written) 
		free(len_written) 
		str = peek({ret, l}) 
		c_proc(g_free_, {ret}) 
	end if 
	return str 
end function 
 
public function g_utf8_to_ucs4_fast(sequence utf8) 
	sequence str = "" 
	atom l 
	atom len_written = allocate(4) 
	if len_written = NULL then 
		return str 
	end if 
	atom utf8p = allocate(length(utf8)) 
	if utf8p = NULL then 
		free(len_written) 
		return str 
	end if 
	poke(utf8p, utf8) 
	atom ret = c_func(g_utf8_to_ucs4_fast_, {utf8p, length(utf8), len_written}) 
	if ret != NULL then 
		l = peek4u(len_written) 
		free(len_written) 
		str = peek4u({ret, l}) 
		c_proc(g_free_, {ret}) 
	end if 
	return str 
end function