Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to format variable as string #435

Closed
awvwgk opened this issue Jun 13, 2021 · 5 comments
Closed

Function to format variable as string #435

awvwgk opened this issue Jun 13, 2021 · 5 comments
Labels
easy Difficulty level is easy and good for starting into this project idea Proposition of an idea and opening an issue to discuss it topic: utilities containers, strings, files, OS/environment integration, unit testing, assertions, logging, ...

Comments

@awvwgk
Copy link
Member

awvwgk commented Jun 13, 2021

We can already format integer and logical variables to strings, but we don't have routines to write floating point values yet. Maybe we can implement a simple solution by wrapping the internal IO in a function as a separate module. The size of the buffer might be problematic since it can overflow in case a format specifier like '(10000x, g0)' is passed.

The actual implementation is pretty straight-forward:

! SPDX-Identifier: MIT
#:include "common.fypp"
#:set KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + LOG_KINDS_TYPES

!> Routines for dealing with format specifiers
module stdlib_format
    use stdlib_kinds, only : sp, dp, qp, int8, int16, int32, int64, lk, c_bool
    implicit none
    private
    public :: format_string

    !> Create a character string representing the value of the provided variable.
    interface format_string
    #:for type, kind in KINDS_TYPES
        module procedure :: format_string_${type[0]}$${kind}$
    #:endfor
    end interface format_string

contains

    #:for type, kind in KINDS_TYPES
    !> Format ${type}$ variable as character sequence
    pure function format_string_${type[0]}$${kind}$(val, format) result(string)
        integer, parameter :: ik = ${kind}$
        ${type}$, intent(in) :: val
        character(len=*), intent(in) :: format
        character(len=:), allocatable :: string
        integer, parameter :: buffer_len = 512
        character(len=buffer_len) :: buffer

        write(buffer, format, iostat=stat) val
        if (stat == 0) then
            string = trim(buffer)
        else
            string = "*"
        end if
    end function format_string_${type[0]}${kind}$
    #:endfor

end module stdlib_format
@awvwgk awvwgk added topic: utilities containers, strings, files, OS/environment integration, unit testing, assertions, logging, ... idea Proposition of an idea and opening an issue to discuss it easy Difficulty level is easy and good for starting into this project labels Jun 13, 2021
@zoziha
Copy link
Contributor

zoziha commented Jun 24, 2021

Hello, if my num2str can be combined with your code, especially the complex type num2str:
(see #337 (comment))

 forlab_num2str.fypp :
 ---------------------- 
 num2str(c):
 (1.00000000,1.00000000)
 (1.00,1.00)
 num2str(i):
 2
 2
 num2str(r):
 1.00000000
 1.00
 forlab_disp.fypp :
 ----------------------
 disp(ones(c)):
          (1.000,1.000)           (1.000,1.000)
          (1.000,1.000)           (1.000,1.000)
 disp(ones(i)):
          1           1
          1           1
 disp(ones(r)):
  1.000       1.000
  1.000       1.000
 disp(l)
          T           T           T
          T           T           T
 ----------------------
 disp(ones(c)):
     (0.1000E-03,0.000)           (1.000,1.000)
          (1.000,1.000)           (1.000,1.000)
 disp(ones(i)):
          1           1
          1           1
 disp(ones(r)):
 0.1000E-03   0.000
  1.000       1.000

@awvwgk
Copy link
Member Author

awvwgk commented Jun 25, 2021

This is great @zoziha, I think the display function would make a great addition to stdlib.

@zoziha
Copy link
Contributor

zoziha commented Jun 25, 2021

disp

Wow, thank you for your approval, I think I will consider perfecting disp routines for stdlib later. If you have any good suggestions, please feel free to put forward them.

num2str?

I think we have implemented the num2str function to a certain extent, and need to add a logical type, which overlaps to a certain extent with your format_string. What do you think is better for this routine name: num2str, string, format_string, to_chars?
Yes, I also want to complete this routine in stdlib, and a little num2str will be used in the disp routine.

function:   num2str(x [, fmt])
subroutine: disp(x [, string])

Hope to receive your reply.

@awvwgk
Copy link
Member Author

awvwgk commented Jun 25, 2021

I think the implementation is uncritical, num2str and format_string are implemented quite similarly. I think it will need more work to reach an agreement on the naming of the routine.

If you are interested, feel free to use the format_string template in your forlab project or to propose the routine to stdlib.

@zoziha
Copy link
Contributor

zoziha commented Jun 25, 2021

Hello, I submitted a PR, which contains disp and format_string, I am not sure whether it is reasonable, welcome to discuss. (see #445)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy Difficulty level is easy and good for starting into this project idea Proposition of an idea and opening an issue to discuss it topic: utilities containers, strings, files, OS/environment integration, unit testing, assertions, logging, ...
Projects
None yet
Development

No branches or pull requests

2 participants