<commit_before>﻿using devScottUtil.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace devScottUtil.IO
{
    public static class FixWidthFileWriter<T>
    {
        public static void WriteFixedWidth(XElement CommandNode, List<T> rows, Stream outputStream)
        {
            StreamWriter Output = new StreamWriter(outputStream);
            int StartAt = CommandNode.Attribute("StartAt") != null ? int.Parse(CommandNode.Attribute("StartAt").Value) : 0;

            var positions = from c in CommandNode.Descendants("Position")
                            orderby int.Parse(c.Attribute("Start").Value) ascending
                            select new
                            {
                                Name = c.Attribute("Name").Value,
                                Start = int.Parse(c.Attribute("Start").Value) - StartAt,
                                Length = int.Parse(c.Attribute("Length").Value),
                                Justification = c.Attribute("Justification") != null ? c.Attribute("Justification").Value.ToLower() : "left",
                                FillChar = c.Attribute("FillChar") != null ? c.Attribute("FillChar").Value.ToLower() : " "
                            };

            int lineLength = positions.Last().Start + positions.Last().Length;

            foreach (T item in rows)
            {
                StringBuilder line = new StringBuilder(lineLength);

                foreach (var p in positions)
                {
                    string val = ReflectionHelpers.GetPropertyValue(item, p.Name).ToString();<commit_msg>Make sure our value doesn't exceed the length of our position.<commit_after>﻿using devScottUtil.Reflection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace devScottUtil.IO
{
    public static class FixWidthFileWriter<T>
    {
        public static void WriteFixedWidth(XElement CommandNode, List<T> rows, Stream outputStream)
        {
            StreamWriter Output = new StreamWriter(outputStream);
            int StartAt = CommandNode.Attribute("StartAt") != null ? int.Parse(CommandNode.Attribute("StartAt").Value) : 0;

            var positions = from c in CommandNode.Descendants("Position")
                            orderby int.Parse(c.Attribute("Start").Value) ascending
                            select new
                            {
                                Name = c.Attribute("Name").Value,
                                Start = int.Parse(c.Attribute("Start").Value) - StartAt,
                                Length = int.Parse(c.Attribute("Length").Value),
                                Justification = c.Attribute("Justification") != null ? c.Attribute("Justification").Value.ToLower() : "left",
                                FillChar = c.Attribute("FillChar") != null ? c.Attribute("FillChar").Value.ToLower() : " "
                            };

            int lineLength = positions.Last().Start + positions.Last().Length;

            foreach (T item in rows)
            {
                StringBuilder line = new StringBuilder(lineLength);

                foreach (var p in positions)
                {
                    string val = ReflectionHelpers.GetPropertyValue(item, p.Name).ToString();

                    val = (String.IsNullOrEmpty(val)) ? String.Empty : val.Trim();

                    if (val.Length > p.Length)
                    {
                        val = val.Substring(0, p.Length);
                    }

{'fraction_non_alphanumeric': 0.10561056105610561, 'fraction_numerical': 0.0008250825082508251, 'mean_word_length': 1.7121551081282624, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5804315', 'n_tokens_mistral': 885, 'n_tokens_neox': 848, 'n_words': 193}