Skip to content

Commit a0a03b8

Browse files
committed
faster to_lower and to_upper
1 parent 2fdfab4 commit a0a03b8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/stdlib_ascii.fypp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,13 @@ contains
257257
pure function to_lower(string) result(lower_string)
258258
character(len=*), intent(in) :: string
259259
character(len=len(string)) :: lower_string
260-
integer :: i
260+
integer, parameter :: wp= 32, la=65, lz= 90
261+
integer :: i, icar
261262

262263
do i = 1, len(string)
263-
lower_string(i:i) = char_to_lower(string(i:i))
264+
icar= ichar(string(i:i))
265+
if (icar>=la.and.icar<=lz) icar= icar + wp
266+
lower_string(i:i) = char(icar)
264267
end do
265268

266269
end function to_lower
@@ -272,10 +275,13 @@ contains
272275
pure function to_upper(string) result(upper_string)
273276
character(len=*), intent(in) :: string
274277
character(len=len(string)) :: upper_string
275-
integer :: i
278+
integer, parameter :: wp= 32, BA=97, BZ= 122
279+
integer :: i, icar
276280

277281
do i = 1, len(string)
278-
upper_string(i:i) = char_to_upper(string(i:i))
282+
icar= ichar(string(i:i))
283+
if (icar>=BA.and.icar<=BZ) icar= icar - wp
284+
upper_string(i:i) = char(icar)
279285
end do
280286

281287
end function to_upper

0 commit comments

Comments
 (0)