Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify NtProcessInfoHelper.GetProcessShortName #71136

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -412,59 +412,18 @@ private static unsafe ProcessInfo[] GetProcessInfos(ReadOnlySpan<byte> data, int
// Check base\screg\winreg\perfdlls\process\perfsprc.c for details.
internal static ReadOnlySpan<char> GetProcessShortName(ReadOnlySpan<char> name)
{
if (name.IsEmpty)
{
return string.Empty;
}

int slash = -1;
int period = -1;

for (int i = 0; i < name.Length; i++)
{
if (name[i] == '\\')
{
slash = i;
}
else if (name[i] == '.')
{
period = i;
}
}

if (period == -1)
{
period = name.Length - 1; // set to end of string
}
else
{
// if a period was found, then see if the extension is
// .EXE, if so drop it, if not, then use end of string
// (i.e. include extension in name)
ReadOnlySpan<char> extension = name.Slice(period);
// Trim off everything up to and including the last slash, if there is one.
// If there isn't, LastIndexOf will return -1 and this will end up as a nop.
name = name.Slice(name.LastIndexOf('\\') + 1);

if (extension.Equals(".exe", StringComparison.OrdinalIgnoreCase))
{
period--; // point to character before period
}
else
{
period = name.Length - 1; // set to end of string
}
}

if (slash == -1)
{
slash = 0; // set to start of string
}
else
// If the name ends with the ".exe" extension, then drop it, otherwise include
// it in the name.
if (name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
slash++; // point to character next to slash
name = name.Slice(0, name.Length - 4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = name.Slice(0, name.Length - 4);
name = name[..^4];

}

// Slice to the characters between a slash (or start of the string)
// and a period (or end of string).
return name.Slice(slash, period - slash + 1);
return name;
}
}
}