Re: [Vala] Convert from byte to character offset in string
- From: Abderrahim Kitouni <a kitouni gmail com>
- To: James Moschou <james moschou gmail com>
- Cc: vala-list <vala-list gnome org>
- Subject: Re: [Vala] Convert from byte to character offset in string
- Date: Wed, 14 Jul 2010 16:07:49 +0100
Hi,
في ر، 14-07-2010 عند 20:45 +0930 ، كتب James Moschou:
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.
I'm not sure what you want exactly, converting from byte offset to
character offset or the other way around?
Either way, you can use offset/pointer_to_offset for character offsets
and raw pointer arithmetic (you'll probably need to cast to char*) for
byte offsets. So :
byte_offset = ((char*) mystring.offset(character_offset)) - ((char*) mystring);
should be ok. I agree it's not very nice (and it could be a builtin
method) but that should do it (also it doesn't require copying anything,
where yours does 2 copies).
HTH,
Abderrahim
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]