示例#1
0
        /// <summary>
        /// Converts the <see cref="string"/> value into Lua expression.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="delimiter">The delimiter used to start the Lua string expression.</param>
        /// <remarks>
        /// Lua supports starting a string expression with <c>'</c>, <c>"</c>, or <c>[====[</c>, where in
        /// the last case, there can be any (including 0) count of equal signs.
        /// </remarks>
        public static string ToString(string value, string delimiter)
        {
            var delm = StringDelimiterInfo.FromStartDelimiter(delimiter);

            using (var writer = Utility.CreateStringWriter((value?.Length ?? 0) + delimiter.Length * 2))
            {
                WriteEscapedString(value, delm, writer);
                return(writer.ToString());
            }
        }
示例#2
0
        internal static void WriteEscapedString(string value, StringDelimiterInfo delimiter, TextWriter writer)
        {
            Debug.Assert(delimiter != null);
            if (string.IsNullOrEmpty(value))
            {
                writer.Write(delimiter.StartDelimiter);
                writer.Write(delimiter.EndDelimiter);
                return;
            }
            if (delimiter.IsBracket)
            {
                if (value[0] == '\n')
                {
                    throw new InvalidOperationException(@"String value cannot start with \n when using bracket as delimiter.");
                }
                if (value.Contains(delimiter.EndDelimiter))
                {
                    throw new InvalidOperationException(@"String value cannot contain closing long bracket sequence of the proper level.");
                }
                writer.Write(delimiter.StartDelimiter);
                writer.Write(value);
                writer.Write(delimiter.EndDelimiter);
            }
            else
            {
                writer.Write(delimiter.StartDelimiter);
                foreach (var c in value)
                {
                    switch (c)
                    {
                    case '\\':
                        writer.Write("\\");
                        break;

                    case '\a':
                        writer.Write("\\a");
                        break;

                    case '\b':
                        writer.Write("\\b");
                        break;

                    case '\f':
                        writer.Write("\\f");
                        break;

                    case '\n':
                        writer.Write("\\n");
                        break;

                    case '\r':
                        writer.Write("\\r");
                        break;

                    case '\t':
                        writer.Write("\\t");
                        break;

                    case '\v':
                        writer.Write("\\v");
                        break;

                    case '\"' when delimiter.IsDoubleQuote:
                        writer.Write("\\\"");
                        break;

                    case '\'' when delimiter.IsSingleQuote:
                        writer.Write("\\\'");
                        break;

                    default:
                        writer.Write(c);
                        break;
                    }
                }
                writer.Write(delimiter.EndDelimiter);
            }
        }
示例#3
0
        public static void WriteString(TextWriter writer, string value, string delimiter)
        {
            var delm = StringDelimiterInfo.FromStartDelimiter(delimiter);

            WriteEscapedString(value, delm, writer);
        }
示例#4
0
        private string ReadComment()
        {
            if (!Consume("--"))
            {
                return(null);
            }
            var equalSigns   = -1;
            var bracketLevel = -1;

            if (Consume('['))
            {
                equalSigns = 0;
                while (Consume('='))
                {
                    equalSigns++;
                }
                if (Consume('['))
                {
                    // Read: --[==...==[
                    bracketLevel = equalSigns;
                }
            }
            // Parse / skip comment
            StringBuilder sb = null;

            if (PreserveComments)
            {
                sb = new StringBuilder("--");
            }
            if (bracketLevel < 0)
            {
                // Single-line comment
                // -- Comment
                if (sb != null)
                {
                    // Restore [ and = that we have already read.
                    if (equalSigns >= 0)
                    {
                        sb.Append('[');
                        if (equalSigns > 0)
                        {
                            sb.Append('=', equalSigns);
                        }
                    }
                }
                ConsumeUntil('\r', '\n', sb);
                Consume();
            }
            else
            {
                // Multi-line comment
                // --[==[ Comment
                // Comment
                // Comment]==] comment
                var bracket = StringDelimiterInfo.FromBracketLevel(bracketLevel);
                sb?.Append(bracket.StartDelimiter);
                while (!BufferEof)
                {
                    ConsumeUntil(']', char.MinValue, sb);
                    if (Consume(bracket.EndDelimiter))
                    {
                        sb?.Append(bracket.EndDelimiter);
                        // right bracket detected
                        // skip current line
                        ConsumeUntil('\r', '\n', sb);
                        Consume();
                        break;
                    }
                    else
                    {
                        // ordinary right bracket.
                        sb?.Append((char)Consume());
                    }
                }
            }
            return(sb != null?sb.ToString() : string.Empty);
        }