Merge pull request #9968 from Phrohdoh/miniyaml-cleanups

MiniYaml.cs cleanups
This commit is contained in:
Oliver Brakmann
2015-11-11 22:19:37 +01:00

View File

@@ -36,6 +36,7 @@ namespace OpenRA
{ {
foreach (var line in kv.Value.ToLines(kv.Key)) foreach (var line in kv.Value.ToLines(kv.Key))
yield return line; yield return line;
if (lowest) if (lowest)
yield return ""; yield return "";
} }
@@ -158,19 +159,22 @@ namespace OpenRA
var commentIndex = line.IndexOf('#'); var commentIndex = line.IndexOf('#');
if (commentIndex != -1) if (commentIndex != -1)
line = line.Substring(0, commentIndex).TrimEnd(' ', '\t'); line = line.Substring(0, commentIndex).TrimEnd(' ', '\t');
if (line.Length == 0) if (line.Length == 0)
continue; continue;
var cp = 0;
var charPosition = 0;
var level = 0; var level = 0;
var spaces = 0; var spaces = 0;
var textStart = false; var textStart = false;
var c = line[cp]; var currChar = line[charPosition];
while (!(c == '\n' || c == '\r') && cp < line.Length && !textStart)
while (!(currChar == '\n' || currChar == '\r') && charPosition < line.Length && !textStart)
{ {
c = line[cp]; currChar = line[charPosition];
switch (c) switch (currChar)
{ {
case ' ': case ' ':
spaces++; spaces++;
if (spaces >= SpacesPerLevel) if (spaces >= SpacesPerLevel)
{ {
@@ -178,11 +182,11 @@ namespace OpenRA
level++; level++;
} }
cp++; charPosition++;
break; break;
case '\t': case '\t':
level++; level++;
cp++; charPosition++;
break; break;
default: default:
textStart = true; textStart = true;
@@ -190,19 +194,21 @@ namespace OpenRA
} }
} }
var t = line.Substring(cp); var realText = line.Substring(charPosition);
if (t.Length == 0) if (realText.Length == 0)
continue; continue;
var location = new MiniYamlNode.SourceLocation { Filename = filename, Line = lineNo }; var location = new MiniYamlNode.SourceLocation { Filename = filename, Line = lineNo };
if (levels.Count <= level) if (levels.Count <= level)
throw new YamlException("Bad indent in miniyaml at {0}".F(location)); throw new YamlException("Bad indent in miniyaml at {0}".F(location));
while (levels.Count > level + 1) while (levels.Count > level + 1)
levels.RemoveAt(levels.Count - 1); levels.RemoveAt(levels.Count - 1);
var d = new List<MiniYamlNode>(); var d = new List<MiniYamlNode>();
var rhs = SplitAtColon(ref t); var rhs = SplitAtColon(ref realText);
levels[level].Add(new MiniYamlNode(t, rhs, d, location)); levels[level].Add(new MiniYamlNode(realText, rhs, d, location));
levels.Add(d); levels.Add(d);
} }
@@ -210,15 +216,17 @@ namespace OpenRA
return levels[0]; return levels[0];
} }
static string SplitAtColon(ref string t) static string SplitAtColon(ref string realText)
{ {
var colon = t.IndexOf(':'); var colon = realText.IndexOf(':');
if (colon == -1) if (colon == -1)
return null; return null;
var ret = t.Substring(colon + 1).Trim();
var ret = realText.Substring(colon + 1).Trim();
if (ret.Length == 0) if (ret.Length == 0)
ret = null; ret = null;
t = t.Substring(0, colon).Trim();
realText = realText.Substring(0, colon).Trim();
return ret; return ret;
} }
@@ -268,6 +276,7 @@ namespace OpenRA
{ {
if (a.Count == 0) if (a.Count == 0)
return b; return b;
if (b.Count == 0) if (b.Count == 0)
return a; return a;
@@ -332,6 +341,7 @@ namespace OpenRA
{ {
if (a == null) if (a == null)
return b; return b;
if (b == null) if (b == null)
return a; return a;
@@ -341,6 +351,7 @@ namespace OpenRA
public IEnumerable<string> ToLines(string name) public IEnumerable<string> ToLines(string name)
{ {
yield return name + ": " + Value; yield return name + ": " + Value;
if (Nodes != null) if (Nodes != null)
foreach (var line in Nodes.ToLines(false)) foreach (var line in Nodes.ToLines(false))
yield return "\t" + line; yield return "\t" + line;