-
Notifications
You must be signed in to change notification settings - Fork 189
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
Changes from 68 commits
Commits
Show all changes
78 commits
Select commit
Hold shift + click to select a range
c0f38d4
Proposal for a reference string to number
jalvesz f0b80ae
Merge branch 'master' of https://github.com/jalvesz/stdlib
jalvesz 94cc5df
str2num: forgot to include file in CMakeLists.txt
jalvesz 001ef27
update interface str2num > to_num( ) using mold
jalvesz 1f44350
Fix: elemental attribute lost in refactoring
jalvesz 90a8b98
hexadecimal def of Inf and NaN
jalvesz ceb0b6c
Mix real64 exponents for reading real32
jalvesz 64dd1c0
parametrized max depth to read number
jalvesz 3ca78a9
Bugfix with Inf and NaN
jalvesz 36ca79a
use ieee_arithmetic for Inf and NaN
jalvesz 3f4e7e3
str2num specs proposal
jalvesz 8c9ba62
to_num quadruple precision
jalvesz 2320766
spec str2num: enrich context information
jalvesz 318e4ac
Update src/stdlib_str2num.f90
jalvesz 7c5de6c
Update src/stdlib_str2num.f90
jalvesz abe20e6
Update src/stdlib_str2num.f90
jalvesz d7dac73
Update src/stdlib_str2num.f90
jalvesz c9fdeee
Update src/stdlib_str2num.f90
jalvesz e73f12a
Update src/stdlib_str2num.f90
jalvesz 79beace
Update src/stdlib_str2num.f90
jalvesz e960436
Update src/stdlib_str2num.f90
jalvesz d5e8696
Update src/stdlib_str2num.f90
jalvesz 798e43d
Update src/stdlib_str2num.f90
jalvesz e0b8da0
Update src/stdlib_str2num.f90
jalvesz 560f1da
Update src/stdlib_str2num.f90
jalvesz e0fa769
Update doc/specs/stdlib_str2num.md
jalvesz 7c72b5c
Update doc/specs/stdlib_str2num.md
jalvesz 9ee4c36
Update doc/specs/stdlib_str2num.md
jalvesz a3df7b6
Support of fypp for stdlib_str2num
jvdp1 ea6560b
Update doc/specs/stdlib_str2num.md
jalvesz ea2ee72
add fypp directives for qp
jvdp1 1585c10
addition of fypp directives for some integer procedures
jvdp1 16ce4fb
Addition of fypp directives for some integer procedures
jvdp1 ad0bb71
fix issue
jvdp1 17d3dae
support of fypp for test_to_number
jvdp1 d9ece9c
Merge pull request #1 from jvdp1/fypp_qp
jalvesz 0b119a2
Update example/strings/example_str2num.f90
jalvesz 3029f8b
Update test/string/test_string_to_number.fypp
jalvesz 4aa28ee
Update test/string/test_string_to_number.fypp
jalvesz 688c3a3
Update test/string/test_string_to_number.fypp
jalvesz 1824763
Update src/stdlib_str2num.fypp
jalvesz a18f358
Update doc/specs/stdlib_str2num.md
jalvesz 57c3e19
Update src/stdlib_str2num.fypp
jalvesz 6c20f80
Update src/stdlib_str2num.fypp
jalvesz c630fd7
Update src/stdlib_str2num.fypp
jalvesz 53cb17e
Update src/stdlib_str2num.fypp
jalvesz 50174a4
Update src/stdlib_str2num.fypp
jalvesz 1eb073f
Update doc/specs/stdlib_str2num.md
jalvesz e840761
Update doc/specs/stdlib_str2num.md
jalvesz 96ef394
Update test/string/test_string_to_number.fypp
jalvesz 0929792
Update doc/specs/stdlib_str2num.md
jalvesz 07389f7
Apply suggestions from code review
jalvesz 62f055a
Update doc/specs/stdlib_str2num.md
jalvesz 2274af7
Update stdlib_str2num.md
jalvesz 1e7a191
Merge branch 'fortran-lang:master' into master
jalvesz 2954856
Update doc/specs/stdlib_str2num.md
jalvesz 6fefa64
Update doc/specs/stdlib_str2num.md
jalvesz ab8c7da
Update doc/specs/stdlib_str2num.md
jalvesz 327a84f
Update stdlib_str2num.fypp
jalvesz ad6cd9a
Update stdlib_str2num.fypp
jalvesz b00895a
fix build with fpm-deployment.sh and fypp
jalvesz 94bf4a8
rewrite test_string_to_number using fypp for real kinds
jalvesz 5d380a0
add support for extended double precision
jalvesz 03c1693
enable testing for xdp
jalvesz 03b4d60
missing xdp kind declaration
jalvesz 44ecace
include xdp test to testsuite
jalvesz c357fa2
Update doc/specs/stdlib_str2num.md
jalvesz cbc05b2
Update doc/specs/stdlib_str2num.md
jalvesz 026a22c
Update src/stdlib_str2num.fypp
jalvesz acefb99
Update src/stdlib_str2num.fypp
jalvesz 89f0015
Update src/stdlib_str2num.fypp
jalvesz fbdf870
autodoc and examples for str2num improvement
jalvesz cfb95da
cmake build str2num examples
jalvesz e45b884
fix fpm processing for maxrank=4
jalvesz 62f2d0b
Merge branch 'master' into master
jalvesz 7022d83
rollback prune command in fpm deployment
jalvesz fa8bdad
rename functions, remove semicolons
jalvesz 3b50f6a
add some white spaces
jalvesz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
--- | ||
title: str2num | ||
--- | ||
|
||
# The `stdlib_str2num` module | ||
|
||
This module proposes a function-style interface for string-to-number conversion. It also profits from Fortran's interfaces to implement precision-dependant algorithms to maximize runtime efficiency. | ||
|
||
[TOC] | ||
|
||
## `to_num` - conversion of strings to numbers | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
Convert a string or an array of strings to numerical types. | ||
|
||
### Syntax | ||
|
||
`number = [[stdlib_str2num(module):to_num(interface)]](string, mold)` | ||
|
||
### Arguments | ||
|
||
`string`: argument has `intent(in)` and is of type `character(*)`. | ||
|
||
`mold`: argument has `intent(in)` and is of numerical type (that is of `integer` or of `real`). **Note**: The type of the `mold` argument defines the type of the result. | ||
|
||
### Return value | ||
|
||
Return a scalar of numerical type (i.e., `integer`, or `real`). | ||
|
||
### Example | ||
|
||
```fortran | ||
program example_string_to_number | ||
use stdlib_kinds, only: dp | ||
use stdlib_str2num, only: to_num | ||
implicit none | ||
character(:), allocatable :: txt | ||
real(dp) :: x | ||
|
||
txt = ' 8.8541878128e−12 ' | ||
x = to_num( txt , x ) | ||
end program example_string_to_number | ||
jalvesz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
## `to_num_p` - conversion of a stream of values in a string to numbers | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
Convert a stream of values in a string to an array of values. | ||
|
||
### Syntax | ||
|
||
`number = [[stdlib_str2num(module):to_num_p(interface)]](string, mold)` | ||
jalvesz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Arguments | ||
|
||
`string`: argument has `intent(in)` and is of type `character(:), pointer`. | ||
|
||
`mold`: argument has `intent(in)` and is of numerical type (currently of `integer` or `real`). **Note**: The type of the `mold` argument defines the type of the result. | ||
|
||
### Return value | ||
|
||
Return a scalar of numerical type (i.e., `integer` or `real`). | ||
|
||
### Example | ||
|
||
```fortran | ||
program example_to_num_p | ||
use stdlib_kinds, only: dp | ||
use stdlib_str2num, only: to_num_p | ||
character(:), allocatable, target :: chain | ||
character(len=:), pointer :: cptr | ||
real(dp) :: r(6) | ||
integer :: i | ||
|
||
chain = " 1.234 1.E1 1e0 0.1234E0 12.21e+001 -34.5E1" | ||
cptr => chain | ||
do i = 1, 6 | ||
r(i) = to_num_p( cptr , r(i) ) !> the cptr pointer is shifted within the function | ||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end do | ||
end program example_to_num_p | ||
``` | ||
|
||
## Note | ||
The accuracy of the conversion is implementation dependent; it is recommended that implementers guarantee precision down to the last 3 bits. | ||
|
||
**The current implementation has been tested to provide for** : | ||
|
||
`sp` : exact match | ||
|
||
`dp` : precision up-to epsilon(0.0_dp) | ||
|
||
`qp` : precision around 200*epsilon(0.0_qp) | ||
|
||
Where precision refers to the relative difference between `to_num` and `read`. On the other hand, `to_num` provides speed-ups ranging from 4x to >10x compared to the intrinsic `read`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ ADD_EXAMPLE(starts_with) | |
ADD_EXAMPLE(strip) | ||
ADD_EXAMPLE(to_string) | ||
ADD_EXAMPLE(zfill) | ||
ADD_EXAMPLE(str2num) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
program example_str2num | ||
use stdlib_kinds, only: dp | ||
use stdlib_str2num, only: to_num_p | ||
character(:), allocatable, target :: chain | ||
character(len=:), pointer :: cptr | ||
real(dp), allocatable :: r(:), p(:) | ||
integer :: i | ||
|
||
chain = " 1.234 1.E1 1e0 0.1234E0 12.21e+001 -34.5E1" | ||
allocate( r(6), p(6) ) | ||
!> Example for streamline conversion using `to_num_p` | ||
cptr => chain | ||
do i =1, 6 | ||
r(i) = to_num_p( cptr , r(i) ) !> the pointer is shifted within the function | ||
end do | ||
read(chain,*) p | ||
print *, "Reading with to_num" | ||
print *, r | ||
print *, "Reading with formatted read" | ||
print *, p | ||
|
||
end program |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.