Skip to content

Created a linebreaks_iter() variant of linebreaks - #11

Open
nwoods-cimpress wants to merge 1 commit into
axelf4:masterfrom
nwoods-cimpress:linebreaks_iter
Open

Created a linebreaks_iter() variant of linebreaks #11
nwoods-cimpress wants to merge 1 commit into
axelf4:masterfrom
nwoods-cimpress:linebreaks_iter

Conversation

@nwoods-cimpress

Copy link
Copy Markdown

linebreaks has a very simple interface - it takes a &str and returns an iterator returning break opportunities tagged by positions. This change introduces a linebreaks_iter() variant that instead of taking a &str:

  • Takes an iterator passing in char and arbitrary indexes that do not necessarily have to be usize
  • A final_idx parameter containing the final index.

The original linebreaks() function can now easily be implemented in terms of linebreaks_iter():

pub fn linebreaks(s: &str) -> impl Iterator<Item = (usize, BreakOpportunity)> + Clone + '_ {
    linebreaks_iter(s.char_indices(), s.len())
}

This allows the core algorithm to be decoupled from the representation of the string, allowing text to be passed in alternative formats (e.g. - UTF-16 or UTF-8) or possibly a complex data structure where everything is not conveniently a singular string.

…lexibility

`linebreaks` has a very simple interface - it takes a `&str` and returns an iterator returning break opportunities tagged by positions.  This change introduces a `linebreaks_iter()` variant that instead of taking a `&str`:
- Takes an iterator passing in `char` and arbitary indexes that do not necessarily have to be `usize`
- A `final_idx` parameter containing the final index.

The original `linebreaks()` function can now easily be implemented in terms of `linebreaks_iter()`:
````
pub fn linebreaks(s: &str) -> impl Iterator<Item = (usize, BreakOpportunity)> + Clone + '_ {
    linebreaks_iter(s.char_indices(), s.len())
}
````

This allows the core algorithm to be decoupled from the representation of the string, allowing text to be passed in alternative formats (e.g. - UTF-16 or UTF-8) or possibily a complex data structure where everything is not conveniently a singular string.
@nwoods-cimpress

Copy link
Copy Markdown
Author

I think that it is unfortunate that the iter parameter passed to linebreaks_iter() needs to implement Clone. This is only because the return iterator needs to implement Clone to support the normal linebreaks() call.

If there is a way in Rust to support implementing Clone on a conditional basis, I don't know it.

@nwoods-cimpress

Copy link
Copy Markdown
Author

@axelf4 any thoughts on this PR?

@ccbrown

ccbrown commented Apr 24, 2025

Copy link
Copy Markdown

I came here to make exactly this change. My use-case is similar to those mentioned in #1 and this PR would be great for my needs.

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