rust/library/core/src
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 Fix a bunch of typos 2021-12-14 16:40:43 +01:00
array Add missing const stability attributes 2022-02-03 19:15:57 -05:00
char Rollup merge of #93392 - GKFX:char-docs, r=scottmcm 2022-01-31 06:58:32 +01:00
convert Rollup merge of #92382 - clarfonthey:const_convert, r=scottmcm 2022-01-15 02:25:14 +01:00
fmt Create core::fmt::ArgumentV1 with generics instead of fn pointer 2022-01-29 13:52:19 +00:00
future Rollup merge of #92887 - pietroalbini:pa-bootstrap-update, r=Mark-Simulacrum 2022-01-30 08:37:46 -08:00
hash change PhantomData type for BuildHasherDefault 2022-01-07 00:39:48 +01:00
iter Fix a typo from #92899 2022-01-28 01:35:33 +00:00
macros Fix invalid special casing of the unreachable! macro 2022-01-31 17:09:31 +01:00
mem Add MaybeUninit::as_bytes 2022-01-19 21:27:29 +00:00
num Rollup merge of #86497 - clarfonthey:nearest_char_boundary, r=scottmcm 2022-02-08 06:47:31 +01:00
ops Add a minimal working append_const_msg argument 2022-01-26 00:48:08 +11:00
panic Clarify safety of PanicInfo::can_unwind 2022-01-30 21:33:51 +01:00
prelude update cfg(bootstrap)s 2022-01-28 15:01:07 +01:00
ptr Make NonNull::new const 2022-01-23 23:04:39 +09:00
slice Auto merge of #86988 - thomcc:chunky-splitz-says-no-checking, r=the8472 2022-02-01 10:11:59 +00:00
str Add {floor,ceil}_char_boundary methods to str 2022-02-07 13:34:08 -05:00
stream Remove P: Unpin bound on impl Stream for Pin 2021-12-17 11:14:02 +08:00
sync Add rustc_diagnostic_item attribute to AtomicBool 2022-01-13 23:32:49 +01:00
task Implement data and vtable getters for RawWaker 2021-12-17 04:30:13 +08:00
unicode Regenerate tables for Unicode 14.0.0 2021-10-06 17:49:33 -07:00
any.rs Reverts #92135 because perf regression 2021-12-26 16:02:33 +03:00
ascii.rs Add #[must_use] to remaining core functions 2021-10-30 18:21:29 -04:00
bool.rs Constify bool::then{,_some} 2021-12-15 00:11:23 +08:00
borrow.rs Make Borrow and BorrowMut impls const 2021-12-04 21:57:39 +09:00
cell.rs Add missing const stability attributes 2022-02-03 19:15:57 -05:00
clone.rs Update Copy/Clone documentation WRT arrays 2021-11-08 13:11:59 -05:00
cmp.rs Edit docs introduction for std::cmp::PartialOrd 2022-01-28 00:46:04 -06:00
default.rs Add #[must_use] to remaining core functions 2021-10-30 18:21:29 -04:00
ffi.rs Use target_family = "wasm" 2021-11-10 08:35:42 -08:00
hint.rs Add is_riscv_feature_detected!; modify impl of hint::spin_loop 2022-01-05 15:44:52 +08:00
internal_macros.rs Added docs to internal_macro const 2021-10-22 10:07:35 +13:00
intrinsics.rs Document about some behaviors of const_(de)allocate and add some tests. 2022-01-29 19:13:23 +09:00
lazy.rs Use UnsafeCell::get_mut() in core::lazy::OnceCell::get_mut() 2021-12-30 05:04:44 +02:00
lib.rs Rollup merge of #92887 - pietroalbini:pa-bootstrap-update, r=Mark-Simulacrum 2022-01-30 08:37:46 -08:00
marker.rs Update Copy/Clone documentation WRT arrays 2021-11-08 13:11:59 -05:00
option.rs Fix is_some_with tests. 2022-01-19 00:12:35 +01:00
panic.rs Fix invalid special casing of the unreachable! macro 2022-01-31 17:09:31 +01:00
panicking.rs Take in account the unreachable! macro in the non_fmt_panic lint 2022-01-31 17:09:31 +01:00
pin.rs Add #[must_use] to remaining core functions 2021-10-30 18:21:29 -04:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
primitive_docs.rs Fix annotation of code blocks 2022-02-01 21:44:53 +00:00
result.rs Fix is_some_with tests. 2022-01-19 00:12:35 +01:00
time.rs Improve Duration::try_from_secs_f32/64 accuracy by directly processing exponent and mantissa 2022-01-26 18:14:25 +03:00
tuple.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
unit.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00