Skip to content

Commit 9a82237

Browse files
committed
Remove superfluous parentheses and implement robust to_lower/to_upper functions
This commit cleans up the code a bit (the extra parentheses in several functions, leftover semicolons) and also implements a robust version of the to_lower and to_upper functions.
1 parent a68430a commit 9a82237

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/stdlib_ascii.f90

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ module stdlib_ascii
6060
character(len=*), public, parameter :: lowercase = letters(27:) !! a .. z
6161
character(len=*), public, parameter :: whitespace = " "//TAB//VT//CR//LF//FF !! ASCII _whitespace
6262

63+
character(len=26), parameter, private :: lower_case = 'abcdefghijklmnopqrstuvwxyz'
64+
character(len=26), parameter, private :: upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
65+
6366
contains
6467

6568
!> Checks whether `c` is an ASCII letter (A .. Z, a .. z).
@@ -135,7 +138,9 @@ pure logical function is_punctuation(c)
135138
pure logical function is_graphical(c)
136139
character(len=1), intent(in) :: c !! The character to test.
137140
integer :: ic
138-
ic = iachar(c) ! '!' '~'
141+
ic = iachar(c)
142+
!The character is graphical if it's between '!' and '~' in the ASCII table,
143+
!that is: printable but not a space
139144
is_graphical = (int(z'21') <= ic) .and. (ic <= int(z'7E'))
140145
end function
141146

@@ -162,7 +167,7 @@ pure logical function is_upper(c)
162167
character(len=1), intent(in) :: c !! The character to test.
163168
integer :: ic
164169
ic = iachar(c)
165-
is_upper = (ic >= iachar('A')) .and. (ic <= iachar('Z'))
170+
is_upper = ic >= iachar('A') .and. ic <= iachar('Z')
166171
end function
167172

168173
!> Checks whether or not `c` is a whitespace character. That includes the
@@ -172,7 +177,7 @@ pure logical function is_white(c)
172177
character(len=1), intent(in) :: c !! The character to test.
173178
integer :: ic
174179
ic = iachar(c) ! TAB, LF, VT, FF, CR
175-
is_white = (ic == iachar(' ')) .or. (ic >= int(z'09') .and. ic <= int(z'0D'));
180+
is_white = ic == iachar(' ') .or. (ic >= int(z'09') .and. ic <= int(z'0D'))
176181
end function
177182

178183
!> Checks whether or not `c` is a blank character. That includes the
@@ -181,31 +186,39 @@ pure logical function is_blank(c)
181186
character(len=1), intent(in) :: c !! The character to test.
182187
integer :: ic
183188
ic = iachar(c) ! TAB
184-
is_blank = (ic == iachar(' ')) .or. (ic == int(z'09'));
189+
is_blank = ic == iachar(' ') .or. ic == int(z'09')
185190
end function
186191

187192
!> Returns the corresponding lowercase letter, if `c` is an uppercase
188193
! ASCII character, otherwise `c` itself.
189194
pure function to_lower(c) result(t)
190195
character(len=1), intent(in) :: c !! A character.
191-
character(len=1) :: t
192-
integer :: diff
193-
diff = iachar('A')-iachar('a')
194-
t = c
195-
! if uppercase, make lowercase
196-
if (is_upper(t)) t = achar(iachar(t) - diff)
196+
character(len=1) :: t
197+
integer :: k
198+
199+
k = index( upper_case, c )
200+
201+
if ( k > 0 ) then
202+
t = lower_case(k:k)
203+
else
204+
t = c
205+
endif
197206
end function
198207

199208
!> Returns the corresponding uppercase letter, if `c` is a lowercase
200209
! ASCII character, otherwise `c` itself.
201210
pure function to_upper(c) result(t)
202211
character(len=1), intent(in) :: c !! A character.
203-
character(len=1) :: t
204-
integer :: diff
205-
diff = iachar('A')-iachar('a')
206-
t = c
207-
! if lowercase, make uppercase
208-
if (is_lower(t)) t = achar(iachar(t) + diff)
212+
character(len=1) :: t
213+
integer :: k
214+
215+
k = index( lower_case, c )
216+
217+
if ( k > 0 ) then
218+
t = upper_case(k:k)
219+
else
220+
t = c
221+
endif
209222
end function
210223

211224
end module

0 commit comments

Comments
 (0)