-
Notifications
You must be signed in to change notification settings - Fork 134
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
Extend string
interface in fms_string_utils_mod
#1142
Changes from 14 commits
f74cd34
6ffcd21
8234fd0
519187e
44ec552
789c6f5
a9a3a28
da07356
6893d4a
69d554e
dc808d4
9370d6a
6ecd9c7
9aec801
16356ac
22ae591
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
!> @{ | ||
module fms_string_utils_mod | ||
use, intrinsic :: iso_c_binding | ||
use platform_mod, only: r4_kind, r8_kind | ||
use mpp_mod | ||
|
||
implicit none | ||
|
@@ -43,6 +44,7 @@ module fms_string_utils_mod | |
public :: fms_cstring2cpointer | ||
public :: string | ||
public :: string_copy | ||
public :: stringify | ||
!> @} | ||
|
||
interface | ||
|
@@ -112,11 +114,12 @@ subroutine c_free(ptr) bind(c,name="free") | |
module procedure cpointer_fortran_conversion | ||
end interface | ||
|
||
!> Converts a number to a string | ||
!> Converts an array of real numbers to a string | ||
!> @ingroup fms_mod | ||
interface string | ||
module procedure string_from_integer | ||
module procedure string_from_real | ||
interface stringify | ||
module procedure stringify_1d_r4, stringify_1d_r8 | ||
module procedure stringify_2d_r4, stringify_2d_r8 | ||
module procedure stringify_3d_r4, stringify_3d_r8 | ||
end interface | ||
|
||
!> @addtogroup fms_string_utils_mod | ||
|
@@ -237,31 +240,51 @@ subroutine fms_f2c_string (dest, str_in) | |
enddo | ||
end subroutine fms_f2c_string | ||
|
||
|
||
!> @brief Converts an integer to a string | ||
!> @return The integer as a string | ||
function string_from_integer(i) result (res) | ||
integer, intent(in) :: i !< Integer to be converted to a string | ||
character(:),allocatable :: res !< String converted frominteger | ||
character(range(i)+2) :: tmp !< Temp string that is set to correct size | ||
write(tmp,'(i0)') i | ||
res = trim(tmp) | ||
return | ||
|
||
end function string_from_integer | ||
|
||
!####################################################################### | ||
!> @brief Converts a real to a string | ||
!> @return The real number as a string | ||
function string_from_real(r) | ||
real, intent(in) :: r !< Real number to be converted to a string | ||
character(len=32) :: string_from_real | ||
|
||
write(string_from_real,*) r | ||
|
||
return | ||
|
||
end function string_from_real | ||
!> @brief Converts a number or a Boolean value to a string | ||
!> @return The argument as a string | ||
function string(v, fmt) | ||
class(*), intent(in) :: v ! Value to be converted to a string | ||
character(*), intent(in), optional :: fmt !< Optional format string for a real argument | ||
character(:), allocatable :: string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's always being allocated to a length of 32, why not just make this a len=32 string instead of allocatable? I'm not saying you have to do this, I'm just wondering if you think that's a better option. That would also make this closer to being a pure/elemental function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By making it |
||
|
||
select type(v) | ||
type is (logical) | ||
if (v) then | ||
string = "True" | ||
else | ||
string = "False" | ||
endif | ||
|
||
type is (integer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 16356ac. |
||
allocate(character(32) :: string) | ||
write(string, '(i0)') v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the user provides a format statement for an integer or a logical or a string, it will be unused. This could produce unexpected behavior for the user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 16356ac via a warning message (in the case of a logical value) and by using |
||
string = trim(adjustl(string)) | ||
|
||
type is (real(r4_kind)) | ||
allocate(character(32) :: string) | ||
if (present(fmt)) then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idk if you can check this, but how will you know that the user's format will work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
write(string, "(" // fmt // ")") v | ||
else | ||
write(string, *) v | ||
endif | ||
string = trim(adjustl(string)) | ||
|
||
type is (real(r8_kind)) | ||
allocate(character(32) :: string) | ||
if (present(fmt)) then | ||
write(string, "(" // fmt // ")") v | ||
else | ||
write(string, *) v | ||
endif | ||
string = trim(adjustl(string)) | ||
|
||
type is (character(*)) | ||
string = v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should avoid this. The class(*) string handling is inconsistent across compilers and this will only lead to problems. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed in 16356ac. |
||
|
||
class default | ||
call mpp_error(FATAL, "string() called with incompatible argument") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this check was not writing to stdout, then this might be able to be an elemental function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you keep the error message, it should be more comprehensive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 16356ac. |
||
end select | ||
end function | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please put the name of the function here
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 16356ac. |
||
!> @brief Safely copy a string from one buffer to another. | ||
subroutine string_copy(dest, source, check_for_null) | ||
|
@@ -290,6 +313,9 @@ subroutine string_copy(dest, source, check_for_null) | |
dest = adjustl(trim(source(1:i))) | ||
end subroutine string_copy | ||
|
||
#include "fms_string_utils_r4.fh" | ||
#include "fms_string_utils_r8.fh" | ||
|
||
end module fms_string_utils_mod | ||
!> @} | ||
! close documentation grouping |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
!*********************************************************************** | ||
!* GNU Lesser General Public License | ||
!* | ||
!* This file is part of the GFDL Flexible Modeling System (FMS). | ||
!* | ||
!* FMS 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 3 of the License, or (at | ||
!* your option) any later version. | ||
!* | ||
!* FMS 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 General Public License | ||
!* for more details. | ||
!* | ||
!* You should have received a copy of the GNU Lesser General Public | ||
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>. | ||
!*********************************************************************** | ||
|
||
!> @brief Converts a 1D array of real numbers to a string | ||
!> @return The 1D array as a string | ||
function STRINGIFY_1D_(arr, fmt) | ||
real(STRING_UTILS_KIND_), dimension(:), intent(in) :: arr !< Real array to be converted to a string | ||
character(*), intent(in), optional :: fmt !< Optional format string for the real array entries | ||
character(:), allocatable :: STRINGIFY_1D_ | ||
integer :: i, n | ||
|
||
n = size(arr) | ||
|
||
if (n .gt. 0) then | ||
STRINGIFY_1D_ = "[" // string(arr(1), fmt) | ||
else | ||
STRINGIFY_1D_ = "[" | ||
endif | ||
|
||
do i = 2,n | ||
STRINGIFY_1D_ = STRINGIFY_1D_ // ", " // string(arr(i), fmt) | ||
enddo | ||
|
||
STRINGIFY_1D_ = STRINGIFY_1D_ // "]" | ||
end function | ||
|
||
!> @brief Converts a 2D array of real numbers to a string | ||
!> @return The 2D array as a string | ||
function STRINGIFY_2D_(arr, fmt) | ||
real(STRING_UTILS_KIND_), dimension(:,:), intent(in) :: arr !< Real array to be converted to a string | ||
character(*), intent(in), optional :: fmt !< Optional format string for the real array entries | ||
character(:), allocatable :: STRINGIFY_2D_ | ||
integer :: i, n | ||
|
||
n = size(arr, 2) | ||
|
||
if (n .gt. 0) then | ||
STRINGIFY_2D_ = "[" // STRINGIFY_1D_(arr(:,1), fmt) | ||
else | ||
STRINGIFY_2D_ = "[" | ||
endif | ||
|
||
do i = 2,n | ||
STRINGIFY_2D_ = STRINGIFY_2D_ // ", " // STRINGIFY_1D_(arr(:,i), fmt) | ||
enddo | ||
|
||
STRINGIFY_2D_ = STRINGIFY_2D_ // "]" | ||
end function | ||
|
||
!> @brief Converts a 3D array of real numbers to a string | ||
!> @return The 3D array as a string | ||
function STRINGIFY_3D_(arr, fmt) | ||
real(STRING_UTILS_KIND_), dimension(:,:,:), intent(in) :: arr !< Real array to be converted to a string | ||
character(*), intent(in), optional :: fmt !< Optional format string for the real array entries | ||
character(:), allocatable :: STRINGIFY_3D_ | ||
integer :: i, n | ||
|
||
n = size(arr, 3) | ||
|
||
if (n .gt. 0) then | ||
STRINGIFY_3D_ = "[" // STRINGIFY_2D_(arr(:,:,1), fmt) | ||
else | ||
STRINGIFY_3D_ = "[" | ||
endif | ||
|
||
do i = 2,n | ||
STRINGIFY_3D_ = STRINGIFY_3D_ // ", " // STRINGIFY_2D_(arr(:,:,i), fmt) | ||
enddo | ||
|
||
STRINGIFY_3D_ = STRINGIFY_3D_ // "]" | ||
end function |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
!*********************************************************************** | ||
!* GNU Lesser General Public License | ||
!* | ||
!* This file is part of the GFDL Flexible Modeling System (FMS). | ||
!* | ||
!* FMS 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 3 of the License, or (at | ||
!* your option) any later version. | ||
!* | ||
!* FMS 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 General Public License | ||
!* for more details. | ||
!* | ||
!* You should have received a copy of the GNU Lesser General Public | ||
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>. | ||
!*********************************************************************** | ||
|
||
#define STRING_UTILS_KIND_ r4_kind | ||
#define STRINGIFY_1D_ stringify_1d_r4 | ||
#define STRINGIFY_2D_ stringify_2d_r4 | ||
#define STRINGIFY_3D_ stringify_3d_r4 | ||
|
||
#include "fms_string_utils.inc" | ||
|
||
#undef STRING_UTILS_KIND_ | ||
#undef STRINGIFY_1D_ | ||
#undef STRINGIFY_2D_ | ||
#undef STRINGIFY_3D_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
!*********************************************************************** | ||
!* GNU Lesser General Public License | ||
!* | ||
!* This file is part of the GFDL Flexible Modeling System (FMS). | ||
!* | ||
!* FMS 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 3 of the License, or (at | ||
!* your option) any later version. | ||
!* | ||
!* FMS 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 General Public License | ||
!* for more details. | ||
!* | ||
!* You should have received a copy of the GNU Lesser General Public | ||
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>. | ||
!*********************************************************************** | ||
|
||
#define STRING_UTILS_KIND_ r8_kind | ||
#define STRINGIFY_1D_ stringify_1d_r8 | ||
#define STRINGIFY_2D_ stringify_2d_r8 | ||
#define STRINGIFY_3D_ stringify_3d_r8 | ||
|
||
#include "fms_string_utils.inc" | ||
|
||
#undef STRING_UTILS_KIND_ | ||
#undef STRINGIFY_1D_ | ||
#undef STRINGIFY_2D_ | ||
#undef STRINGIFY_3D_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable is not correctly doxygenized
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 16356ac.