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

faster to_lower and to_upper #733

Merged
merged 8 commits into from
Oct 13, 2023
24 changes: 10 additions & 14 deletions src/stdlib_ascii.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,27 @@ contains
pure function char_to_lower(c) result(t)
character(len=1), intent(in) :: c !! A character.
character(len=1) :: t
integer, parameter :: wp= iachar('a')-iachar('A'), BA=iachar('A'), BZ=iachar('Z')
integer :: k
!Check whether the integer equivalent is between BA=65 and BZ=90
k = ichar(c)
if (k>=BA.and.k<=BZ) k = k + wp
t = char(k)

k = index( uppercase, c )

if ( k > 0 ) then
t = lowercase(k:k)
else
t = c
endif
end function char_to_lower

!> Returns the corresponding uppercase letter, if `c` is a lowercase
!> ASCII character, otherwise `c` itself.
pure function char_to_upper(c) result(t)
character(len=1), intent(in) :: c !! A character.
character(len=1) :: t
integer, parameter :: wp= iachar('a')-iachar('A'), la=iachar('a'), lz=iachar('z')
integer :: k
!Check whether the integer equivalent is between la=97 and lz=122
k = ichar(c)
if (k>=la.and.k<=lz) k = k - wp
t = char(k)

k = index( lowercase, c )

if ( k > 0 ) then
t = uppercase(k:k)
else
t = c
endif
end function char_to_upper

!> Convert character variable to lower case
Expand Down