Skip to content

Commit 7073525

Browse files
authored
Avoid using Regex when not necessary (PowerShell#18210)
1 parent 9e05d67 commit 7073525

File tree

3 files changed

+28
-38
lines changed

3 files changed

+28
-38
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,16 @@ class ConsoleHostRawUserInterface : System.Management.Automation.Host.PSHostRawU
4141
// (we may load resources which can take some time)
4242
Task.Run(() =>
4343
{
44-
WindowsIdentity identity = WindowsIdentity.GetCurrent();
45-
WindowsPrincipal principal = new WindowsPrincipal(identity);
44+
var identity = WindowsIdentity.GetCurrent();
45+
var principal = new WindowsPrincipal(identity);
4646
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
4747
{
48-
string prefix = ConsoleHostRawUserInterfaceStrings.WindowTitleElevatedPrefix;
49-
50-
// check using Regex if the window already has Administrator: prefix
51-
// (i.e. from the parent console process)
52-
string titlePattern = ConsoleHostRawUserInterfaceStrings.WindowTitleTemplate;
53-
titlePattern = Regex.Escape(titlePattern)
54-
.Replace(@"\{1}", ".*")
55-
.Replace(@"\{0}", Regex.Escape(prefix));
56-
if (!Regex.IsMatch(this.WindowTitle, titlePattern))
48+
// Check if the window already has the "Administrator: " prefix (i.e. from the parent console process).
49+
ReadOnlySpan<char> prefix = ConsoleHostRawUserInterfaceStrings.WindowTitleElevatedPrefix;
50+
ReadOnlySpan<char> windowTitle = WindowTitle;
51+
if (!windowTitle.StartsWith(prefix))
5752
{
58-
this.WindowTitle = StringUtil.Format(ConsoleHostRawUserInterfaceStrings.WindowTitleTemplate,
59-
prefix,
60-
this.WindowTitle);
53+
WindowTitle = string.Concat(prefix, windowTitle);
6154
}
6255
}
6356
});

src/Microsoft.PowerShell.ConsoleHost/resources/ConsoleHostRawUserInterfaceStrings.resx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@
172172
<value>Window title cannot be longer than {0} characters.</value>
173173
</data>
174174
<data name="WindowTitleElevatedPrefix" xml:space="preserve">
175-
<value>Administrator</value>
176-
</data>
177-
<data name="WindowTitleTemplate" xml:space="preserve">
178-
<value>{0}: {1}</value>
175+
<value>Administrator: </value>
179176
</data>
180177
</root>

src/System.Management.Automation/engine/PSVersionInfo.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ public static class PSVersionInfo
5353
/// For each later release of PowerShell, this constant needs to
5454
/// be updated to reflect the right version.
5555
/// </remarks>
56-
private static readonly Version s_psV1Version = new Version(1, 0);
57-
private static readonly Version s_psV2Version = new Version(2, 0);
58-
private static readonly Version s_psV3Version = new Version(3, 0);
59-
private static readonly Version s_psV4Version = new Version(4, 0);
60-
private static readonly Version s_psV5Version = new Version(5, 0);
61-
private static readonly Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE);
62-
private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, preReleaseLabel: null, buildLabel: null);
63-
private static readonly SemanticVersion s_psV61Version = new SemanticVersion(6, 1, 0, preReleaseLabel: null, buildLabel: null);
64-
private static readonly SemanticVersion s_psV62Version = new SemanticVersion(6, 2, 0, preReleaseLabel: null, buildLabel: null);
65-
private static readonly SemanticVersion s_psV7Version = new SemanticVersion(7, 0, 0, preReleaseLabel: null, buildLabel: null);
66-
private static readonly SemanticVersion s_psV71Version = new SemanticVersion(7, 1, 0, preReleaseLabel: null, buildLabel: null);
67-
private static readonly SemanticVersion s_psV72Version = new SemanticVersion(7, 2, 0, preReleaseLabel: null, buildLabel: null);
68-
private static readonly SemanticVersion s_psSemVersion;
56+
private static readonly Version s_psV1Version = new(1, 0);
57+
private static readonly Version s_psV2Version = new(2, 0);
58+
private static readonly Version s_psV3Version = new(3, 0);
59+
private static readonly Version s_psV4Version = new(4, 0);
60+
private static readonly Version s_psV5Version = new(5, 0);
61+
private static readonly Version s_psV51Version = new(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE);
62+
private static readonly Version s_psV6Version = new(6, 0, 0);
63+
private static readonly Version s_psV61Version = new(6, 1, 0);
64+
private static readonly Version s_psV62Version = new(6, 2, 0);
65+
private static readonly Version s_psV7Version = new(7, 0, 0);
66+
private static readonly Version s_psV71Version = new(7, 1, 0);
67+
private static readonly Version s_psV72Version = new(7, 2, 0);
6968
private static readonly Version s_psVersion;
69+
private static readonly SemanticVersion s_psSemVersion;
7070

7171
/// <summary>
7272
/// A constant to track current PowerShell Edition.
@@ -108,13 +108,13 @@ static PSVersionInfo()
108108
s_psSemVersion = new SemanticVersion(mainVersion);
109109
s_psVersion = (Version)s_psSemVersion;
110110

111-
s_psVersionTable[PSVersionInfo.PSVersionName] = s_psSemVersion;
112-
s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue;
111+
s_psVersionTable[PSVersionName] = s_psSemVersion;
112+
s_psVersionTable[PSEditionName] = PSEditionValue;
113113
s_psVersionTable[PSGitCommitIdName] = rawGitCommitId;
114114
s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psV61Version, s_psV62Version, s_psV7Version, s_psV71Version, s_psV72Version, s_psVersion };
115-
s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion);
116-
s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion;
117-
s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion();
115+
s_psVersionTable[SerializationVersionName] = new Version(InternalSerializer.DefaultVersion);
116+
s_psVersionTable[PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion;
117+
s_psVersionTable[WSManStackVersionName] = GetWSManStackVersion();
118118
s_psVersionTable[PSPlatformName] = Environment.OSVersion.Platform.ToString();
119119
s_psVersionTable[PSOSName] = Runtime.InteropServices.RuntimeInformation.OSDescription;
120120
}
@@ -330,12 +330,12 @@ internal static Version PSV51Version
330330
get { return s_psV51Version; }
331331
}
332332

333-
internal static SemanticVersion PSV6Version
333+
internal static Version PSV6Version
334334
{
335335
get { return s_psV6Version; }
336336
}
337337

338-
internal static SemanticVersion PSV7Version
338+
internal static Version PSV7Version
339339
{
340340
get { return s_psV7Version; }
341341
}

0 commit comments

Comments
 (0)