1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-23 21:15:49 +02:00

Get rid of string.Split in FileSerializer

string.Split is not suitable for potentially very large strings, so this
decently improves memory usage
This commit is contained in:
chylex 2018-01-30 15:45:19 +01:00
parent 62449450f3
commit d06e29db15

View File

@ -103,8 +103,27 @@ public void Read(string file, T obj){
case 1:
throw new FormatException("Input appears to be a binary file.");
}
string contents = UnescapeStream(reader);
int currentPos = 0;
do{
string line;
int nextPos = contents.IndexOf(NewLineReal, currentPos);
foreach(string line in UnescapeStream(reader).Split(new string[]{ NewLineReal }, StringSplitOptions.RemoveEmptyEntries)){
if (nextPos == -1){
line = contents.Substring(currentPos);
currentPos = -1;
if (string.IsNullOrEmpty(line)){
break;
}
}
else{
line = contents.Substring(currentPos, nextPos-currentPos);
currentPos = nextPos+NewLineReal.Length;
}
int space = line.IndexOf(' ');
if (space == -1){
@ -115,7 +134,7 @@ public void Read(string file, T obj){
string value = UnescapeLine(line.Substring(space+1));
if (props.TryGetValue(property, out PropertyInfo info)){
if (!converters.TryGetValue(info.PropertyType, out ITypeConverter serializer)) {
if (!converters.TryGetValue(info.PropertyType, out ITypeConverter serializer)){
serializer = BasicSerializerObj;
}
@ -129,7 +148,7 @@ public void Read(string file, T obj){
else{
unknownProperties[property] = value;
}
}
}while(currentPos != -1);
}
if (unknownProperties.Count > 0){