@@ -10,24 +10,24 @@ use deckcraft_model::text::{Paragraph, RunKind};
1010
1111/// RTF-escape `s` (non-ASCII as `\uN?`, UTF-16, signed as RTF wants).
1212fn escape ( out : & mut String , s : & str ) {
13- for c in s. chars ( ) {
14- match c {
15- '\\' | '{' | '}' => {
16- out. push ( '\\' ) ;
17- out. push ( c) ;
18- }
19- '\t' => out. push_str ( "\\ tab " ) ,
20- '\u{b}' | '\n' => out. push_str ( "\\ line " ) ,
21- c if c. is_ascii ( ) && !c. is_ascii_control ( ) => out. push ( c) ,
22- c if c. is_ascii ( ) => { }
23- c => {
24- let mut buf = [ 0u16 ; 2 ] ;
25- for u in c. encode_utf16 ( & mut buf) {
26- out. push_str ( & format ! ( "\\ u{}?" , * u as i16 ) ) ;
27- }
28- }
29- }
13+ // Keep outline text literal: the shared writer also supports page/column breaks and
14+ // shorthand controls that this app's deliberately small outline reader does not consume.
15+ // Delegate ordinary spans, including UTF-16 escaping, without allocating a second string.
16+ let mut start = 0 ;
17+ for ( i, c) in s. char_indices ( ) {
18+ let replacement = match c {
19+ '\u{000C}' | '\u{000E}' => "" ,
20+ '\u{00A0}' => "\\ u160?" ,
21+ '\u{00AD}' => "\\ u173?" ,
22+ '\u{2011}' => "\\ u8209?" ,
23+ '\u{2028}' => "\\ u8232?" ,
24+ _ => continue ,
25+ } ;
26+ craft_rtf:: push_esc ( out, & s[ start..i] ) ;
27+ out. push_str ( replacement) ;
28+ start = i + c. len_utf8 ( ) ;
3029 }
30+ craft_rtf:: push_esc ( out, & s[ start..] ) ;
3131}
3232
3333fn runs ( out : & mut String , p : & Paragraph ) {
@@ -297,7 +297,7 @@ impl Outline<'_> {
297297 && let Some ( h) = hex
298298 && h != 0
299299 {
300- self . line . push ( cp1252 ( h) ) ;
300+ self . line . push ( craft_rtf :: codepage :: cp1252 ( h) ) ;
301301 }
302302 }
303303
@@ -384,22 +384,75 @@ fn finish_surrogates(s: String) -> String {
384384 out
385385}
386386
387- fn cp1252 ( b : u8 ) -> char {
388- const HIGH : [ char ; 32 ] = [
389- '€' , '\u{81}' , '‚' , 'ƒ' , '„' , '…' , '†' , '‡' , 'ˆ' , '‰' , 'Š' , '‹' , 'Œ' , '\u{8d}' , 'Ž' , '\u{8f}' , '\u{90}' , '‘' , '’' , '“' , '”' , '•' , '–' , '—' ,
390- '˜' , '™' , 'š' , '›' , 'œ' , '\u{9d}' , 'ž' , 'Ÿ' ,
391- ] ;
392- match b {
393- 0x80 ..=0x9f => HIGH [ ( b - 0x80 ) as usize ] ,
394- _ => b as char ,
395- }
396- }
397-
398387#[ cfg( test) ]
399388mod tests {
400389 #![ allow( clippy:: unwrap_used) ]
401390 use super :: * ;
402391
392+ // Frozen pre-cutover implementations are test oracles, not production parsers.
393+ fn legacy_escape ( out : & mut String , s : & str ) {
394+ for c in s. chars ( ) {
395+ match c {
396+ '\\' | '{' | '}' => {
397+ out. push ( '\\' ) ;
398+ out. push ( c) ;
399+ }
400+ '\t' => out. push_str ( "\\ tab " ) ,
401+ '\u{b}' | '\n' => out. push_str ( "\\ line " ) ,
402+ c if c. is_ascii ( ) && !c. is_ascii_control ( ) => out. push ( c) ,
403+ c if c. is_ascii ( ) => { }
404+ c => {
405+ let mut buf = [ 0u16 ; 2 ] ;
406+ for u in c. encode_utf16 ( & mut buf) {
407+ out. push_str ( & format ! ( "\\ u{}?" , * u as i16 ) ) ;
408+ }
409+ }
410+ }
411+ }
412+ }
413+
414+ fn legacy_cp1252 ( b : u8 ) -> char {
415+ const HIGH : [ char ; 32 ] = [
416+ '€' , '\u{81}' , '‚' , 'ƒ' , '„' , '…' , '†' , '‡' , 'ˆ' , '‰' , 'Š' , '‹' , 'Œ' , '\u{8d}' , 'Ž' , '\u{8f}' , '\u{90}' , '‘' , '’' , '“' , '”' , '•' , '–' ,
417+ '—' , '˜' , '™' , 'š' , '›' , 'œ' , '\u{9d}' , 'ž' , 'Ÿ' ,
418+ ] ;
419+ match b {
420+ 0x80 ..=0x9f => HIGH [ ( b - 0x80 ) as usize ] ,
421+ _ => b as char ,
422+ }
423+ }
424+
425+ #[ test]
426+ fn shared_escape_preserves_every_unicode_scalar ( ) {
427+ for value in 0 ..=0x10ffff {
428+ let Some ( c) = char:: from_u32 ( value) else { continue } ;
429+ let mut buf = [ 0 ; 4 ] ;
430+ let text = c. encode_utf8 ( & mut buf) ;
431+ let mut expected = String :: new ( ) ;
432+ let mut actual = String :: new ( ) ;
433+ legacy_escape ( & mut expected, text) ;
434+ escape ( & mut actual, text) ;
435+ assert_eq ! ( actual, expected, "U+{value:04X}" ) ;
436+ }
437+ }
438+
439+ #[ test]
440+ fn shared_escape_preserves_mixed_spans_and_append ( ) {
441+ let text = "a\t 中\u{a0} b\u{ad} c\u{2011} d\u{2028} e\u{c} f\u{e} g\n 😀{}\\ " ;
442+ let mut expected = String :: from ( "prefix:" ) ;
443+ let mut actual = expected. clone ( ) ;
444+ legacy_escape ( & mut expected, text) ;
445+ escape ( & mut actual, text) ;
446+ assert_eq ! ( actual, expected) ;
447+ }
448+
449+ #[ test]
450+ fn shared_cp1252_preserves_all_bytes ( ) {
451+ for b in 0 ..=u8:: MAX {
452+ assert_eq ! ( craft_rtf:: codepage:: cp1252( b) , legacy_cp1252( b) ) ;
453+ }
454+ }
455+
403456 /// A sign in a `\'hh` escape is not a hex digit and must be ignored.
404457 #[ test]
405458 fn hex_escape_rejects_sign ( ) {
0 commit comments