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

[para-api] Create ParagraphBidiInfo structs as API for processing a single paragraph at a time #115

Merged
merged 9 commits into from
Dec 6, 2023

Conversation

jfkthame
Copy link
Contributor

@jfkthame jfkthame commented Dec 4, 2023

This gives us ParagraphBidiInfo and utf16::ParagraphBidiInfo structs, which work like BidiInfo but are explicitly single-paragraph APIs.

(If there's a paragraph separator present in the text, they'll simply treat it like a segment separator. An alternative would be to return some kind of error if this happens.... thoughts?)

I extended test_process_text() to also run its single-paragraph testcases through the new APIs, and also added a couple of tests of what happens when text with a paragraph separator is (erroneously) passed here, to confirm that we handle it as expected.

/// (This should be kept in sync with BidiInfo::reorder_line.)
#[cfg_attr(feature = "flame_it", flamer::flame)]
pub fn reorder_line(&self, line: Range<usize>) -> Cow<'text, [u16]> {
if !level::has_rtl(&self.levels[line.clone()]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I do not want to duplicate this code

/// (This should be kept in sync with BidiInfo::reorder_line.)
#[cfg_attr(feature = "flame_it", flamer::flame)]
pub fn reorder_line(&self, line: Range<usize>) -> Cow<'text, str> {
if !level::has_rtl(&self.levels[line.clone()]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I do not want to duplicate this code

The new ParagraphInfo types should not have methods that are more than a single method call, maybe two.

Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost there

src/lib.rs Outdated
@@ -868,6 +840,24 @@ impl<'text> ParagraphBidiInfo<'text> {
}
}

// Implementation of reorder_line for both BidiInfo and ParagraphBidiInfo.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: more docs for what the parameters do, on both functions

Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, still some more stuff

src/lib.rs Outdated
});
pure_ltr.as_mut().unwrap().push(is_pure_ltr);
}
assert_eq!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug assertion please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will do (although this was essentially just moving the already-existing assertion inside the if split_paragraphs condition).

src/lib.rs Outdated
/// This is used for both BidiInfo and ParagraphBidiInfo, with split_paragraphs=true
/// indicating that it should split paragraphs and return the two Option<> results
/// (for BidiInfo), and split_paragraphs=false indicating it should not split paras,
/// and returns the single paragraph_level and is_pure_ltr values instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I don't want the callers to have to unwrap (we want to avoid unwraps as much as possible in unicode_bidi)

can split_paragraphs be implemented as an Option<&mut Vec> outparam instead?

(might need to use an option of a tuple to support pure_ltr as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a look and see what I can come up with...

src/lib.rs Outdated

if split_paragraphs {
if para_start < text.len() {
paragraphs.as_mut().unwrap().push(ParagraphInfo {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no unwraps

Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last bit

/// (This should be kept in sync with BidiInfo::reordered_levels.)
#[cfg_attr(feature = "flame_it", flamer::flame)]
pub fn reordered_levels(&self, line: Range<usize>) -> Vec<Level> {
assert!(line.start <= self.levels.len());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more asserts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was a bit unclear what to do with cases like this (compare the existing asserts at https://docs.rs/unicode-bidi/0.3.13/src/unicode_bidi/lib.rs.html#546-547). But I'm happy to make them debug_assert! if you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, sorry, you're totally right, those are external asserts. My bad!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah just remove the last commit and we're ready to land

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add some docs later that explain that this API panics

@Manishearth Manishearth merged commit 765ec8c into servo:master Dec 6, 2023
13 checks passed
@Manishearth
Copy link
Member

Thanks!

@jfkthame
Copy link
Contributor Author

jfkthame commented Dec 6, 2023

And thank you! :)

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

Successfully merging this pull request may close these issues.

2 participants