valid_json_hex Function

public pure function valid_json_hex(str) result(valid)

Returns true if the string is a valid 4-digit hex string.

Examples

    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)

Arguments

Type IntentOptional AttributesName
character(kind=CK,len=*), intent(in) :: str

the string to check.

Return Value logical(kind=LK)

is str a value 4-digit hex string


Called by

proc~~valid_json_hex~~CalledByGraph proc~valid_json_hex valid_json_hex proc~escape_string escape_string proc~escape_string->proc~valid_json_hex proc~unescape_string unescape_string proc~unescape_string->proc~valid_json_hex proc~json_value_print json_value_print proc~json_value_print->proc~escape_string proc~parse_string parse_string proc~parse_string->proc~unescape_string proc~json_get_string json_get_string proc~json_get_string->proc~escape_string

Contents

Source Code


Source Code

    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  !! length of `str`
    integer(IK) :: i  !! counter

    !> 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