-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Belyaev
committed
Jan 23, 2024
1 parent
b41bdaa
commit 5deac6e
Showing
6 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.DS_Store | ||
bin | ||
|
||
obj | ||
.vs | ||
*.csproj.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
root = true | ||
|
||
[*.cs] | ||
indent_style = space | ||
indent_size = 2 | ||
tab_width = 2 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
end_of_line = lf | ||
charset = utf-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>localidiff</AssemblyName> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<SelfContained>true</SelfContained> | ||
<LangVersion>latest</LangVersion> | ||
<PublishTrimmed>true</PublishTrimmed> | ||
<PublishAot>true</PublishAot> | ||
<PublishSingleFile>false</PublishSingleFile> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 25.0.1706.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Localidiff", "Localidiff.csproj", "{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {7A4DFED6-0E8C-4114-8B68-61F1BC918581} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
static Dictionary<string, string[]> LoadFromFile(string path) | ||
{ | ||
var dict = new Dictionary<string, string[]>(); | ||
foreach (var strings in File.ReadAllLines(path).Select(line => line.Split('\t'))) | ||
{ | ||
switch (strings[0]) | ||
{ | ||
case "//": | ||
dict["//"] = strings; | ||
break; | ||
case "{": | ||
dict[strings[1]] = strings; | ||
break; | ||
case var _ when (strings.All(string.IsNullOrWhiteSpace)): | ||
break; | ||
default: | ||
throw new NotImplementedException(strings[0]); | ||
} | ||
} | ||
return dict; | ||
} | ||
|
||
static string FormatLine(string[] values) => string.Join('\t', values); | ||
|
||
static void Compare(string oldPath, string newPath) | ||
{ | ||
var oldDict = LoadFromFile(oldPath); | ||
var newDict = LoadFromFile(newPath); | ||
|
||
foreach (var key in oldDict.Keys.Union(newDict.Keys).OrderBy(x => x)) | ||
{ | ||
var haveOld = oldDict.TryGetValue(key, out var oldValue); | ||
var haveNew = newDict.TryGetValue(key, out var newValue); | ||
if (!haveOld) | ||
Console.WriteLine($"ADD\t{FormatLine(newValue!)}"); | ||
else if (!haveNew) | ||
Console.WriteLine($"REMOVE\t{FormatLine(oldValue!)}"); | ||
else | ||
{ | ||
if (newValue!.Length > oldValue!.Length) | ||
for (int i = oldValue.Length - 1; i < newValue.Length - 1; i++) | ||
Console.WriteLine($"ADD-LANGUAGE\t{key}\t{i}\t{newValue[i]}"); | ||
else if(newValue.Length < oldValue.Length) | ||
for (int i = newValue.Length - 1; i < oldValue.Length - 1; i++) | ||
Console.WriteLine($"REMOVE-LANGUAGE\t{key}\t{i}\t{oldValue[i]}"); | ||
|
||
for (int i = 1; i < Math.Min(oldValue!.Length, newValue!.Length) - 1; i++) | ||
if (oldValue[i] != newValue![i]) | ||
Console.WriteLine($"CHANGE\t{key}\t{i}\t{oldValue[i]}\t{newValue[i]}"); | ||
} | ||
} | ||
} | ||
|
||
|
||
if (args.Length == 7) | ||
{ | ||
// localidiff old.txt new.txt | ||
Console.WriteLine($"FILE\t{args[0]}"); | ||
Compare(args[1], args[4]); | ||
} | ||
else if (args.Length == 2 && args[0] == "apply") | ||
{ | ||
// localidiff apply patch | ||
var patch = File.ReadAllLines(args[1]); | ||
|
||
Dictionary<string, string[]>? dict = null; | ||
string? path = null; | ||
foreach (var line in patch) | ||
{ | ||
var items = line.Split('\t'); | ||
switch (items[0]) | ||
{ | ||
case "FILE": | ||
if (dict != null && path != null) | ||
{ | ||
Console.WriteLine($"Writing {path}"); | ||
File.WriteAllLines(path, dict.OrderByDescending(pair => pair.Key == "//").ThenBy(pair => pair.Key).Select(pair => FormatLine(pair.Value) + "\r"), System.Text.Encoding.Unicode); | ||
} | ||
path = items[1]; | ||
Console.WriteLine($"Loading {path}"); | ||
dict = LoadFromFile(path); | ||
break; | ||
|
||
case "ADD": | ||
Console.WriteLine($"Adding {FormatLine(items.Skip(1).ToArray())}"); | ||
dict![items[2]] = items.Skip(1).ToArray(); | ||
break; | ||
|
||
case "REMOVE": | ||
Console.WriteLine($"Removing {FormatLine(dict![items[2]])}"); | ||
dict!.Remove(items[2]); | ||
break; | ||
|
||
case "CHANGE": | ||
if (dict!.ContainsKey(items[1])) | ||
{ | ||
Console.WriteLine($"Replacing {dict![items[1]][int.Parse(items[2])]} with {items[4]}"); | ||
dict![items[1]][int.Parse(items[2])] = items[4]; | ||
} | ||
else | ||
Console.WriteLine($"{items[1]} ({items[3]} -> {items[4]}) not found for replace!"); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
Console.WriteLine($"Writing {path}"); | ||
File.WriteAllLines(path!, dict!.OrderByDescending(pair => pair.Key == "//").ThenBy(pair => pair.Key).Select(pair => FormatLine(pair.Value) + "\r"), System.Text.Encoding.Unicode); | ||
} | ||
else if (args.Length == 2) | ||
{ | ||
Console.WriteLine($"FILE\t{Path.GetFileName(args[0])}"); | ||
Compare(args[0], args[1]); | ||
} | ||
else | ||
{ | ||
Console.WriteLine(""" | ||
Usage: | ||
Localidiff 1.txt 2.txt | ||
Compares 2.txt to 1.txt and outputs the patch into the standard output | ||
Localidiff unused 1.txt unused unused 2.txt unused unused | ||
The same but for usage as a git difftool | ||
Localidiff apply patch.patch | ||
Applies patch.patch | ||
"""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters