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 4 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
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
72 changes: 71 additions & 1 deletion src/stdlib_string_type.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
!>
!> 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_upper, to_title, 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 +91,34 @@ module stdlib_string_type
module procedure :: repeat_string
end interface repeat

!> Convert the character sequence hold by the input string to lower case
!>
!> Elemental or pure ???? Does not return anything
interface to_lower
module procedure :: to_lower_string
end interface to_lower

!> Convert the character sequence hold by the input string to upper case
!>
!> Elemental or pure ???? Does not return anything
interface to_upper
module procedure :: to_upper_string
end interface to_upper

!> Convert the character sequence hold by the input string to title case
!>
!> Elemental or pure ???? Does not return anything
interface to_title
module procedure :: to_title_string
end interface to_title

!> Reverse the character sequence hold by the input string
!>
!> Elemental or pure ???? Does not return anything
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 +477,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(lowered_string)
type(string_type), intent(in) :: string
type(string_type) :: lowered_string

lowered_string%raw = to_lower(string%raw)

end function to_lower_string


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

uppered_string%raw = to_upper(string%raw)

end function to_upper_string


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

titled_string%raw = to_title(string%raw)

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(string%raw)

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