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

Created a linebreaks_iter() variant of linebreaks #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nwoods-cimpress
Copy link

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
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.

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.

1 participant