Skip to content

Commit

Permalink
Use string helpers from Target
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky committed Aug 20, 2024
1 parent ee89343 commit 208412b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/tools/StressLogAnalyzer/src/InterestingStringFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public bool IsInteresting(TargetPointer formatStringPointer, out WellKnownString

StringCacheEntry CalculateEntryForString(TargetPointer address)
{
string formatString = target.ReadZeroTerminatedUtf8String(address, 1024);
string formatString = target.ReadUtf8String(address);
bool defaultInteresting = false;
if (_knownStrings.TryGetValue(formatString, out WellKnownString wellKnown))
{
Expand Down Expand Up @@ -144,7 +144,7 @@ int GetCacheIndex(TargetPointer address)

public bool IsWellKnown(TargetPointer formatStringPointer, out WellKnownString wellKnownString)
{
return _knownStrings.TryGetValue(target.ReadZeroTerminatedUtf8String(formatStringPointer, 1024), out wellKnownString);
return _knownStrings.TryGetValue(target.ReadUtf8String(formatStringPointer), out wellKnownString);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task OutputMessageAsync(ThreadStressLogData thread, StressMsgData m

if (writeFormatString)
{
string format = target.ReadZeroTerminatedUtf8String(message.FormatString, 1024);
string format = target.ReadUtf8String(message.FormatString);
await output.WriteAsync($"***|\"{format}\"|*** ").ConfigureAwait(false);
}

Expand Down
28 changes: 3 additions & 25 deletions src/tools/StressLogAnalyzer/src/StressMessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void FormatAsciiString(TargetPointer ptr, PaddingFormat paddingFormat, S
{
try
{
builder.Append(ReadZeroTerminatedString<byte>(ptr, maxLength: 256));
builder.Append(_target.ReadUtf8String(ptr));
}
catch (InvalidOperationException)
{
Expand All @@ -112,7 +112,7 @@ private void FormatUtf16String(TargetPointer ptr, PaddingFormat paddingFormat, S
{
try
{
builder.Append(ReadZeroTerminatedString<char>(ptr, maxLength: 256));
builder.Append(_target.ReadUtf16String(ptr));
}
catch (InvalidOperationException)
{
Expand Down Expand Up @@ -193,32 +193,10 @@ private static void FormatFloatingPoint(TargetPointer valueAsBits, PaddingFormat
}
}

private unsafe string ReadZeroTerminatedString<T>(TargetPointer pointer, int maxLength)
where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
StringBuilder sb = new();
for (T ch = _target.Read<T>(pointer);
ch != T.Zero;
ch = _target.Read<T>(pointer = new TargetPointer((ulong)pointer + (ulong)sizeof(T))))
{
if (sb.Length > maxLength)
{
break;
}

// char implements INumberBase<char> explicitly, so we need to call the helper method to use CreateChecked.
sb.Append(MakeTruncatingHelper<char>(ch));
}
return sb.ToString();

static U MakeTruncatingHelper<U>(T value) where U : INumberBase<U> => U.CreateChecked(value);
}

public string GetFormattedMessage(StressMsgData stressMsg)
{
Debug.Assert(stressMsg.FormatString != TargetPointer.Null);
TargetPointer nextCharPtr = stressMsg.FormatString;
string formatString = ReadZeroTerminatedString<byte>(stressMsg.FormatString, maxLength: 256);
string formatString = _target.ReadUtf8String(stressMsg.FormatString);
int currentArg = 0;
int startIndex = 0;
StringBuilder sb = new();
Expand Down
33 changes: 0 additions & 33 deletions src/tools/StressLogAnalyzer/src/TargetExtensions.cs

This file was deleted.

0 comments on commit 208412b

Please sign in to comment.