<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiffPatch.Core;
using DiffPatch.Data;

namespace DiffPatch.DiffPatcher
{
    public class Patcher
    {
        public static String Patch(String src, IEnumerable<ChunkDiff> chunkDiffs)
        {
            IEnumerable<String> srcLines = StringHelper.SplitLines(src);
            int lineIndex = 1;


            IEnumerable<Tuple<Int32, Int32, ChunkDiff>> chunkDiffByOldStart = chunkDiffs.Select(d => new Tuple<int, int, ChunkDiff>(d.OldStart, d.OldStart + d.OldLines, d));

            StringBuilder stringBuilder = new StringBuilder();


            foreach (string srcLine in srcLines)
            {
                Tuple<int, int, ChunkDiff> tuple = chunkDiffByOldStart.FirstOrDefault(kvp => lineIndex >= kvp.Item1 && lineIndex < kvp.Item2);
                if (tuple == null)
                {
                    stringBuilder.AppendLine(srcLine);
                }
                else
                {
                    LineDiff lineDiff = tuple.Item3.Changes.FirstOrDefault(l => tuple.Item1 + l.OldIndex == lineIndex);
                    if (lineDiff != null)
                    {
                        if (lineDiff.Normal || lineDiff.Add)
                        {
                            stringBuilder.AppendLine(lineDiff.Content);
                        }
                    }

                }
                lineIndex++;
            }

            return stringBuilder.ToString();
        }
    }
}<commit_msg>Amélioration méthode Patch (Work In Progess)<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiffPatch.Core;
using DiffPatch.Data;

namespace DiffPatch.DiffPatcher
{
    public class Patcher
    {
        public static String Patch(String src, IEnumerable<ChunkDiff> chunkDiffs, string lineEnding = "\n")
        {
            IEnumerable<String> srcLines = StringHelper.SplitLines(src, lineEnding);
            IList<string> dstLines = new List<string>(srcLines);

            foreach (ChunkDiff chunkDiff in chunkDiffs)
            {
                int lineIndex = chunkDiff.NewStart - 1; // zero-index the start line 
                if (lineIndex < 0)
                    lineIndex = 0;

                foreach (LineDiff lineDiff in chunkDiff.Changes)
                {
                    if (lineDiff.Add)
                    {
                        dstLines.Insert(lineIndex, lineDiff.Content.Substring(2));
                        lineIndex++;
                    }
                    else if (lineDiff.Delete)
                    {
                        dstLines.RemoveAt(lineIndex);
                    }
                    else if (lineDiff.Normal)
                    {
                        lineIndex++;
                    }
                }

            }

            string patchString = string.Join(lineEnding, dstLines.ToArray());
            return patchString;
        }
    }
}
{'fraction_non_alphanumeric': 0.08471814923427827, 'fraction_numerical': 0.004235907461713913, 'mean_word_length': 1.4619085805934242, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '499208', 'n_tokens_mistral': 804, 'n_tokens_neox': 745, 'n_words': 167}