stdlib_strings_to_string.fypp Source File


This file depends on

sourcefile~~stdlib_strings_to_string.fypp~~EfferentGraph sourcefile~stdlib_strings_to_string.fypp stdlib_strings_to_string.fypp sourcefile~stdlib_strings.fypp stdlib_strings.fypp sourcefile~stdlib_strings_to_string.fypp->sourcefile~stdlib_strings.fypp sourcefile~stdlib_string_type.fypp stdlib_string_type.fypp sourcefile~stdlib_strings.fypp->sourcefile~stdlib_string_type.fypp sourcefile~stdlib_optval.fypp stdlib_optval.fypp sourcefile~stdlib_strings.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_ascii.fypp stdlib_ascii.fypp sourcefile~stdlib_strings.fypp->sourcefile~stdlib_ascii.fypp sourcefile~stdlib_kinds.fypp stdlib_kinds.fypp sourcefile~stdlib_strings.fypp->sourcefile~stdlib_kinds.fypp sourcefile~stdlib_string_type.fypp->sourcefile~stdlib_optval.fypp sourcefile~stdlib_string_type.fypp->sourcefile~stdlib_ascii.fypp sourcefile~stdlib_string_type.fypp->sourcefile~stdlib_kinds.fypp sourcefile~stdlib_optval.fypp->sourcefile~stdlib_kinds.fypp sourcefile~stdlib_ascii.fypp->sourcefile~stdlib_kinds.fypp

Contents


Source Code

#:include "common.fypp"
submodule(stdlib_strings) stdlib_strings_to_string

    integer, parameter :: buffer_len = 128
    character(len=*), parameter :: err_sym = "[*]"
        !!TODO: [*]?

contains

    #:for k1, t1 in REAL_KINDS_TYPES
    !> Format or transfer a ${t1}$ scalar as a string.
    pure module function to_string_${t1[0]}$_${k1}$(value, format) result(string)
        ${t1}$, intent(in) :: value
        character(len=*), intent(in), optional :: format
        character(len=:), allocatable :: string
        
        character(len=buffer_len) :: buffer
        integer :: stat

        write(buffer, '(' // optval(format, "g0") // ')', iostat=stat) value
        if (stat == 0) then
            string = trim(buffer)
        else
            string = err_sym
        end if

    end function to_string_${t1[0]}$_${k1}$
    #:endfor

    #:for k1, t1 in CMPLX_KINDS_TYPES
    !> Format or transfer a ${t1}$ scalar as a string.
    pure module function to_string_${t1[0]}$_${k1}$(value, format) result(string)
        ${t1}$, intent(in) :: value
        character(len=*), intent(in), optional :: format
        character(len=:), allocatable :: string

        string = '(' // to_string_r_${k1}$(value%re, format) // ',' // &
                      & to_string_r_${k1}$(value%im, format) // ')'

    end function to_string_${t1[0]}$_${k1}$
    #:endfor

    #:for k1, t1 in INT_KINDS_TYPES
    !> Represent an integer of kind ${k1}$ as character sequence.
    pure module function to_string_1_${t1[0]}$_${k1}$(value) result(string)
        ${t1}$, intent(in) :: value
        character(len=:), allocatable :: string
        integer, parameter :: buffer_len = range(value)+2
        character(len=buffer_len) :: buffer
        integer :: pos
        ${t1}$ :: n
        character(len=1), parameter :: numbers(0:9) = &
            ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

        if (value == 0_${k1}$) then
            string = numbers(0)
            return
        end if

        n = abs(value)
        buffer = ""

        pos = buffer_len + 1
        do while (n > 0_${k1}$)
            pos = pos - 1
            buffer(pos:pos) = numbers(mod(n, 10_${k1}$))
            n = n/10_${k1}$
        end do
        if (value < 0_${k1}$) then
            pos = pos - 1
            buffer(pos:pos) = '-'
        end if

        string = buffer(pos:)
    end function to_string_1_${t1[0]}$_${k1}$

    pure module function to_string_2_${t1[0]}$_${k1}$(value, format) result(string)
        ${t1}$, intent(in) :: value
        character(len=*), intent(in) :: format
        character(len=:), allocatable :: string
        
        character(len=buffer_len) :: buffer
        integer :: stat

        write(buffer, "(" // format // ")", iostat=stat) value
        if (stat == 0) then
            string = trim(buffer)
        else
            string = err_sym
        end if

    end function to_string_2_${t1[0]}$_${k1}$
    #:endfor

    #:for k1, t1 in LOG_KINDS_TYPES
    !> Represent an logical of kind ${k1}$ as character sequence.
    pure module function to_string_1_${t1[0]}$_${k1}$(value) result(string)
        ${t1}$, intent(in) :: value
        character(len=1) :: string

        string = merge("T", "F", value)

    end function to_string_1_${t1[0]}$_${k1}$

    pure module function to_string_2_${t1[0]}$_${k1}$(value, format) result(string)
        ${t1}$, intent(in) :: value
        character(len=*), intent(in) :: format
        character(len=:), allocatable :: string
        
        character(len=buffer_len) :: buffer
        integer :: stat

        write(buffer, "(" // format // ")", iostat=stat) value
        if (stat == 0) then
            string = trim(buffer)
        else
            string = err_sym
        end if

    end function to_string_2_${t1[0]}$_${k1}$
    #:endfor

end submodule stdlib_strings_to_string