pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Patrice Hédé <phede-ml@islande.org>
To: jm.poure@freesurf.fr
Cc: pgsql-general@postgresql.org
Subject: Re: [HACKERS] UTF-8 safe ascii() function
Date: Sun, 19 May 2002 21:14:42 +0200
Message-ID: <20020519211442.4d19606e.phede-ml@islande.org> (raw )
In-Reply-To: <200205192008.18893.jm.poure@freesurf.fr >
References: <200205181853.19420.jm.poure@freesurf.fr >
<20020519114413.2265b70e.phede-ml@islande.org >
<200205192008.18893.jm.poure@freesurf.fr >
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);
}
view thread (6+ messages) latest in thread
Message-ID: <20020519211442.4d19606e.phede-ml@islande.org>
Permalink: ../20020519211442.4d19606e.phede-ml@islande.org/
Also on: postgresql.org/message-id/20020519211442.4d19606e.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, jm.poure@freesurf.fr, pgsql-general@postgresql.org
Subject: Re: [HACKERS] UTF-8 safe ascii() function
In-Reply-To: <20020519211442.4d19606e.phede-ml@islande.org>
References: <200205181853.19420.jm.poure@freesurf.fr> <20020519114413.2265b70e.phede-ml@islande.org> <200205192008.18893.jm.poure@freesurf.fr> <20020519211442.4d19606e.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