From a899ff83e5a336505cdf0b64d2ef48f2d1fd417a Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 14 Dec 2024 11:46:09 +0800 Subject: [PATCH] Improve code quality --- .../Programs/ShellLinkHelper.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs b/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs index 05cbfd2b1..ad1f3ab56 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs @@ -38,11 +38,7 @@ namespace Flow.Launcher.Plugin.Program.Programs fixed (char* bufferChar = buffer) { ((IShellLinkW)link).GetPath((PWSTR)bufferChar, MAX_PATH, &data, (uint)SLGP_FLAGS.SLGP_SHORTPATH); - - // Truncate the buffer to the actual length of the string - int validLength = Array.IndexOf(buffer, '\0'); - if (validLength < 0) validLength = MAX_PATH; - target = new string(buffer, 0, validLength); + target = GetStringFromBuffer(buffer, MAX_PATH); } // To set the app description @@ -54,9 +50,7 @@ namespace Flow.Launcher.Plugin.Program.Programs fixed (char* buffer1Char = buffer1) { ((IShellLinkW)link).GetDescription((PWSTR)buffer1Char, MAX_PATH); - int validLength = Array.IndexOf(buffer1, '\0'); - if (validLength < 0) validLength = MAX_PATH; - description = new string(buffer1, 0, validLength); + description = GetStringFromBuffer(buffer1, MAX_PATH); } } catch (COMException e) @@ -71,9 +65,7 @@ namespace Flow.Launcher.Plugin.Program.Programs fixed (char* buffer2Char = buffer2) { ((IShellLinkW)link).GetArguments((PWSTR)buffer2Char, MAX_PATH); - int validLength = Array.IndexOf(buffer2, '\0'); - if (validLength < 0) validLength = MAX_PATH; - arguments = new string(buffer2, 0, validLength); + arguments = GetStringFromBuffer(buffer2, MAX_PATH); } } @@ -82,5 +74,13 @@ namespace Flow.Launcher.Plugin.Program.Programs return target; } + + private static unsafe string GetStringFromBuffer(char[] buffer, int maxLength) + { + // Truncate the buffer to the actual length of the string + int validLength = Array.IndexOf(buffer, '\0'); + if (validLength < 0) validLength = maxLength; + return new string(buffer, 0, validLength); + } } }