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

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