[MLton-commit] r6276
Vesa Karvonen
vesak at mlton.org
Sun Dec 16 16:54:26 PST 2007
Added the Cvt : CVT module for relatively concise ad hoc text formatting.
----------------------------------------------------------------------
U mltonlib/trunk/com/ssh/extended-basis/unstable/detail/bootstrap.sml
U mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/sigs.cm
U mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/unsealed.cm
A mltonlib/trunk/com/ssh/extended-basis/unstable/detail/text/cvt.sml
U mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.mlb
U mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.use
A mltonlib/trunk/com/ssh/extended-basis/unstable/public/text/cvt.sig
----------------------------------------------------------------------
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/bootstrap.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/bootstrap.sml 2007-12-16 11:03:59 UTC (rev 6275)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/bootstrap.sml 2007-12-17 00:54:25 UTC (rev 6276)
@@ -23,6 +23,7 @@
structure Effect = struct type 'a t = 'a -> Unit.t end
structure FixedInt = struct open BasisFixedInt type t = int end
structure Int = struct open BasisInt type t = int end
+structure Real = struct open BasisReal type t = real end
structure LargeInt = struct open BasisLargeInt type t = int end
structure LargeReal = struct open BasisLargeReal type t = real end
structure LargeWord = struct open BasisLargeWord type t = word end
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/sigs.cm
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/sigs.cm 2007-12-16 11:03:59 UTC (rev 6275)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/sigs.cm 2007-12-17 00:54:25 UTC (rev 6276)
@@ -80,6 +80,7 @@
../../../public/sequence/vector-slice.sig
../../../public/sequence/vector.sig
../../../public/text/char.sig
+ ../../../public/text/cvt.sig
../../../public/text/string.sig
../../../public/text/substring.sig
../../../public/text/text.sig
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/unsealed.cm
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/unsealed.cm 2007-12-16 11:03:59 UTC (rev 6275)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/ml/smlnj/unsealed.cm 2007-12-17 00:54:25 UTC (rev 6276)
@@ -77,6 +77,7 @@
../../../detail/sequence/stream.sml
../../../detail/sequence/vector-slice.sml
../../../detail/sequence/vector.sml
+ ../../../detail/text/cvt.sml
../../../detail/text/mk-text-ext.fun
../../../detail/time/time.sml
../../../detail/typing/phantom.sml
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/text/cvt.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/text/cvt.sml 2007-12-16 11:03:59 UTC (rev 6275)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/text/cvt.sml 2007-12-17 00:54:25 UTC (rev 6276)
@@ -0,0 +1,54 @@
+(* Copyright (C) 2007 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ *)
+
+structure Cvt :> CVT = struct
+ open BasisStringCvt
+
+ type 'a t = 'a -> String.t
+
+ type ('c, 's) sel = ('c -> 's) -> 's
+
+ val C = str
+ val B = Bool.toString
+ val D = Int.toString
+ val X = Word.toString
+ val G = Real.toString
+
+ fun I k = k {b = Int.fmt BIN,
+ o = Int.fmt OCT,
+ d = Int.fmt DEC,
+ x = Int.fmt HEX}
+
+ fun W k = k {b = Word.fmt BIN,
+ o = Word.fmt OCT,
+ d = Word.fmt DEC,
+ x = Word.fmt HEX}
+
+ fun R k = k {s = Real.fmt (SCI NONE),
+ S = Real.fmt o SCI o SOME,
+ f = Real.fmt (FIX NONE),
+ F = Real.fmt o FIX o SOME,
+ g = Real.fmt (GEN NONE),
+ G = Real.fmt o GEN o SOME,
+ e = Real.fmt EXACT}
+
+ fun seq prefix suffix foldr full get c xs =
+ case get (full xs)
+ of NONE => prefix ^ suffix
+ | SOME (x, xs) =>
+ concat (prefix::c x::foldr (fn (x, ss) => ", "::c x::ss) [suffix] xs)
+
+ fun A ? = let open ArraySlice in seq "[|" "|]" foldr full getItem end ?
+ fun L ? = let open List in seq "[" "]" foldr Fn.id getItem end ?
+ fun V ? = let open VectorSlice in seq "#[" "]" foldr full getItem end ?
+
+ fun O c = fn NONE => "NONE" | SOME x => "SOME " ^ c x
+
+ fun P k = k {l = padLeft #" ",
+ r = padRight #" ",
+ L = padLeft,
+ R = padRight}
+end
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/text/cvt.sml
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.mlb
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.mlb 2007-12-16 11:03:59 UTC (rev 6275)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.mlb 2007-12-17 00:54:25 UTC (rev 6276)
@@ -310,6 +310,10 @@
(* Time *)
public/time/time.sig
detail/time/time.sml
+
+ (* Cvt *)
+ public/text/cvt.sig
+ detail/text/cvt.sml
in
public/export/$(SML_COMPILER).sml
public/export/common.sml
Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.use
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.use 2007-12-16 11:03:59 UTC (rev 6275)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/extensions.use 2007-12-17 00:54:25 UTC (rev 6276)
@@ -162,6 +162,8 @@
"detail/concept/mk-word-flags.fun",
"public/time/time.sig",
"detail/time/time.sml",
+ "public/text/cvt.sig",
+ "detail/text/cvt.sml",
"public/export/${SML_COMPILER}.sml",
"public/export/common.sml",
"public/export/top-level.sml",
Added: mltonlib/trunk/com/ssh/extended-basis/unstable/public/text/cvt.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/text/cvt.sig 2007-12-16 11:03:59 UTC (rev 6275)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/text/cvt.sig 2007-12-17 00:54:25 UTC (rev 6276)
@@ -0,0 +1,77 @@
+(* Copyright (C) 2007 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ *)
+
+(**
+ * The {Cvt : CVT} module provides for relatively concise ad hoc text
+ * formatting. It is designed to yield a notation reminiscent of typical
+ * "printf" facilities found in many languages.
+ *
+ * The intention is that a user opens the {Cvt} structure for use. To
+ * reduce the chance of shadowing user bindings, while allowing concise
+ * notation, the values in the {Cvt} structure use upper-case names.
+ *
+ * For brevity, many of the identifiers use only a single letter. In
+ * addition, several combinators use first-class selectors for selecting
+ * options. An argument to such a combinator is a first-class selector
+ * that selects one of the possible alternatives (continuations) for the
+ * formatter. For example, the first argument to the {R} combinator, for
+ * formatting reals, is such a selector. Suppose you wish to format a real
+ * in scientific notation with 3 digits after the decimal point. To do
+ * that you could write {R#S 3 aReal}, which is equivalent to {Real.fmt
+ * (StringCvt.SCI (SOME 3)) aReal}.
+ *)
+signature CVT = sig
+ type 'a t = 'a -> String.t
+ (** Type of formatters or conversion functions from values to strings. *)
+
+ type ('c, 's) sel = ('c -> 's) -> 's
+ (** Type of selectors parameterized formatters. *)
+
+ (** == Basic Formatters == *)
+
+ val C : Char.t t (** Same as {str}. *)
+ val B : Bool.t t (** Same as {Bool.toString}. *)
+ val D : Int.t t (** Same as {Int.toString} and {I#d}. *)
+ val X : Word.t t (** Same as {Word.toString} and {W#x}. *)
+ val G : Real.t t (** Same as {Real.toString} and {R#g}. *)
+
+ val I : ({b : Int.t t (** {I#b = Int.fmt BIN} *)
+ , o : Int.t t (** {I#o = Int.fmt OCT} *)
+ , d : Int.t t (** {I#d = Int.fmt DEC} *)
+ , x : Int.t t (** {I#x = Int.fmt HEX} *)
+ }, 'k) sel
+
+ val W : ({b : Word.t t (** {W#b = Word.fmt BIN} *)
+ , o : Word.t t (** {W#o = Word.fmt OCT} *)
+ , d : Word.t t (** {W#d = Word.fmt DEC} *)
+ , x : Word.t t (** {W#x = Word.fmt HEX} *)
+ }, 'k) sel
+
+ val R : ({s : Real.t t (** {R#s = Real.fmt (SCI NONE)} *)
+ , S : Int.t -> Real.t t (** {R#S n = Real.fmt (SCI (SOME n))} *)
+ , f : Real.t t (** {R#f = Real.fmt (FIX NONE)} *)
+ , F : Int.t -> Real.t t (** {R#F n = Real.fmt (FIX (SOME n))} *)
+ , g : Real.t t (** {R#g = Real.fmt (GEN NONE)} *)
+ , G : Int.t -> Real.t t (** {R#G n = Real.fmt (GEN (SOME n))} *)
+ , e : Real.t t (** {R#e = Real.fmt EXACT} *)
+ }, 'k) sel
+
+ (** == Formatter Combinators == *)
+
+ val A : 'a t -> 'a Array.t t (** Makes a formatter for arrays. *)
+ val L : 'a t -> 'a List.t t (** Makes a formatter for lists. *)
+ val O : 'a t -> 'a Option.t t (** Makes a formatter for options. *)
+ val V : 'a t -> 'a Vector.t t (** Makes a formatter for vectors. *)
+
+ (** == Formatting Utilities == *)
+
+ val P :
+ ({l : Int.t -> String.t UnOp.t (** {P#l n = padLeft #" " n} *)
+ , r : Int.t -> String.t UnOp.t (** {P#r n = padRight #" " n} *)
+ , L : Char.t -> Int.t -> String.t UnOp.t (** {P#L c n = padLeft c n} *)
+ , R : Char.t -> Int.t -> String.t UnOp.t (** {P#R c n = padRight c n} *)
+ }, 'k) sel
+end
Property changes on: mltonlib/trunk/com/ssh/extended-basis/unstable/public/text/cvt.sig
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the MLton-commit
mailing list