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

[WIP] Implement Needle API (RFC 2500) #59591

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use core::hash::{Hash, Hasher};
use core::iter::{Iterator, FromIterator, FusedIterator};
use core::marker::{Unpin, Unsize};
use core::mem;
use core::needle::Needle;
use core::pin::Pin;
use core::ops::{
CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Receiver, Generator, GeneratorState
Expand Down Expand Up @@ -919,3 +920,35 @@ impl<F: ?Sized + Future + Unpin> Future for Box<F> {
F::poll(Pin::new(&mut *self), cx)
}
}

#[unstable(feature = "needle", issue = "56345")]
impl<'p, 'h, T: PartialEq + 'p + 'h> Needle<&'h [T]> for &'p Box<[T]> {
type Searcher = <&'p [T] as Needle<&'h [T]>>::Searcher;
type Consumer = <&'p [T] as Needle<&'h [T]>>::Consumer;

#[inline]
fn into_searcher(self) -> Self::Searcher {
<&'p [T] as Needle<&'h [T]>>::into_searcher(&**self)
}

#[inline]
fn into_consumer(self) -> Self::Consumer {
<&'p [T] as Needle<&'h [T]>>::into_consumer(&**self)
}
}

#[unstable(feature = "needle", issue = "56345")]
impl<'p, 'h, T: PartialEq + 'p + 'h> Needle<&'h mut [T]> for &'p Box<[T]> {
type Searcher = <&'p [T] as Needle<&'h mut [T]>>::Searcher;
type Consumer = <&'p [T] as Needle<&'h mut [T]>>::Consumer;

#[inline]
fn into_searcher(self) -> Self::Searcher {
<&'p [T] as Needle<&'h mut [T]>>::into_searcher(&**self)
}

#[inline]
fn into_consumer(self) -> Self::Consumer {
<&'p [T] as Needle<&'h mut [T]>>::into_consumer(&**self)
}
}
2 changes: 1 addition & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
#![feature(needs_allocator)]
#![feature(nll)]
#![feature(optin_builtin_traits)]
#![feature(pattern)]
#![feature(needle)]
#![feature(ptr_internals)]
#![feature(ptr_offset_from)]
#![feature(rustc_attrs)]
Expand Down
35 changes: 35 additions & 0 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ use core::cmp::Ordering::{self, Less};
use core::mem::{self, size_of};
use core::ptr;
use core::{u8, u16, u32};
use core::needle::{ext, Needle, Searcher, Consumer};

use crate::borrow::ToOwned;
use crate::boxed::Box;
Expand Down Expand Up @@ -485,6 +486,40 @@ impl<T> [T] {
}
buf
}

/// Replaces all matches of a predicate with another slice.
#[unstable(feature = "slice_needle_methods", issue = "56345")]
#[inline]
#[must_use = "this returns the replaced slice as a new allocation, \
without modifying the original"]
pub fn replace<'s: 'a, 'a, F>(&'s self, from: F, to: &'a [T]) -> Vec<T>
where
T: Clone,
F: Needle<&'a [T]>,
F::Searcher: Searcher<[T]>, // FIXME: RFC 2089
F::Consumer: Consumer<[T]>, // FIXME: RFC 2089
{
let mut result = Vec::with_capacity(self.len());
ext::replace_with(self, from, |_| to, |s| result.extend_from_slice(s));
result
}

/// Replaces first N matches of a predicate with another slice.
#[unstable(feature = "slice_needle_methods", issue = "56345")]
#[inline]
#[must_use = "this returns the replaced slice as a new allocation, \
without modifying the original"]
pub fn replacen<'s: 'a, 'a, F>(&'s self, from: F, to: &'a [T], count: usize) -> Vec<T>
where
T: Clone,
F: Needle<&'a [T]>,
F::Searcher: Searcher<[T]>, // FIXME: RFC 2089
F::Consumer: Consumer<[T]>, // FIXME: RFC 2089
{
let mut result = Vec::with_capacity(self.len());
ext::replacen_with(self, from, |_| to, count, |s| result.extend_from_slice(s));
result
}
}

#[lang = "slice_u8_alloc"]
Expand Down
42 changes: 19 additions & 23 deletions src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
// It's cleaner to just turn off the unused_imports warning than to fix them.
#![allow(unused_imports)]

use core::borrow::Borrow;
use core::str::pattern::{Pattern, Searcher, ReverseSearcher, DoubleEndedSearcher};
use core::fmt;
use core::needle::{ext, Needle, Searcher, Consumer};
use core::mem;
use core::ptr;
use core::iter::FusedIterator;
use core::unicode::conversions;

use crate::borrow::ToOwned;
use crate::borrow::{Borrow, ToOwned};
use crate::boxed::Box;
use crate::slice::{SliceConcatExt, SliceIndex};
use crate::string::String;
Expand All @@ -62,8 +62,6 @@ pub use core::str::{from_utf8, from_utf8_mut, Chars, CharIndices, Bytes};
pub use core::str::{from_utf8_unchecked, from_utf8_unchecked_mut, ParseBoolError};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::str::SplitWhitespace;
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::str::pattern;
#[stable(feature = "encode_utf16", since = "1.8.0")]
pub use core::str::EncodeUtf16;
#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
Expand Down Expand Up @@ -255,15 +253,14 @@ impl str {
without modifying the original"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
let mut result = String::new();
let mut last_end = 0;
for (start, part) in self.match_indices(from) {
result.push_str(unsafe { self.get_unchecked(last_end..start) });
result.push_str(to);
last_end = start + part.len();
}
result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
pub fn replace<'s: 'a, 'a, P>(&'s self, from: P, to: &'a str) -> String
where
P: Needle<&'a str>,
P::Searcher: Searcher<str>, // FIXME: RFC 2089
P::Consumer: Consumer<str>, // FIXME: RFC 2089
{
let mut result = String::with_capacity(self.len());
ext::replace_with(self, from, |_| to, |s| result.push_str(s));
result
}

Expand Down Expand Up @@ -295,16 +292,15 @@ impl str {
#[must_use = "this returns the replaced string as a new allocation, \
without modifying the original"]
#[stable(feature = "str_replacen", since = "1.16.0")]
pub fn replacen<'a, P: Pattern<'a>>(&'a self, pat: P, to: &str, count: usize) -> String {
pub fn replacen<'s: 'a, 'a, P>(&'s self, pat: P, to: &'a str, count: usize) -> String
where
P: Needle<&'a str>,
P::Searcher: Searcher<str>, // FIXME: RFC 2089
P::Consumer: Consumer<str>, // FIXME: RFC 2089
{
// Hope to reduce the times of re-allocation
let mut result = String::with_capacity(32);
let mut last_end = 0;
for (start, part) in self.match_indices(pat).take(count) {
result.push_str(unsafe { self.get_unchecked(last_end..start) });
result.push_str(to);
last_end = start + part.len();
}
result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
let mut result = String::with_capacity(self.len());
ext::replacen_with(self, pat, |_| to, count, |s| result.push_str(s));
result
}

Expand Down
24 changes: 10 additions & 14 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ use core::iter::{FromIterator, FusedIterator};
use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeBounds};
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ptr;
use core::str::{pattern::Pattern, lossy};
use core::needle::Needle;
use core::str::lossy;

use crate::borrow::{Cow, ToOwned};
use crate::collections::CollectionAllocErr;
Expand Down Expand Up @@ -1792,24 +1793,19 @@ impl<'a> Extend<Cow<'a, str>> for String {
}

/// A convenience impl that delegates to the impl for `&str`
#[unstable(feature = "pattern",
reason = "API not fully fleshed out and ready to be stabilized",
issue = "27721")]
impl<'a, 'b> Pattern<'a> for &'b String {
type Searcher = <&'b str as Pattern<'a>>::Searcher;

fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
self[..].into_searcher(haystack)
}
#[unstable(feature = "needle", issue = "56345")]
impl<'a, 'b> Needle<&'a str> for &'b String {
type Searcher = <&'b str as Needle<&'a str>>::Searcher;
type Consumer = <&'b str as Needle<&'a str>>::Consumer;

#[inline]
fn is_contained_in(self, haystack: &'a str) -> bool {
self[..].is_contained_in(haystack)
fn into_searcher(self) -> Self::Searcher {
<&'b str as Needle<&'a str>>::into_searcher(&**self)
}

#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
self[..].is_prefix_of(haystack)
fn into_consumer(self) -> Self::Consumer {
<&'b str as Needle<&'a str>>::into_consumer(&**self)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#![feature(box_syntax)]
#![feature(drain_filter)]
#![feature(exact_size_is_empty)]
#![feature(pattern)]
#![feature(repeat_generic_slice)]
#![feature(needle)]
#![feature(try_reserve)]
#![feature(unboxed_closures)]
#![feature(vecdeque_rotate)]
#![feature(mut_str_needle_methods)]
#![feature(slice_needle_methods)]

use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
Expand Down
Loading