Skip to content

Add `@graphemes(..)`, `@bytes(..)` and `@chars(..)`

Michael Aaron Murphy requested to merge huntergoldstein:select_refactor into master

Created by: huntergoldstein

Problem: Users cannot iterate over the contents of a string easily.

Solution: Add in three new ArrayMethods that map from string variables to arrays:

  • @graphemes(..) iterates over the unicode graphemes of the string as per this crate
  • @bytes(..) iterates over the UTF-8 byte sequence that corresponds to the given string
  • @chars(..) iterates over the unicode scalars of the byte sequence as per String::chars

Changes introduced by this pull request:

  • Add the three above methods to parser::shell_expand::words
  • Introduce a new trait for applying a Select variant to an iterator DRYer

Drawbacks:

  • The SelectWithSize trait is not optimal as it always requires the size of the iterator that is passed in; in most cases we are not dealing with ExactSizeIterator so this requires making a copy of the iterator and using Iterator::count().
  • Having SelectWithSize::select work like collect means that it is costly to map / filter / reduce after calling select, but I don't think this will be an issue.

Fixes: Closes #316 (closed)

State: WIP: I'm not quite sure if SelectWithSize is the right approach. Any and all thoughts are appreciated.

Other:

  • Depending on how we feel about SelectWithSize I want to start refactoring code that can be replaced with a similar pattern.
  • The alternative for SelectWithSize is a Select<I> iterator; we could implement From<I> for other iterators, which would allow us to specialize the logic more efficiently (versus having a catch-all solution that).

Merge request reports