rust/library
Matthias Krüger 1f841fc5fe
Rollup merge of #86497 - clarfonthey:nearest_char_boundary, r=scottmcm
Add {floor,ceil}_char_boundary methods to str

This is technically already used internally by the standard library in the form of `truncate_to_char_boundary`.

Essentially these are two building blocks to allow for approximate string truncation, where you want to cut off the string at "approximately" a given length in bytes but don't know exactly where the character boundaries lie. It's also a good candidate for the standard library as it can easily be done naively, but would be difficult to properly optimise. Although the existing code that's done in error messages is done naively, this code will explicitly only check a window of 4 bytes since we know that a boundary must lie in that range, and because it will make it possible to vectorise.

Although this method doesn't take into account graphemes or other properties, this would still be a required building block for splitting that takes those into account. For example, if you wanted to split at a grapheme boundary, you could take your approximate splitting point and then determine the graphemes immediately following and preceeding the split. If you then notice that these two graphemes could be merged, you can decide to either include the whole grapheme or exclude it depending on whether you decide splitting should shrink or expand the string.

This takes the most conservative approach and just offers the raw indices to the user, and they can decide how to use them. That way, the methods are as useful as possible despite having as few methods as possible.

(Note: I'll add some tests and a tracking issue if it's decided that this is worth including.)
2022-02-08 06:47:31 +01:00
..
alloc Add {floor,ceil}_char_boundary methods to str 2022-02-07 13:34:08 -05:00
backtrace@b02ed04a7e
core Rollup merge of #86497 - clarfonthey:nearest_char_boundary, r=scottmcm 2022-02-08 06:47:31 +01:00
panic_abort
panic_unwind adapt L4Bender implementation 2022-01-21 16:50:33 +01:00
portable-simd Sync rust-lang/portable-simd@03f6fbb21e 2022-01-27 11:23:40 -08:00
proc_macro
profiler_builtins
rtstartup
rustc-std-workspace-alloc
rustc-std-workspace-core
rustc-std-workspace-std
std Auto merge of #87869 - thomcc:skinny-io-error, r=yaahc 2022-02-07 20:32:56 +00:00
stdarch@eaee02ffdf Update stdarch submodule 2022-01-28 12:46:22 +00:00
test Rollup merge of #93416 - name1e5s:chore/remove_allow_fail, r=m-ou-se 2022-02-07 14:08:34 +00:00
unwind