public static Apply ( StringBuilder dest, string diff ) : void | ||
dest | StringBuilder | Destination string |
diff | string | Patch string |
return | void |
public static void Apply(StringBuilder dest, string diff)
{
try
{
if (diff == null)
{
return;
}
int pos = dest.Length - 1;
if (pos < 0)
{
return;
}
// orig == ""
for (int i = 0; i < diff.Length / 2; i++)
{
char cmd = diff[2 * i];
char param = diff[2 * i + 1];
int par_num = (param - 'a' + 1);
switch (cmd)
{
case '-':
pos = pos - par_num + 1;
break;
case 'R':
dest[pos] = param;
break;
case 'D':
int o = pos;
pos -= par_num - 1;
/*
* delete par_num chars from index pos
*/
// String s = orig.toString();
// s = s.substring( 0, pos ) + s.substring( o + 1 );
// orig = new StringBuffer( s );
dest.Remove(pos, (o + 1) - pos);
break;
case 'I':
dest.Insert(pos += 1, param);
break;
}
pos--;
}
}
catch (IndexOutOfRangeException /*x*/)
{
// x.printStackTrace();
}
catch (ArgumentOutOfRangeException /*x*/)
{
// x.printStackTrace();
}
}
private static void AssertTrie(Trie trie, string file, bool usefull, bool storeorig) { using (TextReader @in = new StreamReader(new FileStream(file, FileMode.Open), Encoding.UTF8)) { for (string line = @in.ReadLine(); line != null; line = @in.ReadLine()) { try { line = line.ToLowerInvariant(); StringTokenizer st = new StringTokenizer(line); st.MoveNext(); string stem = st.Current; if (storeorig) { string cmd = (usefull) ? trie.GetFully(stem) : trie .GetLastOnPath(stem); StringBuilder stm = new StringBuilder(stem); Diff.Apply(stm, cmd); assertEquals(stem.ToLowerInvariant(), stm.ToString().ToLowerInvariant()); } while (st.MoveNext()) { string token = st.Current; if (token.Equals(stem, StringComparison.Ordinal)) { continue; } string cmd = (usefull) ? trie.GetFully(token) : trie .GetLastOnPath(token); StringBuilder stm = new StringBuilder(token); Diff.Apply(stm, cmd); assertEquals(stem.ToLowerInvariant(), stm.ToString().ToLowerInvariant()); } } catch (InvalidOperationException /*x*/) { // no base token (stem) on a line } } } }