Closed erf closed 10 months ago
What do you need the indices for, that you cannot do with the character range itself?
(They are actually indirectly exposed, which was likely a mistake, as cr.stringBeforeLength
and cr.source.string.length - cr.stringAfterLength
.)
Given an index
position in a String text
, i use CharacterRange.at(text, index)
to get the range r
. I would then like to advance that range to the next character using r.moveNext()
to get the next string index at that position, to use in a common replaceRange
method which works on a string given a start, end
index. I could use CharacterRange
to delete the char at the position, but i would just like to use the common API to do this, as i also have some other methods calling this and i do some additional operations there.
deleteAt(Position p) {
final index = byteIndexFromPosition(p);
final r = CharacterRange.at(text, index)..moveNext();
final length = r.current.length;
replace(index, index + length, '', UndoOpType.delete);
}
The code above works fine, but if i could access the start
and end
properties i could do:
deleteAt(Position p) {
final index = byteIndexFromPosition(p);
final r = CharacterRange.at(text, index)..moveNext();
replace(r.start, r.end, '', UndoOpType.delete);
}
Although i can see start
and end
being confusing as you could believe it is the length of the the characters and not the source string indicies.
PS: I do have another branch, which does the replace
method in "character space" but i think it might be less overhead and more flexible to use string as the main data type so i'm experimenting with both.
I saw when debugging the
CharacterRange
you have a private_start
and_end
index for the range, is there a reason these are not public? It would be useful for me when i need the indices after using theCharacterRange
for a search.