Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=CK,len=*), | intent(in) | :: | str | the string to check. |
is str a value 4-digit hex string
Returns true if the string is a valid 4-digit hex string.
valid_json_hex('0000') !returns true valid_json_hex('ABC4') !returns true valid_json_hex('AB') !returns false (< 4 characters) valid_json_hex('WXYZ') !returns false (invalid characters)
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer(kind=IK), | public | :: | n | ||||
integer(kind=IK), | public | :: | i | ||||
character(kind=CK,len=1), | public, | parameter, dimension(22) | :: | valid_chars | = | [(achar(i), i=48, 57), (achar(i), i=65, 70), (achar(i), i=97, 102)] |
pure function valid_json_hex(str) result(valid)
implicit none
logical(LK) :: valid !! is str a value 4-digit hex string
character(kind=CK,len=*),intent(in) :: str !! the string to check.
integer(IK) :: n,i
!an array of the valid hex characters:
character(kind=CK,len=1),dimension(22),parameter :: valid_chars = &
[ (achar(i),i=48,57), & ! decimal digits
(achar(i),i=65,70), & ! capital A-F
(achar(i),i=97,102) ] ! lowercase a-f
!initialize
valid = .false.
!check all the characters in the string:
n = len(str)
if (n==4) then
do i=1,n
if (.not. any(str(i:i)==valid_chars)) return
end do
valid = .true. !all are in the set, so it is OK
end if
end function valid_json_hex