|
| 1 | +! SPDX-Identifier: MIT |
| 2 | + |
| 3 | +!> This module implements basic string handling routines. |
| 4 | +!> |
| 5 | +!> The specification of this module is available [here](../page/specs/stdlib_strings.html). |
| 6 | +module stdlib_strings |
| 7 | + use stdlib_ascii, only : whitespace |
| 8 | + use stdlib_string_type, only : string_type, char, verify |
| 9 | + implicit none |
| 10 | + private |
| 11 | + |
| 12 | + public :: strip, chomp |
| 13 | + |
| 14 | + |
| 15 | + !> Remove leading and trailing whitespace characters. |
| 16 | + !> |
| 17 | + !> Version: experimental |
| 18 | + interface strip |
| 19 | + module procedure :: strip_string |
| 20 | + module procedure :: strip_char |
| 21 | + end interface strip |
| 22 | + |
| 23 | + !> Remove trailing characters in set from string. |
| 24 | + !> If no character set is provided trailing whitespace is removed. |
| 25 | + !> |
| 26 | + !> Version: experimental |
| 27 | + interface chomp |
| 28 | + module procedure :: chomp_string |
| 29 | + module procedure :: chomp_char |
| 30 | + module procedure :: chomp_set_string_char |
| 31 | + module procedure :: chomp_set_char_char |
| 32 | + module procedure :: chomp_substring_string_string |
| 33 | + module procedure :: chomp_substring_char_string |
| 34 | + module procedure :: chomp_substring_string_char |
| 35 | + module procedure :: chomp_substring_char_char |
| 36 | + end interface chomp |
| 37 | + |
| 38 | + |
| 39 | +contains |
| 40 | + |
| 41 | + |
| 42 | + !> Remove leading and trailing whitespace characters. |
| 43 | + pure function strip_string(string) result(stripped_string) |
| 44 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 45 | + use stdlib_string_type, only : assignment(=) |
| 46 | + type(string_type), intent(in) :: string |
| 47 | + type(string_type) :: stripped_string |
| 48 | + |
| 49 | + stripped_string = strip(char(string)) |
| 50 | + end function strip_string |
| 51 | + |
| 52 | + !> Remove leading and trailing whitespace characters. |
| 53 | + pure function strip_char(string) result(stripped_string) |
| 54 | + character(len=*), intent(in) :: string |
| 55 | + character(len=:), allocatable :: stripped_string |
| 56 | + integer :: first, last |
| 57 | + |
| 58 | + first = verify(string, whitespace) |
| 59 | + if (first == 0) then |
| 60 | + stripped_string = "" |
| 61 | + else |
| 62 | + last = verify(string, whitespace, back=.true.) |
| 63 | + stripped_string = string(first:last) |
| 64 | + end if |
| 65 | + |
| 66 | + end function strip_char |
| 67 | + |
| 68 | + |
| 69 | + !> Remove trailing characters in set from string. |
| 70 | + !> Default character set variant where trailing whitespace is removed. |
| 71 | + pure function chomp_string(string) result(chomped_string) |
| 72 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 73 | + use stdlib_string_type, only : assignment(=) |
| 74 | + type(string_type), intent(in) :: string |
| 75 | + type(string_type) :: chomped_string |
| 76 | + integer :: last |
| 77 | + |
| 78 | + last = verify(string, whitespace, back=.true.) |
| 79 | + chomped_string = char(string, 1, last) |
| 80 | + end function chomp_string |
| 81 | + |
| 82 | + !> Remove trailing characters in set from string. |
| 83 | + !> Default character set variant where trailing whitespace is removed. |
| 84 | + pure function chomp_char(string) result(chomped_string) |
| 85 | + character(len=*), intent(in) :: string |
| 86 | + character(len=:), allocatable :: chomped_string |
| 87 | + integer :: last |
| 88 | + |
| 89 | + last = verify(string, whitespace, back=.true.) |
| 90 | + chomped_string = string(1:last) |
| 91 | + end function chomp_char |
| 92 | + |
| 93 | + !> Remove trailing characters in set from string. |
| 94 | + pure function chomp_set_string_char(string, set) result(chomped_string) |
| 95 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 96 | + use stdlib_string_type, only : assignment(=) |
| 97 | + type(string_type), intent(in) :: string |
| 98 | + character(len=1), intent(in) :: set(:) |
| 99 | + type(string_type) :: chomped_string |
| 100 | + |
| 101 | + chomped_string = chomp(char(string), set) |
| 102 | + end function chomp_set_string_char |
| 103 | + |
| 104 | + !> Remove trailing characters in set from string. |
| 105 | + pure function chomp_set_char_char(string, set) result(chomped_string) |
| 106 | + character(len=*), intent(in) :: string |
| 107 | + character(len=1), intent(in) :: set(:) |
| 108 | + character(len=:), allocatable :: chomped_string |
| 109 | + integer :: last |
| 110 | + |
| 111 | + last = verify(string, set_to_string(set), back=.true.) |
| 112 | + chomped_string = string(1:last) |
| 113 | + |
| 114 | + end function chomp_set_char_char |
| 115 | + |
| 116 | + !> Remove trailing substrings from string. |
| 117 | + pure function chomp_substring_string_string(string, substring) result(chomped_string) |
| 118 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 119 | + use stdlib_string_type, only : assignment(=) |
| 120 | + type(string_type), intent(in) :: string |
| 121 | + type(string_type), intent(in) :: substring |
| 122 | + type(string_type) :: chomped_string |
| 123 | + |
| 124 | + chomped_string = chomp(char(string), char(substring)) |
| 125 | + end function chomp_substring_string_string |
| 126 | + |
| 127 | + !> Remove trailing substrings from string. |
| 128 | + pure function chomp_substring_string_char(string, substring) result(chomped_string) |
| 129 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 130 | + use stdlib_string_type, only : assignment(=) |
| 131 | + type(string_type), intent(in) :: string |
| 132 | + character(len=*), intent(in) :: substring |
| 133 | + type(string_type) :: chomped_string |
| 134 | + |
| 135 | + chomped_string = chomp(char(string), substring) |
| 136 | + end function chomp_substring_string_char |
| 137 | + |
| 138 | + !> Remove trailing substrings from string. |
| 139 | + pure function chomp_substring_char_string(string, substring) result(chomped_string) |
| 140 | + character(len=*), intent(in) :: string |
| 141 | + type(string_type), intent(in) :: substring |
| 142 | + character(len=:), allocatable :: chomped_string |
| 143 | + |
| 144 | + chomped_string = chomp(string, char(substring)) |
| 145 | + end function chomp_substring_char_string |
| 146 | + |
| 147 | + !> Remove trailing substrings from string. |
| 148 | + pure function chomp_substring_char_char(string, substring) result(chomped_string) |
| 149 | + character(len=*), intent(in) :: string |
| 150 | + character(len=*), intent(in) :: substring |
| 151 | + character(len=:), allocatable :: chomped_string |
| 152 | + integer :: last, nsub |
| 153 | + |
| 154 | + last = len(string) |
| 155 | + nsub = len(substring) |
| 156 | + if (nsub > 0) then |
| 157 | + do while(string(last-nsub+1:last) == substring) |
| 158 | + last = last - nsub |
| 159 | + end do |
| 160 | + end if |
| 161 | + chomped_string = string(1:last) |
| 162 | + |
| 163 | + end function chomp_substring_char_char |
| 164 | + |
| 165 | + !> Implementation to transfer a set of characters to a string representing the set. |
| 166 | + !> |
| 167 | + !> This function is internal and not part of the public API. |
| 168 | + pure function set_to_string(set) result(string) |
| 169 | + character(len=1), intent(in) :: set(:) |
| 170 | + character(len=size(set)) :: string |
| 171 | + |
| 172 | + string = transfer(set, string) |
| 173 | + end function set_to_string |
| 174 | + |
| 175 | + |
| 176 | +end module stdlib_strings |
0 commit comments