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

implemented intelligent slice functionality #414

Merged
merged 19 commits into from
Jun 12, 2021
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
90eb9aa
implemented slice function for stdlib_ascii
aman-godara May 23, 2021
e235bc4
added module dependencies of stdlib_math for function slice in Makefi…
aman-godara May 23, 2021
0742ca0
changed names from start to first and end to last
aman-godara May 24, 2021
1a5f78c
forgot to change the dummy argument start to first
aman-godara May 24, 2021
15827d2
shifted slice from stdlib_ascii to stdlib_strings and modified module…
aman-godara May 24, 2021
c7c1e48
removed include_last functionality
aman-godara May 25, 2021
ac607f1
added tests for slice function (with no include_last functionality)
aman-godara May 25, 2021
9d72c69
made complete use of slice interface: added test cases for character …
aman-godara May 25, 2021
a733bc3
documented function slice, corrected documentation of to_title and to…
aman-godara May 26, 2021
fa88905
improved function slice for invalid cases, added new invalid test cases
aman-godara May 27, 2021
42a905d
improved the implementation of last commit fa88905
aman-godara May 28, 2021
ffcb7e4
removed redundant outer loop, improved documentation of slice function
aman-godara May 29, 2021
4598eec
removed dependency of clip function by stdlib_strings.f90
aman-godara May 29, 2021
24d417f
improved documentation and comments for function slice
aman-godara Jun 7, 2021
323bcd9
Add general tester against intrinsic array slice
awvwgk Jun 10, 2021
a895085
Merge pull request #3 from awvwgk/slice
aman-godara Jun 10, 2021
d60dad3
added -inf and +inf concept to make code more intuitive, added descri…
aman-godara Jun 11, 2021
048b638
added the concept of +inf and -inf in documentation
aman-godara Jun 11, 2021
d38e0f4
added fail messages to unit tests
aman-godara Jun 11, 2021
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
56 changes: 55 additions & 1 deletion src/stdlib_ascii.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
!> The specification of this module is available [here](../page/specs/stdlib_ascii.html).
module stdlib_ascii
use stdlib_kinds, only : int8, int16, int32, int64
use stdlib_math, only: clip

implicit none
private
Expand All @@ -20,7 +21,7 @@ module stdlib_ascii

! Character conversion functions
public :: to_lower, to_upper, to_title, to_sentence, reverse
public :: to_string
public :: to_string, slice

!> Version: experimental
!>
Expand Down Expand Up @@ -360,6 +361,59 @@ contains

end function reverse

pure function slice(string, start, end, stride, include_end) result(sliced_string)
character(len=*), intent(in) :: string
integer, intent(in), optional :: start, end, stride
logical, intent(in), optional :: include_end
integer :: start_index, end_index, stride_vector, n, i, j
character(len=:), allocatable :: sliced_string

start_index = 1
end_index = len(string)
stride_vector = 1
if (len(string) > 0) then
if (present(stride)) then
if (stride /= 0) then
if (stride < 0) then
start_index = len(string)
end_index = 1
end if
stride_vector = stride
end if
else
if (present(start) .and. present(end)) then
if (end < start) then
stride_vector = -1
end if
end if
end if

if (present(start)) then
start_index = clip(start, 1, len(string))
end if
if (present(end)) then
end_index = clip(end, 1, len(string))
end if

n = int((end_index - start_index) / stride_vector)
allocate(character(len=max(0, n + 1)) :: sliced_string)

if (present(include_end)) then
if (include_end) then
start_index = end_index - (n * stride_vector)
end if
end if

j = 1
do i = start_index, end_index, stride_vector
sliced_string(j:j) = string(i:i)
j = j + 1
end do
else
sliced_string = ''
end if
end function slice

#:for kind in INT_KINDS
!> Represent an integer of kind ${kind}$ as character sequence
pure function to_string_integer_${kind}$(val) result(string)
Expand Down