static int CountValues(ReadOnlySpan <char> span) { int count = 0; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { count += 2; } var e = new PathVariableFragmentEnumerator(span); while (e.MoveNext()) { count++; } return(count); }
/// <summary> /// <para>Split the value of the PATH environment variable to an array of strings.</para> /// <para>(Windows-specific) The 32-bit Windows system directory (system32) and the Windows directory are prepended.</para> /// </summary> /// <param name="envStr">The value of the PATH environment variable.</param> /// <returns>Array of directories in the search path.</returns> public static string[] ResolveSearchPath(string?envStr) { var fragments = new string[CountValues(envStr)]; int index = 0; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { fragments[0] = Environment.GetFolderPath(Environment.SpecialFolder.System, Environment.SpecialFolderOption.DoNotVerify); fragments[1] = Environment.GetFolderPath(Environment.SpecialFolder.Windows, Environment.SpecialFolderOption.DoNotVerify); index = 2; } var e = new PathVariableFragmentEnumerator(envStr); while (e.MoveNext()) { fragments[index++] = new string(e.Current); } Debug.Assert(index == fragments.Length); return(fragments);