OSDN Git Service

#30244 Visual Studio 2012 Express for Windows Desktop, StyleCop 4.7, WiX 3.6 に合わせたソース...
[wptscs/wpts.git] / HmLib / Utilities / StringUtils.cs
index 9e45f7c..ad317fe 100644 (file)
@@ -16,7 +16,7 @@ namespace Honememo.Utilities
     /// <summary>
     /// 文字列処理に関するユーティリティクラスです。
     /// </summary>
-    /// <remarks>一部メソッドは、Apache Commons Lang の StringUtils やJava標準の String を参考にしています。</remarks>
+    /// <remarks>一部メソッドは、Apache Commons LangのStringUtilsやJava標準のStringを参考にしています。</remarks>
     public static class StringUtils
     {
         #region 定数
@@ -38,7 +38,7 @@ namespace Honememo.Utilities
         /// <returns>渡された文字列、<c>null</c>の場合には空の文字列。</returns>
         public static string DefaultString(string str)
         {
-            return StringUtils.DefaultString(str, String.Empty);
+            return StringUtils.DefaultString(str, string.Empty);
         }
 
         /// <summary>
@@ -70,7 +70,7 @@ namespace Honememo.Utilities
         /// <returns>開始位置からの部分文字列。</returns>
         public static string Substring(string str, int startIndex)
         {
-            return StringUtils.Substring(str, startIndex, Int32.MaxValue);
+            return StringUtils.Substring(str, startIndex, int.MaxValue);
         }
 
         /// <summary>
@@ -90,7 +90,7 @@ namespace Honememo.Utilities
             int i = startIndex > 0 ? startIndex : 0;
             if (i > str.Length)
             {
-                return String.Empty;
+                return string.Empty;
             }
 
             int l = length > 0 ? length : 0;
@@ -112,8 +112,8 @@ namespace Honememo.Utilities
         /// <param name="str">チェックを行う対象となる文字列。</param>
         /// <param name="prefix">接頭辞。</param>
         /// <param name="toffset">この文字列の比較を開始する位置。</param>
-        /// <returns>始まる場合<c>true</c>。<c>toffset</c>が負の値の場合、<c>str</c>の長さより大きい場合<c>false</c>。それ以外で<c>prefix</c>が空の場合は<c>true</c>。</returns>
-        /// <remarks>引数の<c>null</c>は許容、<c>str</c>のみまたは<c>prefix</c>のみ<c>null</c>は<c>false</c>、<c>prefix</c>も<c>null</c>は<c>true</c>を返す。</remarks>
+        /// <returns>始まる場合<c>true</c>。<paramref name="toffset"/>が負の値の場合、<paramref name="str"/>の長さより大きい場合<c>false</c>。それ以外で<paramref name="prefix"/>が空の場合は<c>true</c>。</returns>
+        /// <remarks>引数の<c>null</c>は許容、<paramref name="str"/>のみまたは<paramref name="prefix"/>のみ<c>null</c>は<c>false</c>、<paramref name="prefix"/>も<c>null</c>は<c>true</c>を返す。</remarks>
         public static bool StartsWith(string str, string prefix, int toffset)
         {
             // nullチェック
@@ -157,14 +157,14 @@ namespace Honememo.Utilities
         /// </summary>
         /// <param name="format">$1~$数値の形式でパラメータを指定する書式指定文字列。</param>
         /// <param name="args">書式設定対象オブジェクト。</param>
-        /// <returns>書式項目が <para>args</para> の対応するオブジェクトの文字列形式に置換された <para>format</para> のコピー。</returns>
-        /// <exception cref="ArgumentNullException"><para>format</para>または<para>args</para>が<c>null</c>の場合。</exception>
+        /// <returns>書式項目が<paramref name="args"/>の対応するオブジェクトの文字列形式に置換された<paramref name="format"/>のコピー。</returns>
+        /// <exception cref="ArgumentNullException"><paramref name="format"/>または<paramref name="args"/>が<c>null</c>の場合。</exception>
         /// <remarks>.netではなくPerl等で見かける$~形式のフォーマットを行う。</remarks>
         public static string FormatDollarVariable(string format, params object[] args)
         {
             // nullチェック
-            Validate.NotNull(format);
-            Validate.NotNull(args);
+            Validate.NotNull(format, "format");
+            Validate.NotNull(args, "args");
 
             // 正規表現で$1~$数値のパラメータ部分を抜き出し、対応するパラメータに置き換える
             // 対応するパラメータが存在しない場合、空文字列となる
@@ -173,11 +173,63 @@ namespace Honememo.Utilities
                 (Match match)
                 =>
                 {
-                    int index = Int32.Parse(match.Groups[1].Value) - 1;
-                    return args.Length > index ? ObjectUtils.ToString(args[index]) : String.Empty;
+                    int index = int.Parse(match.Groups[1].Value) - 1;
+                    return args.Length > index ? ObjectUtils.ToString(args[index]) : string.Empty;
                 });
         }
 
         #endregion
+
+        #region 比較メソッド
+
+        /// <summary>
+        /// 指定した2つのStringオブジェクトを比較し、並べ替え順序におけるそれらの相対位置を示す整数を返します。
+        /// </summary>
+        /// <param name="strA">比較対象の第1文字列。</param>
+        /// <param name="strB">比較対象の第2文字列。</param>
+        /// <returns>
+        /// 0未満: <paramref name="strA"/>が<paramref name="strB"/>より小さい,
+        /// 0: <paramref name="strA"/>と<paramref name="strB"/>は等しい,
+        /// 0より大きい: <paramref name="strA"/>が<paramref name="strB"/>より大きい。
+        /// </returns>
+        /// <remarks>
+        /// パラメータには<c>null</c>が指定可能です。<c>null</c>または空文字列は最も大きい値とみなします。
+        /// <c>null</c>と空文字列を比較した場合、<c>null</c>を大きい値とみなします。
+        /// </remarks>
+        public static int CompareNullsLast(string strA, string strB)
+        {
+            // まずnullの判定
+            if (strA == null && strB == null)
+            {
+                return 0;
+            }
+            else if (strA == null)
+            {
+                return 1;
+            }
+            else if (strB == null)
+            {
+                return -1;
+            }
+
+            // 次に空文字列の判定(nullと空文字列は一応区別)
+            if (string.IsNullOrEmpty(strA) && string.IsNullOrEmpty(strB))
+            {
+                return 0;
+            }
+            else if (string.IsNullOrEmpty(strA))
+            {
+                return 1;
+            }
+            else if (string.IsNullOrEmpty(strB))
+            {
+                return -1;
+            }
+
+            // どちらもnull or 空で無い場合は普通に判定
+            return string.Compare(strA, strB);
+        }
+
+        #endregion
     }
 }