dart-lang / characters

A package for characters represented as unicode extended grapheme clusters
https://pub.dev/packages/characters
BSD 3-Clause "New" or "Revised" License
68 stars 17 forks source link

Make CharacterRange start and end public #85

Closed erf closed 10 months ago

erf commented 1 year ago

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 the CharacterRange for a search.

lrhn commented 1 year 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.)

erf commented 1 year ago

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.

erf commented 10 months ago

I'll close this for now as I'm doing OK without this change. BTW you can see my usage of the characters package now in my minimal vi like text editor over at vid