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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphemeCursor::next_boundary() returns incorrect boundary #115

Open
noib3 opened this issue Feb 6, 2023 · 2 comments
Open

GraphemeCursor::next_boundary() returns incorrect boundary #115

noib3 opened this issue Feb 6, 2023 · 2 comments

Comments

@noib3
Copy link

noib3 commented Feb 6, 2023

The grapheme boundaries of "馃嚪馃嚫馃嚠馃嚧" should be 8 and 16, but by feeding GraphemeCursor the individual RIS codepoints I get 8 and 12. Am I using the API incorrectly or is this a bug?

use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete};

fn main() {
    let s = "馃嚪馃嚫馃嚠馃嚧";

    let mut cursor = GraphemeCursor::new(0, s.len(), true);

    // 馃嚪馃嚫

    match cursor.next_boundary("馃嚪", 0) {
        Err(GraphemeIncomplete::NextChunk) => {}
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚫", 4) {
        Err(GraphemeIncomplete::PreContext(4)) => {
            cursor.provide_context("馃嚪", 0);
        }
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚫", 4) {
        Err(GraphemeIncomplete::NextChunk) => {}
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚠", 8) {
        Err(GraphemeIncomplete::PreContext(8)) => {
            cursor.provide_context("馃嚫", 4);
        }
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚠", 8) {
        Err(GraphemeIncomplete::PreContext(4)) => {
            cursor.provide_context("馃嚪", 0);
        }
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚠", 8) {
        Ok(Some(8)) => {}
        _ => unreachable!(),
    }

    // 馃嚠馃嚧

    match cursor.next_boundary("馃嚠", 8) {
        Err(GraphemeIncomplete::NextChunk) => {}
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚧", 12) {
        Err(GraphemeIncomplete::PreContext(12)) => {
            cursor.provide_context("馃嚠", 8);
        }
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚧", 12) {
        Err(GraphemeIncomplete::PreContext(8)) => {
            cursor.provide_context("馃嚫", 4);
        }
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚧", 12) {
        Err(GraphemeIncomplete::PreContext(4)) => {
            cursor.provide_context("馃嚪", 0);
        }
        _ => unreachable!(),
    }

    match cursor.next_boundary("馃嚧", 12) {
        Ok(Some(16)) => {}
        Ok(Some(12)) => panic!("this should be 16"),
        _ => unreachable!(),
    }
}
@Manishearth
Copy link
Member

Seems like the regional indicator state isn't getting properly reset by the cursor.

Also I don't think the cursor should have to ask for precontext if you've been feeding it stuff from the beginning.

I don't fully understand how the cursor works and don't have time right now to pick that up, @raphlinus would you be able to take a look?

@Manishearth
Copy link
Member

Potentially related: #118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants