Skip to content

Commit

Permalink
Skip first empty line while building log property with multiple lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster620 committed Mar 16, 2024
1 parent fc1b869 commit dfeca41
Showing 1 changed file with 55 additions and 22 deletions.
77 changes: 55 additions & 22 deletions ULogViewer/Logs/LogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,31 @@ public void Append(string propertyName, ReadOnlyMemory<char> value)
/// </summary>
/// <param name="propertyName">Name of property of log.</param>
/// <param name="value">Value to append.</param>
public void AppendToNextLine(string propertyName, string value)
/// <param name="skipFirstEmptyLine">True to skip first line if it is empty.</param>
public void AppendToNextLine(string propertyName, string value, bool skipFirstEmptyLine = true)
{
if (this.properties.TryGetValue(propertyName, out var current))
{
if (current is ReadOnlyMemory<char> memory)
{
var oldLength = memory.Length;
var newValue = new char[oldLength + value.Length + 1];
memory.CopyTo(newValue);
newValue[oldLength] = '\n';
value.AsMemory().CopyTo(new Memory<char>(newValue).Slice(oldLength + 1));
properties[propertyName] = newValue;
return;
if (!skipFirstEmptyLine || !IsWhiteSpace(memory))
{
var oldLength = memory.Length;
var newValue = new char[oldLength + value.Length + 1];
memory.CopyTo(newValue);
newValue[oldLength] = '\n';
value.AsMemory().CopyTo(new Memory<char>(newValue).Slice(oldLength + 1));
properties[propertyName] = newValue;
return;
}
}
if (current is string str)
else if (current is string str)
{
properties[propertyName] = str + '\n' + value;
return;
if (!skipFirstEmptyLine || !string.IsNullOrWhiteSpace(str))
{
properties[propertyName] = str + '\n' + value;
return;
}
}
}
properties[propertyName] = value;
Expand All @@ -124,24 +131,31 @@ public void AppendToNextLine(string propertyName, string value)
/// </summary>
/// <param name="propertyName">Name of property of log.</param>
/// <param name="value">Value to append.</param>
public void AppendToNextLine(string propertyName, ReadOnlyMemory<char> value)
/// <param name="skipFirstEmptyLine">True to skip first line if it is empty.</param>
public void AppendToNextLine(string propertyName, ReadOnlyMemory<char> value, bool skipFirstEmptyLine = true)
{
if (this.properties.TryGetValue(propertyName, out var current))
{
if (current is ReadOnlyMemory<char> memory)
{
var oldLength = memory.Length;
var newValue = new char[oldLength + value.Length + 1];
memory.CopyTo(newValue);
newValue[oldLength] = '\n';
value.CopyTo(new Memory<char>(newValue).Slice(oldLength + 1));
properties[propertyName] = newValue;
return;
if (!skipFirstEmptyLine || !IsWhiteSpace(memory))
{
var oldLength = memory.Length;
var newValue = new char[oldLength + value.Length + 1];
memory.CopyTo(newValue);
newValue[oldLength] = '\n';
value.CopyTo(new Memory<char>(newValue).Slice(oldLength + 1));
properties[propertyName] = newValue;
return;
}
}
if (current is string str)
else if (current is string str)
{
properties[propertyName] = str + '\n' + new string(value.Span);
return;
if (!skipFirstEmptyLine || !string.IsNullOrWhiteSpace(str))
{
properties[propertyName] = str + '\n' + new string(value.Span);
return;
}
}
}
properties[propertyName] = value;
Expand Down Expand Up @@ -446,6 +460,25 @@ static IStringSource GetStringWithLessMemoryUsageMup(ReadOnlyMemory<char> s)
public bool IsNotEmpty() => this.properties.Count > 0;


// Check whether given character sequence is consist of white spaces or not.
[Obsolete("Should be moved in AppBase.")]
static unsafe bool IsWhiteSpace(ReadOnlyMemory<char> s) =>
s.PinAs((char* cPtr) =>
{
if (cPtr is null)
return true;
var count = s.Length;
while (count > 0)
{
if (!char.IsWhiteSpace(*cPtr))
return false;
++cPtr;
--count;
}
return true;
});


/// <summary>
/// Get or set memory usage policy.
/// </summary>
Expand Down

0 comments on commit dfeca41

Please sign in to comment.