diff options
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Console.scala | 11 | ||||
-rw-r--r-- | src/library/scala/runtime/RichString.scala | 22 |
2 files changed, 28 insertions, 5 deletions
diff --git a/src/library/scala/Console.scala b/src/library/scala/Console.scala index 6136c25bc7..6e122f450e 100644 --- a/src/library/scala/Console.scala +++ b/src/library/scala/Console.scala @@ -175,16 +175,17 @@ object Console { * @param args the arguments used to instantiating the pattern. * @throws java.lang.IllegalArgumentException */ - def printf(text: String, args: Any*) { format(text, args: _*) } + def printf(text: String, args: Any*) { out.print(text format (args : _*)) } /** * @see <a href="#printf(java.lang.String,scala.Any*)" * target="contentFrame">Console.printf</a>. + * @deprecated For console output, use <code>Console.printf</code>. For <code>String</code> formatting, + * <code>RichString</code>'s <code>format</code> method. */ - def format(text: String, args: Any*): Unit = - if (text eq null) "null" else - out.printf(text, args.asInstanceOf[scala.runtime.BoxedObjectArray]. - unbox(args.getClass).asInstanceOf[Array[Object]] : _*) + @deprecated def format(text: String, args: Any*) { + if (text eq null) out.printf("null") else (out.print(text format (args : _*))) + } /** Read a full line from the terminal. Returns <code>null</code> if the end of the * input stream has been reached. diff --git a/src/library/scala/runtime/RichString.scala b/src/library/scala/runtime/RichString.scala index 412f2fd86c..ea4dd44e5f 100644 --- a/src/library/scala/runtime/RichString.scala +++ b/src/library/scala/runtime/RichString.scala @@ -220,6 +220,28 @@ final class RichString(val self: String) extends Proxy with RandomAccessSeq[Char self.getChars(0, length, result, 0) result } + + + /** <p> + * Uses the underlying string as a pattern (in a fashion similar to + * printf in C), and uses the supplied arguments to fill in the + * holes. Only works on Java 1.5 or higher! + * </p> + * <p> + * The interpretation of the formatting patterns is described in + * <a href="" target="contentFrame" class="java/util/Formatter"> + * <code>java.util.Formatter</code></a>. + * </p> + * + * @param args the arguments used to instantiating the pattern. + * @throws java.lang.IllegalArgumentException + */ + def format(args : Any*) : String = { + val m = classOf[String].getDeclaredMethod("format", classOf[String], classOf[Array[Object]]) + m.invoke(null, self, + args.asInstanceOf[scala.runtime.BoxedObjectArray]. + unbox(args.getClass).asInstanceOf[Array[Object]]).asInstanceOf[String] + } } object RichString { |