1+ use rustpython_wtf8:: { CodePoint , Wtf8 } ;
2+
13#[ derive( Debug , PartialEq , Eq , Copy , Clone , Hash , is_macro:: Is ) ]
24pub enum Quote {
35 Single ,
@@ -35,20 +37,32 @@ pub struct EscapeLayout {
3537 pub len : Option < usize > ,
3638}
3739
38- pub trait Escape {
40+ /// Represents string types that can be escape-printed.
41+ ///
42+ /// # Safety
43+ ///
44+ /// `source_len` and `layout` must be accurate, and `layout.len` must not be equal
45+ /// to `Some(source_len)` if the string contains non-printable characters.
46+ pub unsafe trait Escape {
3947 fn source_len ( & self ) -> usize ;
4048 fn layout ( & self ) -> & EscapeLayout ;
4149 fn changed ( & self ) -> bool {
4250 self . layout ( ) . len != Some ( self . source_len ( ) )
4351 }
4452
45- fn write_source ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result ;
53+ /// Write the body of the string directly to the formatter.
54+ ///
55+ /// # Safety
56+ ///
57+ /// This string must only contain printable characters.
58+ unsafe fn write_source ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result ;
4659 fn write_body_slow ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result ;
4760 fn write_body ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result {
4861 if self . changed ( ) {
4962 self . write_body_slow ( formatter)
5063 } else {
51- self . write_source ( formatter)
64+ // SAFETY: verified the string contains only printable characters.
65+ unsafe { self . write_source ( formatter) }
5266 }
5367 }
5468}
@@ -75,23 +89,23 @@ pub(crate) const fn choose_quote(
7589}
7690
7791pub struct UnicodeEscape < ' a > {
78- source : & ' a str ,
92+ source : & ' a Wtf8 ,
7993 layout : EscapeLayout ,
8094}
8195
8296impl < ' a > UnicodeEscape < ' a > {
8397 #[ inline]
84- pub fn with_forced_quote ( source : & ' a str , quote : Quote ) -> Self {
98+ pub fn with_forced_quote ( source : & ' a Wtf8 , quote : Quote ) -> Self {
8599 let layout = EscapeLayout { quote, len : None } ;
86100 Self { source, layout }
87101 }
88102 #[ inline]
89- pub fn with_preferred_quote ( source : & ' a str , quote : Quote ) -> Self {
103+ pub fn with_preferred_quote ( source : & ' a Wtf8 , quote : Quote ) -> Self {
90104 let layout = Self :: repr_layout ( source, quote) ;
91105 Self { source, layout }
92106 }
93107 #[ inline]
94- pub fn new_repr ( source : & ' a str ) -> Self {
108+ pub fn new_repr ( source : & ' a Wtf8 ) -> Self {
95109 Self :: with_preferred_quote ( source, Quote :: Single )
96110 }
97111 #[ inline]
@@ -126,32 +140,32 @@ impl std::fmt::Display for StrRepr<'_, '_> {
126140impl UnicodeEscape < ' _ > {
127141 const REPR_RESERVED_LEN : usize = 2 ; // for quotes
128142
129- pub fn repr_layout ( source : & str , preferred_quote : Quote ) -> EscapeLayout {
143+ pub fn repr_layout ( source : & Wtf8 , preferred_quote : Quote ) -> EscapeLayout {
130144 Self :: output_layout_with_checker ( source, preferred_quote, |a, b| {
131145 Some ( ( a as isize ) . checked_add ( b as isize ) ? as usize )
132146 } )
133147 }
134148
135149 fn output_layout_with_checker (
136- source : & str ,
150+ source : & Wtf8 ,
137151 preferred_quote : Quote ,
138152 length_add : impl Fn ( usize , usize ) -> Option < usize > ,
139153 ) -> EscapeLayout {
140154 let mut out_len = Self :: REPR_RESERVED_LEN ;
141155 let mut single_count = 0 ;
142156 let mut double_count = 0 ;
143157
144- for ch in source. chars ( ) {
145- let incr = match ch {
146- '\'' => {
158+ for ch in source. code_points ( ) {
159+ let incr = match ch. to_char ( ) {
160+ Some ( '\'' ) => {
147161 single_count += 1 ;
148162 1
149163 }
150- '"' => {
164+ Some ( '"' ) => {
151165 double_count += 1 ;
152166 1
153167 }
154- c => Self :: escaped_char_len ( c ) ,
168+ _ => Self :: escaped_char_len ( ch ) ,
155169 } ;
156170 let Some ( new_len) = length_add ( out_len, incr) else {
157171 #[ cold]
@@ -182,7 +196,9 @@ impl UnicodeEscape<'_> {
182196 }
183197 }
184198
185- fn escaped_char_len ( ch : char ) -> usize {
199+ fn escaped_char_len ( ch : CodePoint ) -> usize {
200+ // surrogates are \uHHHH
201+ let Some ( ch) = ch. to_char ( ) else { return 6 } ;
186202 match ch {
187203 '\\' | '\t' | '\r' | '\n' => 2 ,
188204 ch if ch < ' ' || ch as u32 == 0x7f => 4 , // \xHH
@@ -198,10 +214,13 @@ impl UnicodeEscape<'_> {
198214 }
199215
200216 fn write_char (
201- ch : char ,
217+ ch : CodePoint ,
202218 quote : Quote ,
203219 formatter : & mut impl std:: fmt:: Write ,
204220 ) -> std:: fmt:: Result {
221+ let Some ( ch) = ch. to_char ( ) else {
222+ return write ! ( formatter, "\\ u{:04x}" , ch. to_u32( ) ) ;
223+ } ;
205224 match ch {
206225 '\n' => formatter. write_str ( "\\ n" ) ,
207226 '\t' => formatter. write_str ( "\\ t" ) ,
@@ -232,7 +251,7 @@ impl UnicodeEscape<'_> {
232251 }
233252}
234253
235- impl Escape for UnicodeEscape < ' _ > {
254+ unsafe impl Escape for UnicodeEscape < ' _ > {
236255 fn source_len ( & self ) -> usize {
237256 self . source . len ( )
238257 }
@@ -241,13 +260,16 @@ impl Escape for UnicodeEscape<'_> {
241260 & self . layout
242261 }
243262
244- fn write_source ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result {
245- formatter. write_str ( self . source )
263+ unsafe fn write_source ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result {
264+ formatter. write_str ( unsafe {
265+ // SAFETY: this function must be called only when source is printable characters (i.e. no surrogates)
266+ std:: str:: from_utf8_unchecked ( self . source . as_bytes ( ) )
267+ } )
246268 }
247269
248270 #[ cold]
249271 fn write_body_slow ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result {
250- for ch in self . source . chars ( ) {
272+ for ch in self . source . code_points ( ) {
251273 Self :: write_char ( ch, self . layout ( ) . quote , formatter) ?;
252274 }
253275 Ok ( ( ) )
@@ -373,7 +395,7 @@ impl AsciiEscape<'_> {
373395 }
374396}
375397
376- impl Escape for AsciiEscape < ' _ > {
398+ unsafe impl Escape for AsciiEscape < ' _ > {
377399 fn source_len ( & self ) -> usize {
378400 self . source . len ( )
379401 }
@@ -382,7 +404,7 @@ impl Escape for AsciiEscape<'_> {
382404 & self . layout
383405 }
384406
385- fn write_source ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result {
407+ unsafe fn write_source ( & self , formatter : & mut impl std:: fmt:: Write ) -> std:: fmt:: Result {
386408 formatter. write_str ( unsafe {
387409 // SAFETY: this function must be called only when source is printable ascii characters
388410 std:: str:: from_utf8_unchecked ( self . source )
@@ -429,7 +451,7 @@ mod unicode_escape_tests {
429451 #[ test]
430452 fn changed ( ) {
431453 fn test ( s : & str ) -> bool {
432- UnicodeEscape :: new_repr ( s) . changed ( )
454+ UnicodeEscape :: new_repr ( s. as_ref ( ) ) . changed ( )
433455 }
434456 assert ! ( !test( "hello" ) ) ;
435457 assert ! ( !test( "'hello'" ) ) ;
0 commit comments