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

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

Merged
merged 78 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
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 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
8 changes: 7 additions & 1 deletion ci/fpm-deployment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ destdir="${DESTDIR:-stdlib-fpm}"
fypp="${FYPP:-$(which fypp)}"

# Arguments for the fypp preprocessor
fyflags="${FYFLAGS:--DMAXRANK=4}"
fyflags="${FYFLAGS:--DMAXRANK=8}"
jalvesz marked this conversation as resolved.
Show resolved Hide resolved

# Number of parallel jobs for preprocessing
if [ $(uname) = "Darwin" ]; then
Expand Down Expand Up @@ -45,6 +45,12 @@ mkdir -p "$destdir/src" "$destdir/test" "$destdir/example"
find src -maxdepth 1 -iname "*.fypp" \
| cut -f1 -d. | xargs -P "$njob" -I{} "$fypp" "{}.fypp" "$destdir/{}.f90" $fyflags

find test -name "test_*.fypp" -exec cp {} "$destdir/test/" \;
fyflags="${fyflags} -I src"
find $destdir/test -maxdepth 1 -iname "*.fypp" \
| cut -f1 -d. | xargs -P "$njob" -I{} "$fypp" "{}.fypp" "{}.f90" $fyflags
find $destdir/test -name "test_*.fypp" -exec rm {} \;

# Collect stdlib source files
find src -maxdepth 1 -iname "*.f90" -exec cp {} "$destdir/src/" \;
find test -name "test_*.f90" -exec cp {} "$destdir/test/" \;
Expand Down
104 changes: 104 additions & 0 deletions doc/specs/stdlib_str2num.md
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`.
1 change: 1 addition & 0 deletions example/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ ADD_EXAMPLE(starts_with)
ADD_EXAMPLE(strip)
ADD_EXAMPLE(to_string)
ADD_EXAMPLE(zfill)
ADD_EXAMPLE(str2num)
22 changes: 22 additions & 0 deletions example/strings/example_str2num.f90
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(fppFiles
stdlib_math_is_close.fypp
stdlib_math_all_close.fypp
stdlib_math_diff.fypp
stdlib_str2num.fypp
stdlib_string_type.fypp
stdlib_string_type_constructor.fypp
stdlib_strings_to_string.fypp
Expand Down
Loading
Loading