[svn] r7389: nemerle/trunk/lib/nstring.n

pbludov svnadmin at nemerle.org
Tue Feb 6 08:27:57 CET 2007


Log:
System.Text.StringBuilder helpers (AppendList & conditional append)

Author: pbludov
Date: Tue Feb  6 08:27:55 2007
New Revision: 7389

Modified:
   nemerle/trunk/lib/nstring.n

Modified: nemerle/trunk/lib/nstring.n
==============================================================================
--- nemerle/trunk/lib/nstring.n	(original)
+++ nemerle/trunk/lib/nstring.n	Tue Feb  6 08:27:55 2007
@@ -254,6 +254,100 @@
         _ = sb.Append (ch);
       sb.ToString ()
     }
+  }    
+}
+
+namespace Nemerle.Utility.Text
+{
+  public module NStringBuilder
+  {
+    /// <summary>Appends the string representation of a specified list items to the end of a <see href="StringBuilder"/> instance.</summary>
+    /// <returns>A reference to the StringBuilder instance after the append operation has completed.</returns>
+    /// <param name="sb">A <see href="StringBuilder"/> instance pointer. </param>
+    /// <param name="l">A list. </param>
+    /// <param name="sep">The string used as element separator. </param>
+    public AppendList['a] (this sb : StringBuilder, l : list ['a], sep : string) : StringBuilder
+    {
+      match (l) {
+        | [x] => sb.Append (x)
+        | x :: xs => AppendList (sb.Append (x).Append (sep), xs, sep)
+        | [] => sb
+      }
+    }
+
+    /// <summary>Appends the string representation of a specified list items to the end of a <see href="StringBuilder"/> instance.</summary>
+    /// <returns>A reference to the StringBuilder instance after the append operation has completed.</returns>
+    /// <param name="sb">A <see href="StringBuilder"/> instance pointer. </param>
+    /// <param name="l">A list. </param>
+    /// <param name="append">A function used to append elements. </param>
+    /// <param name="sep">The string used as element separator. </param>
+    public AppendList['a] (this sb : StringBuilder, l : list ['a], append : StringBuilder * 'a -> StringBuilder, sep : string) : StringBuilder
+    {
+      match (l) {
+        | [x] => append (sb, x)
+        | x :: xs => AppendList (append (sb, x).Append (sep), xs, append, sep)
+        | [] => sb
+      }
+    }
+
+    /// <summary>Appends to the end of a <see href="StringBuilder"/> instance if a condition is true.</summary>
+    /// <returns>A reference to the StringBuilder instance after the append operation has completed.</returns>
+    /// <param name="sb">A <see href="StringBuilder"/> instance pointer. </param>
+    /// <param name="condition">true to cause a message to be written; otherwise, false. </param>
+    /// <param name="append">A function used to append elements. </param>
+    public AppendWhen (this sb : StringBuilder, condition : bool, append : StringBuilder -> StringBuilder) : StringBuilder
+    {
+      if (condition)
+        append (sb)
+      else
+        sb
+    }
     
+    /// <summary>Appends to the end of a <see href="StringBuilder"/> instance if a condition is false.</summary>
+    /// <returns>A reference to the StringBuilder instance after the append operation has completed.</returns>
+    /// <param name="sb">A <see href="StringBuilder"/> instance pointer. </param>
+    /// <param name="condition">true to cause a message to be written; otherwise, false. </param>
+    /// <param name="append">A function used to append elements. </param>
+    public AppendUnless (this sb : StringBuilder, condition : bool, append : StringBuilder -> StringBuilder) : StringBuilder
+    {
+      if (condition)
+        sb
+      else
+        append (sb)
+    }
+
+    /// <summary>Appends the string representation of a specified item to the end of a <see href="StringBuilder"/> instance number of times.</summary>
+    /// <returns>A reference to the StringBuilder instance after the append operation has completed.</returns>
+    /// <param name="sb">A <see href="StringBuilder"/> instance pointer. </param>
+    /// <param name="count">The number of times when the item should be written. </param>
+    /// <param name="a">The item. </param>
+    /// <param name="sep">The string used as element separator. </param>
+    public AppendNTimes['a] (this sb : StringBuilder, count : int, a : 'a, sep : string) : StringBuilder
+    {
+      def loop (cnt) {
+        | 1 => sb.Append(a)
+        | x when x > 0 => loop(x - 1).Append(sep).Append(a)
+        | _ => sb
+      }
+
+      loop (count)
+    }
+
+    /// <summary>Appends to the end of a <see href="StringBuilder"/> instance number of times.</summary>
+    /// <returns>A reference to the StringBuilder instance after the append operation has completed.</returns>
+    /// <param name="sb">A <see href="StringBuilder"/> instance pointer. </param>
+    /// <param name="count">The number of times when the function should be invoked. </param>
+    /// <param name="append">A function used to append elements. </param>
+    /// <param name="sep">The string used as element separator. </param>
+    public AppendNTimes (this sb : StringBuilder, count : int, append : StringBuilder -> StringBuilder, sep : string) : StringBuilder
+    {
+      def loop (cnt) {
+        | 1 => append(sb)
+        | x when x > 0 => append(loop(x - 1).Append(sep))
+        | _ => sb
+      }
+
+      loop (count)
+    }
   }
 }



More information about the svn mailing list