Thread: question about char/wchar/mb utils

question about char/wchar/mb utils

From
Fabien COELHO
Date:
Hello hackers,

I'm working on a very small patch to add syntax error localisation on the
client side in psql, as it seems to be the place. Something like:

ERROR:  syntax error at character 12345
QUERY: ... WHERE foo IS NUL AND ...
QUERY:                  ^

My current issue is how to build the query excerpt to be displayed.
I'm in the "psql/common.c" file.

The query is a char*, and I guess it is in the client encoding, so that
one character is not one byte.

My first idea was to transform the char into a wchar and then work
directly and simply on the wchar string (to fix tabs and so) ...

However, although I have found a function to build the wchar string
(pg_mb2wchar), I haven't found the reverse (yet) :-(, that is to convert
my wchar extract to a char string compatible with the client encoding.

Is there such a function somewhere? It seems that wchar is a one-way
type, once you're there you're stuck with it:-(

If not, what are the other options?

The issue is to be able to work on a constant width string without having
to care about different mb encodings, otherwise the code is going to be a
little bit tricky... I guess I can do it, but the reverse conversion
function looked more appealing to me;-)

Or am still I off my shoes? (translator: a cote de mes pompes;-);

Thanks, for any advice on this point or others,

have a nice day,

-- 
Fabien Coelho - coelho@cri.ensmp.fr


Re: question about char/wchar/mb utils

From
Fabien COELHO
Date:
> > If not, what are the other options?
>
> I don't think you have any: you have to use PQmblen.  Depending on wchar
> facilities would be unportable even if they did everything you wanted.

I meant pg_wchar. It's in the postgres source, should be pretty portable.

> It might be convenient to build an integer array of character start
> offsets and then work with that as if it were a wchar array.

Yep, I thought about this kind of thing.

Thanks for your advice,

-- 
Fabien Coelho - coelho@cri.ensmp.fr


Re: question about char/wchar/mb utils

From
Tom Lane
Date:
Fabien COELHO <coelho@cri.ensmp.fr> writes:
> If not, what are the other options?

I don't think you have any: you have to use PQmblen.  Depending on wchar
facilities would be unportable even if they did everything you wanted.

It might be convenient to build an integer array of character start
offsets and then work with that as if it were a wchar array.
        regards, tom lane