Antlr4.Codegen.Target.JavaTarget.GetTargetStringLiteralFromANTLRStringLiteral C# (CSharp) Метод

GetTargetStringLiteralFromANTLRStringLiteral() публичный Метод

public GetTargetStringLiteralFromANTLRStringLiteral ( CodeGenerator generator, string literal, bool addQuotes ) : string
generator CodeGenerator
literal string
addQuotes bool
Результат string
        public override string GetTargetStringLiteralFromANTLRStringLiteral(
            CodeGenerator generator,
            string literal, bool addQuotes)
        {
            StringBuilder sb = new StringBuilder();
            string @is = literal;

            if (addQuotes)
                sb.Append('"');

            for (int i = 1; i < @is.Length - 1; i++)
            {
                if (@is[i] == '\\')
                {
                    // Anything escaped is what it is! We assume that
                    // people know how to escape characters correctly. However
                    // we catch anything that does not need an escape in Java (which
                    // is what the default implementation is dealing with and remove
                    // the escape. The C target does this for instance.
                    //
                    switch (@is[i + 1])
                    {
                    // Pass through any escapes that Java also needs
                    //
                    case '"':
                    case 'n':
                    case 'r':
                    case 't':
                    case 'b':
                    case 'f':
                    case '\\':
                        // Pass the escape through
                        sb.Append('\\');
                        break;

                    case 'u':    // Assume uNNNN
                                 // Pass the escape through as double \\
                                 // so that Java leaves as \u0000 string not char
                        sb.Append('\\');
                        sb.Append('\\');
                        break;

                    default:
                        // Remove the escape by virtue of not adding it here
                        // Thus \' becomes ' and so on
                        break;
                    }

                    // Go past the \ character
                    i++;
                }
                else
                {
                    // Characters that don't need \ in ANTLR 'strings' but do in Java
                    if (@is[i] == '"')
                    {
                        // We need to escape " in Java
                        sb.Append('\\');
                    }
                }
                // Add in the next character, which may have been escaped
                sb.Append(@is[i]);
            }

            if (addQuotes)
                sb.Append('"');

            return sb.ToString();
        }