]> git.lizzy.rs Git - rust.git/blob - crates/syntax/src/ast/token_ext.rs
Merge #9260
[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 = if let Some(char_ranges) = self.char_ranges() {
287             char_ranges
288         } else {
289             return;
290         };
291         let mut chars = char_ranges.iter().peekable();
292
293         while let Some((range, first_char)) = chars.next() {
294             match first_char {
295                 Ok('{') => {
296                     // Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
297                     if let Some((_, Ok('{'))) = chars.peek() {
298                         // Escaped format specifier, `{{`
299                         chars.next();
300                         continue;
301                     }
302
303                     callback(*range, FormatSpecifier::Open);
304
305                     // check for integer/identifier
306                     match chars
307                         .peek()
308                         .and_then(|next| next.1.as_ref().ok())
309                         .copied()
310                         .unwrap_or_default()
311                     {
312                         '0'..='9' => {
313                             // integer
314                             read_integer(&mut chars, &mut callback);
315                         }
316                         c if c == '_' || c.is_alphabetic() => {
317                             // identifier
318                             read_identifier(&mut chars, &mut callback);
319                         }
320                         _ => {}
321                     }
322
323                     if let Some((_, Ok(':'))) = chars.peek() {
324                         skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback);
325
326                         // check for fill/align
327                         let mut cloned = chars.clone().take(2);
328                         let first = cloned
329                             .next()
330                             .and_then(|next| next.1.as_ref().ok())
331                             .copied()
332                             .unwrap_or_default();
333                         let second = cloned
334                             .next()
335                             .and_then(|next| next.1.as_ref().ok())
336                             .copied()
337                             .unwrap_or_default();
338                         match second {
339                             '<' | '^' | '>' => {
340                                 // alignment specifier, first char specifies fillment
341                                 skip_char_and_emit(
342                                     &mut chars,
343                                     FormatSpecifier::Fill,
344                                     &mut callback,
345                                 );
346                                 skip_char_and_emit(
347                                     &mut chars,
348                                     FormatSpecifier::Align,
349                                     &mut callback,
350                                 );
351                             }
352                             _ => match first {
353                                 '<' | '^' | '>' => {
354                                     skip_char_and_emit(
355                                         &mut chars,
356                                         FormatSpecifier::Align,
357                                         &mut callback,
358                                     );
359                                 }
360                                 _ => {}
361                             },
362                         }
363
364                         // check for sign
365                         match chars
366                             .peek()
367                             .and_then(|next| next.1.as_ref().ok())
368                             .copied()
369                             .unwrap_or_default()
370                         {
371                             '+' | '-' => {
372                                 skip_char_and_emit(
373                                     &mut chars,
374                                     FormatSpecifier::Sign,
375                                     &mut callback,
376                                 );
377                             }
378                             _ => {}
379                         }
380
381                         // check for `#`
382                         if let Some((_, Ok('#'))) = chars.peek() {
383                             skip_char_and_emit(
384                                 &mut chars,
385                                 FormatSpecifier::NumberSign,
386                                 &mut callback,
387                             );
388                         }
389
390                         // check for `0`
391                         let mut cloned = chars.clone().take(2);
392                         let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
393                         let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
394
395                         if first == Some('0') && second != Some('$') {
396                             skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
397                         }
398
399                         // width
400                         match chars
401                             .peek()
402                             .and_then(|next| next.1.as_ref().ok())
403                             .copied()
404                             .unwrap_or_default()
405                         {
406                             '0'..='9' => {
407                                 read_integer(&mut chars, &mut callback);
408                                 if let Some((_, Ok('$'))) = chars.peek() {
409                                     skip_char_and_emit(
410                                         &mut chars,
411                                         FormatSpecifier::DollarSign,
412                                         &mut callback,
413                                     );
414                                 }
415                             }
416                             c if c == '_' || c.is_alphabetic() => {
417                                 read_identifier(&mut chars, &mut callback);
418
419                                 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
420                                     == Some('?')
421                                 {
422                                     skip_char_and_emit(
423                                         &mut chars,
424                                         FormatSpecifier::QuestionMark,
425                                         &mut callback,
426                                     );
427                                 }
428
429                                 // can be either width (indicated by dollar sign, or type in which case
430                                 // the next sign has to be `}`)
431                                 let next =
432                                     chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
433
434                                 match next {
435                                     Some('$') => skip_char_and_emit(
436                                         &mut chars,
437                                         FormatSpecifier::DollarSign,
438                                         &mut callback,
439                                     ),
440                                     Some('}') => {
441                                         skip_char_and_emit(
442                                             &mut chars,
443                                             FormatSpecifier::Close,
444                                             &mut callback,
445                                         );
446                                         continue;
447                                     }
448                                     _ => continue,
449                                 };
450                             }
451                             _ => {}
452                         }
453
454                         // precision
455                         if let Some((_, Ok('.'))) = chars.peek() {
456                             skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
457
458                             match chars
459                                 .peek()
460                                 .and_then(|next| next.1.as_ref().ok())
461                                 .copied()
462                                 .unwrap_or_default()
463                             {
464                                 '*' => {
465                                     skip_char_and_emit(
466                                         &mut chars,
467                                         FormatSpecifier::Asterisk,
468                                         &mut callback,
469                                     );
470                                 }
471                                 '0'..='9' => {
472                                     read_integer(&mut chars, &mut callback);
473                                     if let Some((_, Ok('$'))) = chars.peek() {
474                                         skip_char_and_emit(
475                                             &mut chars,
476                                             FormatSpecifier::DollarSign,
477                                             &mut callback,
478                                         );
479                                     }
480                                 }
481                                 c if c == '_' || c.is_alphabetic() => {
482                                     read_identifier(&mut chars, &mut callback);
483                                     if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
484                                         != Some('$')
485                                     {
486                                         continue;
487                                     }
488                                     skip_char_and_emit(
489                                         &mut chars,
490                                         FormatSpecifier::DollarSign,
491                                         &mut callback,
492                                     );
493                                 }
494                                 _ => {
495                                     continue;
496                                 }
497                             }
498                         }
499
500                         // type
501                         match chars
502                             .peek()
503                             .and_then(|next| next.1.as_ref().ok())
504                             .copied()
505                             .unwrap_or_default()
506                         {
507                             '?' => {
508                                 skip_char_and_emit(
509                                     &mut chars,
510                                     FormatSpecifier::QuestionMark,
511                                     &mut callback,
512                                 );
513                             }
514                             c if c == '_' || c.is_alphabetic() => {
515                                 read_identifier(&mut chars, &mut callback);
516
517                                 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
518                                     == Some('?')
519                                 {
520                                     skip_char_and_emit(
521                                         &mut chars,
522                                         FormatSpecifier::QuestionMark,
523                                         &mut callback,
524                                     );
525                                 }
526                             }
527                             _ => {}
528                         }
529                     }
530
531                     if let Some((_, Ok('}'))) = chars.peek() {
532                         skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
533                     } else {
534                         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 }