Loyc.Syntax.PrintHelpers.EscapeCStyle C# (CSharp) Method

EscapeCStyle() public static method

Writes a character c to a StringBuilder, either as a normal character or as a C-style escape sequence.
EscapeC.HasLongEscape can be used to force a 6-digit unicode escape; this may be needed if the next character after this one is a digit.
public static EscapeCStyle ( int c, StringBuilder @out, EscapeC flags = EscapeC.Default, char quoteType = '\0' ) : bool
c int
@out StringBuilder
flags EscapeC Specifies which characters should be escaped.
quoteType char Specifies a character that should always be /// escaped (typically one of ' " `)
return bool
		public static bool EscapeCStyle(int c, StringBuilder @out, EscapeC flags = EscapeC.Default, char quoteType = '\0')
		{
			for(;;) {
				if (c >= 128) {
					if ((flags & EscapeC.NonAscii) != 0) {
						EscapeU(c, @out, flags);
					} else if (c >= 0xDC00) {
						if ((flags & EscapeC.UnicodeNonCharacters) != 0 && (
							c >= 0xFDD0 && c <= 0xFDEF || // 0xFDD0...0xFDEF 
							(c & 0xFFFE) == 0xFFFE) || // 0xFFFE, 0xFFFF, 0x1FFFE, 0x1FFFF, etc.
							(c & 0xFC00) == 0xDC00) { // 0xDC00...0xDCFF 
							EscapeU(c, @out, flags);
						} else if ((flags & EscapeC.UnicodePrivateUse) != 0 && (
							c >= 0xE000 && c <= 0xF8FF ||
							c >= 0xF0000 && c <= 0xFFFFD ||
							c >= 0x100000 && c <= 0x10FFFD)) {
							EscapeU(c, @out, flags);
						} else
							break;
					} else
						break;
				} else if (c < 32) {
					if (c == '\n')
						@out.Append(@"\n");
					else if (c == '\r')
						@out.Append(@"\r");
					else if (c == '\0')
						@out.Append(@"\0");
					else {
						if ((flags & EscapeC.ABFV) != 0) {
							if (c == '\a') { // 7 (alert)
								@out.Append(@"\a");
								return true;
							}
							if (c == '\b') { // 8 (backspace)
								@out.Append(@"\b");
								return true;
							}
							if (c == '\f') { // 12 (form feed)
								@out.Append(@"\f");
								return true;
							}
							if (c == '\v') { // 11 (vertical tab)
								@out.Append(@"\v");
								return true;
							}
						}
						if ((flags & EscapeC.Control) != 0) {
							if (c == '\t')
								@out.Append(@"\t");
							else
								EscapeU(c, @out, flags);
						} else
							@out.Append(c);
					}
				} else if (c == '\"' && (flags & EscapeC.DoubleQuotes) != 0) {
					@out.Append("\\\"");
				} else if (c == '\'' && (flags & EscapeC.SingleQuotes) != 0) {
					@out.Append("\\'");
				} else if (c == '\\')
					@out.Append(@"\\");
				else
					break;
				return true;
			}

			if (c == quoteType) {
				@out.Append('\\');
				@out.Append((char)c);
				return true;
			} else 
				@out.AppendCodePoint(c);
			return false;
		}

Same methods

PrintHelpers::EscapeCStyle ( UString s, EscapeC flags = EscapeC.Default ) : string
PrintHelpers::EscapeCStyle ( UString s, EscapeC flags, char quoteType ) : string

Usage Example

Exemplo n.º 1
0
 /// <inheritdoc cref="ILiteralParser.TryParse(UString, Symbol)"/>
 public Either <object, ILogMessage> TryParse(UString textValue, Symbol typeMarker)
 {
     typeMarker = typeMarker ?? GSymbol.Empty;
     if (Parsers.TryGetValue(typeMarker, out var parser))
     {
         try {
             return(parser(textValue, typeMarker).MapRight(m => (ILogMessage)m));
         } catch (Exception e) {
             return(new Either <object, ILogMessage>((ILogMessage) new LogMessage(Severity.Error, textValue, e.Description())));
         }
     }
     return(new Either <object, ILogMessage>((ILogMessage) new LogMessage(Severity.Note, textValue, "No parser is registered for type marker '{0}'".Localized(PrintHelpers.EscapeCStyle(typeMarker.Name)))));
 }