Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not clear how to map some String position to the Characters position, and vice-versa. #381

Closed
marcglasberg opened this issue May 31, 2020 · 2 comments
Assignees

Comments

@marcglasberg
Copy link

If you have some text in a TextField you can get the caret's (also called cursor) position. If you want to delete the character at the caret's position, you must be able to say that, say, the 7th position in the String maps to the 5th position in the Characters, so that you can delete this char.

The same problem arises if you want to add some character in the caret's position.

But is there a way to do this? Doesn't seem so, or at least it's not obvious to me.

This is important, since the TextField will report positions in the String, not in the Characters. I suggest the following to methods:

int convertFromStringIndex(int stringIndex)
int convertFromCharactersIndex(int charactersIndex)

If you want to be really complete, you can also have these methods:

List<int> getAllStringIndexes()
List<int> getAllCharactersIndexes()
@lrhn
Copy link
Member

lrhn commented Sep 4, 2020

I'd prefer to not expose indices in the Characters class API.

A more indirect approach would be constructors for the CharacterRange class which take a position, or a range, in the input code units, and then produces a CharacterRange covering that position or range.

To go the other way, the CharacterRange has stringBefore and stringAfter, which can give you the code unit count before and after the range. We could easily add stringBeforeLength and stringAfterLength as well, to avoid unnecessary allocation when finding that length.

It's not an "index'. But it is, really.

@lrhn
Copy link
Member

lrhn commented Sep 22, 2020

Some functionality added.

@lrhn lrhn closed this as completed Sep 22, 2020
mosuem referenced this issue Oct 14, 2024
@mosuem mosuem transferred this issue from dart-archive/characters Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants