Skip to content

Commit

Permalink
line ending fix
Browse files Browse the repository at this point in the history
  • Loading branch information
YunHsiao committed Nov 1, 2024
1 parent e0cc6fd commit c3b9b0d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Crysknife/CrysknifeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public ConfigFileSectionNode(ConfigFileSection Source)
}

private ConfigFileSectionNode? Section;
private readonly Dictionary<string, ConfigSectionHierarchy> Children = new ();
private readonly Dictionary<string, ConfigSectionHierarchy> Children = new();

public void InheritancePatch(ConfigFileSection? Parent = null)
{
Expand Down
10 changes: 5 additions & 5 deletions Crysknife/CrysknifeConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override string ToString()
internal class ConfigFileSection
{
public readonly string Name;
public readonly List<ConfigLine> Lines = new ();
public readonly List<ConfigLine> Lines = new();

public void ParseLines(IDictionary<string, string> Result, char Separator, Func<string, string>? MapFunc = null)
{
Expand Down Expand Up @@ -93,7 +93,7 @@ private static string RemapSectionOrKey(IDictionary<string, string>? Remap, stri

private static void ReadIntoSections(string Location, IDictionary<string, ConfigFileSection> Sections, ConfigLineAction DefaultAction)
{
using StreamReader Reader = new(Location);
using StreamReader Reader = new (Location);
ConfigFileSection? CurrentSection = null;
Dictionary<string, string>? CurrentRemap = null;

Expand Down Expand Up @@ -282,11 +282,11 @@ private ConfigFileSection FindOrAddSection(string SectionName)
// Remap of config names/sections
private static readonly Dictionary<string, string> SectionNameRemap = new();
private static readonly Dictionary<string, Dictionary<string, string>> SectionKeyRemap = new();
private static readonly HashSet<string> WarnedKeys = new(StringComparer.InvariantCultureIgnoreCase);
private static readonly HashSet<string> WarnedKeys = new (StringComparer.InvariantCultureIgnoreCase);

public static void Init(string RootDirectory)
{
Dictionary<string, ConfigFileSection> Sections = new(StringComparer.InvariantCultureIgnoreCase);
Dictionary<string, ConfigFileSection> Sections = new (StringComparer.InvariantCultureIgnoreCase);
try
{
// read the special ConfigRedirects.ini file into sections
Expand Down Expand Up @@ -317,7 +317,7 @@ public static void Init(string RootDirectory)
else
{
// any other section is rmembered by the section name here, and each key/value pair is a remap for the given section
Dictionary<string, string> KeyRemap = new(StringComparer.InvariantCultureIgnoreCase);
Dictionary<string, string> KeyRemap = new (StringComparer.InvariantCultureIgnoreCase);
SectionKeyRemap.Add(Pair.Key, KeyRemap);
foreach (var Line in Pair.Value.Lines)
{
Expand Down
11 changes: 7 additions & 4 deletions Crysknife/CrysknifeInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ private void ProcessPatch(JobType Job, string PatchPath, string TargetPath, Sour
return;
}

var TargetContent = SourcePatch.PatchRegex.ClearResiduals(Utils.UnifyLineEndings(File.ReadAllText(TargetPath)));
var CurrentContent = File.ReadAllText(TargetPath);
var TargetContent = SourcePatch.PatchRegex.ClearResiduals(Utils.UnifyLineEndings(CurrentContent));
var ClearedTarget = SourcePatch.PatchRegex.Unpatch(TargetContent);

if (Job.HasFlag(JobType.Generate) && Job.HasFlag(JobType.Clear)) Generate();
Expand Down Expand Up @@ -129,7 +130,9 @@ void Apply()
Path.GetRelativePath(Utils.GetSourceDirectory(), TargetPath));

var Success = PatcherInstance.Apply(Patches, ClearedTarget, DumpPath, Options.HasFlag(JobOptions.DryRun), out var Patched);
if (Success && !Patched.Equals(TargetContent, StringComparison.Ordinal))
var FinalContent = Utils.UnifyLineEndings(Patched, SourcePatch.Format.Crlf);
if (Success && (!Patched.Equals(TargetContent, StringComparison.Ordinal) ||
(Options.HasFlag(JobOptions.Force) && !FinalContent.Equals(CurrentContent, StringComparison.Ordinal))))
{
if (TargetContent.Length != ClearedTarget.Length)
{
Expand All @@ -141,7 +144,7 @@ void Apply()
if (OverrideConfirm.HasFlag(Utils.ConfirmResult.No)) return;
}

Utils.FileAccessGuard(() => File.WriteAllText(TargetPath, Utils.UnifyLineEndings(Patched, SourcePatch.Format.Crlf)), TargetPath);
Utils.FileAccessGuard(() => File.WriteAllText(TargetPath, FinalContent), TargetPath);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Patched: " + TargetPath);
TargetContent = Patched;
Expand All @@ -156,7 +159,7 @@ private void ProcessFile(JobType Job, string SrcPath, string DstPath, bool Crlf)
var Exists = File.Exists(DstPath);
var IsSymLink = Exists && new FileInfo(DstPath).Attributes.HasFlag(FileAttributes.ReparsePoint);
var SourceContent = File.ReadAllText(SrcPath);
var TargetContent = Utils.UnifyLineEndings(File.ReadAllText(DstPath));
var TargetContent = Exists ? Utils.UnifyLineEndings(File.ReadAllText(DstPath)) : string.Empty;
var UpToDate = Exists && !IsSymLink && SourceContent == TargetContent;

if (Job.HasFlag(JobType.Generate) && !IsSymLink && !UpToDate)
Expand Down
4 changes: 2 additions & 2 deletions Crysknife/CrysknifeSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ private static void GenerateSetupScripts(string TargetDirectory, string PluginNa
Console.WriteLine("Setup scripts created: " + Path.Combine(TargetDirectory, "Setup"));
}

private static readonly JsonObject PluginReference = new(
new[]
private static readonly JsonObject PluginReference = new (
new []
{
KeyValuePair.Create<string, JsonNode?>("Name", "Crysknife"),
KeyValuePair.Create<string, JsonNode?>("Enabled", true)
Expand Down
6 changes: 4 additions & 2 deletions Crysknife/CrysknifeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ public static string UnifySeparators(string Value)
return UnifySeparators(Value, Path.DirectorySeparatorChar.ToString());
}

private static readonly Regex NonWindowsRE = new (@"(?<!\r)\n|\r", RegexOptions.Compiled);
private static readonly Regex NonUnixRE = new (@"\r\n|\r(?!\n)", RegexOptions.Compiled);
public static string UnifyLineEndings(string Content, bool Crlf = false)
{
return Content.Replace(Crlf ? "\n" : "\r\n", Crlf ? "\r\n" : "\n");
return Crlf ? NonWindowsRE.Replace(Content, "\r\n") : NonUnixRE.Replace(Content, "\n");
}

private static readonly Regex TruthyRegex = new ("^(?:T|On)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Expand Down Expand Up @@ -500,7 +502,7 @@ public static string CamelCaseToSnakeCase(string Value)
return CaseRegex.Replace(Value, Match => $"_{Match.Value}").ToLower();
}

private static readonly Regex EscapeRegex = new("\\" + string.Join<char>("|\\", "^$()[].*+?"), RegexOptions.Compiled);
private static readonly Regex EscapeRegex = new ("\\" + string.Join<char>("|\\", "^$()[].*+?"), RegexOptions.Compiled);
public static string EscapeLiteralsForRegex(string Value)
{
return EscapeRegex.Replace(Value, Matched => $"\\{Matched.Value}");
Expand Down
6 changes: 3 additions & 3 deletions Crysknife/DiffMatchPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ protected List<Diff> diff_bisect(string Text1, string Text2, DateTime Deadline)

// Diff took too long and hit the deadline or
// number of diffs equals number of characters, no commonality at all.
var Diffs = new List<Diff> { new(Operation.Delete, Text1), new(Operation.Insert, Text2) };
var Diffs = new List<Diff> { new (Operation.Delete, Text1), new (Operation.Insert, Text2) };
return Diffs;
}

Expand Down Expand Up @@ -1238,8 +1238,8 @@ private int diff_cleanupSemanticScore(string One, string Two)
}

// Define some regex patterns for matching boundaries.
private readonly Regex Blanklineend = new("\\n\\r?\\n\\Z");
private readonly Regex Blanklinestart = new("\\A\\r?\\n\\r?\\n");
private readonly Regex Blanklineend = new ("\\n\\r?\\n\\Z");
private readonly Regex Blanklinestart = new ("\\A\\r?\\n\\r?\\n");

/**
* Reduce the number of edits by eliminating operationally trivial
Expand Down

0 comments on commit c3b9b0d

Please sign in to comment.