OSDN Git Service

na-get-lib,パフォーマンスチューニングなど(動作変更なし)
[applistation/AppliStation.git] / na-get-lib / NaGet / Utils.cs
index 7f662d9..ed97de8 100644 (file)
@@ -40,6 +40,10 @@ namespace NaGet
                        }\r
                }\r
                \r
+               #endregion\r
+               \r
+               #region リスト関連関数\r
+               \r
                /// <summary>\r
                /// イテレータを結合して、返す\r
                /// </summary>\r
@@ -81,7 +85,7 @@ namespace NaGet
                /// </summary>\r
                /// <param name="enus">元となる複数のイテレータ</param>\r
                /// <returns>結合されたイテレータ</returns>\r
-               public static List<T> MeargeList<T>(params IEnumerable<T>[] enus)\r
+               public static List<T> MergeList<T>(params IEnumerable<T>[] enus)\r
                {\r
                        List<T> list = new List<T>();\r
                        \r
@@ -100,15 +104,38 @@ namespace NaGet
                /// <param name="enu">元となるイテレータ</param>\r
                /// <returns>変換された配列</returns>\r
                public static T[] IEnumerable2Array<T>(IEnumerable<T> enu) {\r
-                       if (enu is T[]) {\r
-                               return (T[]) enu;\r
-                       } else {\r
-                               return ((enu is List<T>)? ((List<T>)enu):new List<T>(enu)).ToArray();\r
+                       T[] retval = enu as T[];\r
+                       \r
+                       if (retval == null) {\r
+                               List<T> list = enu as List<T>;\r
+                               if (list == null) {\r
+                                       list = new List<T>(enu);\r
+                               }\r
+                               retval = list.ToArray();\r
                        }\r
+                       \r
+                       return retval;\r
                }\r
                \r
-               #endregion\r
+               /// <summary>\r
+               /// リストに対して指定した2つの要素の位置を入れ替える\r
+               /// </summary>\r
+               /// <param name="list">操作対象のリスト</param>\r
+               /// <param name="indexA">位置</param>\r
+               /// <param name="indexB">位置</param>\r
+               public static void ListSwap(System.Collections.IList list, int indexA, int indexB)\r
+               {\r
+                       if ((indexA < 0) || (list.Count <= indexA) || (indexB < 0) || (list.Count <= indexB)) {\r
+                               throw new IndexOutOfRangeException();\r
+                       } else if (indexA != indexB) {\r
+                               object temp = list[indexA];\r
+                               list[indexA] = list[indexB];\r
+                               list[indexB] = temp;\r
+                       }\r
+               }\r
                \r
+               #endregion\r
+                               \r
                #region ファイル情報関連ユーテイリティ関数\r
                \r
                /// <summary>\r
@@ -152,9 +179,9 @@ namespace NaGet
                /// URLからそのファイル名を生成する\r
                /// </summary>\r
                /// <param name="url">対象のurl</param>\r
-               public static string Url2filename(string url)\r
+               public static string Url2filename(Uri url)\r
                {\r
-                       string filename = Path.GetFileName(System.Web.HttpUtility.UrlDecode(url, Encoding.UTF8));\r
+                       string filename = Path.GetFileName(System.Web.HttpUtility.UrlDecode(url.ToString(), Encoding.UTF8));\r
                        \r
                        int pos;\r
                        if ((pos = filename.IndexOfAny(Path.GetInvalidFileNameChars())) >= 0) {\r
@@ -295,38 +322,35 @@ namespace NaGet
                }\r
                \r
                /// <summary>\r
-               /// Converts a given absolute path and a given base path to a path that leads\r
-               /// from the base path to the absoulte path. (as a relative path)\r
+               /// パスがフォルダのとき、最後がパスセパレータで終了するようにする。\r
+               /// </summary>\r
+               /// <param name="path">パス</param>\r
+               /// <returns>処理されたパス</returns>\r
+               private static string fixLastPathCharForDirectory(string path)\r
+               {\r
+                       string fixedPath = path;\r
+                       if (Directory.Exists(path) && path[path.Length-1] != Path.DirectorySeparatorChar) {\r
+                               fixedPath += Path.DirectorySeparatorChar;\r
+                       }\r
+                       return fixedPath;\r
+               }\r
+               \r
+               /// <summary>\r
+               /// 絶対パスを相対パスに変換して返します。\r
                /// </summary>\r
+               /// <param name="baseDirectoryPath">相対パスの基準のフォルダ</param>\r
+               /// <param name="absPath">絶対パス</param>\r
+               /// <returns><code>absPath</code>の絶対パス表現</returns>\r
                public static string GetRelativePath(string baseDirectoryPath, string absPath)\r
                {\r
-                       // TODO SharpDevelopのICSharpCode.Core.FileUtilityからのコピペ(GPL)\r
+                       Uri baseuri     = new Uri(fixLastPathCharForDirectory(baseDirectoryPath));\r
+                       Uri absuri      = new Uri(fixLastPathCharForDirectory(absPath));\r
                        \r
-                       string[] bPath = splitPath(baseDirectoryPath);\r
-                       string[] aPath = splitPath(absPath);\r
-                       int indx = 0;\r
-                       for(; indx < Math.Min(bPath.Length, aPath.Length); ++indx){\r
-                               if(!bPath[indx].Equals(aPath[indx], StringComparison.OrdinalIgnoreCase))\r
-                                       break;\r
-                       }\r
-\r
-                       if (indx == 0) {\r
-                               return absPath;\r
-                       }\r
-\r
-                       StringBuilder erg = new StringBuilder();\r
-\r
-                       if(indx == bPath.Length) {\r
-//                             erg.Append('.');\r
-//                             erg.Append(Path.DirectorySeparatorChar);\r
-                       } else {\r
-                               for (int i = indx; i < bPath.Length; ++i) {\r
-                                       erg.Append("..");\r
-                                       erg.Append(Path.DirectorySeparatorChar);\r
-                               }\r
-                       }\r
-                       erg.Append(String.Join(Path.DirectorySeparatorChar.ToString(), aPath, indx, aPath.Length-indx));\r
-                       return erg.ToString();\r
+                       string relative = baseuri.MakeRelativeUri(absuri).ToString();\r
+                       relative = System.Web.HttpUtility.UrlDecode(relative);\r
+                       relative = relative.Replace('/', Path.DirectorySeparatorChar);\r
+                       \r
+                       return relative;\r
                }\r
                \r
                /// <summary>\r
@@ -481,6 +505,7 @@ namespace NaGet
                \r
                #endregion\r
 \r
+               #region イベント情報\r
                \r
                /// <summary>\r
                /// 任意データのイベント情報を表現するクラス\r
@@ -504,5 +529,7 @@ namespace NaGet
                                get { return data; }\r
                        }\r
                }\r
+\r
+               #endregion\r
        }\r
 }\r