REFACTORING
This commit is contained in:
@@ -24,8 +24,8 @@ 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);
|
||||
}
|
||||
|
||||
@@ -34,13 +34,17 @@ namespace DTLib.Dtsod
|
||||
{
|
||||
get
|
||||
{
|
||||
if (TryGetValue(key, out dynamic value)) return value;
|
||||
else throw new Exception($"Dtsod[{key}] key not found");
|
||||
if(TryGetValue(key, out dynamic value))
|
||||
return value;
|
||||
else
|
||||
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))
|
||||
return;
|
||||
else
|
||||
throw new Exception($"Dtsod[{key}] key not found");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,12 +53,12 @@ namespace DTLib.Dtsod
|
||||
{
|
||||
try
|
||||
{
|
||||
value = base[key];
|
||||
value=base[key];
|
||||
return true;
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
catch(KeyNotFoundException)
|
||||
{
|
||||
value = null;
|
||||
value=null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -62,10 +66,10 @@ namespace DTLib.Dtsod
|
||||
{
|
||||
try
|
||||
{
|
||||
base[key] = value;
|
||||
base[key]=value;
|
||||
return true;
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
catch(KeyNotFoundException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -94,7 +98,8 @@ namespace DTLib.Dtsod
|
||||
{
|
||||
Dictionary<string, dynamic> parsed = new();
|
||||
int i = 0;
|
||||
for (; i < text.Length; i++) ReadName();
|
||||
for(; i<text.Length; i++)
|
||||
ReadName();
|
||||
DebugNoTime("g", $"Parse returns {parsed.Keys.Count} keys\n");
|
||||
return parsed;
|
||||
|
||||
@@ -112,9 +117,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':
|
||||
@@ -124,29 +129,32 @@ 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)) parsed.Add(name, new List<dynamic>());
|
||||
if(!parsed.ContainsKey(name))
|
||||
parsed.Add(name, new List<dynamic>());
|
||||
parsed[name].Add(value);
|
||||
}
|
||||
else parsed.Add(name, value);
|
||||
else
|
||||
parsed.Add(name, value);
|
||||
return;
|
||||
// строка, начинающаяся с # будет считаться комментом
|
||||
case '#':
|
||||
//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 +173,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 +189,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 +209,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 +224,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 +235,8 @@ namespace DTLib.Dtsod
|
||||
case '}':
|
||||
balance--;
|
||||
DebugNoTime("b", $"\nbalance -- = {balance}\n");
|
||||
if (balance != 0) valueBuilder.Append(text[i]);
|
||||
if(balance!=0)
|
||||
valueBuilder.Append(text[i]);
|
||||
break;
|
||||
case '{':
|
||||
balance++;
|
||||
@@ -240,7 +249,7 @@ namespace DTLib.Dtsod
|
||||
}
|
||||
}
|
||||
i--; // i++ в for выполняется даже когда balance == 0, то есть text[i] получается == ;, что ломает всё
|
||||
type = ValueType.Complex;
|
||||
type=ValueType.Complex;
|
||||
return Parse(valueBuilder.ToString());
|
||||
}
|
||||
|
||||
@@ -248,51 +257,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])
|
||||
else
|
||||
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;
|
||||
@@ -301,10 +313,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':
|
||||
@@ -312,10 +324,10 @@ namespace DTLib.Dtsod
|
||||
case '\n':
|
||||
break;
|
||||
case '"':
|
||||
value = ReadString();
|
||||
value=ReadString();
|
||||
break;
|
||||
case ';':
|
||||
switch (type)
|
||||
switch(type)
|
||||
{
|
||||
case ValueType.String:
|
||||
ParseValueToRightType(value);
|
||||
@@ -326,10 +338,10 @@ namespace DTLib.Dtsod
|
||||
};
|
||||
return value;
|
||||
case '[':
|
||||
value = ReadList();
|
||||
value=ReadList();
|
||||
break;
|
||||
case '{':
|
||||
value = ReadComplex();
|
||||
value=ReadComplex();
|
||||
break;
|
||||
// строка, начинающаяся с # будет считаться комментом
|
||||
case '#':
|
||||
@@ -347,11 +359,13 @@ namespace DTLib.Dtsod
|
||||
|
||||
void Debug(params string[] msg)
|
||||
{
|
||||
if (debug) Log(msg);
|
||||
if(debug)
|
||||
Log(msg);
|
||||
}
|
||||
void DebugNoTime(params string[] msg)
|
||||
{
|
||||
if (debug) LogNoTime(msg);
|
||||
if(debug)
|
||||
LogNoTime(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,15 +34,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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,14 +50,14 @@ namespace DTLib.Dtsod
|
||||
|
||||
public DtsodV23(string text)
|
||||
{
|
||||
foreach (KeyValuePair<string, ValueStruct> pair in Parse(text))
|
||||
foreach(KeyValuePair<string, ValueStruct> pair in Parse(text))
|
||||
Add(pair.Key, pair.Value);
|
||||
}
|
||||
|
||||
|
||||
public DtsodV23(Dictionary<string, DtsodV23.ValueStruct> dict)
|
||||
{
|
||||
foreach (KeyValuePair<string, ValueStruct> pair in dict)
|
||||
foreach(KeyValuePair<string, ValueStruct> pair in dict)
|
||||
Add(pair.Key, pair.Value);
|
||||
}
|
||||
|
||||
@@ -66,13 +66,17 @@ namespace DTLib.Dtsod
|
||||
{
|
||||
get
|
||||
{
|
||||
if (TryGetValue(key, out dynamic value)) return value;
|
||||
else throw new Exception($"Dtsod[{key}] key not found");
|
||||
if(TryGetValue(key, out dynamic value))
|
||||
return value;
|
||||
else
|
||||
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))
|
||||
return;
|
||||
else
|
||||
throw new Exception($"Dtsod[{key}] key not found");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,12 +85,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;
|
||||
}
|
||||
}
|
||||
@@ -95,12 +99,14 @@ namespace DTLib.Dtsod
|
||||
try
|
||||
{
|
||||
bool isList;
|
||||
if (value is IList) isList = true;
|
||||
else isList = false;
|
||||
base[key] = new(base[key].Type, value, isList);
|
||||
if(value is IList)
|
||||
isList=true;
|
||||
else
|
||||
isList=false;
|
||||
base[key]=new(base[key].Type, value, isList);
|
||||
return true;
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
catch(KeyNotFoundException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -110,7 +116,8 @@ namespace DTLib.Dtsod
|
||||
{
|
||||
Dictionary<string, ValueStruct> parsed = new();
|
||||
int i = 0;
|
||||
for (; i < text.Length; i++) ReadName();
|
||||
for(; i<text.Length; i++)
|
||||
ReadName();
|
||||
DebugNoTime("g", $"Parse returns {parsed.Keys.Count} keys\n");
|
||||
return new DtsodV23(parsed);
|
||||
|
||||
@@ -128,9 +135,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':
|
||||
@@ -140,29 +147,32 @@ namespace DTLib.Dtsod
|
||||
case ':':
|
||||
i++;
|
||||
string name = defaultNameBuilder.ToString();
|
||||
value = ReadValue(out var type, out var 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)) parsed.Add(name, new(type, new List<dynamic>(), isList));
|
||||
if(!parsed.ContainsKey(name))
|
||||
parsed.Add(name, new(type, new List<dynamic>(), isList));
|
||||
parsed[name].Value.Add(value);
|
||||
}
|
||||
else parsed.Add(name, new(type, value, isList));
|
||||
else
|
||||
parsed.Add(name, new(type, value, isList));
|
||||
return;
|
||||
// строка, начинающаяся с # будет считаться комментом
|
||||
case '#':
|
||||
//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]);
|
||||
@@ -174,7 +184,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()
|
||||
@@ -182,14 +192,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();
|
||||
}
|
||||
|
||||
@@ -198,10 +208,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':
|
||||
@@ -218,13 +228,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;
|
||||
}
|
||||
|
||||
@@ -233,10 +243,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());
|
||||
@@ -244,7 +254,8 @@ namespace DTLib.Dtsod
|
||||
case '}':
|
||||
balance--;
|
||||
DebugNoTime("b", $"\nbalance -- = {balance}\n");
|
||||
if (balance != 0) valueBuilder.Append(text[i]);
|
||||
if(balance!=0)
|
||||
valueBuilder.Append(text[i]);
|
||||
break;
|
||||
case '{':
|
||||
balance++;
|
||||
@@ -257,74 +268,75 @@ 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");
|
||||
};
|
||||
}
|
||||
// short; long; int
|
||||
else switch (stringValue[stringValue.Length - 1])
|
||||
else
|
||||
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;
|
||||
@@ -333,10 +345,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':
|
||||
@@ -344,16 +356,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);
|
||||
@@ -362,10 +374,10 @@ namespace DTLib.Dtsod
|
||||
ParseValueToRightType(defaultValueBuilder.ToString());
|
||||
break;
|
||||
case ValueTypes.List:
|
||||
isList = true;
|
||||
isList=true;
|
||||
break;
|
||||
};
|
||||
outType = type;
|
||||
outType=type;
|
||||
return value;
|
||||
// строка, начинающаяся с # будет считаться комментом
|
||||
case '#':
|
||||
@@ -386,13 +398,13 @@ namespace DTLib.Dtsod
|
||||
string Deconstruct(DtsodV23 dtsod)
|
||||
{
|
||||
StringBuilder outBuilder = new();
|
||||
foreach (var 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\"");
|
||||
@@ -455,12 +467,13 @@ namespace DTLib.Dtsod
|
||||
|
||||
void DebugNoTime(params string[] msg)
|
||||
{
|
||||
if (debug) PublicLog.LogNoTime(msg);
|
||||
if(debug)
|
||||
PublicLog.LogNoTime(msg);
|
||||
}
|
||||
|
||||
public void Expand(DtsodV23 newPart)
|
||||
{
|
||||
foreach (KeyValuePair<string, ValueStruct> pair in newPart)
|
||||
foreach(KeyValuePair<string, ValueStruct> pair in newPart)
|
||||
Add(pair.Key, pair.Value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user