Re: Converting byte position to character position

Chris Angelico <[email protected]>
Newsgroups gmane.comp.lang.pike.user
Message-ID <CAPTjJmo6jE=rsxy=ABCEvo9V-Ts0UFTkr=to3qj7QeHUOP75YA@mail.gmail.com>
On Fri, Aug 15, 2014 at 8:45 PM, Per Hedbor () @ Pike (-) importmöte
för mailinglistan <[email protected]> wrote:
> I am fairly sure there is a function somewhere in glib or gtk that
> will actually return the char position given a byte position in a
> utf-8 string.
>
> And, indeed, maping at least some of the g_utf8 family of functions
> would probably be helpful.
>
> Say, g_utf8_pointer_to_offset(utf8,utf8+byteoffset).

That quite probably would be best. However, I have to support the most
easily downloadable Windows version of Pike, so making use of this
feature would have to wait. (Though if it's significantly faster than
the double conversion, I might put a #if into the code to take
advantage of this if it's available.)

> It might be argued that the GTK module should do this conversion
> automatically.

I did toy with this possibility. Considering that I create the Pango
layout object by giving it a Unicode string (specifically,
some_object->create_pango_layout(txt)), it would make the most sense
to have xy_to_index return a character position, and in fact I wrote
the code assuming naively that this was the case. It's only just today
that I noticed exactly *why* cursor selection was doing the wrong
thing around non-ASCII characters.

However, everything seems to be in agreement that xy_to_index returns
a byte offset (looking at Python, PHP, C, and Vala bindings). Plus, of
course, there's backward compat. So I was thinking the best way would
be to create a new function, xy_to_char(), which would wrap
xy_to_index() and convert the byte position to character position
before returning it. That'd be extremely clean and tidy, and would
justify a #if guard in my code and consequent support for multiple
Pike versions. Arne was talking about me being given commit access to
take charge of the GTK2 module; this would probably be my first
feature addition.

ChrisA
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.