summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-08-14 15:44:49 +0000
committerRich Hickey <richhickey@gmail.com>2006-08-14 15:44:49 +0000
commit73b3f73d469b4a37dd3827e4fff45efce7459d24 (patch)
tree8e6af0217db72abbb5285ffbc05082f85ee3d939 /src
parenta6aa6a69dd251e2a47fe6c1ef17c4e56c0dc5314 (diff)
added simple format
Diffstat (limited to 'src')
-rw-r--r--src/cli/runtime/RT.cs96
-rw-r--r--src/jvm/clojure/lang/RT.java93
2 files changed, 189 insertions, 0 deletions
diff --git a/src/cli/runtime/RT.cs b/src/cli/runtime/RT.cs
index 2dfb46b9..76d19cc7 100644
--- a/src/cli/runtime/RT.cs
+++ b/src/cli/runtime/RT.cs
@@ -502,6 +502,102 @@ static public void print(Object x, TextWriter w) {
}
else w.Write(x.ToString());
}
+
+static public void formatAesthetic(TextWriter w, Object obj) {
+ if(obj == null)
+ w.Write("null");
+ else
+ w.Write(obj.ToString());
+}
+
+static public void formatStandard(TextWriter w,Object obj) {
+ if(obj == null)
+ w.Write("null");
+ else if(obj is String)
+ {
+ w.Write('"');
+ w.Write((String)obj);
+ w.Write('"');
+ }
+ else if(obj is Char)
+ {
+ w.Write('\\');
+ char c = (Char)obj;
+ switch(c){
+ case '\n':
+ w.Write("newline");
+ break;
+ case '\t':
+ w.Write("tab");
+ break;
+ case ' ':
+ w.Write("space");
+ break;
+ default:
+ w.Write(c);
+ break;
+ }
+ }
+ else
+ w.Write(obj.ToString());
+}
+
+static public void format(TextWriter w, String s, ISeq args) {
+ for (int i = 0; i < s.Length;)
+ {
+ char c = s[i++];
+ switch (Char.ToLower(c))
+ {
+ case '~':
+ char d = s[i++];
+ switch (Char.ToLower(d))
+ {
+ case '%':
+ w.Write('\n');
+ break;
+ case 't':
+ w.Write('\t');
+ break;
+ case 'a':
+ if(args == null)
+ throw new Exception("Missing argument");
+ RT.formatAesthetic(w, RT.first(args));
+ args = RT.rest(args);
+ break;
+ case 's':
+ if(args == null)
+ throw new Exception("Missing argument");
+ RT.formatStandard(w, RT.first(args));
+ args = RT.rest(args);
+ break;
+ case '{':
+ int j = s.IndexOf("~}", i); //note - does not nest
+ if(j == -1)
+ throw new Exception("Missing ~}");
+ String subs = s.Substring(i, j-i);
+ format(w, subs, RT.seq(RT.first(args)));
+ args = RT.rest(args);
+ i = j+2; //skip ~}
+ break;
+ case '^':
+ if(args == null)
+ return;
+ break;
+ case '~':
+ w.Write('~');
+ break;
+ default:
+ throw new Exception("Unsupported ~ directive: " + d);
+ break;
+ }
+ break;
+ default:
+ w.Write(c);
+ break;
+ }
+ }
+}
+
/*-------------------------------- values --------------*/
static public Object setValues(params Object[] vals)
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java
index 773376cd..a528da17 100644
--- a/src/jvm/clojure/lang/RT.java
+++ b/src/jvm/clojure/lang/RT.java
@@ -16,6 +16,7 @@ import java.util.Iterator;
import java.io.Reader;
import java.io.PushbackReader;
import java.io.Writer;
+import java.io.IOException;
public class RT{
@@ -503,6 +504,98 @@ static public void print(Object x, Writer w) throws Exception {
}
else w.write(x.toString());
}
+
+static public void formatAesthetic(Writer w,Object obj) throws IOException {
+ if(obj == null)
+ w.write("null");
+ else
+ w.write(obj.toString());
+}
+
+static public void formatStandard(Writer w,Object obj) throws IOException {
+ if(obj == null)
+ w.write("null");
+ else if(obj instanceof String)
+ {
+ w.write('"');
+ w.write((String) obj);
+ w.write('"');
+ }
+ else if(obj instanceof Character)
+ {
+ w.write('\\');
+ char c = ((Character)obj).charValue();
+ switch(c){
+ case '\n':
+ w.write("newline");
+ break;
+ case '\t':
+ w.write("tab");
+ break;
+ case ' ':
+ w.write("space");
+ break;
+ default:
+ w.write(c);
+ }
+ }
+ else
+ w.write(obj.toString());
+}
+
+static public void format(Writer w, String s, ISeq args) throws Exception {
+ for (int i = 0; i < s.length();)
+ {
+ char c = s.charAt(i++);
+ switch (Character.toLowerCase(c))
+ {
+ case '~':
+ char d = s.charAt(i++);
+ switch (Character.toLowerCase(d))
+ {
+ case '%':
+ w.write('\n');
+ break;
+ case 't':
+ w.write('\t');
+ break;
+ case 'a':
+ if(args == null)
+ throw new IllegalArgumentException("Missing argument");
+ RT.formatAesthetic(w, RT.first(args));
+ args = RT.rest(args);
+ break;
+ case 's':
+ if(args == null)
+ throw new IllegalArgumentException("Missing argument");
+ RT.formatStandard(w, RT.first(args));
+ args = RT.rest(args);
+ break;
+ case '{':
+ int j = s.indexOf("~}", i); //note - does not nest
+ if(j == -1)
+ throw new IllegalArgumentException("Missing ~}");
+ String subs = s.substring(i, j);
+ format(w, subs, RT.seq(RT.first(args)));
+ args = RT.rest(args);
+ i = j+2; //skip ~}
+ break;
+ case '^':
+ if(args == null)
+ return;
+ break;
+ case '~':
+ w.write('~');
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported ~ directive: " + d);
+ }
+ break;
+ default:
+ w.write(c);
+ }
+ }
+}
///////////////////////////////// values //////////////////////////
static public Object setValues(Object... vals)