Skip to content

Proposal for a reference string to number conversion facility in stdlib #743

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

Merged
merged 78 commits into from
Jan 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
c0f38d4
Proposal for a reference string to number
jalvesz Oct 31, 2023
f0b80ae
Merge branch 'master' of https://github.com/jalvesz/stdlib
jalvesz Oct 31, 2023
94cc5df
str2num: forgot to include file in CMakeLists.txt
jalvesz Nov 1, 2023
001ef27
update interface str2num > to_num( ) using mold
jalvesz Nov 2, 2023
1f44350
Fix: elemental attribute lost in refactoring
jalvesz Nov 2, 2023
90a8b98
hexadecimal def of Inf and NaN
jalvesz Nov 2, 2023
ceb0b6c
Mix real64 exponents for reading real32
jalvesz Nov 3, 2023
64dd1c0
parametrized max depth to read number
jalvesz Nov 3, 2023
3ca78a9
Bugfix with Inf and NaN
jalvesz Nov 3, 2023
36ca79a
use ieee_arithmetic for Inf and NaN
jalvesz Nov 23, 2023
3f4e7e3
str2num specs proposal
jalvesz Dec 18, 2023
8c9ba62
to_num quadruple precision
jalvesz Dec 18, 2023
2320766
spec str2num: enrich context information
jalvesz Dec 19, 2023
318e4ac
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
7c5de6c
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
abe20e6
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
d7dac73
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
c9fdeee
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
e73f12a
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
79beace
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
e960436
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
d5e8696
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
798e43d
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
e0b8da0
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
560f1da
Update src/stdlib_str2num.f90
jalvesz Dec 26, 2023
e0fa769
Update doc/specs/stdlib_str2num.md
jalvesz Dec 26, 2023
7c72b5c
Update doc/specs/stdlib_str2num.md
jalvesz Dec 26, 2023
9ee4c36
Update doc/specs/stdlib_str2num.md
jalvesz Dec 26, 2023
a3df7b6
Support of fypp for stdlib_str2num
jvdp1 Dec 26, 2023
ea6560b
Update doc/specs/stdlib_str2num.md
jalvesz Dec 26, 2023
ea2ee72
add fypp directives for qp
jvdp1 Dec 26, 2023
1585c10
addition of fypp directives for some integer procedures
jvdp1 Dec 26, 2023
16ce4fb
Addition of fypp directives for some integer procedures
jvdp1 Dec 26, 2023
ad0bb71
fix issue
jvdp1 Dec 26, 2023
17d3dae
support of fypp for test_to_number
jvdp1 Dec 26, 2023
d9ece9c
Merge pull request #1 from jvdp1/fypp_qp
jalvesz Dec 31, 2023
0b119a2
Update example/strings/example_str2num.f90
jalvesz Dec 31, 2023
3029f8b
Update test/string/test_string_to_number.fypp
jalvesz Dec 31, 2023
4aa28ee
Update test/string/test_string_to_number.fypp
jalvesz Dec 31, 2023
688c3a3
Update test/string/test_string_to_number.fypp
jalvesz Dec 31, 2023
1824763
Update src/stdlib_str2num.fypp
jalvesz Dec 31, 2023
a18f358
Update doc/specs/stdlib_str2num.md
jalvesz Dec 31, 2023
57c3e19
Update src/stdlib_str2num.fypp
jalvesz Dec 31, 2023
6c20f80
Update src/stdlib_str2num.fypp
jalvesz Dec 31, 2023
c630fd7
Update src/stdlib_str2num.fypp
jalvesz Dec 31, 2023
53cb17e
Update src/stdlib_str2num.fypp
jalvesz Dec 31, 2023
50174a4
Update src/stdlib_str2num.fypp
jalvesz Dec 31, 2023
1eb073f
Update doc/specs/stdlib_str2num.md
jalvesz Dec 31, 2023
e840761
Update doc/specs/stdlib_str2num.md
jalvesz Jan 1, 2024
96ef394
Update test/string/test_string_to_number.fypp
jalvesz Jan 1, 2024
0929792
Update doc/specs/stdlib_str2num.md
jalvesz Jan 1, 2024
07389f7
Apply suggestions from code review
jalvesz Jan 1, 2024
62f055a
Update doc/specs/stdlib_str2num.md
jalvesz Jan 1, 2024
2274af7
Update stdlib_str2num.md
jalvesz Jan 1, 2024
1e7a191
Merge branch 'fortran-lang:master' into master
jalvesz Jan 1, 2024
2954856
Update doc/specs/stdlib_str2num.md
jalvesz Jan 1, 2024
6fefa64
Update doc/specs/stdlib_str2num.md
jalvesz Jan 1, 2024
ab8c7da
Update doc/specs/stdlib_str2num.md
jalvesz Jan 1, 2024
327a84f
Update stdlib_str2num.fypp
jalvesz Jan 1, 2024
ad6cd9a
Update stdlib_str2num.fypp
jalvesz Jan 1, 2024
b00895a
fix build with fpm-deployment.sh and fypp
jalvesz Jan 1, 2024
94bf4a8
rewrite test_string_to_number using fypp for real kinds
jalvesz Jan 1, 2024
5d380a0
add support for extended double precision
jalvesz Jan 1, 2024
03c1693
enable testing for xdp
jalvesz Jan 2, 2024
03b4d60
missing xdp kind declaration
jalvesz Jan 2, 2024
44ecace
include xdp test to testsuite
jalvesz Jan 2, 2024
c357fa2
Update doc/specs/stdlib_str2num.md
jalvesz Jan 5, 2024
cbc05b2
Update doc/specs/stdlib_str2num.md
jalvesz Jan 5, 2024
026a22c
Update src/stdlib_str2num.fypp
jalvesz Jan 7, 2024
acefb99
Update src/stdlib_str2num.fypp
jalvesz Jan 7, 2024
89f0015
Update src/stdlib_str2num.fypp
jalvesz Jan 7, 2024
fbdf870
autodoc and examples for str2num improvement
jalvesz Jan 7, 2024
cfb95da
cmake build str2num examples
jalvesz Jan 7, 2024
e45b884
fix fpm processing for maxrank=4
jalvesz Jan 7, 2024
62f2d0b
Merge branch 'master' into master
jalvesz Jan 9, 2024
7022d83
rollback prune command in fpm deployment
jalvesz Jan 9, 2024
fa8bdad
rename functions, remove semicolons
jalvesz Jan 15, 2024
3b50f6a
add some white spaces
jalvesz Jan 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add support for extended double precision
jalvesz committed Jan 1, 2024
commit 5d380a05237f0b48fe5d1ff7bc90bff247529873
117 changes: 106 additions & 11 deletions src/stdlib_str2num.fypp
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
!> difference rel : 0.3300E-029%

module stdlib_str2num
use stdlib_kinds, only: sp, dp, qp, int8, int16, int32, int64
use stdlib_kinds, only: sp, dp, xdp, qp, int8, int16, int32, int64
use, intrinsic:: ieee_arithmetic, only: ieee_value, ieee_positive_inf, ieee_quiet_nan
implicit none
private
@@ -44,25 +44,19 @@ module stdlib_str2num

interface to_num
#:for k1, t1 in (INT_KINDS_TYPES + REAL_KINDS_TYPES)
#:if k1 != "xdp"
module procedure to_${k1}$
#:endif
#:endfor
end interface

interface to_num_p
#:for k1, t1 in (INT_KINDS_TYPES + REAL_KINDS_TYPES)
#:if k1 != "xdp"
module procedure to_${k1}$_p
#:endif
#:endfor
end interface

interface to_num_base
#:for k1, t1 in (INT_KINDS_TYPES + REAL_KINDS_TYPES)
#:if k1 != "xdp"
module procedure to_${k1}$_base
#:endif
#:endfor
end interface

@@ -73,7 +67,6 @@ module stdlib_str2num
!---------------------------------------------

#:for k1, t1 in (INT_KINDS_TYPES + REAL_KINDS_TYPES)
#:if k1 != "xdp"
elemental function to_${k1}$(s,mold) result(v)
! -- In/out Variables
character(*), intent(in) :: s !> input string
@@ -102,14 +95,12 @@ module stdlib_str2num
if(present(stat)) stat = err
end function

#:endif
#:endfor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have an interface to a shallow wrapper that takes string_type as input. Better integration with the rest of stdlib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't used the string_type from stdlib so didn't think about that. Maybe after merging the PR we could take a look at that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, since it's an additional feature, can be a future PR.

!---------------------------------------------
! String To Number Implementations
!---------------------------------------------

#:for k1, t1 in INT_KINDS_TYPES
#:if k1 != "xdp"
elemental subroutine to_${k1}$_base(s,v,p,stat)
!> Return an unsigned 32-bit integer
! -- In/out Variables
@@ -137,7 +128,6 @@ module stdlib_str2num
stat = 0
end subroutine

#:endif
#:endfor

elemental subroutine to_sp_base(s,v,p,stat)
@@ -331,6 +321,111 @@ module stdlib_str2num
stat = 0
end subroutine

#:if WITH_XDP
elemental subroutine to_xdp_base(s,v,p,stat)
integer, parameter :: wp = xdp
!> Sequentially unroll the character and get the sub integers composing the whole number, fraction and exponent
! -- In/out Variables
character(*), intent(in) :: s !> input string
real(wp), intent(inout) :: v !> Output real value
integer(int8), intent(out) :: p !> last position within the string
integer(int8), intent(out) :: stat !> status upon success or failure to read

! -- Internal Variables
integer(int8), parameter :: nwnb = 50 !> number of whole number factors
integer(int8), parameter :: nfnb = 64 !> number of fractional number factors
integer :: e
real(wp), parameter :: whole_number_base(nwnb) = [(10._wp**(nwnb-e),e=1,nwnb)]
real(wp), parameter :: fractional_base(nfnb) = [(10._wp**(-e),e=1,nfnb)]
real(wp), parameter :: expbase(nwnb+nfnb) = [whole_number_base, fractional_base]

integer(int8) :: sign, sige !> sign of integer number and exponential
integer, parameter :: maxdpt = 19 !> Maximum depth to read values on int_dp
integer(dp) :: int_dp1, int_dp2 !> long integers to capture whole and fractional part
integer :: i_exp !> integer to capture exponent number
integer :: exp_aux
integer(int8) :: i, pP, pE, val , resp, icount, aux
!----------------------------------------------
stat = 23 !> initialize error status with any number > 0
!----------------------------------------------
! Find first non white space
p = mvs2nwsp(s)
!----------------------------------------------
! Verify leading negative
sign = 1
if( iachar(s(p:p)) == minus_sign+digit_0 ) then
sign = -1 ; p = p + 1
end if
if( iachar(s(p:p)) == Inf ) then
v = sign*ieee_value(v, ieee_positive_inf); return
else if( iachar(s(p:p)) == NaN ) then
v = ieee_value(v, ieee_quiet_nan); return
end if
!----------------------------------------------
! read whole and fractional number using two int64 values
pP = 127
int_dp1 = 0; int_dp2 = 0; icount = 0; aux = 1
do i = p, min(2*maxdpt+p-1,len(s))
val = iachar(s(i:i))-digit_0
if( val >= 0 .and. val <= 9 ) then
icount = icount + 1
if(icount<=maxdpt)then
int_dp1 = int_dp1*10 + val
else if(icount<2*maxdpt)then
int_dp2 = int_dp2*10 + val
end if
else if( val == period ) then
pP = i; aux = 0
else
exit
end if
end do
pE = i ! Fix the exponent position
do while( i<=len(s) )
val = iachar(s(i:i))-digit_0
if( val < 0 .or. val > 9 ) exit
i = i + 1
end do
p = i
resp = pE-min(pP,p) ! If no decimal indicator found it is taken as being in the current p position
if( resp <= 0 ) resp = resp+1
!----------------------------------------------
! Get exponential
sige = 1
if( p<len(s) ) then
if( any([le,BE,ld,BD]+digit_0==iachar(s(p:p))) ) p = p + 1
if( iachar(s(p:p)) == minus_sign+digit_0 ) then
sige = -1
p = p + 1
else if( iachar(s(p:p)) == plus_sign+digit_0 ) then
p = p + 1
end if
end if

i_exp = 0
do while( p<=len(s) )
val = iachar(s(p:p))-digit_0
if( val >= 0 .and. val <= 9) then
i_exp = i_exp*10_int8 + val ; p = p + 1
else
exit
end if
end do

exp_aux = nwnb-1+resp-sige*i_exp
if( exp_aux>0 .and. exp_aux<=nwnb+nfnb) then
if(icount<=maxdpt)then
v = sign*int_dp1*expbase(exp_aux)
else
v = sign*(int_dp1 + int_dp2*fractional_base(maxdpt-1))*expbase(exp_aux-icount+maxdpt)
end if
else
v = sign*(int_dp1 + int_dp2*fractional_base(maxdpt-1))*10._wp**(sige*i_exp-resp+maxdpt+aux)
end if
stat = 0
end subroutine
#:endif

#:if WITH_QP
elemental subroutine to_qp_base(s,v,p,stat)
integer, parameter :: wp = qp