pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedUTF-8 safe ascii() function
6+ messages / 2 participants
[nested] [flat]
* UTF-8 safe ascii() function
@ 2002-05-18 16:53 Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 09:44 ` Re: [HACKERS] UTF-8 safe ascii() function Patrice <phede-ml@islande.org>
0 siblings, 1 reply; 6+ messages in thread
From: Jean-Michel POURE @ 2002-05-18 16:53 UTC (permalink / raw)
To: pgsql-admin; pgsql-general <pgsql-general@postgresql.org>; pgsql-odbc <pgsql-odbc@postgresql.org>; +Cc: pgsql-hackers@postgresql.org
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.
2) Are there plans to add an ascii() UTF-8 safe function to PostrgeSQL?
Best regards,
Jean-Michel POURE
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [HACKERS] UTF-8 safe ascii() function
2002-05-18 16:53 UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
@ 2002-05-19 09:44 ` Patrice <phede-ml@islande.org>
2002-05-19 10:44 ` Re: [HACKERS] UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 18:08 ` Re: [HACKERS] UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
0 siblings, 2 replies; 6+ messages in thread
From: Patrice Hédé @ 2002-05-19 09:44 UTC (permalink / raw)
To: pgsql-general@postgresql.org; +Cc: 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
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [HACKERS] UTF-8 safe ascii() function
2002-05-18 16:53 UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 09:44 ` Re: [HACKERS] UTF-8 safe ascii() function Patrice <phede-ml@islande.org>
@ 2002-05-19 10:44 ` Jean-Michel POURE <jm.poure@freesurf.fr>
1 sibling, 0 replies; 6+ messages in thread
From: Jean-Michel POURE @ 2002-05-19 10:44 UTC (permalink / raw)
To: Patrice Hédé <phede-ml@islande.org>; pgsql-general@postgresql.org
Dear Patrice,
Thank you very much. This will save the lives of Java users.
> 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.
Yes, this would save the lives of some Javascript users. Java Unicode notation
is the only Unicode understood by Javascript.
> 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 ?
Java Unicode notation is not case sensitive ('\u' = '\U') and is followed by
an hexadecimal value.
> 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.
This would be perfect. We should also handle the HTML unicode nation :
&#{dec_digits} and &#x{hex_digits} as it is unambiguous.
Cheers,
Jean-Michel
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [HACKERS] UTF-8 safe ascii() function
2002-05-18 16:53 UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 09:44 ` Re: [HACKERS] UTF-8 safe ascii() function Patrice <phede-ml@islande.org>
@ 2002-05-19 18:08 ` Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 19:14 ` Re: [HACKERS] UTF-8 safe ascii() function Patrice <phede-ml@islande.org>
1 sibling, 1 reply; 6+ messages in thread
From: Jean-Michel POURE @ 2002-05-19 18:08 UTC (permalink / raw)
To: Patrice Hédé <phede-ml@islande.org>; pgsql-general@postgresql.org
Le Dimanche 19 Mai 2002 11:44, Patrice Hédé a écrit :
> 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.
Hi Patrice,
I tried a Japanese character :
SELECT utf8toucs2 ('支'::text) which returns -1
Do you know why it does not return the UCS-2 value?
Cheers,
Jean-Michel POURE
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [HACKERS] UTF-8 safe ascii() function
2002-05-18 16:53 UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 09:44 ` Re: [HACKERS] UTF-8 safe ascii() function Patrice <phede-ml@islande.org>
2002-05-19 18:08 ` Re: [HACKERS] UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
@ 2002-05-19 19:14 ` Patrice <phede-ml@islande.org>
2002-05-20 07:31 ` Re: [HACKERS] UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
0 siblings, 1 reply; 6+ messages in thread
From: Patrice Hédé @ 2002-05-19 19:14 UTC (permalink / raw)
To: jm.poure@freesurf.fr; +Cc: pgsql-general@postgresql.org
Jean-Michel POURE <jm.poure@freesurf.fr> a écrit :
> I tried a Japanese character :
> SELECT utf8toucs2 ('æ_¯'::text) which returns -1
>
> Do you know why it does not return the UCS-2 value?
Oops, my mistake. I forgot to update a test after a copy-paste. Here is
a new version which should be correct this time ! :)
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: utf8toucs2.c,v 1.2 2002/05/19 19:11:47 patrice Exp $
*
* 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] >= 0x80) && (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);
}
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [HACKERS] UTF-8 safe ascii() function
2002-05-18 16:53 UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 09:44 ` Re: [HACKERS] UTF-8 safe ascii() function Patrice <phede-ml@islande.org>
2002-05-19 18:08 ` Re: [HACKERS] UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 19:14 ` Re: [HACKERS] UTF-8 safe ascii() function Patrice <phede-ml@islande.org>
@ 2002-05-20 07:31 ` Jean-Michel POURE <jm.poure@freesurf.fr>
0 siblings, 0 replies; 6+ messages in thread
From: Jean-Michel POURE @ 2002-05-20 07:31 UTC (permalink / raw)
To: Patrice Hédé <phede-ml@islande.org>; +Cc: pgsql-general@postgresql.org
Le Dimanche 19 Mai 2002 21:14, Patrice Hédé a écrit :
> Oops, my mistake. I forgot to update a test after a copy-paste. Here is
> a new version which should be correct this time ! :)
Thanks Patrice, merci Patrice !
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2002-05-20 07:31 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-05-18 16:53 UTF-8 safe ascii() function Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 09:44 ` Patrice <phede-ml@islande.org>
2002-05-19 10:44 ` Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 18:08 ` Jean-Michel POURE <jm.poure@freesurf.fr>
2002-05-19 19:14 ` Patrice <phede-ml@islande.org>
2002-05-20 07:31 ` Jean-Michel POURE <jm.poure@freesurf.fr>
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