pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Patrice Hédé <phede-ml@islande.org>
To: pgsql-general@postgresql.org
Cc: jm.poure@freesurf.fr
Subject: Re: [HACKERS] UTF-8 safe ascii() function
Date: Sun, 19 May 2002 11:44:13 +0200
Message-ID: <20020519114413.2265b70e.phede-ml@islande.org> (raw )
In-Reply-To: <200205181853.19420.jm.poure@freesurf.fr >
References: <200205181853.19420.jm.poure@freesurf.fr >
Hi Jean-Michel,
Jean-Michel POURE <jm.poure@freesurf.fr> a écrit :
> Dear all,
>
> I would like to transform UTF-8 strings into Java-Unicode. Example :
> - Latin1 : 'é'
> - UTF-8 : 'é'
> - Java Unicode = '\u00233'
>
> Basically, a Unicode compatible ascii() function would be fine.
> ascii('é') should return 233.
>
> 1) Has anyone written an ascii UTF-8 safe wrapper to ascii() function?
> If yes, would you be so kind to publish this function on the list.
OK, I just gave it a try, see the attachment.
The function is taking the first character of a TEXT element, and
returns its UCS2 value. I just did some basic test (i.e. I have not
tried with 3 or 4 bytes UTF-8 chars). The function is following the
Unicode 3.2 spec.
SELECT utf8toucs2('a'), utf8toucs2('é');
utf8toucs2 | utf8toucs2
------------+------------
97 | 233
(1 row)
The function returns -1 on error.
> 2) Are there plans to add an ascii() UTF-8 safe function to
> PostrgeSQL?
I don't think the function I did is useful as such. It would be better
to make a function that converts the whole string or something.
By the way, what is the encoding for Java Unicode ? is it always "\u"
followed by 5 hex digits (in which case your example is wrong) ? Then,
it shouldn't be too difficult to make the relevant function, though I'm
wondering if the Java programme would convert an incoming '\' 'u' '0'
'0' '2' '3' '3' to the corresponding UCS2/UTF16 character ?
Maybe we should have some similar input (and output ?) functionality in
psql, but then I would much prefer the Perl way, which is
\x{hex_digits}, which is unambiguous.
Regards,
Patrice
--
Patrice Hédé
email: patrice hede(à)islande org
www : http://www.islande.org/
Attachments:
utf8toucs2.c (text/x-csrc, 3.2K) download
/*
* utf8toucs2
*
* $Id: $
*
* PostgreSQL user function to convert a utf8 char to a ucs2 int.
*
* 2002-05-19 Patrice Hédé <patrice.hede@islande.org>
* Initial Release
*
* Function created with :
* CREATE FUNCTION utf8toucs2 ( text )
* RETURNS int AS '/absolute/path/to/utf8toucs2.so'
* LANGUAGE 'c';
*
* typical use :
* SELECT utf8toucs2( "é" );
*/
#include "postgres.h"
#include "fmgr.h"
#include <string.h>
/*
* int4 utf8toucs2(text)
*/
PG_FUNCTION_INFO_V1(utf8toucs2);
Datum utf8toucs2(PG_FUNCTION_ARGS) {
text *src_string = PG_GETARG_TEXT_P(0);
int4 ucs2 = 0;
unsigned char *src = (unsigned char *)(src_string->vl_dat);
if(!src_string) {
PG_RETURN_INT32(-1);
}
if(*src < 0x80) {
PG_RETURN_INT32(*src);
}
else if(*src <= 0xc1) {
PG_RETURN_INT32(-1);
}
else if(*src <= 0xdf) {
if((src_string->vl_len >= 2) &&
(src[1] >= 0x80) && (src[1] <= 0xbf)) {
ucs2=((src[0] & 0x1f) << 6) | (src[1] & 0x3f);
PG_RETURN_INT32(ucs2);
}
else {
PG_RETURN_INT32(-1);
}
}
else if(*src == 0xe0) {
if((src_string->vl_len >= 3) &&
(src[1] >= 0xa0) && (src[1] <= 0xbf) &&
(src[2] >= 0x80) && (src[2] <= 0xbf)) {
ucs2=((src[0] & 0x0f) << 12) | ((src[1] & 0x3f) << 6) | (src[2] & 0x3f);
PG_RETURN_INT32(ucs2);
}
else {
PG_RETURN_INT32(-1);
}
}
else if(*src == 0xed) {
if((src_string->vl_len >= 3) &&
(src[1] >= 0x80) && (src[1] <= 0x9f) &&
(src[2] >= 0x80) && (src[2] <= 0xbf)) {
ucs2=((src[0] & 0x0f) << 12) | ((src[1] & 0x3f) << 6) | (src[2] & 0x3f);
PG_RETURN_INT32(ucs2);
}
else {
PG_RETURN_INT32(-1);
}
}
else if(*src <= 0xef) {
if((src_string->vl_len >= 3) &&
(src[1] >= 0xa0) && (src[1] <= 0xbf) &&
(src[2] >= 0x80) && (src[2] <= 0xbf)) {
ucs2=((src[0] & 0x0f) << 12) | ((src[1] & 0x3f) << 6) | (src[2] & 0x3f);
PG_RETURN_INT32(ucs2);
}
else {
PG_RETURN_INT32(-1);
}
}
else if(*src == 0xf0) {
if((src_string->vl_len >= 4) &&
(src[1] >= 0x90) && (src[2] <= 0xbf) &&
(src[2] >= 0x80) && (src[2] <= 0xbf) &&
(src[3] >= 0x80) && (src[3] <= 0xbf)) {
ucs2=((src[0] & 0x07) << 18) | ((src[1] & 0x3f) << 12) |
((src[2] & 0x3f) << 6) | (src[3] & 0x3f);
PG_RETURN_INT32(ucs2);
}
else {
PG_RETURN_INT32(-1);
}
}
else if(*src <= 0xf3) {
if((src_string->vl_len >= 4) &&
(src[1] >= 0x80) && (src[2] <= 0xbf) &&
(src[2] >= 0x80) && (src[2] <= 0xbf) &&
(src[3] >= 0x80) && (src[3] <= 0xbf)) {
ucs2=((src[0] & 0x07) << 18) | ((src[1] & 0x3f) << 12) |
((src[2] & 0x3f) << 6) | (src[3] & 0x3f);
PG_RETURN_INT32(ucs2);
}
else {
PG_RETURN_INT32(-1);
}
}
else if(*src == 0xf4) {
if((src_string->vl_len >= 4) &&
(src[1] >= 0x80) && (src[2] <= 0x8f) &&
(src[2] >= 0x80) && (src[2] <= 0xbf) &&
(src[3] >= 0x80) && (src[3] <= 0xbf)) {
ucs2=((src[0] & 0x07) << 18) | ((src[1] & 0x3f) << 12) |
((src[2] & 0x3f) << 6) | (src[3] & 0x3f);
PG_RETURN_INT32(ucs2);
}
else {
PG_RETURN_INT32(-1);
}
}
PG_RETURN_INT32(-1);
}
utf8toucs2.sql (text/x-sql, 140B) download
DROP FUNCTION utf8toucs2 (text);
CREATE FUNCTION utf8toucs2 ( text )
RETURNS integer AS '/usr/local/postgresql/utf8toucs2.so'
LANGUAGE 'c';
Makefile (text/x-makefile, 397B) download
CC = gcc
LD = ld
SERVERINC=`pg_config --includedir-server`
CFLAGS = -I${SERVERINC}
SOURCES = Makefile utf8toucs2.c utf8toucs2.sql
all : utf8toucs2.so
utf8toucs2.so : utf8toucs2.o
$(LD) -fpic -shared -o utf8toucs2.so utf8toucs2.o
utf8toucs2.o : utf8toucs2.c
tarball: utf8toucs2.tar.gz
utf8toucs2.tar.gz: $(SOURCES)
tar -czf utf8toucs2.tar.gz $(SOURCES)
clean :
rm -f *.o *.so *~ *.tar.gz
view thread (6+ messages) latest in thread
Message-ID: <20020519114413.2265b70e.phede-ml@islande.org>
Permalink: ../20020519114413.2265b70e.phede-ml@islande.org/
Also on: postgresql.org/message-id/20020519114413.2265b70e.phede-ml@islande.org
copy link · copy postgr.es
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: phede-ml@islande.org, pgsql-general@postgresql.org, jm.poure@freesurf.fr
Subject: Re: [HACKERS] UTF-8 safe ascii() function
In-Reply-To: <20020519114413.2265b70e.phede-ml@islande.org>
References: <200205181853.19420.jm.poure@freesurf.fr> <20020519114413.2265b70e.phede-ml@islande.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox