Skip to content

Commit 92cce78

Browse files
committed
Move str::escape_* to libcore
1 parent 55216f8 commit 92cce78

File tree

4 files changed

+169
-165
lines changed

4 files changed

+169
-165
lines changed

src/liballoc/str.rs

-118
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@
2929
#![allow(unused_imports)]
3030

3131
use core::borrow::Borrow;
32-
use core::fmt::{self, Write};
33-
use core::char;
34-
use core::iter::{Chain, Flatten, FlatMap};
3532
use core::str::pattern::{Pattern, Searcher, ReverseSearcher, DoubleEndedSearcher};
3633
use core::mem;
37-
use core::ops::Try;
38-
use core::option;
3934
use core::ptr;
4035
use core::iter::FusedIterator;
4136
use core::unicode::conversions;
@@ -446,40 +441,6 @@ impl str {
446441
return s;
447442
}
448443

449-
/// Escapes each char in `s` with [`char::escape_debug`].
450-
///
451-
/// Note: only extended grapheme codepoints that begin the string will be
452-
/// escaped.
453-
///
454-
/// [`char::escape_debug`]: primitive.char.html#method.escape_debug
455-
#[stable(feature = "str_escape", since = "1.34.0")]
456-
pub fn escape_debug(&self) -> EscapeDebug {
457-
let mut chars = self.chars();
458-
EscapeDebug {
459-
inner: chars.next()
460-
.map(|first| first.escape_debug_ext(true))
461-
.into_iter()
462-
.flatten()
463-
.chain(chars.flat_map(CharEscapeDebugContinue))
464-
}
465-
}
466-
467-
/// Escapes each char in `s` with [`char::escape_default`].
468-
///
469-
/// [`char::escape_default`]: primitive.char.html#method.escape_default
470-
#[stable(feature = "str_escape", since = "1.34.0")]
471-
pub fn escape_default(&self) -> EscapeDefault {
472-
EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
473-
}
474-
475-
/// Escapes each char in `s` with [`char::escape_unicode`].
476-
///
477-
/// [`char::escape_unicode`]: primitive.char.html#method.escape_unicode
478-
#[stable(feature = "str_escape", since = "1.34.0")]
479-
pub fn escape_unicode(&self) -> EscapeUnicode {
480-
EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }
481-
}
482-
483444
/// Converts a [`Box<str>`] into a [`String`] without copying or allocating.
484445
///
485446
/// [`String`]: string/struct.String.html
@@ -611,82 +572,3 @@ pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
611572
Box::from_raw(Box::into_raw(v) as *mut str)
612573
}
613574

614-
impl_fn_for_zst! {
615-
#[derive(Clone)]
616-
struct CharEscapeDebugContinue impl Fn = |c: char| -> char::EscapeDebug {
617-
c.escape_debug_ext(false)
618-
};
619-
620-
#[derive(Clone)]
621-
struct CharEscapeUnicode impl Fn = |c: char| -> char::EscapeUnicode {
622-
c.escape_unicode()
623-
};
624-
#[derive(Clone)]
625-
struct CharEscapeDefault impl Fn = |c: char| -> char::EscapeDefault {
626-
c.escape_default()
627-
};
628-
}
629-
630-
macro_rules! escape_types {
631-
($(
632-
struct $Name: ident<'a> {
633-
inner: $Inner: ty,
634-
}
635-
)+) => {$(
636-
#[stable(feature = "str_escape", since = "1.34.0")]
637-
#[derive(Clone, Debug)]
638-
pub struct $Name<'a> {
639-
inner: $Inner,
640-
}
641-
642-
#[stable(feature = "str_escape", since = "1.34.0")]
643-
impl<'a> fmt::Display for $Name<'a> {
644-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
645-
self.clone().try_for_each(|c| f.write_char(c))
646-
}
647-
}
648-
649-
#[stable(feature = "str_escape", since = "1.34.0")]
650-
impl<'a> Iterator for $Name<'a> {
651-
type Item = char;
652-
653-
#[inline]
654-
fn next(&mut self) -> Option<char> { self.inner.next() }
655-
656-
#[inline]
657-
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
658-
659-
#[inline]
660-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
661-
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
662-
{
663-
self.inner.try_fold(init, fold)
664-
}
665-
666-
#[inline]
667-
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
668-
where Fold: FnMut(Acc, Self::Item) -> Acc,
669-
{
670-
self.inner.fold(init, fold)
671-
}
672-
}
673-
674-
#[stable(feature = "str_escape", since = "1.34.0")]
675-
impl<'a> FusedIterator for $Name<'a> {}
676-
)+}
677-
}
678-
679-
escape_types! {
680-
struct EscapeDebug<'a> {
681-
inner: Chain<
682-
Flatten<option::IntoIter<char::EscapeDebug>>,
683-
FlatMap<Chars<'a>, char::EscapeDebug, CharEscapeDebugContinue>
684-
>,
685-
}
686-
struct EscapeUnicode<'a> {
687-
inner: FlatMap<Chars<'a>, char::EscapeUnicode, CharEscapeUnicode>,
688-
}
689-
struct EscapeDefault<'a> {
690-
inner: FlatMap<Chars<'a>, char::EscapeDefault, CharEscapeDefault>,
691-
}
692-
}

src/libcore/internal_macros.rs

+44
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,47 @@ macro_rules! forward_ref_op_assign {
7575
}
7676
}
7777
}
78+
79+
/// Create a zero-size type similar to a closure type, but named.
80+
#[unstable(feature = "std_internals", issue = "0")]
81+
macro_rules! impl_fn_for_zst {
82+
($(
83+
$( #[$attr: meta] )*
84+
// FIXME: when libcore is in the 2018 edition, use `?` repetition in
85+
// $( <$( $li : lifetime ),+> )?
86+
struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )* Fn =
87+
|$( $arg: ident: $ArgTy: ty ),*| -> $ReturnTy: ty
88+
$body: block;
89+
)+) => {
90+
$(
91+
$( #[$attr] )*
92+
struct $Name;
93+
94+
impl $( <$( $lifetime ),+> )* Fn<($( $ArgTy, )*)> for $Name {
95+
#[inline]
96+
extern "rust-call" fn call(&self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
97+
$body
98+
}
99+
}
100+
101+
impl $( <$( $lifetime ),+> )* FnMut<($( $ArgTy, )*)> for $Name {
102+
#[inline]
103+
extern "rust-call" fn call_mut(
104+
&mut self,
105+
($( $arg, )*): ($( $ArgTy, )*)
106+
) -> $ReturnTy {
107+
Fn::call(&*self, ($( $arg, )*))
108+
}
109+
}
110+
111+
impl $( <$( $lifetime ),+> )* FnOnce<($( $ArgTy, )*)> for $Name {
112+
type Output = $ReturnTy;
113+
114+
#[inline]
115+
extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
116+
Fn::call(&self, ($( $arg, )*))
117+
}
118+
}
119+
)+
120+
}
121+
}

src/libcore/macros.rs

-46
Original file line numberDiff line numberDiff line change
@@ -749,49 +749,3 @@ mod builtin {
749749
($cond:expr, $($arg:tt)+) => ({ /* compiler built-in */ });
750750
}
751751
}
752-
753-
/// Create a named zero-size type similar to a closure.
754-
#[doc(hidden)]
755-
#[macro_export]
756-
#[unstable(feature = "std_internals", issue = "0")]
757-
macro_rules! impl_fn_for_zst {
758-
($(
759-
$( #[$attr: meta] )*
760-
// FIXME: when libcore is in the 2018 edition, use `?` repetition in
761-
// $( <$( $li : lifetime ),+> )?
762-
struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )* Fn =
763-
|$( $arg: ident: $ArgTy: ty ),*| -> $ReturnTy: ty
764-
$body: block;
765-
)+) => {
766-
$(
767-
$( #[$attr] )*
768-
struct $Name;
769-
770-
impl $( <$( $lifetime ),+> )* Fn<($( $ArgTy, )*)> for $Name {
771-
#[inline]
772-
extern "rust-call" fn call(&self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
773-
$body
774-
}
775-
}
776-
777-
impl $( <$( $lifetime ),+> )* FnMut<($( $ArgTy, )*)> for $Name {
778-
#[inline]
779-
extern "rust-call" fn call_mut(
780-
&mut self,
781-
($( $arg, )*): ($( $ArgTy, )*)
782-
) -> $ReturnTy {
783-
Fn::call(&*self, ($( $arg, )*))
784-
}
785-
}
786-
787-
impl $( <$( $lifetime ),+> )* FnOnce<($( $ArgTy, )*)> for $Name {
788-
type Output = $ReturnTy;
789-
790-
#[inline]
791-
extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
792-
Fn::call(&self, ($( $arg, )*))
793-
}
794-
}
795-
)+
796-
}
797-
}

src/libcore/str/mod.rs

+125-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ use self::pattern::Pattern;
88
use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
99

1010
use char;
11-
use fmt;
11+
use fmt::{self, Write};
1212
use iter::{Map, Cloned, FusedIterator, TrustedLen, TrustedRandomAccess, Filter};
13+
use iter::{Flatten, FlatMap, Chain};
1314
use slice::{self, SliceIndex, Split as SliceSplit};
1415
use mem;
16+
use ops::Try;
17+
use option;
1518

1619
pub mod pattern;
1720

@@ -3945,6 +3948,56 @@ impl str {
39453948
let me = unsafe { self.as_bytes_mut() };
39463949
me.make_ascii_lowercase()
39473950
}
3951+
3952+
/// Escapes each char in `s` with [`char::escape_debug`].
3953+
///
3954+
/// Note: only extended grapheme codepoints that begin the string will be
3955+
/// escaped.
3956+
///
3957+
/// [`char::escape_debug`]: ../std/primitive.char.html#method.escape_debug
3958+
#[stable(feature = "str_escape", since = "1.34.0")]
3959+
pub fn escape_debug(&self) -> EscapeDebug {
3960+
let mut chars = self.chars();
3961+
EscapeDebug {
3962+
inner: chars.next()
3963+
.map(|first| first.escape_debug_ext(true))
3964+
.into_iter()
3965+
.flatten()
3966+
.chain(chars.flat_map(CharEscapeDebugContinue))
3967+
}
3968+
}
3969+
3970+
/// Escapes each char in `s` with [`char::escape_default`].
3971+
///
3972+
/// [`char::escape_default`]: ../std/primitive.char.html#method.escape_default
3973+
#[stable(feature = "str_escape", since = "1.34.0")]
3974+
pub fn escape_default(&self) -> EscapeDefault {
3975+
EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
3976+
}
3977+
3978+
/// Escapes each char in `s` with [`char::escape_unicode`].
3979+
///
3980+
/// [`char::escape_unicode`]: ../std/primitive.char.html#method.escape_unicode
3981+
#[stable(feature = "str_escape", since = "1.34.0")]
3982+
pub fn escape_unicode(&self) -> EscapeUnicode {
3983+
EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }
3984+
}
3985+
}
3986+
3987+
impl_fn_for_zst! {
3988+
#[derive(Clone)]
3989+
struct CharEscapeDebugContinue impl Fn = |c: char| -> char::EscapeDebug {
3990+
c.escape_debug_ext(false)
3991+
};
3992+
3993+
#[derive(Clone)]
3994+
struct CharEscapeUnicode impl Fn = |c: char| -> char::EscapeUnicode {
3995+
c.escape_unicode()
3996+
};
3997+
#[derive(Clone)]
3998+
struct CharEscapeDefault impl Fn = |c: char| -> char::EscapeDefault {
3999+
c.escape_default()
4000+
};
39484001
}
39494002

39504003
#[stable(feature = "rust1", since = "1.0.0")]
@@ -4131,3 +4184,74 @@ impl<'a> Iterator for EncodeUtf16<'a> {
41314184

41324185
#[stable(feature = "fused", since = "1.26.0")]
41334186
impl FusedIterator for EncodeUtf16<'_> {}
4187+
4188+
/// The return type of [`str::escape_debug`].
4189+
///
4190+
/// [`str::escape_debug`]: ../../std/primitive.str.html#method.escape_debug
4191+
#[stable(feature = "str_escape", since = "1.34.0")]
4192+
#[derive(Clone, Debug)]
4193+
pub struct EscapeDebug<'a> {
4194+
inner: Chain<
4195+
Flatten<option::IntoIter<char::EscapeDebug>>,
4196+
FlatMap<Chars<'a>, char::EscapeDebug, CharEscapeDebugContinue>
4197+
>,
4198+
}
4199+
4200+
/// The return type of [`str::escape_default`].
4201+
///
4202+
/// [`str::escape_default`]: ../../std/primitive.str.html#method.escape_default
4203+
#[stable(feature = "str_escape", since = "1.34.0")]
4204+
#[derive(Clone, Debug)]
4205+
pub struct EscapeDefault<'a> {
4206+
inner: FlatMap<Chars<'a>, char::EscapeDefault, CharEscapeDefault>,
4207+
}
4208+
4209+
/// The return type of [`str::escape_unicode`].
4210+
///
4211+
/// [`str::escape_unicode`]: ../../std/primitive.str.html#method.escape_unicode
4212+
#[stable(feature = "str_escape", since = "1.34.0")]
4213+
#[derive(Clone, Debug)]
4214+
pub struct EscapeUnicode<'a> {
4215+
inner: FlatMap<Chars<'a>, char::EscapeUnicode, CharEscapeUnicode>,
4216+
}
4217+
4218+
macro_rules! escape_types_impls {
4219+
($( $Name: ident ),+) => {$(
4220+
#[stable(feature = "str_escape", since = "1.34.0")]
4221+
impl<'a> fmt::Display for $Name<'a> {
4222+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4223+
self.clone().try_for_each(|c| f.write_char(c))
4224+
}
4225+
}
4226+
4227+
#[stable(feature = "str_escape", since = "1.34.0")]
4228+
impl<'a> Iterator for $Name<'a> {
4229+
type Item = char;
4230+
4231+
#[inline]
4232+
fn next(&mut self) -> Option<char> { self.inner.next() }
4233+
4234+
#[inline]
4235+
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
4236+
4237+
#[inline]
4238+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
4239+
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
4240+
{
4241+
self.inner.try_fold(init, fold)
4242+
}
4243+
4244+
#[inline]
4245+
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
4246+
where Fold: FnMut(Acc, Self::Item) -> Acc,
4247+
{
4248+
self.inner.fold(init, fold)
4249+
}
4250+
}
4251+
4252+
#[stable(feature = "str_escape", since = "1.34.0")]
4253+
impl<'a> FusedIterator for $Name<'a> {}
4254+
)+}
4255+
}
4256+
4257+
escape_types_impls!(EscapeDebug, EscapeDefault, EscapeUnicode);

0 commit comments

Comments
 (0)