forked from fortran-lang/stdlib-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib_strings.f90
294 lines (228 loc) · 10.2 KB
/
stdlib_strings.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
! SPDX-Identifier: MIT
!> This module implements basic string handling routines.
!>
!> The specification of this module is available [here](../page/specs/stdlib_strings.html).
module stdlib_strings
use stdlib_ascii, only : whitespace
use stdlib_string_type, only : string_type, char, verify
implicit none
private
public :: strip, chomp
public :: starts_with, ends_with
!> Remove leading and trailing whitespace characters.
!>
!> Version: experimental
interface strip
module procedure :: strip_string
module procedure :: strip_char
end interface strip
!> Remove trailing characters in set from string.
!> If no character set is provided trailing whitespace is removed.
!>
!> Version: experimental
interface chomp
module procedure :: chomp_string
module procedure :: chomp_char
module procedure :: chomp_set_string_char
module procedure :: chomp_set_char_char
module procedure :: chomp_substring_string_string
module procedure :: chomp_substring_char_string
module procedure :: chomp_substring_string_char
module procedure :: chomp_substring_char_char
end interface chomp
!> Check whether a string starts with substring or not
!>
!> Version: experimental
interface starts_with
module procedure :: starts_with_string_string
module procedure :: starts_with_string_char
module procedure :: starts_with_char_string
module procedure :: starts_with_char_char
end interface starts_with
!> Check whether a string ends with substring or not
!>
!> Version: experimental
interface ends_with
module procedure :: ends_with_string_string
module procedure :: ends_with_string_char
module procedure :: ends_with_char_string
module procedure :: ends_with_char_char
end interface ends_with
contains
!> Remove leading and trailing whitespace characters.
pure function strip_string(string) result(stripped_string)
! Avoid polluting the module scope and use the assignment only in this scope
use stdlib_string_type, only : assignment(=)
type(string_type), intent(in) :: string
type(string_type) :: stripped_string
stripped_string = strip(char(string))
end function strip_string
!> Remove leading and trailing whitespace characters.
pure function strip_char(string) result(stripped_string)
character(len=*), intent(in) :: string
character(len=:), allocatable :: stripped_string
integer :: first, last
first = verify(string, whitespace)
if (first == 0) then
stripped_string = ""
else
last = verify(string, whitespace, back=.true.)
stripped_string = string(first:last)
end if
end function strip_char
!> Remove trailing characters in set from string.
!> Default character set variant where trailing whitespace is removed.
pure function chomp_string(string) result(chomped_string)
! Avoid polluting the module scope and use the assignment only in this scope
use stdlib_string_type, only : assignment(=)
type(string_type), intent(in) :: string
type(string_type) :: chomped_string
integer :: last
last = verify(string, whitespace, back=.true.)
chomped_string = char(string, 1, last)
end function chomp_string
!> Remove trailing characters in set from string.
!> Default character set variant where trailing whitespace is removed.
pure function chomp_char(string) result(chomped_string)
character(len=*), intent(in) :: string
character(len=:), allocatable :: chomped_string
integer :: last
last = verify(string, whitespace, back=.true.)
chomped_string = string(1:last)
end function chomp_char
!> Remove trailing characters in set from string.
pure function chomp_set_string_char(string, set) result(chomped_string)
! Avoid polluting the module scope and use the assignment only in this scope
use stdlib_string_type, only : assignment(=)
type(string_type), intent(in) :: string
character(len=1), intent(in) :: set(:)
type(string_type) :: chomped_string
chomped_string = chomp(char(string), set)
end function chomp_set_string_char
!> Remove trailing characters in set from string.
pure function chomp_set_char_char(string, set) result(chomped_string)
character(len=*), intent(in) :: string
character(len=1), intent(in) :: set(:)
character(len=:), allocatable :: chomped_string
integer :: last
last = verify(string, set_to_string(set), back=.true.)
chomped_string = string(1:last)
end function chomp_set_char_char
!> Remove trailing substrings from string.
pure function chomp_substring_string_string(string, substring) result(chomped_string)
! Avoid polluting the module scope and use the assignment only in this scope
use stdlib_string_type, only : assignment(=)
type(string_type), intent(in) :: string
type(string_type), intent(in) :: substring
type(string_type) :: chomped_string
chomped_string = chomp(char(string), char(substring))
end function chomp_substring_string_string
!> Remove trailing substrings from string.
pure function chomp_substring_string_char(string, substring) result(chomped_string)
! Avoid polluting the module scope and use the assignment only in this scope
use stdlib_string_type, only : assignment(=)
type(string_type), intent(in) :: string
character(len=*), intent(in) :: substring
type(string_type) :: chomped_string
chomped_string = chomp(char(string), substring)
end function chomp_substring_string_char
!> Remove trailing substrings from string.
pure function chomp_substring_char_string(string, substring) result(chomped_string)
character(len=*), intent(in) :: string
type(string_type), intent(in) :: substring
character(len=:), allocatable :: chomped_string
chomped_string = chomp(string, char(substring))
end function chomp_substring_char_string
!> Remove trailing substrings from string.
pure function chomp_substring_char_char(string, substring) result(chomped_string)
character(len=*), intent(in) :: string
character(len=*), intent(in) :: substring
character(len=:), allocatable :: chomped_string
integer :: last, nsub
last = len(string)
nsub = len(substring)
if (nsub > 0) then
do while(string(last-nsub+1:last) == substring)
last = last - nsub
end do
end if
chomped_string = string(1:last)
end function chomp_substring_char_char
!> Implementation to transfer a set of characters to a string representing the set.
!>
!> This function is internal and not part of the public API.
pure function set_to_string(set) result(string)
character(len=1), intent(in) :: set(:)
character(len=size(set)) :: string
string = transfer(set, string)
end function set_to_string
!> Check whether a string starts with substring or not
pure function starts_with_char_char(string, substring) result(match)
character(len=*), intent(in) :: string
character(len=*), intent(in) :: substring
logical :: match
integer :: nsub
nsub = len(substring)
if (len(string) < nsub) then
match = .false.
return
end if
match = string(1:nsub) == substring
end function starts_with_char_char
!> Check whether a string starts with substring or not
elemental function starts_with_string_char(string, substring) result(match)
type(string_type), intent(in) :: string
character(len=*), intent(in) :: substring
logical :: match
match = starts_with(char(string), substring)
end function starts_with_string_char
!> Check whether a string starts with substring or not
elemental function starts_with_char_string(string, substring) result(match)
character(len=*), intent(in) :: string
type(string_type), intent(in) :: substring
logical :: match
match = starts_with(string, char(substring))
end function starts_with_char_string
!> Check whether a string starts with substring or not
elemental function starts_with_string_string(string, substring) result(match)
type(string_type), intent(in) :: string
type(string_type), intent(in) :: substring
logical :: match
match = starts_with(char(string), char(substring))
end function starts_with_string_string
!> Check whether a string ends with substring or not
pure function ends_with_char_char(string, substring) result(match)
character(len=*), intent(in) :: string
character(len=*), intent(in) :: substring
logical :: match
integer :: last, nsub
last = len(string)
nsub = len(substring)
if (last < nsub) then
match = .false.
return
end if
match = string(last-nsub+1:last) == substring
end function ends_with_char_char
!> Check whether a string ends with substring or not
elemental function ends_with_string_char(string, substring) result(match)
type(string_type), intent(in) :: string
character(len=*), intent(in) :: substring
logical :: match
match = ends_with(char(string), substring)
end function ends_with_string_char
!> Check whether a string ends with substring or not
elemental function ends_with_char_string(string, substring) result(match)
character(len=*), intent(in) :: string
type(string_type), intent(in) :: substring
logical :: match
match = ends_with(string, char(substring))
end function ends_with_char_string
!> Check whether a string ends with substring or not
elemental function ends_with_string_string(string, substring) result(match)
type(string_type), intent(in) :: string
type(string_type), intent(in) :: substring
logical :: match
match = ends_with(char(string), char(substring))
end function ends_with_string_string
end module stdlib_strings