Re: [Vala] Convert from byte to character offset in string



On 14 July 2010 16:40, Luca Bruno <lethalman88 gmail com> wrote:
On Wed, Jul 14, 2010 at 12:00:58PM +0930, James Moschou wrote:
Hello,

Is there a convenient way to convert from a byte index in a string to
its character offset and vice-versa?

I know string has pointer_to_offset(string pos), but that's for
pointers, I have the value of a byte index, i.e. an int/uint/long
value, not a pointer.

Maybe you're looking for: yourstring+index


I'm sorry I don't see how this is what I want. My problem is that I am
implementing a text view with a cursor that works in characters.
However I use Pango to figure out where to draw the cursor, and Pango
works in bytes, so I need to convert between the two.

I reckon I could do:
    byte_offset = mystring.substring(0, character_offset).to_utf8().length;
But that seems unwieldy, and I'm not sure how to go the other way.

Thanks,
James



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]