Skip to content

Commit 4204079

Browse files
authored
Merge pull request #733 from jalvesz/master
faster to_lower and to_upper
2 parents d379587 + 3e0811a commit 4204079

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Diff for: src/stdlib_ascii.fypp

+10-14
Original file line numberDiff line numberDiff line change
@@ -223,31 +223,27 @@ contains
223223
pure function char_to_lower(c) result(t)
224224
character(len=1), intent(in) :: c !! A character.
225225
character(len=1) :: t
226+
integer, parameter :: wp= iachar('a')-iachar('A'), BA=iachar('A'), BZ=iachar('Z')
226227
integer :: k
228+
!Check whether the integer equivalent is between BA=65 and BZ=90
229+
k = ichar(c)
230+
if (k>=BA.and.k<=BZ) k = k + wp
231+
t = char(k)
227232

228-
k = index( uppercase, c )
229-
230-
if ( k > 0 ) then
231-
t = lowercase(k:k)
232-
else
233-
t = c
234-
endif
235233
end function char_to_lower
236234

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

244-
k = index( lowercase, c )
245-
246-
if ( k > 0 ) then
247-
t = uppercase(k:k)
248-
else
249-
t = c
250-
endif
251247
end function char_to_upper
252248

253249
!> Convert character variable to lower case

0 commit comments

Comments
 (0)