|
| 1 | +!> Procedures for handling characters |
| 2 | +module stdlib_character |
| 3 | + use stdlib_ascii, only : & |
| 4 | + char_is_alphanum => is_alphanum, & |
| 5 | + char_to_lower => to_lower, & |
| 6 | + char_to_upper => to_upper |
| 7 | + implicit none |
| 8 | + private |
| 9 | + |
| 10 | + public :: to_lower, to_upper, to_title, reverse |
| 11 | + |
| 12 | + interface to_lower |
| 13 | + !! Convert character variable to lower case |
| 14 | + pure module function to_lower(string) result(lower_string) |
| 15 | + character(len=*), intent(in) :: string |
| 16 | + character(len=len(string)) :: lower_string |
| 17 | + end function to_lower |
| 18 | + end interface to_lower |
| 19 | + |
| 20 | + interface to_upper |
| 21 | + !! Convert character variable to upper case |
| 22 | + pure module function to_upper(string) result(upper_string) |
| 23 | + character(len=*), intent(in) :: string |
| 24 | + character(len=len(string)) :: upper_string |
| 25 | + end function to_upper |
| 26 | + end interface to_upper |
| 27 | + |
| 28 | + interface to_title |
| 29 | + !! Convert character variable to title case |
| 30 | + pure module function to_title(string) result(title_string) |
| 31 | + character(len=*), intent(in) :: string |
| 32 | + character(len=len(string)) :: title_string |
| 33 | + end function to_title |
| 34 | + end interface to_title |
| 35 | + |
| 36 | + interface reverse |
| 37 | + !! Reverse the character order in the input character variable |
| 38 | + pure module function reverse(string) result(reverse_string) |
| 39 | + character(len=*), intent(in) :: string |
| 40 | + character(len=len(string)) :: reverse_string |
| 41 | + end function reverse |
| 42 | + end interface reverse |
| 43 | + |
| 44 | +contains |
| 45 | + |
| 46 | + pure module function to_lower(string) result(lower_string) |
| 47 | + !! Convert character variable to lower case |
| 48 | + character(len=*), intent(in) :: string |
| 49 | + character(len=len(string)) :: lower_string |
| 50 | + integer :: i |
| 51 | + |
| 52 | + do i = 1, len(string) |
| 53 | + lower_string(i:i) = char_to_lower(string(i:i)) |
| 54 | + end do |
| 55 | + |
| 56 | + end function to_lower |
| 57 | + |
| 58 | + pure module function to_upper(string) result(upper_string) |
| 59 | + !! Convert character variable to upper case |
| 60 | + character(len=*), intent(in) :: string |
| 61 | + character(len=len(string)) :: upper_string |
| 62 | + integer :: i |
| 63 | + |
| 64 | + do i = 1, len(string) |
| 65 | + upper_string(i:i) = char_to_upper(string(i:i)) |
| 66 | + end do |
| 67 | + |
| 68 | + end function to_upper |
| 69 | + |
| 70 | + pure module function to_title(string) result(title_string) |
| 71 | + !! Convert character variable to title case |
| 72 | + character(len=*), intent(in) :: string |
| 73 | + character(len=len(string)) :: title_string |
| 74 | + integer :: i, n |
| 75 | + |
| 76 | + n = len(string) |
| 77 | + do i = 1, len(string) |
| 78 | + if (char_is_alphanum(string(i:i))) then |
| 79 | + title_string(i:i) = char_to_upper(string(i:i)) |
| 80 | + n = i |
| 81 | + exit |
| 82 | + else |
| 83 | + title_string(i:i) = string(i:i) |
| 84 | + end if |
| 85 | + end do |
| 86 | + |
| 87 | + do i = n + 1, len(string) |
| 88 | + title_string(i:i) = char_to_lower(string(i:i)) |
| 89 | + end do |
| 90 | + |
| 91 | + end function to_title |
| 92 | + |
| 93 | + pure module function reverse(string) result(reverse_string) |
| 94 | + !! Reverse the character order in the input character variable |
| 95 | + character(len=*), intent(in) :: string |
| 96 | + character(len=len(string)) :: reverse_string |
| 97 | + integer :: i, n |
| 98 | + |
| 99 | + n = len(string) |
| 100 | + do i = 1, n |
| 101 | + reverse_string(n-i+1:n-i+1) = string(i:i) |
| 102 | + end do |
| 103 | + |
| 104 | + end function reverse |
| 105 | + |
| 106 | +end module stdlib_character |
0 commit comments