Exemplo n.º 1
0
        private Emitter WriteString(Emitter emitter)
        {
            if (Style == ScalarStyle.Plain)
            {
                if (emitter.IsFormatKeys && emitter.IsKey)
                {
                    emitter.WriteFormat(m_string);
                }
                else
                {
                    emitter.Write(m_string);
                }
            }
            else if (Style == ScalarStyle.SingleQuoted)
            {
                emitter.WriteDelayed();
                for (int i = 0; i < m_string.Length; i++)
                {
                    char c = m_string[i];
                    emitter.WriteRaw(c);
                    if (c == '\'')
                    {
                        emitter.WriteRaw(c);
                    }
                    else if (c == '\n')
                    {
                        emitter.WriteRaw("\n    ");
                    }
                }
            }
            else if (Style == ScalarStyle.DoubleQuoted)
            {
                emitter.WriteDelayed();
                int lineLimit = MaxLineLength;
                for (int i = 0; i < m_string.Length; i++)
                {
                    char c = m_string[i];
                    if (i >= lineLimit)
                    {
                        // space at the beginning of the line is discarded
                        if (c != ' ')
                        {
                            emitter.WriteRaw('\\').WriteRaw('\n').WriteRaw(' ');
                            lineLimit += MaxLineLength;
                        }
                    }

                    switch (c)
                    {
                    case '\\':
                        emitter.WriteRaw('\\').WriteRaw('\\');
                        break;

                    case '\n':
                        emitter.WriteRaw('\\').WriteRaw('n');
                        break;

                    case '\r':
                        emitter.WriteRaw('\\').WriteRaw('r');
                        break;

                    case '\t':
                        emitter.WriteRaw('\\').WriteRaw('t');
                        break;

                    case '"':
                        emitter.WriteRaw('\\').WriteRaw('"');
                        break;

                    default:
                        emitter.WriteRaw(c);
                        break;
                    }
                }
            }
            else
            {
                throw new NotSupportedException(Style.ToString());
            }
            return(emitter);
        }
Exemplo n.º 2
0
        internal Emitter ToString(Emitter emitter)
        {
            if (Style == ScalarStyle.Hex)
            {
                switch (m_objectType)
                {
                case ScalarType.Byte:
                    return(emitter.WriteHex((byte)m_value));

                case ScalarType.Int16:
                    return(emitter.WriteHex(unchecked ((short)m_value)));

                case ScalarType.UInt16:
                    return(emitter.WriteHex((ushort)m_value));

                case ScalarType.Int32:
                    return(emitter.WriteHex(unchecked ((int)m_value)));

                case ScalarType.UInt32:
                    return(emitter.WriteHex((uint)m_value));

                case ScalarType.Int64:
                    return(emitter.WriteHex(unchecked ((long)m_value)));

                case ScalarType.UInt64:
                    return(emitter.WriteHex(m_value));

                case ScalarType.Single:
                    return(emitter.WriteHex((uint)m_value));

                case ScalarType.Double:
                    return(emitter.WriteHex(m_value));

                default:
                    throw new NotImplementedException(m_objectType.ToString());
                }
            }

            switch (m_objectType)
            {
            case ScalarType.Boolean:
                return(emitter.Write(m_value));

            case ScalarType.Byte:
                return(emitter.Write(m_value));

            case ScalarType.Int16:
                return(emitter.Write(unchecked ((short)m_value)));

            case ScalarType.UInt16:
                return(emitter.Write(m_value));

            case ScalarType.Int32:
                return(emitter.Write(unchecked ((int)m_value)));

            case ScalarType.UInt32:
                return(emitter.Write(m_value));

            case ScalarType.Int64:
                return(emitter.Write(unchecked ((long)m_value)));

            case ScalarType.UInt64:
                return(emitter.Write(m_value));

            case ScalarType.Single:
                return(emitter.Write(BitConverterExtensions.ToSingle((uint)m_value)));

            case ScalarType.Double:
                return(emitter.Write(BitConverterExtensions.ToDouble(m_value)));

            case ScalarType.String:
                return(WriteString(emitter));

            default:
                throw new NotImplementedException(m_objectType.ToString());
            }
        }