]> git.lizzy.rs Git - rust.git/blob - crates/syntax/src/ast/token_ext.rs
Merge #10420
[rust.git] / crates / syntax / src / ast / token_ext.rs
1 //! There are many AstNodes, but only a few tokens, so we hand-write them here.
2
3 use std::{
4     borrow::Cow,
5     convert::{TryFrom, TryInto},
6 };
7
8 use rustc_lexer::unescape::{unescape_literal, Mode};
9
10 use crate::{
11     ast::{self, AstToken},
12     TextRange, TextSize,
13 };
14
15 impl ast::Comment {
16     pub fn kind(&self) -> CommentKind {
17         CommentKind::from_text(self.text())
18     }
19
20     pub fn is_inner(&self) -> bool {
21         self.kind().doc == Some(CommentPlacement::Inner)
22     }
23
24     pub fn is_outer(&self) -> bool {
25         self.kind().doc == Some(CommentPlacement::Outer)
26     }
27
28     pub fn prefix(&self) -> &'static str {
29         let &(prefix, _kind) = CommentKind::BY_PREFIX
30             .iter()
31             .find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
32             .unwrap();
33         prefix
34     }
35
36     /// Returns the textual content of a doc comment node as a single string with prefix and suffix
37     /// removed.
38     pub fn doc_comment(&self) -> Option<&str> {
39         let kind = self.kind();
40         match kind {
41             CommentKind { shape, doc: Some(_) } => {
42                 let prefix = kind.prefix();
43                 let text = &self.text()[prefix.len()..];
44                 let text = if shape == CommentShape::Block {
45                     text.strip_suffix("*/").unwrap_or(text)
46                 } else {
47                     text
48                 };
49                 Some(text)
50             }
51             _ => None,
52         }
53     }
54 }
55
56 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
57 pub struct CommentKind {
58     pub shape: CommentShape,
59     pub doc: Option<CommentPlacement>,
60 }
61
62 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
63 pub enum CommentShape {
64     Line,
65     Block,
66 }
67
68 impl CommentShape {
69     pub fn is_line(self) -> bool {
70         self == CommentShape::Line
71     }
72
73     pub fn is_block(self) -> bool {
74         self == CommentShape::Block
75     }
76 }
77
78 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
79 pub enum CommentPlacement {
80     Inner,
81     Outer,
82 }
83
84 impl CommentKind {
85     const BY_PREFIX: [(&'static str, CommentKind); 9] = [
86         ("/**/", CommentKind { shape: CommentShape::Block, doc: None }),
87         ("/***", CommentKind { shape: CommentShape::Block, doc: None }),
88         ("////", CommentKind { shape: CommentShape::Line, doc: None }),
89         ("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }),
90         ("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }),
91         ("/**", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Outer) }),
92         ("/*!", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Inner) }),
93         ("//", CommentKind { shape: CommentShape::Line, doc: None }),
94         ("/*", CommentKind { shape: CommentShape::Block, doc: None }),
95     ];
96
97     pub(crate) fn from_text(text: &str) -> CommentKind {
98         let &(_prefix, kind) = CommentKind::BY_PREFIX
99             .iter()
100             .find(|&(prefix, _kind)| text.starts_with(prefix))
101             .unwrap();
102         kind
103     }
104
105     pub fn prefix(&self) -> &'static str {
106         let &(prefix, _) =
107             CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap();
108         prefix
109     }
110 }
111
112 impl ast::Whitespace {
113     pub fn spans_multiple_lines(&self) -> bool {
114         let text = self.text();
115         text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
116     }
117 }
118
119 pub struct QuoteOffsets {
120     pub quotes: (TextRange, TextRange),
121     pub contents: TextRange,
122 }
123
124 impl QuoteOffsets {
125     fn new(literal: &str) -> Option<QuoteOffsets> {
126         let left_quote = literal.find('"')?;
127         let right_quote = literal.rfind('"')?;
128         if left_quote == right_quote {
129             // `literal` only contains one quote
130             return None;
131         }
132
133         let start = TextSize::from(0);
134         let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"');
135         let right_quote = TextSize::try_from(right_quote).unwrap();
136         let end = TextSize::of(literal);
137
138         let res = QuoteOffsets {
139             quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)),
140             contents: TextRange::new(left_quote, right_quote),
141         };
142         Some(res)
143     }
144 }
145
146 pub trait IsString: AstToken {
147     fn quote_offsets(&self) -> Option<QuoteOffsets> {
148         let text = self.text();
149         let offsets = QuoteOffsets::new(text)?;
150         let o = self.syntax().text_range().start();
151         let offsets = QuoteOffsets {
152             quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
153             contents: offsets.contents + o,
154         };
155         Some(offsets)
156     }
157     fn text_range_between_quotes(&self) -> Option<TextRange> {
158         self.quote_offsets().map(|it| it.contents)
159     }
160     fn open_quote_text_range(&self) -> Option<TextRange> {
161         self.quote_offsets().map(|it| it.quotes.0)
162     }
163     fn close_quote_text_range(&self) -> Option<TextRange> {
164         self.quote_offsets().map(|it| it.quotes.1)
165     }
166 }
167
168 impl IsString for ast::String {}
169
170 impl ast::String {
171     pub fn is_raw(&self) -> bool {
172         self.text().starts_with('r')
173     }
174     pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
175         let contents_range = self.text_range_between_quotes()?;
176         assert!(TextRange::up_to(contents_range.len()).contains_range(range));
177         Some(range + contents_range.start())
178     }
179
180     pub fn value(&self) -> Option<Cow<'_, str>> {
181         if self.is_raw() {
182             let text = self.text();
183             let text =
184                 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
185             return Some(Cow::Borrowed(text));
186         }
187
188         let text = self.text();
189         let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
190
191         let mut buf = String::new();
192         let mut text_iter = text.chars();
193         let mut has_error = false;
194         unescape_literal(text, Mode::Str, &mut |char_range, unescaped_char| match (
195             unescaped_char,
196             buf.capacity() == 0,
197         ) {
198             (Ok(c), false) => buf.push(c),
199             (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
200             (Ok(c), true) => {
201                 buf.reserve_exact(text.len());
202                 buf.push_str(&text[..char_range.start]);
203                 buf.push(c);
204             }
205             (Err(_), _) => has_error = true,
206         });
207
208         match (has_error, buf.capacity() == 0) {
209             (true, _) => None,
210             (false, true) => Some(Cow::Borrowed(text)),
211             (false, false) => Some(Cow::Owned(buf)),
212         }
213     }
214 }
215
216 impl IsString for ast::ByteString {}
217
218 impl ast::ByteString {
219     pub fn is_raw(&self) -> bool {
220         self.text().starts_with("br")
221     }
222
223     pub fn value(&self) -> Option<Cow<'_, [u8]>> {
224         if self.is_raw() {
225             let text = self.text();
226             let text =
227                 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
228             return Some(Cow::Borrowed(text.as_bytes()));
229         }
230
231         let text = self.text();
232         let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
233
234         let mut buf: Vec<u8> = Vec::new();
235         let mut text_iter = text.chars();
236         let mut has_error = false;
237         unescape_literal(text, Mode::ByteStr, &mut |char_range, unescaped_char| match (
238             unescaped_char,
239             buf.capacity() == 0,
240         ) {
241             (Ok(c), false) => buf.push(c as u8),
242             (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
243             (Ok(c), true) => {
244                 buf.reserve_exact(text.len());
245                 buf.extend_from_slice(text[..char_range.start].as_bytes());
246                 buf.push(c as u8);
247             }
248             (Err(_), _) => has_error = true,
249         });
250
251         match (has_error, buf.capacity() == 0) {
252             (true, _) => None,
253             (false, true) => Some(Cow::Borrowed(text.as_bytes())),
254             (false, false) => Some(Cow::Owned(buf)),
255         }
256     }
257 }
258
259 #[derive(Debug)]
260 pub enum FormatSpecifier {
261     Open,
262     Close,
263     Integer,
264     Identifier,
265     Colon,
266     Fill,
267     Align,
268     Sign,
269     NumberSign,
270     Zero,
271     DollarSign,
272     Dot,
273     Asterisk,
274     QuestionMark,
275 }
276
277 pub trait HasFormatSpecifier: AstToken {
278     fn char_ranges(
279         &self,
280     ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>;
281
282     fn lex_format_specifier<F>(&self, mut callback: F)
283     where
284         F: FnMut(TextRange, FormatSpecifier),
285     {
286         let char_ranges = match self.char_ranges() {
287             Some(char_ranges) => char_ranges,
288             None => return,
289         };
290         let mut chars = char_ranges.iter().peekable();
291
292         while let Some((range, first_char)) = chars.next() {
293             match first_char {
294                 Ok('{') => {
295                     // Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
296                     if let Some((_, Ok('{'))) = chars.peek() {
297                         // Escaped format specifier, `{{`
298                         chars.next();
299                         continue;
300                     }
301
302                     callback(*range, FormatSpecifier::Open);
303
304                     // check for integer/identifier
305                     match chars
306                         .peek()
307                         .and_then(|next| next.1.as_ref().ok())
308                         .copied()
309                         .unwrap_or_default()
310                     {
311                         '0'..='9' => {
312                             // integer
313                             read_integer(&mut chars, &mut callback);
314                         }
315                         c if c == '_' || c.is_alphabetic() => {
316                             // identifier
317                             read_identifier(&mut chars, &mut callback);
318                         }
319                         _ => {}
320                     }
321
322                     if let Some((_, Ok(':'))) = chars.peek() {
323                         skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback);
324
325                         // check for fill/align
326                         let mut cloned = chars.clone().take(2);
327                         let first = cloned
328                             .next()
329                             .and_then(|next| next.1.as_ref().ok())
330                             .copied()
331                             .unwrap_or_default();
332                         let second = cloned
333                             .next()
334                             .and_then(|next| next.1.as_ref().ok())
335                             .copied()
336                             .unwrap_or_default();
337                         match second {
338                             '<' | '^' | '>' => {
339                                 // alignment specifier, first char specifies fillment
340                                 skip_char_and_emit(
341                                     &mut chars,
342                                     FormatSpecifier::Fill,
343                                     &mut callback,
344                                 );
345                                 skip_char_and_emit(
346                                     &mut chars,
347                                     FormatSpecifier::Align,
348                                     &mut callback,
349                                 );
350                             }
351                             _ => match first {
352                                 '<' | '^' | '>' => {
353                                     skip_char_and_emit(
354                                         &mut chars,
355                                         FormatSpecifier::Align,
356                                         &mut callback,
357                                     );
358                                 }
359                                 _ => {}
360                             },
361                         }
362
363                         // check for sign
364                         match chars
365                             .peek()
366                             .and_then(|next| next.1.as_ref().ok())
367                             .copied()
368                             .unwrap_or_default()
369                         {
370                             '+' | '-' => {
371                                 skip_char_and_emit(
372                                     &mut chars,
373                                     FormatSpecifier::Sign,
374                                     &mut callback,
375                                 );
376                             }
377                             _ => {}
378                         }
379
380                         // check for `#`
381                         if let Some((_, Ok('#'))) = chars.peek() {
382                             skip_char_and_emit(
383                                 &mut chars,
384                                 FormatSpecifier::NumberSign,
385                                 &mut callback,
386                             );
387                         }
388
389                         // check for `0`
390                         let mut cloned = chars.clone().take(2);
391                         let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
392                         let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
393
394                         if first == Some('0') && second != Some('$') {
395                             skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
396                         }
397
398                         // width
399                         match chars
400                             .peek()
401                             .and_then(|next| next.1.as_ref().ok())
402                             .copied()
403                             .unwrap_or_default()
404                         {
405                             '0'..='9' => {
406                                 read_integer(&mut chars, &mut callback);
407                                 if let Some((_, Ok('$'))) = chars.peek() {
408                                     skip_char_and_emit(
409                                         &mut chars,
410                                         FormatSpecifier::DollarSign,
411                                         &mut callback,
412                                     );
413                                 }
414                             }
415                             c if c == '_' || c.is_alphabetic() => {
416                                 read_identifier(&mut chars, &mut callback);
417
418                                 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
419                                     == Some('?')
420                                 {
421                                     skip_char_and_emit(
422                                         &mut chars,
423                                         FormatSpecifier::QuestionMark,
424                                         &mut callback,
425                                     );
426                                 }
427
428                                 // can be either width (indicated by dollar sign, or type in which case
429                                 // the next sign has to be `}`)
430                                 let next =
431                                     chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
432
433                                 match next {
434                                     Some('$') => skip_char_and_emit(
435                                         &mut chars,
436                                         FormatSpecifier::DollarSign,
437                                         &mut callback,
438                                     ),
439                                     Some('}') => {
440                                         skip_char_and_emit(
441                                             &mut chars,
442                                             FormatSpecifier::Close,
443                                             &mut callback,
444                                         );
445                                         continue;
446                                     }
447                                     _ => continue,
448                                 };
449                             }
450                             _ => {}
451                         }
452
453                         // precision
454                         if let Some((_, Ok('.'))) = chars.peek() {
455                             skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
456
457                             match chars
458                                 .peek()
459                                 .and_then(|next| next.1.as_ref().ok())
460                                 .copied()
461                                 .unwrap_or_default()
462                             {
463                                 '*' => {
464                                     skip_char_and_emit(
465                                         &mut chars,
466                                         FormatSpecifier::Asterisk,
467                                         &mut callback,
468                                     );
469                                 }
470                                 '0'..='9' => {
471                                     read_integer(&mut chars, &mut callback);
472                                     if let Some((_, Ok('$'))) = chars.peek() {
473                                         skip_char_and_emit(
474                                             &mut chars,
475                                             FormatSpecifier::DollarSign,
476                                             &mut callback,
477                                         );
478                                     }
479                                 }
480                                 c if c == '_' || c.is_alphabetic() => {
481                                     read_identifier(&mut chars, &mut callback);
482                                     if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
483                                         != Some('$')
484                                     {
485                                         continue;
486                                     }
487                                     skip_char_and_emit(
488                                         &mut chars,
489                                         FormatSpecifier::DollarSign,
490                                         &mut callback,
491                                     );
492                                 }
493                                 _ => {
494                                     continue;
495                                 }
496                             }
497                         }
498
499                         // type
500                         match chars
501                             .peek()
502                             .and_then(|next| next.1.as_ref().ok())
503                             .copied()
504                             .unwrap_or_default()
505                         {
506                             '?' => {
507                                 skip_char_and_emit(
508                                     &mut chars,
509                                     FormatSpecifier::QuestionMark,
510                                     &mut callback,
511                                 );
512                             }
513                             c if c == '_' || c.is_alphabetic() => {
514                                 read_identifier(&mut chars, &mut callback);
515
516                                 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
517                                     == Some('?')
518                                 {
519                                     skip_char_and_emit(
520                                         &mut chars,
521                                         FormatSpecifier::QuestionMark,
522                                         &mut callback,
523                                     );
524                                 }
525                             }
526                             _ => {}
527                         }
528                     }
529
530                     match chars.peek() {
531                         Some((_, Ok('}'))) => {
532                             skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
533                         }
534                         Some((_, _)) | None => continue,
535                     }
536                 }
537                 _ => {
538                     while let Some((_, Ok(next_char))) = chars.peek() {
539                         if next_char == &'{' {
540                             break;
541                         }
542                         chars.next();
543                     }
544                 }
545             };
546         }
547
548         fn skip_char_and_emit<'a, I, F>(
549             chars: &mut std::iter::Peekable<I>,
550             emit: FormatSpecifier,
551             callback: &mut F,
552         ) where
553             I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
554             F: FnMut(TextRange, FormatSpecifier),
555         {
556             let (range, _) = chars.next().unwrap();
557             callback(*range, emit);
558         }
559
560         fn read_integer<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
561         where
562             I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
563             F: FnMut(TextRange, FormatSpecifier),
564         {
565             let (mut range, c) = chars.next().unwrap();
566             assert!(c.as_ref().unwrap().is_ascii_digit());
567             while let Some((r, Ok(next_char))) = chars.peek() {
568                 if next_char.is_ascii_digit() {
569                     chars.next();
570                     range = range.cover(*r);
571                 } else {
572                     break;
573                 }
574             }
575             callback(range, FormatSpecifier::Integer);
576         }
577
578         fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
579         where
580             I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
581             F: FnMut(TextRange, FormatSpecifier),
582         {
583             let (mut range, c) = chars.next().unwrap();
584             assert!(c.as_ref().unwrap().is_alphabetic() || *c.as_ref().unwrap() == '_');
585             while let Some((r, Ok(next_char))) = chars.peek() {
586                 if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() {
587                     chars.next();
588                     range = range.cover(*r);
589                 } else {
590                     break;
591                 }
592             }
593             callback(range, FormatSpecifier::Identifier);
594         }
595     }
596 }
597
598 impl HasFormatSpecifier for ast::String {
599     fn char_ranges(
600         &self,
601     ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
602         let text = self.text();
603         let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
604         let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
605
606         let mut res = Vec::with_capacity(text.len());
607         unescape_literal(text, Mode::Str, &mut |range, unescaped_char| {
608             res.push((
609                 TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap())
610                     + offset,
611                 unescaped_char,
612             ));
613         });
614
615         Some(res)
616     }
617 }
618
619 impl ast::IntNumber {
620     pub fn radix(&self) -> Radix {
621         match self.text().get(..2).unwrap_or_default() {
622             "0b" => Radix::Binary,
623             "0o" => Radix::Octal,
624             "0x" => Radix::Hexadecimal,
625             _ => Radix::Decimal,
626         }
627     }
628
629     pub fn value(&self) -> Option<u128> {
630         let token = self.syntax();
631
632         let mut text = token.text();
633         if let Some(suffix) = self.suffix() {
634             text = &text[..text.len() - suffix.len()];
635         }
636
637         let radix = self.radix();
638         text = &text[radix.prefix_len()..];
639
640         let buf;
641         if text.contains('_') {
642             buf = text.replace('_', "");
643             text = buf.as_str();
644         };
645
646         let value = u128::from_str_radix(text, radix as u32).ok()?;
647         Some(value)
648     }
649
650     pub fn suffix(&self) -> Option<&str> {
651         let text = self.text();
652         let radix = self.radix();
653         let mut indices = text.char_indices();
654         if radix != Radix::Decimal {
655             indices.next()?;
656             indices.next()?;
657         }
658         let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
659             Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
660             _ => |(_, c)| c.is_ascii_alphabetic(),
661         };
662         let (suffix_start, _) = indices.find(is_suffix_start)?;
663         Some(&text[suffix_start..])
664     }
665 }
666
667 impl ast::FloatNumber {
668     pub fn suffix(&self) -> Option<&str> {
669         let text = self.text();
670         let mut indices = text.char_indices();
671         let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
672         if c == 'e' || c == 'E' {
673             suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
674         }
675         Some(&text[suffix_start..])
676     }
677 }
678
679 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
680 pub enum Radix {
681     Binary = 2,
682     Octal = 8,
683     Decimal = 10,
684     Hexadecimal = 16,
685 }
686
687 impl Radix {
688     pub const ALL: &'static [Radix] =
689         &[Radix::Binary, Radix::Octal, Radix::Decimal, Radix::Hexadecimal];
690
691     const fn prefix_len(self) -> usize {
692         match self {
693             Self::Decimal => 0,
694             _ => 2,
695         }
696     }
697 }
698
699 #[cfg(test)]
700 mod tests {
701     use crate::ast::{self, make, FloatNumber, IntNumber};
702
703     fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
704         assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
705     }
706
707     fn check_int_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
708         assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
709     }
710
711     #[test]
712     fn test_float_number_suffix() {
713         check_float_suffix("123.0", None);
714         check_float_suffix("123f32", "f32");
715         check_float_suffix("123.0e", None);
716         check_float_suffix("123.0e4", None);
717         check_float_suffix("123.0ef32", "f32");
718         check_float_suffix("123.0E4f32", "f32");
719         check_float_suffix("1_2_3.0_f32", "f32");
720     }
721
722     #[test]
723     fn test_int_number_suffix() {
724         check_int_suffix("123", None);
725         check_int_suffix("123i32", "i32");
726         check_int_suffix("1_0_1_l_o_l", "l_o_l");
727         check_int_suffix("0b11", None);
728         check_int_suffix("0o11", None);
729         check_int_suffix("0xff", None);
730         check_int_suffix("0b11u32", "u32");
731         check_int_suffix("0o11u32", "u32");
732         check_int_suffix("0xffu32", "u32");
733     }
734
735     fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
736         assert_eq!(
737             ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
738                 .value()
739                 .as_deref(),
740             expected.into()
741         );
742     }
743
744     #[test]
745     fn test_string_escape() {
746         check_string_value(r"foobar", "foobar");
747         check_string_value(r"\foobar", None);
748         check_string_value(r"\nfoobar", "\nfoobar");
749         check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");
750     }
751 }