refactoring

This commit is contained in:
2021-10-26 19:05:31 +03:00
parent dcc1ad3e17
commit 5538178cab
18 changed files with 405 additions and 457 deletions

View File

@@ -25,27 +25,18 @@ namespace DTLib.Dtsod
//public Dictionary<string, dynamic> Values { get; set; }
public DtsodV21(string text)
{
Text=text;
foreach(KeyValuePair<string, dynamic> pair in Parse(text))
Text = text;
foreach (KeyValuePair<string, dynamic> pair in Parse(text))
Add(pair.Key, pair.Value);
}
// выдаёт Exception
public new dynamic this[string key]
{
get
{
if(TryGetValue(key, out dynamic value))
return value;
else
throw new Exception($"Dtsod[{key}] key not found");
}
get => TryGetValue(key, out dynamic value) ? value : throw new Exception($"Dtsod[{key}] key not found");
set
{
if(TrySetValue(key, value))
return;
else
throw new Exception($"Dtsod[{key}] key not found");
if (!TrySetValue(key, value)) throw new Exception($"Dtsod[{key}] key not found");
}
}
@@ -54,12 +45,12 @@ namespace DTLib.Dtsod
{
try
{
value=base[key];
value = base[key];
return true;
}
catch(KeyNotFoundException)
catch (KeyNotFoundException)
{
value=null;
value = null;
return false;
}
}
@@ -67,10 +58,10 @@ namespace DTLib.Dtsod
{
try
{
base[key]=value;
base[key] = value;
return true;
}
catch(KeyNotFoundException)
catch (KeyNotFoundException)
{
return false;
}
@@ -90,7 +81,7 @@ namespace DTLib.Dtsod
{
Dictionary<string, dynamic> parsed = new();
int i = 0;
for(; i<text.Length; i++)
for (; i < text.Length; i++)
ReadName();
DebugNoTime("g", $"Parse returns {parsed.Keys.Count} keys\n");
return parsed;
@@ -109,9 +100,9 @@ namespace DTLib.Dtsod
StringBuilder defaultNameBuilder = new();
DebugNoTime("m", "ReadName\n");
for(; i<text.Length; i++)
for (; i < text.Length; i++)
{
switch(text[i])
switch (text[i])
{
case ' ':
case '\t':
@@ -121,11 +112,11 @@ namespace DTLib.Dtsod
case ':':
i++;
string name = defaultNameBuilder.ToString();
value=ReadValue();
value = ReadValue();
DebugNoTime("c", $"parsed.Add({name}, {value} { value.GetType() })\n");
if(isListElem)
if (isListElem)
{
if(!parsed.ContainsKey(name))
if (!parsed.ContainsKey(name))
parsed.Add(name, new List<dynamic>());
parsed[name].Add(value);
}
@@ -137,16 +128,16 @@ namespace DTLib.Dtsod
//ReadCommentLine();
break;
case '}':
throw new Exception("Parse.ReadName() error: unexpected '}' at "+i+" char");
throw new Exception("Parse.ReadName() error: unexpected '}' at " + i + " char");
// если $ перед названием параметра поставить, значение value добавится в лист с названием name
case '$':
DebugNoTime("w", text[i].ToString());
if(defaultNameBuilder.ToString().Length!=0)
throw new Exception("Parse.ReadName() error: unexpected '$' at "+i+" char");
isListElem=true;
if (defaultNameBuilder.ToString().Length != 0)
throw new Exception("Parse.ReadName() error: unexpected '$' at " + i + " char");
isListElem = true;
break;
case ';':
throw new Exception("Parse.ReadName() error: unexpected ';' at "+i+" char");
throw new Exception("Parse.ReadName() error: unexpected ';' at " + i + " char");
default:
DebugNoTime("w", text[i].ToString());
defaultNameBuilder.Append(text[i]);
@@ -165,14 +156,14 @@ namespace DTLib.Dtsod
i++;
StringBuilder valueBuilder = new();
valueBuilder.Append('"');
for(; text[i]!='"'||text[i-1]=='\\'; i++)
for (; text[i] != '"' || text[i - 1] == '\\'; i++)
{
DebugNoTime("gray", text[i].ToString());
valueBuilder.Append(text[i]);
}
valueBuilder.Append('"');
DebugNoTime("gray", text[i].ToString());
type=ValueType.String;
type = ValueType.String;
return valueBuilder.ToString();
}
@@ -181,10 +172,10 @@ namespace DTLib.Dtsod
i++;
List<dynamic> output = new();
StringBuilder valueBuilder = new();
for(; text[i]!=']'; i++)
for (; text[i] != ']'; i++)
{
DebugNoTime("c", text[i].ToString());
switch(text[i])
switch (text[i])
{
case ' ':
case '\t':
@@ -201,13 +192,13 @@ namespace DTLib.Dtsod
break;
}
}
if(valueBuilder.Length>0)
if (valueBuilder.Length > 0)
{
ParseValueToRightType(valueBuilder.ToString());
output.Add(value);
}
DebugNoTime("c", text[i].ToString());
type=ValueType.List;
type = ValueType.List;
return output;
}
@@ -216,10 +207,10 @@ namespace DTLib.Dtsod
StringBuilder valueBuilder = new();
int balance = 1;
i++;
for(; balance!=0; i++)
for (; balance != 0; i++)
{
DebugNoTime("y", text[i].ToString());
switch(text[i])
switch (text[i])
{
case '"':
valueBuilder.Append(ReadString());
@@ -227,7 +218,7 @@ namespace DTLib.Dtsod
case '}':
balance--;
DebugNoTime("b", $"\nbalance -- = {balance}\n");
if(balance!=0)
if (balance != 0)
valueBuilder.Append(text[i]);
break;
case '{':
@@ -241,7 +232,7 @@ namespace DTLib.Dtsod
}
}
i--; // i++ в for выполняется даже когда balance == 0, то есть text[i] получается == ;, что ломает всё
type=ValueType.Complex;
type = ValueType.Complex;
return Parse(valueBuilder.ToString());
}
@@ -249,54 +240,54 @@ namespace DTLib.Dtsod
{
DebugNoTime("b", $"\nParseValueToRightType({stringValue})\n");
switch(stringValue)
switch (stringValue)
{
// bool
case "true":
case "false":
value=stringValue.ToBool();
value = stringValue.ToBool();
break;
// null
case "null":
value=null;
value = null;
break;
default:
if(stringValue.Contains('"'))
value=stringValue.Remove(stringValue.Length-1).Remove(0, 1);
if (stringValue.Contains('"'))
value = stringValue.Remove(stringValue.Length - 1).Remove(0, 1);
// double
else if(stringValue.Contains('.'))
value=stringValue.ToDouble();
else if (stringValue.Contains('.'))
value = stringValue.ToDouble();
// ushort; ulong; uint
else if(stringValue.Length>2&&stringValue[stringValue.Length-2]=='u')
else if (stringValue.Length > 2 && stringValue[stringValue.Length - 2] == 'u')
{
switch(stringValue[stringValue.Length-1])
switch (stringValue[stringValue.Length - 1])
{
case 's':
value=stringValue.Remove(stringValue.Length-2).ToUShort();
value = stringValue.Remove(stringValue.Length - 2).ToUShort();
break;
case 'i':
value=stringValue.Remove(stringValue.Length-2).ToUInt();
value = stringValue.Remove(stringValue.Length - 2).ToUInt();
break;
case 'l':
value=stringValue.Remove(stringValue.Length-2).ToULong();
value = stringValue.Remove(stringValue.Length - 2).ToULong();
break;
default:
throw new Exception($"Dtsod.Parse.ReadValue() error: value= wrong type <u{stringValue[stringValue.Length-1]}>");
throw new Exception($"Dtsod.Parse.ReadValue() error: value= wrong type <u{stringValue[stringValue.Length - 1]}>");
};
}
// short; long; int
else
switch(stringValue[stringValue.Length-1])
switch (stringValue[stringValue.Length - 1])
{
case 's':
value=stringValue.Remove(stringValue.Length-1).ToShort();
value = stringValue.Remove(stringValue.Length - 1).ToShort();
break;
case 'l':
value=stringValue.Remove(stringValue.Length-1).ToLong();
value = stringValue.Remove(stringValue.Length - 1).ToLong();
break;
default:
value=stringValue.ToShort();
value = stringValue.ToShort();
break;
}
break;
@@ -305,10 +296,10 @@ namespace DTLib.Dtsod
StringBuilder defaultValueBuilder = new();
DebugNoTime("m", "\nReadValue\n");
for(; i<text.Length; i++)
for (; i < text.Length; i++)
{
DebugNoTime("b", text[i].ToString());
switch(text[i])
switch (text[i])
{
case ' ':
case '\t':
@@ -316,10 +307,10 @@ namespace DTLib.Dtsod
case '\n':
break;
case '"':
value=ReadString();
value = ReadString();
break;
case ';':
switch(type)
switch (type)
{
case ValueType.String:
ParseValueToRightType(value);
@@ -330,10 +321,10 @@ namespace DTLib.Dtsod
};
return value;
case '[':
value=ReadList();
value = ReadList();
break;
case '{':
value=ReadComplex();
value = ReadComplex();
break;
// строка, начинающаяся с # будет считаться комментом
case '#':
@@ -350,12 +341,12 @@ namespace DTLib.Dtsod
void Debug(params string[] msg)
{
if(debug)
if (debug)
Log(msg);
}
void DebugNoTime(params string[] msg)
{
if(debug)
if (debug)
LogNoTime(msg);
}
}

View File

@@ -31,15 +31,15 @@ namespace DTLib.Dtsod
public bool IsList;
public ValueStruct(ValueTypes type, dynamic value, bool isList)
{
Value=value;
Type=type;
IsList=isList;
Value = value;
Type = type;
IsList = isList;
}
public ValueStruct(ValueTypes type, dynamic value)
{
Value=value;
Type=type;
IsList=false;
Value = value;
Type = type;
IsList = false;
}
}
@@ -64,32 +64,23 @@ namespace DTLib.Dtsod
public DtsodV22(string text)
{
foreach(KeyValuePair<string, ValueStruct> pair in Parse(text))
foreach (KeyValuePair<string, ValueStruct> pair in Parse(text))
Add(pair.Key, pair.Value);
}
public DtsodV22(Dictionary<string, DtsodV22.ValueStruct> dict)
{
foreach(KeyValuePair<string, ValueStruct> pair in dict)
foreach (KeyValuePair<string, ValueStruct> pair in dict)
Add(pair.Key, pair.Value);
}
// выдаёт Exception
public new dynamic this[string key]
{
get
{
if(TryGetValue(key, out dynamic value))
return value;
else
throw new Exception($"Dtsod[{key}] key not found");
}
get => TryGetValue(key, out dynamic value) ? value : throw new Exception($"Dtsod[{key}] key not found");
set
{
if(TrySetValue(key, value))
return;
else
throw new Exception($"Dtsod[{key}] key not found");
if (!TrySetValue(key, value)) throw new Exception($"Dtsod[{key}] key not found");
}
}
@@ -98,12 +89,12 @@ namespace DTLib.Dtsod
{
try
{
value=base[key].Value;
value = base[key].Value;
return true;
}
catch(KeyNotFoundException)
catch (KeyNotFoundException)
{
value=null;
value = null;
return false;
}
}
@@ -111,25 +102,19 @@ namespace DTLib.Dtsod
{
try
{
bool isList;
if(value is IList)
isList=true;
else
isList=false;
base[key]=new(base[key].Type, value, isList);
bool isList = value is IList;
base[key] = new(base[key].Type, value, isList);
return true;
}
catch(KeyNotFoundException)
{
return false;
}
catch (KeyNotFoundException)
{ return false; }
}
DtsodV22 Parse(string text)
{
Dictionary<string, ValueStruct> parsed = new();
int i = 0;
for(; i<text.Length; i++)
for (; i < text.Length; i++)
ReadName();
DebugNoTime("g", $"Parse returns {parsed.Keys.Count} keys\n");
return new DtsodV22(parsed);
@@ -148,9 +133,9 @@ namespace DTLib.Dtsod
StringBuilder defaultNameBuilder = new();
DebugNoTime("m", "ReadName\n");
for(; i<text.Length; i++)
for (; i < text.Length; i++)
{
switch(text[i])
switch (text[i])
{
case ' ':
case '\t':
@@ -160,11 +145,11 @@ namespace DTLib.Dtsod
case ':':
i++;
string name = defaultNameBuilder.ToString();
value=ReadValue(out ValueTypes type, out bool isList);
value = ReadValue(out ValueTypes type, out bool isList);
DebugNoTime("c", $"parsed.Add({name},{type} {value} )\n");
if(isListElem)
if (isListElem)
{
if(!parsed.ContainsKey(name))
if (!parsed.ContainsKey(name))
parsed.Add(name, new(type, new List<dynamic>(), isList));
parsed[name].Value.Add(value);
}
@@ -176,16 +161,16 @@ namespace DTLib.Dtsod
//ReadCommentLine();
break;
case '}':
throw new Exception("Parse.ReadName() error: unexpected '}' at "+i+" char");
throw new Exception("Parse.ReadName() error: unexpected '}' at " + i + " char");
// если $ перед названием параметра поставить, значение value добавится в лист с названием name
case '$':
DebugNoTime("w", text[i].ToString());
if(defaultNameBuilder.ToString().Length!=0)
throw new Exception("Parse.ReadName() error: unexpected '$' at "+i+" char");
isListElem=true;
if (defaultNameBuilder.ToString().Length != 0)
throw new Exception("Parse.ReadName() error: unexpected '$' at " + i + " char");
isListElem = true;
break;
case ';':
throw new Exception("Parse.ReadName() error: unexpected ';' at "+i+" char");
throw new Exception("Parse.ReadName() error: unexpected ';' at " + i + " char");
default:
DebugNoTime("w", text[i].ToString());
defaultNameBuilder.Append(text[i]);
@@ -197,7 +182,7 @@ namespace DTLib.Dtsod
dynamic ReadValue(out ValueTypes outType, out bool isList)
{
ValueTypes type = ValueTypes.Unknown;
isList=false;
isList = false;
dynamic value = null;
string ReadString()
@@ -205,14 +190,14 @@ namespace DTLib.Dtsod
i++;
StringBuilder valueBuilder = new();
valueBuilder.Append('"');
for(; text[i]!='"'||text[i-1]=='\\'; i++)
for (; text[i] != '"' || text[i - 1] == '\\'; i++)
{
DebugNoTime("gray", text[i].ToString());
valueBuilder.Append(text[i]);
}
valueBuilder.Append('"');
DebugNoTime("gray", text[i].ToString());
type=ValueTypes.String;
type = ValueTypes.String;
return valueBuilder.ToString();
}
@@ -221,10 +206,10 @@ namespace DTLib.Dtsod
i++;
List<dynamic> output = new();
StringBuilder valueBuilder = new();
for(; text[i]!=']'; i++)
for (; text[i] != ']'; i++)
{
DebugNoTime("c", text[i].ToString());
switch(text[i])
switch (text[i])
{
case ' ':
case '\t':
@@ -241,13 +226,13 @@ namespace DTLib.Dtsod
break;
}
}
if(valueBuilder.Length>0)
if (valueBuilder.Length > 0)
{
ParseValueToRightType(valueBuilder.ToString());
output.Add(value);
}
DebugNoTime("c", text[i].ToString());
type=ValueTypes.List;
type = ValueTypes.List;
return output;
}
@@ -256,10 +241,10 @@ namespace DTLib.Dtsod
StringBuilder valueBuilder = new();
int balance = 1;
i++;
for(; balance!=0; i++)
for (; balance != 0; i++)
{
DebugNoTime("y", text[i].ToString());
switch(text[i])
switch (text[i])
{
case '"':
valueBuilder.Append(ReadString());
@@ -267,7 +252,7 @@ namespace DTLib.Dtsod
case '}':
balance--;
DebugNoTime("b", $"\nbalance -- = {balance}\n");
if(balance!=0)
if (balance != 0)
valueBuilder.Append(text[i]);
break;
case '{':
@@ -281,55 +266,55 @@ namespace DTLib.Dtsod
}
}
i--; // i++ в for выполняется даже когда balance == 0, то есть text[i] получается == ;, что ломает всё
type=ValueTypes.Complex;
type = ValueTypes.Complex;
return Parse(valueBuilder.ToString());
}
void ParseValueToRightType(string stringValue)
{
DebugNoTime("b", $"\nParseValueToRightType({stringValue})\n");
switch(stringValue)
switch (stringValue)
{
// bool
case "true":
case "false":
type=ValueTypes.Bool;
value=stringValue.ToBool();
type = ValueTypes.Bool;
value = stringValue.ToBool();
break;
// null
case "null":
type=ValueTypes.Null;
value=null;
type = ValueTypes.Null;
value = null;
break;
default:
if(stringValue.Contains('"'))
if (stringValue.Contains('"'))
{
type=ValueTypes.String;
value=stringValue.Remove(stringValue.Length-1).Remove(0, 1);
type = ValueTypes.String;
value = stringValue.Remove(stringValue.Length - 1).Remove(0, 1);
}
// double
else if(stringValue.Contains('.'))
else if (stringValue.Contains('.'))
{
type=ValueTypes.Double;
value=stringValue.ToDouble();
type = ValueTypes.Double;
value = stringValue.ToDouble();
}
// ushort; ulong; uint
else if(stringValue.Length>2&&stringValue[stringValue.Length-2]=='u')
else if (stringValue.Length > 2 && stringValue[stringValue.Length - 2] == 'u')
{
switch(stringValue[stringValue.Length-1])
switch (stringValue[stringValue.Length - 1])
{
case 's':
type=ValueTypes.UShort;
value=stringValue.Remove(stringValue.Length-2).ToUShort();
type = ValueTypes.UShort;
value = stringValue.Remove(stringValue.Length - 2).ToUShort();
break;
case 'i':
type=ValueTypes.UInt;
value=stringValue.Remove(stringValue.Length-2).ToUInt();
type = ValueTypes.UInt;
value = stringValue.Remove(stringValue.Length - 2).ToUInt();
break;
case 'l':
type=ValueTypes.ULong;
value=stringValue.Remove(stringValue.Length-2).ToULong();
type = ValueTypes.ULong;
value = stringValue.Remove(stringValue.Length - 2).ToULong();
break;
default:
throw new Exception($"Dtsod.Parse.ReadValue() error: value <{stringValue}> has wrong type");
@@ -337,19 +322,19 @@ namespace DTLib.Dtsod
}
// short; long; int
else
switch(stringValue[stringValue.Length-1])
switch (stringValue[stringValue.Length - 1])
{
case 's':
type=ValueTypes.Short;
value=stringValue.Remove(stringValue.Length-1).ToShort();
type = ValueTypes.Short;
value = stringValue.Remove(stringValue.Length - 1).ToShort();
break;
case 'l':
type=ValueTypes.Long;
value=stringValue.Remove(stringValue.Length-1).ToLong();
type = ValueTypes.Long;
value = stringValue.Remove(stringValue.Length - 1).ToLong();
break;
default:
type=ValueTypes.Int;
value=stringValue.ToShort();
type = ValueTypes.Int;
value = stringValue.ToShort();
break;
}
break;
@@ -358,10 +343,10 @@ namespace DTLib.Dtsod
StringBuilder defaultValueBuilder = new();
DebugNoTime("m", "\nReadValue\n");
for(; i<text.Length; i++)
for (; i < text.Length; i++)
{
DebugNoTime("b", text[i].ToString());
switch(text[i])
switch (text[i])
{
case ' ':
case '\t':
@@ -369,16 +354,16 @@ namespace DTLib.Dtsod
case '\n':
break;
case '"':
value=ReadString();
value = ReadString();
break;
case '[':
value=ReadList();
value = ReadList();
break;
case '{':
value=ReadComplex();
value = ReadComplex();
break;
case ';':
switch(type)
switch (type)
{
case ValueTypes.String:
ParseValueToRightType(value);
@@ -387,10 +372,10 @@ namespace DTLib.Dtsod
ParseValueToRightType(defaultValueBuilder.ToString());
break;
case ValueTypes.List:
isList=true;
isList = true;
break;
};
outType=type;
outType = type;
return value;
// строка, начинающаяся с # будет считаться комментом
case '#':
@@ -411,13 +396,13 @@ namespace DTLib.Dtsod
string Deconstruct(DtsodV22 dtsod)
{
StringBuilder outBuilder = new();
foreach(string key in dtsod.Keys)
foreach (string key in dtsod.Keys)
{
outBuilder.Append('\t', tabCount);
outBuilder.Append(key);
outBuilder.Append(": ");
dtsod.TryGetValue(key, out ValueStruct value);
switch(value.Type)
switch (value.Type)
{
case ValueTypes.List:
outBuilder.Append("\"list deconstruction is'nt implemented yet\"");
@@ -480,13 +465,13 @@ namespace DTLib.Dtsod
void DebugNoTime(params string[] msg)
{
if(debug)
if (debug)
PublicLog.LogNoTime(msg);
}
public DtsodV22 Extend(DtsodV22 newPart)
{
foreach(KeyValuePair<string, ValueStruct> pair in newPart)
foreach (KeyValuePair<string, ValueStruct> pair in newPart)
Add(pair.Key, pair.Value);
return this;
}

View File

@@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTLib.Dtsod
{
public class DtsodSerializableAttribute : Attribute
{
public DtsodVersion Version;
public DtsodSerializableAttribute(DtsodVersion ver) => Version=ver;
public DtsodSerializableAttribute(DtsodVersion ver) => Version = ver;
}
}

View File

@@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTLib.Dtsod
namespace DTLib.Dtsod
{
static public class DtsodV30
public static class DtsodV30
{
/*
public static DtsodV30 FromObject(object target)