YamlSequence FromSpan <T>(ReadOnlySpan <T> collection, Func <T, YamlObject> selector)
        {
            var result = new YamlSequence();

            foreach (var item in collection)
            {
                result.Add(selector(item));
            }

            return(result);
        }
        YamlSequence FromEnumerable <T>(IEnumerable <T> collection, Func <T, YamlObject> selector)
        {
            var result = new YamlSequence();

            foreach (var item in collection)
            {
                result.Add(selector(item));
            }

            return(result);
        }
예제 #3
0
        void WriteSequence(YamlSequence obj)
        {
            if (TryInlineSequence(obj, _maximumWidth - _indentColumn, out var inlined))
            {
                WriteInline(inlined);
            }
            else
            {
                var oldInlineColumn = _inlineColumn;
                _inlineColumn = _indentColumn + _indentSize;
                foreach (var item in obj)
                {
                    WriteLine();
                    Write("- ");
                    _indentColumn += _indentSize;
                    _inlineColumn  = _indentColumn;
                    WriteObject(item);
                    _indentColumn -= _indentSize;
                }

                _inlineColumn = oldInlineColumn;
            }
        }
예제 #4
0
        bool TryInlineSequence(YamlSequence obj, int maximumWidth, out string result)
        {
            result = null;
            var sb = new StringBuilder();

            sb.Append("[ ");
            var firstSeen = false;

            foreach (var value in obj)
            {
                if (firstSeen)
                {
                    sb.Append(", ");
                }

                firstSeen = true;

                if (TryInlineObject(value, maximumWidth - sb.Length, out var valueInlined))
                {
                    sb.Append(valueInlined);
                }
                else
                {
                    result = null;
                    return(false);
                }
            }

            sb.Append(firstSeen ? " ]" : "]");
            if (sb.Length <= maximumWidth)
            {
                result = sb.ToString();
            }

            return(result != null);
        }