From a0a03b82acfa199d98ce706964bc7928dc853122 Mon Sep 17 00:00:00 2001 From: Jose Alves Date: Fri, 24 Mar 2023 23:55:05 +0100 Subject: [PATCH 1/4] faster to_lower and to_upper --- src/stdlib_ascii.fypp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index 8dc47388a..51f5a1304 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -257,10 +257,13 @@ contains pure function to_lower(string) result(lower_string) character(len=*), intent(in) :: string character(len=len(string)) :: lower_string - integer :: i + integer, parameter :: wp= 32, la=65, lz= 90 + integer :: i, icar do i = 1, len(string) - lower_string(i:i) = char_to_lower(string(i:i)) + icar= ichar(string(i:i)) + if (icar>=la.and.icar<=lz) icar= icar + wp + lower_string(i:i) = char(icar) end do end function to_lower @@ -272,10 +275,13 @@ contains pure function to_upper(string) result(upper_string) character(len=*), intent(in) :: string character(len=len(string)) :: upper_string - integer :: i + integer, parameter :: wp= 32, BA=97, BZ= 122 + integer :: i, icar do i = 1, len(string) - upper_string(i:i) = char_to_upper(string(i:i)) + icar= ichar(string(i:i)) + if (icar>=BA.and.icar<=BZ) icar= icar - wp + upper_string(i:i) = char(icar) end do end function to_upper From 8b7b1e8ae967b99d11d23d28fbcda9887922208f Mon Sep 17 00:00:00 2001 From: Jose Alves Date: Tue, 12 Sep 2023 14:42:54 +0200 Subject: [PATCH 2/4] faster chat_to_lower/upper --- src/stdlib_ascii.fypp | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index 51f5a1304..064a1aaef 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -223,15 +223,13 @@ contains pure function char_to_lower(c) result(t) character(len=1), intent(in) :: c !! A character. character(len=1) :: t + integer, parameter :: wp= 32, la=65, lz= 90 integer :: k - k = index( uppercase, c ) + k = ichar(c) + if (k>=la.and.k<=lz) k = k + wp + t = char(k) - 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 @@ -239,15 +237,13 @@ contains pure function char_to_upper(c) result(t) character(len=1), intent(in) :: c !! A character. character(len=1) :: t + integer, parameter :: wp= 32, BA=97, BZ= 122 integer :: k - k = index( lowercase, c ) + k = ichar(c) + if (k>=BA.and.k<=BZ) k = k - wp + t = char(k) - if ( k > 0 ) then - t = uppercase(k:k) - else - t = c - endif end function char_to_upper !> Convert character variable to lower case @@ -257,13 +253,10 @@ contains pure function to_lower(string) result(lower_string) character(len=*), intent(in) :: string character(len=len(string)) :: lower_string - integer, parameter :: wp= 32, la=65, lz= 90 - integer :: i, icar + integer :: i do i = 1, len(string) - icar= ichar(string(i:i)) - if (icar>=la.and.icar<=lz) icar= icar + wp - lower_string(i:i) = char(icar) + lower_string(i:i) = char_to_lower(string(i:i)) end do end function to_lower @@ -275,13 +268,10 @@ contains pure function to_upper(string) result(upper_string) character(len=*), intent(in) :: string character(len=len(string)) :: upper_string - integer, parameter :: wp= 32, BA=97, BZ= 122 - integer :: i, icar + integer :: i do i = 1, len(string) - icar= ichar(string(i:i)) - if (icar>=BA.and.icar<=BZ) icar= icar - wp - upper_string(i:i) = char(icar) + upper_string(i:i) = char_to_upper(string(i:i)) end do end function to_upper From b8dd3b4ad9d907d999055ba701784b1b64b9f93a Mon Sep 17 00:00:00 2001 From: Jose Alves Date: Thu, 14 Sep 2023 08:28:14 +0200 Subject: [PATCH 3/4] to_lower/upper clearer constants definition --- src/stdlib_ascii.fypp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index 064a1aaef..39b5ef993 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -223,11 +223,11 @@ contains pure function char_to_lower(c) result(t) character(len=1), intent(in) :: c !! A character. character(len=1) :: t - integer, parameter :: wp= 32, la=65, lz= 90 + integer, parameter :: wp= 32, 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>=la.and.k<=lz) k = k + wp + if (k>=BA.and.k<=BZ) k = k + wp t = char(k) end function char_to_lower @@ -237,11 +237,11 @@ contains pure function char_to_upper(c) result(t) character(len=1), intent(in) :: c !! A character. character(len=1) :: t - integer, parameter :: wp= 32, BA=97, BZ= 122 + integer, parameter :: wp= 32, 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>=BA.and.k<=BZ) k = k - wp + if (k>=la.and.k<=lz) k = k - wp t = char(k) end function char_to_upper From 27e928a91332f3622e099c3d702ccb7ac0626500 Mon Sep 17 00:00:00 2001 From: Jose Alves Date: Wed, 11 Oct 2023 08:52:30 +0200 Subject: [PATCH 4/4] implicit whitespace shift parameter identification --- src/stdlib_ascii.fypp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index 39b5ef993..5a4ea2811 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -223,7 +223,7 @@ contains pure function char_to_lower(c) result(t) character(len=1), intent(in) :: c !! A character. character(len=1) :: t - integer, parameter :: wp= 32, BA=iachar('A'), BZ=iachar('Z') + 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) @@ -237,7 +237,7 @@ contains pure function char_to_upper(c) result(t) character(len=1), intent(in) :: c !! A character. character(len=1) :: t - integer, parameter :: wp= 32, la=iachar('a'), lz=iachar('z') + 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)