|
| 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 |
| 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_string_string |
| 31 | + module procedure :: chomp_char_string |
| 32 | + module procedure :: chomp_string_char |
| 33 | + module procedure :: chomp_char_char |
| 34 | + end interface chomp |
| 35 | + |
| 36 | + |
| 37 | +contains |
| 38 | + |
| 39 | + |
| 40 | + !> Remove leading and trailing whitespace characters. |
| 41 | + pure function strip_string(string) result(stripped_string) |
| 42 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 43 | + use stdlib_string_type, only : assignment(=) |
| 44 | + type(string_type), intent(in) :: string |
| 45 | + type(string_type) :: stripped_string |
| 46 | + |
| 47 | + stripped_string = strip(char(string)) |
| 48 | + end function strip_string |
| 49 | + |
| 50 | + !> Remove leading and trailing whitespace characters. |
| 51 | + pure function strip_char(string) result(stripped_string) |
| 52 | + character(len=*), intent(in) :: string |
| 53 | + character(len=:), allocatable :: stripped_string |
| 54 | + integer :: first, last |
| 55 | + |
| 56 | + first = verify(string, whitespace) |
| 57 | + if (first == 0) then |
| 58 | + stripped_string = "" |
| 59 | + else |
| 60 | + last = verify(string, whitespace, back=.true.) |
| 61 | + stripped_string = string(first:last) |
| 62 | + end if |
| 63 | + |
| 64 | + end function strip_char |
| 65 | + |
| 66 | + |
| 67 | + !> Remove trailing characters in set from string. |
| 68 | + !> Default character set variant where trailing whitespace is removed. |
| 69 | + pure function chomp_string(string) result(chomped_string) |
| 70 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 71 | + use stdlib_string_type, only : assignment(=) |
| 72 | + type(string_type), intent(in) :: string |
| 73 | + type(string_type) :: chomped_string |
| 74 | + |
| 75 | + chomped_string = chomp(char(string), whitespace) |
| 76 | + end function chomp_string |
| 77 | + |
| 78 | + !> Remove trailing characters in set from string. |
| 79 | + !> Default character set variant where trailing whitespace is removed. |
| 80 | + pure function chomp_char(string) result(chomped_string) |
| 81 | + character(len=*), intent(in) :: string |
| 82 | + character(len=:), allocatable :: chomped_string |
| 83 | + |
| 84 | + chomped_string = chomp(string, whitespace) |
| 85 | + end function chomp_char |
| 86 | + |
| 87 | + !> Remove trailing characters in set from string. |
| 88 | + pure function chomp_string_string(string, set) result(chomped_string) |
| 89 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 90 | + use stdlib_string_type, only : assignment(=) |
| 91 | + type(string_type), intent(in) :: string |
| 92 | + type(string_type), intent(in) :: set |
| 93 | + type(string_type) :: chomped_string |
| 94 | + |
| 95 | + chomped_string = chomp(char(string), char(set)) |
| 96 | + end function chomp_string_string |
| 97 | + |
| 98 | + !> Remove trailing characters in set from string. |
| 99 | + pure function chomp_string_char(string, set) result(chomped_string) |
| 100 | + ! Avoid polluting the module scope and use the assignment only in this scope |
| 101 | + use stdlib_string_type, only : assignment(=) |
| 102 | + type(string_type), intent(in) :: string |
| 103 | + character(len=*), intent(in) :: set |
| 104 | + type(string_type) :: chomped_string |
| 105 | + |
| 106 | + chomped_string = chomp(char(string), set) |
| 107 | + end function chomp_string_char |
| 108 | + |
| 109 | + !> Remove trailing characters in set from string. |
| 110 | + pure function chomp_char_string(string, set) result(chomped_string) |
| 111 | + character(len=*), intent(in) :: string |
| 112 | + type(string_type), intent(in) :: set |
| 113 | + character(len=:), allocatable :: chomped_string |
| 114 | + |
| 115 | + chomped_string = chomp(string, char(set)) |
| 116 | + end function chomp_char_string |
| 117 | + |
| 118 | + !> Remove trailing characters in set from string. |
| 119 | + pure function chomp_char_char(string, set) result(chomped_string) |
| 120 | + character(len=*), intent(in) :: string |
| 121 | + character(len=*), intent(in) :: set |
| 122 | + character(len=:), allocatable :: chomped_string |
| 123 | + integer :: last |
| 124 | + |
| 125 | + last = verify(string, set, back=.true.) |
| 126 | + chomped_string = string(1:last) |
| 127 | + |
| 128 | + end function chomp_char_char |
| 129 | + |
| 130 | + |
| 131 | +end module stdlib_strings |
0 commit comments