private bool ParseFormatSpecifier(ref StringPart result) { // https://en.wikipedia.org/wiki/Printf_format_string#Syntax // %[parameter][flags][width][.precision][length]type // text.Char() == '%' int start = text.Position; text.Next(); // skip % int len = 1; while ( true ) { if ( text.EndOfLine || text.Char() == '\\' ) { break; } len++; if ( Char.IsLetter(text.Char()) ) { text.Next(); break; } text.Next(); } // if len == 1, then we found %" if ( len < 2 ) return false; result = new StringPart(start, len, StringPartType.FormatSpecifier); return true; }
public override bool Equals(object obj) { if (obj == null) { return(false); } StringPart part = (StringPart)obj; return(part.Span == this.Span && part.Type == this.Type); }
public StringPart? Next() { while ( !text.EndOfLine ) { if ( text.Char() == '\\' ) { return BasicCStringScanner.ParseEscapeSequence(text); } else if ( text.Char() == '{' && text.NChar() == '{' ) { text.Next(); // skip it } else if ( text.Char() == '{' && !isInterpolated ) { StringPart part = new StringPart(); if ( ParseFormatSpecifier(ref part) ) return part; } text.Next(); } return null; }
private bool ParseFormatSpecifier(ref StringPart result) { // text.Char() == '{' int start = text.Position; int len = 1; text.Next(); while ( !text.EndOfLine ) { len++; if ( text.Char() == '}' ) { result = new StringPart(start, len, StringPartType.FormatSpecifier); text.Next(); return true; } text.Next(); } return false; }
public StringPart? Next() { while ( !text.EndOfLine ) { if ( text.Char() == '\\' ) { StringPart part = new StringPart(); if ( TryParseEscapeSequence(ref part) ) return part; } else if ( text.Char() == '%' ) { StringPart part = new StringPart(); if ( TryParseFormatSpecifier(ref part) ) return part; } else { text.Next(); } } return null; }
public StringPart? Next() { while ( !text.EndOfLine ) { if ( text.Char() == '\\' ) { return BasicCStringScanner.ParseEscapeSequence(text); } else if ( text.Char() == '%' ) { // skip %% if ( text.NChar() == '%' ) { text.Skip(2); continue; } StringPart part = new StringPart(); if ( ParseFormatSpecifier(ref part) ) return part; } text.Next(); } return null; }
private bool TryParseEscapeSequence(ref StringPart part) { text.Next(); if ( escapeChar.IndexOf(text.Char()) >= 0 ) { text.Next(); part = new StringPart(new Span(text.Position - 2, 2)); return true; } if ( Char.IsDigit(text.Char()) && Char.IsDigit(text.NChar()) && Char.IsDigit(text.NNChar()) ) { // a trigraph text.Skip(3); part = new StringPart(new Span(text.Position - 4, 4)); return true; } if ( text.Char() == '0' && !Char.IsDigit(text.NChar()) ) { // \0 text.Next(); part = new StringPart(new Span(text.Position - 2, 2)); return true; } if ( text.Char() == 'u' ) { text.Next(); text.Mark(); Span? span = TryParseShortUnicode(); if ( span.HasValue ) { part = new StringPart(span.Value); return true; } text.BackToMark(); } if ( text.Char() == 'U' ) { text.Next(); text.Mark(); Span? span = TryParseLongUnicode(); if ( span.HasValue ) { part = new StringPart(span.Value); return true; } text.BackToMark(); } return false; }
private bool TryParseFormatSpecifier(ref StringPart result) { // https://msdn.microsoft.com/en-us/library/ee370560.aspx // %[flags][width][.precision]type int start = text.Position; text.Next(); // skip '%' // ignore %% if ( text.Char() == '%' ) { text.Next(); return false; } // ignore EOF if ( text.EndOfLine || text.Char() == '\"' ) return false; int len = 1; while ( !text.EndOfLine ) { len++; if ( Char.IsLetter(text.Char()) ) { text.Next(); break; } text.Next(); } result = new StringPart(start, len, StringPartType.FormatSpecifier); return true; }