選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

106 行
3.9 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Security;
  23. using System.Text;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. public static partial class Path
  27. {
  28. /// <summary>Combines an array of strings into a path.</summary>
  29. /// <returns>The combined paths.</returns>
  30. /// <exception cref="ArgumentException"/>
  31. /// <exception cref="ArgumentNullException"/>
  32. /// <param name="paths">An array of parts of the path.</param>
  33. [SecurityCritical]
  34. public static string Combine(params string[] paths)
  35. {
  36. return CombineCore(true, paths);
  37. }
  38. /// <summary>Combines an array of strings into a path.</summary>
  39. /// <returns>The combined paths.</returns>
  40. /// <remarks>
  41. /// <para>The parameters are not parsed if they have white space.</para>
  42. /// <para>Therefore, if path2 includes white space (for example, " c:\\ "),</para>
  43. /// <para>the Combine method appends path2 to path1 instead of returning only path2.</para>
  44. /// </remarks>
  45. /// <exception cref="ArgumentNullException"/>
  46. /// <exception cref="ArgumentException"/>
  47. /// <param name="checkInvalidPathChars"><see langword="true"/> will not check <paramref name="paths"/> for invalid path characters.</param>
  48. /// <param name="paths">An array of parts of the path.</param>
  49. [SecurityCritical]
  50. internal static string CombineCore(bool checkInvalidPathChars, params string[] paths)
  51. {
  52. if (paths == null)
  53. throw new ArgumentNullException("paths");
  54. int capacity = 0;
  55. int num = 0;
  56. for (int index = 0, l = paths.Length; index < l; ++index)
  57. {
  58. if (paths[index] == null)
  59. throw new ArgumentNullException("paths");
  60. if (paths[index].Length != 0)
  61. {
  62. if (IsPathRooted(paths[index], checkInvalidPathChars))
  63. {
  64. num = index;
  65. capacity = paths[index].Length;
  66. }
  67. else
  68. capacity += paths[index].Length;
  69. char ch = paths[index][paths[index].Length - 1];
  70. if (!IsDVsc(ch, null))
  71. ++capacity;
  72. }
  73. }
  74. var buffer = new StringBuilder(capacity);
  75. for (int index = num; index < paths.Length; ++index)
  76. {
  77. if (paths[index].Length != 0)
  78. {
  79. if (buffer.Length == 0)
  80. buffer.Append(paths[index]);
  81. else
  82. {
  83. char ch = buffer[buffer.Length - 1];
  84. if (!IsDVsc(ch, null))
  85. buffer.Append(DirectorySeparatorChar);
  86. buffer.Append(paths[index]);
  87. }
  88. }
  89. }
  90. return buffer.ToString();
  91. }
  92. }
  93. }