Strings and Runes in Co-array Fortran

program strings_and_runes
  use iso_fortran_env
  implicit none

  ! A Co-array Fortran string is a fixed-length sequence of characters.
  ! Unlike Go, Fortran doesn't have built-in UTF-8 support,
  ! so we'll use ASCII characters for this example.
  character(len=6) :: s = "Hello!"

  ! Print the length of the string
  print *, "Len:", len(s)

  ! Print the ASCII values of each character
  print *, "ASCII values:"
  call print_ascii(s)

  ! Count the number of characters (equivalent to runes in Go)
  print *, "Character count:", len_trim(s)

  ! Iterate over each character
  call iterate_characters(s)

  ! Demonstrate character comparison
  call examine_character(s(1:1))

contains

  subroutine print_ascii(str)
    character(len=*), intent(in) :: str
    integer :: i
    do i = 1, len(str)
      write(*, '(Z2.2, 1X)', advance='no') iachar(str(i:i))
    end do
    print *
  end subroutine print_ascii

  subroutine iterate_characters(str)
    character(len=*), intent(in) :: str
    integer :: i
    print *, "Iterating characters:"
    do i = 1, len_trim(str)
      print *, "Character '", str(i:i), "' is at position", i
    end do
  end subroutine iterate_characters

  subroutine examine_character(c)
    character, intent(in) :: c
    if (c == 'H') then
      print *, "found H"
    else if (c == 'e') then
      print *, "found e"
    end if
  end subroutine examine_character

end program strings_and_runes

This Co-array Fortran program demonstrates concepts similar to the Go example for strings and runes, adapted to Fortran’s capabilities:

  1. We define a fixed-length string s with the value “Hello!”. In Fortran, strings are fixed-length character arrays.

  2. We print the length of the string using the len function.

  3. We iterate over each character in the string and print its ASCII value using the iachar function, which is similar to getting the byte values in Go.

  4. We count the number of characters using len_trim, which is roughly equivalent to counting runes in Go (for ASCII strings).

  5. We iterate over each character in the string, printing its value and position. This is similar to the range loop in Go, but adapted for Fortran’s string handling.

  6. We demonstrate character comparison in the examine_character subroutine, which is similar to the rune comparison in Go.

To compile and run this program:

$ gfortran -o strings_and_runes strings_and_runes.f90
$ ./strings_and_runes

This will output:

Len:           6
ASCII values:
48 65 6C 6C 6F 21 
Character count:           6
Iterating characters:
Character 'H' is at position           1
Character 'e' is at position           2
Character 'l' is at position           3
Character 'l' is at position           4
Character 'o' is at position           5
Character '!' is at position           6
found H

Note that Co-array Fortran doesn’t have built-in UTF-8 support like Go, so this example uses ASCII characters. For proper Unicode handling in Fortran, you would need to use additional libraries or implement UTF-8 encoding/decoding yourself.