!== Formatted output conversion
!
! Authors::   Yasuhiro MORIKAWA, Eizi TOYODA
! Version::   $Id: dcstringsprintf.f90,v 1.2 2009-03-20 09:50:19 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2000-2005. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview
!
! C ã® sprintf(3) ã®ã‚ˆã†ã«æ–‡å—åˆ—ã‚’ãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆã—ã¦è¿”ã—ã¾ã™ã€‚
! ãŸã ã—ã€å®Ÿè£…ã¯ C ã® sprintf(3) ã¨ã¯å¤§åˆ†é•ã†ã®ã§ã”æ³¨æ„ãã ã•ã„ã€‚
!
!== Formatter
!
! dc_string#CPrintf, dc_string#Printf ã®ãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆå¼•æ•°ã«
! ç”¨ã„ã‚‰ã‚Œã‚‹æŒ‡ç¤ºåã¯ <b><tt>%</tt></b> ã§å§‹ã¾ã‚Šã¾ã™ã€‚ç¨®é¡žã¯
! ä»¥ä¸‹ã®é€šã‚Šã§ã™ã€‚
!
! <b><tt>æŒ‡ç¤ºå</tt></b> ::
!    <tt>å¯¾å¿œã™ã‚‹å¼•æ•°</tt> :: ãƒ‡ãƒ¼ã‚¿ã®ç¨®é¡žã¨å‡ºåŠ›å½¢å¼
!
! <b><tt>%d, %D</tt></b> ::
!    <tt>i(:)</tt>       :: æ•´æ•°ãƒ‡ãƒ¼ã‚¿ (10 é€²æ•°) ã‚’è¡¨ç¤º.
!              %2d ã‚„ %04d ã®ã‚ˆã†ã«'%' ã®å¾Œã‚ã«æ•°å—ã‚’æŒ‡å®šã™ã‚‹ã“ã¨ã§
!              å‡ºåŠ›ã™ã‚‹æ¡æ•°ã‚’å¤‰æ›´ã§ãã¾ã™.
!              '%' ã®ç›´å¾ŒãŒ 0 ã®å ´åˆã¯å…ˆé ã« 0 ã‚’, ãã†ã§ãªã„å ´åˆã¯ç©ºç™½ã‚’åŸ‹ã‚ã¾ã™.
!
! <b><tt>%o, %O</tt></b> ::
!    <tt>i(:)</tt>       :: 8 é€²æ•°ãƒ‡ãƒ¼ã‚¿ã‚’è¡¨ç¤º
!
! <b><tt>%x, %X</tt></b> ::
!    <tt>i(:)</tt>       :: 16 é€²æ•°ãƒ‡ãƒ¼ã‚¿ã‚’è¡¨ç¤º
!
! <b><tt>%f, %F</tt></b> ::
!    <tt>d(:)</tt>       :: å€ç²¾åº¦å®Ÿæ•°ãƒ‡ãƒ¼ã‚¿ã‚’æœ€å¤§å…¨æ¡æ•° 80ã€å°æ•°éƒ¨ã®æ¡æ•° 40ã§è¡¨ç¤º
!
! <b><tt>%r, %R</tt></b> ::
!    <tt>r(:)</tt>       :: å˜ç²¾åº¦å®Ÿæ•°ãƒ‡ãƒ¼ã‚¿ã‚’æœ€å¤§å…¨æ¡æ•° 80ã€å°æ•°éƒ¨ã®æ¡æ•° 40ã§è¡¨ç¤º
!
! <b><tt>%b, %B</tt></b> ::
!    <tt>L(:)</tt>       :: è«–ç†ãƒ‡ãƒ¼ã‚¿ã‚’ çœŸï¼šTã€å½ï¼šF ã§è¡¨ç¤º
!
! <b><tt>%y, %Y</tt></b> ::
!    <tt>L(:)</tt>       :: è«–ç†ãƒ‡ãƒ¼ã‚¿ã‚’ çœŸï¼šyesã€å½ï¼šno ã§è¡¨ç¤º
!
! <b><tt>%c, %C</tt></b> ::
!    <tt>c1ã€c2ã€c3</tt> :: æ–‡å—ãƒ‡ãƒ¼ã‚¿ (å¤‰æ•°)
!
! <b><tt>%a, %A</tt></b> ::
!    <tt>ca</tt>         :: æ–‡å—ãƒ‡ãƒ¼ã‚¿ (é…åˆ—)
!
!
! æ–‡å—ãƒ‡ãƒ¼ã‚¿ (å¤‰æ•°) ä»¥å¤–ã¯ã€1ã¤ã®åž‹ã®ãƒ‡ãƒ¼ã‚¿ã‚’ã„ãã¤ã§ã‚‚ä¸Žãˆã‚‹ã“ã¨ãŒå¯èƒ½ã§ã™ã€‚
! æ–‡å—ãƒ‡ãƒ¼ã‚¿ (å¤‰æ•°) ã¯ c1ã€c2ã€c3 ã«ãã‚Œãžã‚Œ 1
! ã¤ã¥ã¤ã®æ–‡å—ãƒ‡ãƒ¼ã‚¿ã—ã‹ä¸Žãˆã‚‹ã“ã¨ãŒã§ãã¾ã›ã‚“ã€‚
! +ca+ å¼•æ•°ã‚’ç”¨ã„ã‚‹å ´åˆã¯ dc_string#StoA ã‚’ä½µç”¨ã™ã‚‹ã¨ä¾¿åˆ©ã§ã™ã€‚
!
! ã¾ãŸã€ãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆæŒ‡å®šåã¨ã—ã¦ <b><tt>%*</tt></b> ã‚’ä¸Žãˆã‚‹ã“ã¨ã§ã€
! è¤‡æ•°ã®ãƒ‡ãƒ¼ã‚¿ã‚’ä¸€åº¦ã«å‡ºåŠ›ã™ã‚‹ã“ã¨ã‚‚å¯èƒ½ã§ã™ã€‚
! ãã®å ´åˆã€ã„ãã¤ã®ãƒ‡ãƒ¼ã‚¿ã‚’ä¸€åº¦ã«å‡ºåŠ›ã™ã‚‹ã‹ã‚’ <tt>n(:)</tt>
! ã«ä¸Žãˆã‚‹å¿…è¦ãŒã‚ã‚Šã¾ã™ã€‚
!
!== Example
!
!=== dc_string#CPrintf ã‚’ç”¨ã„ãŸå‡ºåŠ›ã®ä¾‹
!
!      use dc_types,  only: STRING
!      use dc_string, only: CPrintf
!      character(len = STRING) :: output, color="RED", size="Large"
!      integer, parameter      :: n1 = 2, n2 = 3
!      integer                 :: int = 10, arrayI1(n1), arrayI2(n2), i
!      real                    :: arrayR(n1)
!      logical                 :: eq
!
!      do, i = 1, n1
!        arrayI1(i) = 123 * i ; arrayR(i)  = 1.23 * i
!      enddo
!      do, i = 1, n2
!        arrayI2(i) = 345 * i
!      enddo
!      eq = (maxval(arrayI1) == minval(arrayI2))
!      output = CPrintf(fmt="color=%c size=%c int=%03d I1=%*d I2=%*04d R=%*r equal=%y", &
!        &              c1=trim(color), c2=trim(size), i=(/int, arrayI1, arrayI2/), &
!        &              r=(/arrayR/), L=(/eq/), n=(/n1, n2, n1/))
!
!      write(*,*) trim(output)
!
! æ–‡å—ãƒ‡ãƒ¼ã‚¿ä»¥å¤–ã®ã‚‚ã®ã¯åŸºæœ¬çš„ã«1æ¬¡å…ƒé…åˆ—ã—ã‹å¼•æ•°ã«ã¨ã‚Œãªã„ãŸã‚ã€
! å¤šæ¬¡å…ƒé…åˆ—ã‚’å‡ºåŠ›ã—ãŸã„å ´åˆã«ã¯çµ„è¾¼ã¿é–¢æ•°ã§ã‚ã‚‹ pack é–¢æ•°ã‚’
! ç”¨ã„ã‚‹ã¨è‰¯ã„ã§ã—ã‚‡ã†ã€‚ä»¥ä¸‹ã«ãã®ä¾‹ã‚’è¨˜ã—ã¾ã™ã€‚
!
!      use dc_types,  only: STRING
!      use dc_string, only: CPrintf
!      character(len = STRING) :: output
!      integer                 :: i,j,k
!      integer, parameter      :: n1 = 2, n2 = 3, n3 = 4
!      real                    :: array(n1,n2,n3)
!
!      do, i = 1, n1
!        do, j = 1, n2
!          do, k = 1, n3
!            array(i,j,k) = i * 0.1 + j * 1.0 + k * 10.0
!          enddo
!        enddo
!      enddo
!      output = CPrintf('array=<%*r>', &
!        &              r=(/pack(array(:,:,:), .true.)/), n=(/size(array(:,:,:))/))
!      write(*,*) trim(output)
!
!=== dc_string#Printf ã‚’ç”¨ã„ãŸå‡ºåŠ›ã®ä¾‹
!
!      use dc_types,  only: STRING
!      use dc_string, only: Printf
!      character(len = STRING) :: output, color="RED", size="Large"
!      integer, parameter      :: n1 = 2, n2 = 3
!      integer                 :: int = 10, arrayI1(n1), arrayI2(n2), i
!      real                    :: arrayR(n1)
!      logical                 :: eq
!
!      do, i = 1, n1
!        arrayI1(i) = 123 * i   ; arrayR(i)  = 1.23 * i
!      enddo
!      do, i = 1, n2
!        arrayI2(i) = 345 * i
!      enddo
!      eq = (maxval(arrayI1) == minval(arrayI2))
!
!      ! è£…ç½®ç•ªå· 6 (æ¨™æº–å‡ºåŠ›) ã«ç›´æŽ¥å‡ºåŠ›ã™ã‚‹å ´åˆ
!      call Printf(unit=6, &
!        &         fmt="color=%c size=%c int=%03d I1=%*d I2=%*04d R=%*r equal=%y", &
!        &         c1=trim(color), c2=trim(size), i=(/int, arrayI1, arrayI2/), &
!        &         r=(/arrayR/), L=(/eq/), n=(/n1, n2, n1/))
!
!      ! æ–‡å—åˆ— output ã«æ¸¡ã™å ´åˆ
!      call Printf(unit=output, &
!        &         fmt="color=%c size=%c int=%03d I1=%*d I2=%*04d R=%*r equal=%y", &
!        &         c1=trim(color), c2=trim(size), i=(/int, arrayI1, arrayI2/), &
!        &         r=(/arrayR/), L=(/eq/), n=(/n1, n2, n1/))
!      write(*,*) trim(output)

subroutine DCStringSPrintf(unit, fmt, i, r, d, L, n, c1, c2, c3, ca)
  !
  ! ãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆæ–‡å—åˆ— fmt ã«å¾“ã£ã¦å¤‰æ›ã•ã‚ŒãŸæ–‡å—åˆ—ã‚’ unit ã«è¿”ã—ã¾ã™ã€‚
  ! ç¬¬2å¼•æ•° fmt ã«ã¯æŒ‡ç¤ºåã‚’å«ã‚€æ–‡å—åˆ—ã‚’ä¸Žãˆã¾ã™ã€‚
  ! æŒ‡ç¤ºåã«ã¯ã€Œ<tt>%</tt>ã€ã‚’ç”¨ã„ã¾ã™ã€‚
  ! <tt>%</tt> ã‚’ç”¨ã„ãŸã„å ´åˆã¯ ã€Œ<tt>%%</tt>ã€ã¨è¨˜è¿°ã—ã¾ã™ã€‚
  ! æŒ‡ç¤ºåãŠã‚ˆã³ç”¨ä¾‹ã«é–¢ã—ã¦ã®è©³ç´°ã¯ dc_utils/dcstringsprintf.f90 ã‚’å‚ç…§ãã ã•ã„ã€‚
  !
  use dc_types,      only: DP
  implicit none
  character(*),     intent(out)         :: unit
  character(*),     intent(in)          :: fmt
  integer,          intent(in), optional:: i(:), n(:)
  real,             intent(in), optional:: r(:)
  real(DP),         intent(in), optional:: d(:)
  logical,          intent(in), optional:: L(:)
  character(*),     intent(in), optional:: c1, c2, c3
  character(*),     intent(in), optional:: ca(:)

  ! ä¸Šè¨˜é…åˆ—å¼•æ•°ã®ã‚«ã‚¦ãƒ³ã‚¿
  integer:: ni, nr, nd, nl, nc, na, nn
  integer:: ucur       ! unit ã«æ›¸ã‹ã‚ŒãŸæ–‡å—æ•°
  integer:: endp       ! æ—¢ã«å‡¦ç†ã•ã‚ŒãŸ fmt ã®æ–‡å—æ•°
  integer:: cur        ! ç¾åœ¨ç€ç›®ä¸ã®æ–‡å—ã¯ fmt(cur:cur) ã§ã‚ã‚‹
  integer:: ptr        ! fmt ã‹ã‚‰æ¤œç´¢ã‚’ã™ã‚‹ã¨ãã«ä½¿ç”¨
  integer:: exp_ptr    ! fmt ã‹ã‚‰æ•°å€¤ã®æŒ‡æ•°éƒ¨ã‚’æ¤œç´¢ã‚’ã™ã‚‹ã¨ãã«ä½¿ç”¨
  integer:: minus_ptr  ! '-' ã‚’æ¤œç´¢ã™ã‚‹æ™‚ã«ä½¿ç”¨
  integer:: repeat     ! %æ•°å— ã¾ãŸã¯ %* ã‹ã‚‰æ±ºå®šã•ã‚ŒãŸç¹°è¿”ã—æ•°
  integer:: m          ! 1:repeat ã®ç¯„å›²ã§å‹•ããƒ«ãƒ¼ãƒ—å¤‰æ•°
  integer:: stat       ! ã‚¨ãƒ©ãƒ¼å‡¦ç†
  character(80):: cbuf ! read/write æ–‡ã®ãƒãƒƒãƒ•ã‚¡
  character(80):: exp_buf ! real/write æ–‡ã®æŒ‡æ•°éƒ¨ã®ãƒãƒƒãƒ•ã‚¡ (å®Ÿæ•°åž‹ç”¨)
  character(80):: ibuf ! real/write æ–‡ã®ãƒãƒƒãƒ•ã‚¡ (æ•´æ•°åž‹ç”¨)
  integer:: len_ibuf   ! ibuf ã®é•·ã•
  integer:: figs_ibuf  ! ibuf ã®æœ‰åŠ¹ãªæ¡æ•°
  logical:: int_zero_fill ! å…ˆé ã‚’ 0 ã§åŸ‹ã‚ã‚‹ã‹ã©ã†ã‹ã‚’åˆ¤å®šã™ã‚‹ãƒ•ãƒ©ã‚° (æ•´æ•°åž‹ç”¨)
  integer:: int_figs   ! æ•´æ•°åž‹ã‚’å‡ºåŠ›ã™ã‚‹éš›ã®æ¡æ•° (æ•´æ•°åž‹ç”¨)
continue
  ni = 0;  nr = 0;  nd = 0;  nl = 0;  nc = 0;  na = 0;  nn = 0
  unit = ""
  ucur = 0
  endp = 0
  int_figs = 0
  int_zero_fill = .false.
  MainLoop: do
    cur = endp + 1
    if (cur > len(fmt)) exit MainLoop
    !
    ! ãƒªãƒ†ãƒ©ãƒ«ã«è»¢å†™ã§ãã‚‹æ–‡å—åˆ— fmt(cur:endp-1) ã‚’ç™ºè¦‹å‡¦ç†
    !
    endp = cur - 1 + scan(fmt(cur: ), '%')
    if (endp > cur) then
      call append(unit, ucur, fmt(cur:endp-1), stat)
      if (stat /= 0) exit MainLoop
    else if (endp == cur - 1) then
      call append(unit, ucur, fmt(cur: ), stat)
      exit MainLoop
    endif
    !
    ! % ã‹ã‚‰æ›¸å¼æŒ‡å®šæ–‡å—ã¾ã§ã‚’ fmt(cur:endp) ã¨ã™ã‚‹
    !
    cur = endp + 1
    endp = cur - 1 + scan(fmt(cur: ), 'DdOoXxFfRrBbYySsCcAa%')
    if (endp < cur) then
      call append(unit, ucur, fmt(cur-1: ), stat)
      exit MainLoop
    endif
    cbuf = fmt(cur:endp-1)
    !
    ! %* ãŒã‚ã‚‹å ´åˆã€n(:) ã«æ¸¡ã•ã‚ŒãŸæ•°ã‹ã‚‰ç¹°ã‚Šè¿”ã—å›žæ•°ã‚’å–å¾—
    !
    if (cbuf(1:1) == '*') then
      nn = nn + 1
      if (nn > size(n)) then
        repeat = 1
      else
        repeat = n(nn)
      endif
      ibuf = cbuf(2:)
    else
      repeat = 1
      ibuf = cbuf
!    else if (cbuf == '') then
!      repeat = 1
!    else
!      ptr = verify(cbuf, " 0123456789")
!      if (ptr > 0) cbuf(ptr: ) = " "
!      read(cbuf, "(I80)", iostat=ptr) repeat
    endif
    !
    ! %2d ã‚„ %04d ã®ã‚ˆã†ã« '%' ã®å¾Œã‚ã«æ•°å—ãŒæŒ‡å®šã•ã‚Œã€
    ! ã‹ã¤ d (æ•´æ•°åž‹å¤‰æ•°ã®è¡¨ç¤º) ã®å ´åˆã«ã¯å…ˆé ã«ç©ºç™½
    ! ã¾ãŸã¯ 0 ã‚’åŸ‹ã‚ã‚‹.
    !
    if (scan(ibuf(1:1),'1234567890') > 0) then
      if (ibuf(1:1) == '0') then
        int_zero_fill = .true.
      else
        int_zero_fill = .false.
      end if
      read(unit=ibuf, fmt="(i80)") int_figs
    else
      int_figs = 0
      int_zero_fill = .false.
    endif
    PercentRepeat: do, m = 1, repeat
      if (m > 1) then
        call append(unit, ucur, ", ", stat)
        if (stat /= 0) exit MainLoop
      endif
      select case(fmt(endp:endp))
      case('d', 'D')
        if (.not. present(i)) cycle MainLoop
        ni = ni + 1;  if (ni > size(i)) cycle MainLoop
        write(ibuf, "(i20)") i(ni)
        len_ibuf = len(trim(adjustl(ibuf)))
        figs_ibuf = verify(ibuf, ' ')
        cbuf = ' '
        if (int_figs > len_ibuf) then
          minus_ptr = scan(ibuf, '-')
          if (int_zero_fill) then
            if (minus_ptr /= 0) then
              len_ibuf = len_ibuf - 1
              figs_ibuf = figs_ibuf + 1
              cbuf(1:int_figs-len_ibuf) = '-0000000000000000000'
            else
              cbuf(1:int_figs-len_ibuf) = '00000000000000000000'
            end if
          end if
          cbuf(int_figs-len_ibuf+1:) = ibuf(figs_ibuf:20)
        else
          cbuf = ibuf(figs_ibuf:20)
        end if
        call append(unit, ucur, trim(cbuf), stat)
        if (stat /= 0) exit MainLoop
      case('o', 'O')
        if (.not. present(i)) cycle MainLoop
        ni = ni + 1;  if (ni > size(i)) cycle MainLoop
        write(cbuf, "(o20)") i(ni)
        call append(unit, ucur, trim(adjustl(cbuf)), stat)
        if (stat /= 0) exit MainLoop
      case('x', 'X')
        if (.not. present(i)) cycle MainLoop
        ni = ni + 1;  if (ni > size(i)) cycle MainLoop
        write(cbuf, "(z20)") i(ni)
        call append(unit, ucur, trim(adjustl(cbuf)), stat)
        if (stat /= 0) exit MainLoop
      case('f', 'F')
        if (.not. present(d)) cycle MainLoop
        nd = nd + 1;  if (nd > size(d)) cycle MainLoop
        write(cbuf, "(g80.40)") d(nd)
        cbuf = adjustl(cbuf)
        exp_ptr = verify(cbuf, ' 1234567890-+.', back=.TRUE.)
        exp_buf = ' '
        if (exp_ptr > 0) then
          exp_buf = cbuf(exp_ptr: )
          cbuf(exp_ptr: ) = " "
        end if
        ptr = verify(cbuf, " 0", back=.TRUE.)
        if (ptr > 0) cbuf(ptr+1: ) = " "
        cbuf = trim(cbuf) // trim(exp_buf)
        call append(unit, ucur, trim(adjustl(cbuf)), stat)
        if (stat /= 0) exit MainLoop
      case('r', 'R')
        if (.not. present(r)) cycle MainLoop
        nr = nr + 1;  if (nr > size(r)) cycle MainLoop
        write(cbuf, "(g80.40)") r(nr)
        cbuf = adjustl(cbuf)
        exp_ptr = verify(cbuf, ' 1234567890-+.', back=.TRUE.)
        exp_buf = ' '
        if (exp_ptr > 0) then
          exp_buf = cbuf(exp_ptr: )
          cbuf(exp_ptr: ) = " "
        end if
        ptr = verify(cbuf, " 0", back=.TRUE.)
        if (ptr > 0) cbuf(ptr+1: ) = " "
        cbuf = trim(cbuf) // trim(exp_buf)
        call append(unit, ucur, trim(adjustl(cbuf)), stat)
        if (stat /= 0) exit MainLoop
      case('b', 'B')
        if (.not. present(L)) cycle MainLoop
        nl = nl + 1;  if (nl > size(L)) cycle MainLoop
        write(cbuf, "(L1)") L(nl)
        call append(unit, ucur, trim(adjustl(cbuf)), stat)
        if (stat /= 0) exit MainLoop
      case('y', 'Y')
        if (.not. present(L)) cycle MainLoop
        nl = nl + 1;  if (nl > size(L)) cycle MainLoop
        if (L(nl)) then
          call append(unit, ucur, "yes", stat)
          if (stat /= 0) exit MainLoop
        else
          call append(unit, ucur, "no", stat)
          if (stat /= 0) exit MainLoop
        endif
      case('c', 'C')
        nc = nc + 1
        if (nc == 1) then
          if (.not. present(c1)) cycle PercentRepeat
          call append(unit, ucur, c1, stat)
          if (stat /= 0) exit MainLoop
        else if (nc == 2) then
          if (.not. present(c2)) cycle PercentRepeat
          call append(unit, ucur, c2, stat)
          if (stat /= 0) exit MainLoop
        else if (nc == 3) then
          if (.not. present(c3)) cycle PercentRepeat
          call append(unit, ucur, c3, stat)
          if (stat /= 0) exit MainLoop
        endif
      case('a', 'A')
        if (.not. present(ca)) cycle MainLoop
        na = na + 1;  if (na > size(ca)) cycle MainLoop
        call append(unit, ucur, trim(adjustl(ca(na))), stat)
        if (stat /= 0) exit MainLoop
      case('%')
        call append(unit, ucur, '%', stat)
        if (stat /= 0) exit MainLoop
      end select
    enddo PercentRepeat
  enddo MainLoop
  return
contains

  subroutine append(unitx, ucur, val, stat)
    !
    ! unitx ã« val ã‚’ä»˜åŠ ã€‚ãã®éš›ã€unitx ãŒãã®æœ€å¤§æ–‡å—åˆ—é•·ã‚’è¶ŠãˆãŸå ´åˆ
    ! ã«ã¯ stat = 2 ã‚’è¿”ã™ã€‚
    !
    character(*), intent(inout):: unitx ! æœ€çµ‚çš„ã«è¿”ã•ã‚Œã‚‹æ–‡å—åˆ—
    integer,      intent(inout):: ucur ! unitx ã®æ–‡å—æ•°
    character(*), intent(in)   :: val  ! unitx ã«ä»˜åŠ ã•ã‚Œã‚‹æ–‡å—åˆ—
    integer,      intent(out)  :: stat ! ã‚¹ãƒ†ãƒ¼ã‚¿ã‚¹
    integer                    :: wrsz ! val ã®æ–‡å—åˆ—
    continue
    ! unitx ã®æœ€å¤§é•·ã‚’è¶ŠãˆãŸå ´åˆã«ã¯ stat = 2 ã‚’è¿”ã™ã€‚
    if (ucur >= len(unitx)) then
      stat = 2
      ! æ£å¸¸æ™‚ã®å‡¦ç†
    else
      ! unitx ã®é•·ã•ã‚’è¶ŠãˆãŸå ´åˆã‚‚è€ƒæ…®ã—ã¦ unitx ã« val ã‚’ä»˜åŠ ã™ã‚‹ã€‚
      wrsz = min(len(val), len(unitx) - ucur)
      unitx(1+ucur: wrsz+ucur) = val(1: wrsz)
      ucur = ucur + wrsz
      stat = 0
      if (wrsz < len(val)) stat = 1
    endif
  end subroutine append

end subroutine DCStringSPrintf