--
--	Formatted output support code
--	Copyright (C) 2008 Adam Sampson <ats@offog.org>
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation, either
--	version 2 of the License, or (at your option) any later version.
--
--	This library is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library.  If not, see
--	<http://www.gnu.org/licenses/>.
--
--** @module useful

--{{{  externals
#PRAGMA EXTERNAL "PROC C.format.print.stderr (VAL []BYTE s) = 0"
--}}}

--{{{  PROC format.print
--* Trivially print a string to a channel.
-- @private
PROC format.print (CHAN BYTE out!, VAL []BYTE s)
  SEQ i = 0 FOR SIZE s
    out ! s[i]
:
--}}}
--{{{  PROC format.trace
--* Print a string directly to standard error.
-- @private
PROC format.trace (VAL []BYTE s)
  C.format.print.stderr (s)
:
--}}}
--{{{  PROC format.int
--* Format an integer in decimal.
-- @private
PROC format.int (RESULT []BYTE s, RESULT INT i, VAL INT n)
  INITIAL INT n IS n:
  INITIAL BOOL neg IS FALSE:
  SEQ
    IF
      n < 0
        SEQ
          neg := TRUE
          n := -n
      TRUE
        SKIP
    i := (SIZE s) - 1
    WHILE n >= 10
      SEQ
        s[i] := '0' + (BYTE (n \ 10))
        n := n / 10
        i := i - 1
    s[i] := '0' + (BYTE (n \ 10))
    IF
      neg
        SEQ
          i := i - 1
          s[i] := '-'
      TRUE
        SKIP
:
--}}}
