Skip to content

Added to_lower, to_upper, reverse and to_title function to stdlib_string_type.f90 file #346

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 12 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ To test your build, run the test suite after the build has finished with
cmake --build build --target test
```

Please report failing tests on our [issue tracker](https://github.com/fortran-lang/stdlib/issues/new/choose) including details on the compiler used, the operating system and platform architecture.
Please report failing tests on our [issue tracker](https://github.com/fortran-lang/stdlib/issues/new/choose) including details of the compiler used, the operating system and platform architecture.

To install the project to the declared prefix run

Expand Down
1 change: 1 addition & 0 deletions src/Makefile.manual
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ stdlib_stats_var.o: \
stdlib_stats_distribution_PRNG.o: \
stdlib_kinds.o \
stdlib_error.o
stdlib_string_type.o: stdlib_ascii.o
30 changes: 30 additions & 0 deletions src/stdlib_ascii.f90
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ module stdlib_ascii
character(len=*), public, parameter :: lowercase = letters(27:) !! a .. z
character(len=*), public, parameter :: whitespace = " "//TAB//VT//CR//LF//FF !! ASCII _whitespace


!> Returns a new character sequence which is the lower case
!> version of the input character sequence
!> This method is pure and returns a character sequence
interface to_lower
module procedure :: to_lower
end interface to_lower

!> Returns a new character sequence which is the upper case
!> version of the input character sequence
!> This method is pure and returns a character sequence
interface to_upper
module procedure :: to_upper
end interface to_upper

!> Returns a new character sequence which is the titled
!> version of the input character sequence
!> This method is pure and returns a character sequence
interface to_title
module procedure :: to_title
end interface to_title

!> Returns a new character sequence which is the reversed
!> version of the input charater sequence
!> This method is pure and returns a character sequence
interface reverse
module procedure :: reverse
end interface reverse


contains

!> Checks whether `c` is an ASCII letter (A .. Z, a .. z).
Expand Down
78 changes: 77 additions & 1 deletion src/stdlib_string_type.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
!>
!> The specification of this module is available [here](../page/specs/stdlib_string_type.html).
module stdlib_string_type
use stdlib_ascii, only: to_lower_ => to_lower, to_upper_ => to_upper, &
to_title_ => to_title, reverse_ => reverse

implicit none
private

public :: string_type
public :: len, len_trim, trim, index, scan, verify, repeat, adjustr, adjustl
public :: len, len_trim, trim, index, scan, verify, repeat, adjustr, &
adjustl, to_lower, to_upper, to_title, reverse
public :: lgt, lge, llt, lle, char, ichar, iachar
public :: assignment(=)
public :: operator(>), operator(>=), operator(<), operator(<=)
Expand Down Expand Up @@ -89,6 +93,38 @@ module stdlib_string_type
module procedure :: repeat_string
end interface repeat

!> Convert the character sequence hold by the input string to lower case
!>
!> This method is Elemental and returns a new string_type instance which holds lowercase
!> version of the character sequence hold by the input string
interface to_lower
module procedure :: to_lower_string
end interface to_lower

!> Convert the character sequence hold by the input string to upper case
!>
!> This method is Elemental and returns a new string_type instance which holds uppercase
!> version of the character sequence hold by the input string
interface to_upper
module procedure :: to_upper_string
end interface to_upper

!> Convert the character sequence hold by the input string to title case
!>
!> This method is Elemental and returns a new string_type instance which holds titlecase
!> version of the character sequence hold by the input string
interface to_title
module procedure :: to_title_string
end interface to_title

!> Reverse the character sequence hold by the input string
!>
!> This method is Elemental and returns a new string_type instance which holds reversed
!> sequence of the character sequence hold by the input string
interface reverse
module procedure :: reverse_string
end interface reverse

!> Return the character sequence represented by the string.
!>
!> This method is elemental and returns a scalar character value.
Expand Down Expand Up @@ -447,6 +483,46 @@ elemental function repeat_string(string, ncopies) result(repeated_string)
end function repeat_string


!> Convert the character sequence hold by the input string to lower case
elemental function to_lower_string(string) result(lowercase_string)
type(string_type), intent(in) :: string
type(string_type) :: lowercase_string

lowercase_string%raw = to_lower_(maybe(string))

end function to_lower_string


!> Convert the character sequence hold by the input string to upper case
elemental function to_upper_string(string) result(uppercase_string)
type(string_type), intent(in) :: string
type(string_type) :: uppercase_string

uppercase_string%raw = to_upper_(maybe(string))

end function to_upper_string


!> Convert the character sequence hold by the input string to title case
elemental function to_title_string(string) result(titlecase_string)
type(string_type), intent(in) :: string
type(string_type) :: titlecase_string

titlecase_string%raw = to_title_(maybe(string))

end function to_title_string


!> Reverse the character sequence hold by the input string
elemental function reverse_string(string) result(reversed_string)
type(string_type), intent(in) :: string
type(string_type) :: reversed_string

reversed_string%raw = reverse_(maybe(string))

end function reverse_string


!> Position of a sequence of character within a character sequence.
!> In this version both character sequences are represented by a string.
elemental function index_string_string(string, substring, back) result(pos)
Expand Down
58 changes: 58 additions & 0 deletions src/tests/string/test_string_functions.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
! SPDX-Identifier: MIT
module test_string_functions
use stdlib_error, only : check
use stdlib_string_type, only : string_type, assignment(=), operator(==), &
to_lower, to_upper, to_title, reverse
implicit none

contains

subroutine test_to_lower_string
type(string_type) :: test_string, compare_string
test_string = "To_LoWEr !$%-az09AZ"
compare_string = "to_lower !$%-az09az"

call check(to_lower(test_string) == compare_string)

end subroutine test_to_lower_string

subroutine test_to_upper_string
type(string_type) :: test_string, compare_string
test_string = "To_UpPeR !$%-az09AZ"
compare_string = "TO_UPPER !$%-AZ09AZ"

call check(to_upper(test_string) == compare_string)

end subroutine test_to_upper_string

subroutine test_to_title_string
type(string_type) :: test_string, compare_string
test_string = "_#To tiTlE !$%-az09AZ"
compare_string = "_#To title !$%-a09az"

call check(to_title(test_string) == compare_string)

end subroutine test_to_title_string

subroutine test_reverse_string
type(string_type) :: test_string, compare_string
test_string = "_To ReVerSe !$%-az09AZ "
compare_string = " ZA90za-%$! eSreVeR oT_"

call check(reverse(test_string) == compare_string)

end subroutine test_reverse_string

end module test_string_functions


program tester
use test_string_functions
implicit none

call test_to_lower_string
call test_to_upper_string
call test_to_title_string
call test_reverse_string

end program tester