-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.old
130 lines (113 loc) · 3.71 KB
/
Example.old
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Linq;
using System.Collections.Generic;
namespace patchiron
{
public static class Conversion
{
static string? ProcessLine (string line)
{
if (line.Contains ("enum"))
return line;
return null;
}
// Add patching logic here.
public static void ProcessChunk (PatchChunk chunk, string fileName)
{
//string [] removedLines = chunk.Lines.Where (x => x.StartsWith ("-", StringComparison.Ordinal)).ToArray ();
//string [] addedLines = chunk.Lines.Where (x => x.StartsWith ("-", StringComparison.Ordinal)).ToArray ();
Dictionary<int, Delta> deltaList = new Dictionary<int, Delta> ();
List<Range> diffs = chunk.CalculateDiffs ();
foreach (var range in diffs)
{
for (int i = range.Low; i < range.High; ++i)
{
int index = i;// + rangeOffSet;
string line = chunk.Lines [index];
if (ProcessLine (line) == null)
{
if (line.StartsWith ("-", StringComparison.InvariantCulture))
deltaList.Add (index, Delta.CreateAddAfter ("+" + line.Substring (1)));
else
deltaList.Add (index, Delta.Removal);
}
}
}
foreach (var kv in deltaList.OrderBy (x => x.Key).Reverse ())
{
var action = kv.Value.Type;
switch (action)
{
case DeltaType.Addition:
chunk.InsertAt (kv.Key, kv.Value.Data);
break;
case DeltaType.AddAfter:
chunk.InsertAt (kv.Key + 1, kv.Value.Data);
break;
case DeltaType.Removal:
chunk.RemoveAt (kv.Key);
break;
default:
throw new NotImplementedException ();
}
}
// If you have unbound add/removals you might have to tweak the chunk header
//for (int i = range.Low; i >= 0; --i)
//{
// string line = chunk.Lines [i];
// if (line.StartsWith ("@@ -", StringComparison.Ordinal))
// {
// var bits = line.Split (new char [] { ',' }, 3);
// var correctPart = bits [1].Substring (0, bits [1].IndexOf (' '));
// var fixedLine = correctPart + bits [2].Substring (bits [2].IndexOf (' '));
// chunk.Replace (line, bits [0] + "," + bits [1] + "," + fixedLine);
// break;
// }
//}
}
public static string ReplaceFirst (string text, string search, string replace)
{
int pos = text.IndexOf (search);
if (pos < 0)
{
return text;
}
return text.Substring (0, pos) + replace + text.Substring (pos + search.Length);
}
// Doesn't have to be perfect
//static string ProcessAvailabilityToSpecifc (string line)
//{
// string name = null;
// if (line.Contains ("[Introduced (PlatformName.iOS"))
// name = "iOS";
// else if (line.Contains ("[Availability (Introduced = "))
// name = "Introduced";
// else if (line.Contains ("[Availability (Unavailable = "))
// name = "Unavailable";
// if (name != null)
// {
// foreach (var replacement in FindPlatformBits (line))
// line = line.Replace (replacement.Key, replacement.Value);
// return line.Replace ($"[Availability ({name} = ", $"[{name} (").Replace ("Message =", "message :");
// }
// return line;
//}
//static IEnumerable<KeyValuePair<string, string>> FindPlatformBits (string line)
//{
// int offset = 0;
// while (true)
// {
// int start = line.IndexOf ("Platform.", offset, StringComparison.Ordinal);
// if (start == -1)
// yield break;
// int end = start + 9 /* Platform. */;
// while (char.IsLetterOrDigit (line [end]) || line [end] == '_')
// end++;
// string token = line.Substring (start, end - start);
// string convertedToken = token.Replace ("Platform", "PlatformName").Replace ("_", ", ").Replace ("Mac", "MacOSX").Replace ("Watch", "WatchOS").Replace ("TV", "TvOS");
// yield return new KeyValuePair<string, string> (token, convertedToken);
// offset = end + 1;
// }
//}
}
}