@@ -4,7 +4,7 @@ module test_string_functions
4
4
use stdlib_error, only : check
5
5
use stdlib_string_type, only : string_type, assignment (= ), operator (==), &
6
6
to_lower, to_upper, to_title, to_sentence, reverse
7
- use stdlib_strings, only: slice, find
7
+ use stdlib_strings, only: slice, find, replace_all
8
8
use stdlib_optval, only: optval
9
9
use stdlib_ascii, only : to_string
10
10
implicit none
@@ -63,102 +63,102 @@ subroutine test_slice_string
63
63
! Only one argument is given
64
64
! Valid
65
65
call check(slice(test_string, first= 10 ) == " jklmnopqrstuvwxyz" , &
66
- " Slice , Valid arguments: first=10" ) ! last=+inf
66
+ " slice , Valid arguments: first=10" ) ! last=+inf
67
67
call check(slice(test_string, last= 10 ) == " abcdefghij" , &
68
- " Slice , Valid arguments: last=10" ) ! first=-inf
68
+ " slice , Valid arguments: last=10" ) ! first=-inf
69
69
call check(slice(test_string, stride= 3 ) == " adgjmpsvy" , &
70
- " Slice , Valid arguments: stride=3" ) ! first=-inf, last=+inf
70
+ " slice , Valid arguments: stride=3" ) ! first=-inf, last=+inf
71
71
call check(slice(test_string, stride=- 3 ) == " zwtqnkheb" , &
72
- " Slice , Valid arguments: stride=-3" ) ! first=+inf, last=-inf
72
+ " slice , Valid arguments: stride=-3" ) ! first=+inf, last=-inf
73
73
74
74
! Invalid
75
75
call check(slice(test_string, first= 27 ) == " " , &
76
- " Slice , Invalid arguments: first=27" ) ! last=+inf
76
+ " slice , Invalid arguments: first=27" ) ! last=+inf
77
77
call check(slice(test_string, first=- 10 ) == " abcdefghijklmnopqrstuvwxyz" , &
78
- " Slice , Invalid arguments: first=-10" ) ! last=+inf
78
+ " slice , Invalid arguments: first=-10" ) ! last=+inf
79
79
call check(slice(test_string, last=- 2 ) == " " , &
80
- " Slice , Invalid arguments: last=-2" ) ! first=-inf
80
+ " slice , Invalid arguments: last=-2" ) ! first=-inf
81
81
call check(slice(test_string, last= 30 ) == " abcdefghijklmnopqrstuvwxyz" , &
82
- " Slice , Invalid arguments: last=30" ) ! first=-inf
82
+ " slice , Invalid arguments: last=30" ) ! first=-inf
83
83
call check(slice(test_string, stride= 0 ) == " abcdefghijklmnopqrstuvwxyz" , &
84
- " Slice , Invalid arguments: stride=0" ) ! stride=1
84
+ " slice , Invalid arguments: stride=0" ) ! stride=1
85
85
86
86
! Only two arguments are given
87
87
! Valid
88
88
call check(slice(test_string, first= 10 , last= 20 ) == " jklmnopqrst" , &
89
- " Slice , Valid arguments: first=10, last=20" )
89
+ " slice , Valid arguments: first=10, last=20" )
90
90
call check(slice(test_string, first= 7 , last= 2 ) == " gfedcb" , &
91
- " Slice , Valid arguments: first=7, last=2" ) ! stride=-1
91
+ " slice , Valid arguments: first=7, last=2" ) ! stride=-1
92
92
call check(slice(test_string, first= 10 , stride=- 2 ) == " jhfdb" , &
93
- " Slice , Valid arguments: first=10, stride=-2" ) ! last=-inf
93
+ " slice , Valid arguments: first=10, stride=-2" ) ! last=-inf
94
94
call check(slice(test_string, last= 21 , stride=- 2 ) == " zxv" , &
95
- " Slice , Valid arguments: last=21, stride=-2" ) ! first=+inf
95
+ " slice , Valid arguments: last=21, stride=-2" ) ! first=+inf
96
96
97
97
! Atleast one argument is invalid
98
98
call check(slice(test_string, first= 30 , last=- 3 ) == " zyxwvutsrqponmlkjihgfedcba" , &
99
- " Slice , Invalid arguments: first=30, last=-3" )
99
+ " slice , Invalid arguments: first=30, last=-3" )
100
100
call check(slice(test_string, first= 1 , last=- 20 ) == " a" , &
101
- " Slice , Invalid arguments: first=1, last=-20" )
101
+ " slice , Invalid arguments: first=1, last=-20" )
102
102
call check(slice(test_string, first= 7 , last=- 10 ) == " gfedcba" , &
103
- " Slice , Invalid arguments: first=7, last=-10" )
103
+ " slice , Invalid arguments: first=7, last=-10" )
104
104
call check(slice(test_string, first= 500 , last= 22 ) == " zyxwv" , &
105
- " Slice , Invalid arguments: first=500, last=22" )
105
+ " slice , Invalid arguments: first=500, last=22" )
106
106
call check(slice(test_string, first= 50 , last= 27 ) == " " , &
107
- " Slice , Invalid arguments: first=50, last=27" )
107
+ " slice , Invalid arguments: first=50, last=27" )
108
108
call check(slice(test_string, first=- 20 , last= 0 ) == " " , &
109
- " Slice , Invalid arguments: first=-20, last=0" )
109
+ " slice , Invalid arguments: first=-20, last=0" )
110
110
call check(slice(test_string, last=- 3 , stride=- 2 ) == " zxvtrpnljhfdb" , &
111
- " Slice , Invalid arguments: last=-3, stride=-2" ) ! first=+inf
111
+ " slice , Invalid arguments: last=-3, stride=-2" ) ! first=+inf
112
112
call check(slice(test_string, last= 10 , stride= 0 ) == " abcdefghij" , &
113
- " Slice , Invalid arguments: last=10, stride=0" ) ! stride=1
113
+ " slice , Invalid arguments: last=10, stride=0" ) ! stride=1
114
114
call check(slice(test_string, first=- 2 , stride=- 2 ) == " " , &
115
- " Slice , Invalid arguments: first=-2, stride=-2" ) ! last=-inf
115
+ " slice , Invalid arguments: first=-2, stride=-2" ) ! last=-inf
116
116
call check(slice(test_string, first= 27 , stride= 2 ) == " " , &
117
- " Slice , Invalid arguments: first=27, stride=2" ) ! last=+inf
117
+ " slice , Invalid arguments: first=27, stride=2" ) ! last=+inf
118
118
call check(slice(test_string, last= 27 , stride=- 1 ) == " " , &
119
- " Slice , Invalid arguments: last=27, stride=-1" ) ! first=+inf
119
+ " slice , Invalid arguments: last=27, stride=-1" ) ! first=+inf
120
120
121
121
! All three arguments are given
122
122
! Valid
123
123
call check(slice(test_string, first= 2 , last= 16 , stride= 3 ) == " behkn" , &
124
- " Slice , Valid arguments: first=2, last=16, stride=3" )
124
+ " slice , Valid arguments: first=2, last=16, stride=3" )
125
125
call check(slice(test_string, first= 16 , last= 2 , stride=- 3 ) == " pmjgd" , &
126
- " Slice , Valid arguments: first=16, last=2, stride=-3" )
126
+ " slice , Valid arguments: first=16, last=2, stride=-3" )
127
127
call check(slice(test_string, first= 7 , last= 7 , stride=- 4 ) == " g" , &
128
- " Slice , Valid arguments: first=7, last=7, stride=-4" )
128
+ " slice , Valid arguments: first=7, last=7, stride=-4" )
129
129
call check(slice(test_string, first= 7 , last= 7 , stride= 3 ) == " g" , &
130
- " Slice , Valid arguments: first=7, last=7, stride=3" )
130
+ " slice , Valid arguments: first=7, last=7, stride=3" )
131
131
call check(slice(test_string, first= 2 , last= 6 , stride=- 1 ) == " " , &
132
- " Slice , Valid arguments: first=2, last=6, stride=-1" )
132
+ " slice , Valid arguments: first=2, last=6, stride=-1" )
133
133
call check(slice(test_string, first= 20 , last= 10 , stride= 2 ) == " " , &
134
- " Slice , Valid arguments: first=20, last=10, stride=2" )
134
+ " slice , Valid arguments: first=20, last=10, stride=2" )
135
135
136
136
! Atleast one argument is invalid
137
137
call check(slice(test_string, first= 20 , last= 30 , stride= 2 ) == " tvxz" , &
138
- " Slice , Invalid arguments: first=20, last=30, stride=2" )
138
+ " slice , Invalid arguments: first=20, last=30, stride=2" )
139
139
call check(slice(test_string, first=- 20 , last= 30 , stride= 2 ) == " acegikmoqsuwy" , &
140
- " Slice , Invalid arguments: first=-20, last=30, stride=2" )
140
+ " slice , Invalid arguments: first=-20, last=30, stride=2" )
141
141
call check(slice(test_string, first= 26 , last= 30 , stride= 1 ) == " z" , &
142
- " Slice , Invalid arguments: first=26, last=30, stride=1" )
142
+ " slice , Invalid arguments: first=26, last=30, stride=1" )
143
143
call check(slice(test_string, first= 1 , last=- 20 , stride=- 1 ) == " a" , &
144
- " Slice , Invalid arguments: first=1, last=-20, stride=-1" )
144
+ " slice , Invalid arguments: first=1, last=-20, stride=-1" )
145
145
call check(slice(test_string, first= 26 , last= 20 , stride= 1 ) == " " , &
146
- " Slice , Invalid arguments: first=26, last=20, stride=1" )
146
+ " slice , Invalid arguments: first=26, last=20, stride=1" )
147
147
call check(slice(test_string, first= 1 , last= 20 , stride=- 1 ) == " " , &
148
- " Slice , Invalid arguments: first=1, last=20, stride=-1" )
148
+ " slice , Invalid arguments: first=1, last=20, stride=-1" )
149
149
150
150
test_string = " "
151
151
! Empty string input
152
152
call check(slice(test_string, first=- 2 , last= 6 ) == " " , &
153
- " Slice , Empty string: first=-2, last=6" )
153
+ " slice , Empty string: first=-2, last=6" )
154
154
call check(slice(test_string, first= 6 , last=- 2 ) == " " , &
155
- " Slice , Empty string: first=6, last=-2" )
155
+ " slice , Empty string: first=6, last=-2" )
156
156
call check(slice(test_string, first=- 10 ) == " " , &
157
- " Slice , Empty string: first=-10" ) ! last=+inf
157
+ " slice , Empty string: first=-10" ) ! last=+inf
158
158
call check(slice(test_string, last= 10 ) == " " , &
159
- " Slice , Empty string: last=10" ) ! first=-inf
159
+ " slice , Empty string: last=10" ) ! first=-inf
160
160
call check(slice(test_string) == " " , &
161
- " Slice , Empty string: no arguments provided" )
161
+ " slice , Empty string: no arguments provided" )
162
162
163
163
end subroutine test_slice_string
164
164
@@ -170,27 +170,27 @@ subroutine test_find
170
170
test_pattern_2 = " abccbabc"
171
171
172
172
call check(all (find([test_string_1, test_string_2], test_pattern_1, 4 ) == [7 , 0 ]), &
173
- & ' Find : [test_string_1, test_string_2], test_pattern_1, 4' )
173
+ & ' find : [test_string_1, test_string_2], test_pattern_1, 4' )
174
174
call check(all (find(test_string_1, [test_pattern_1, test_pattern_2], 3 , .false. ) == [9 , 0 ]), &
175
- & ' Find : test_string_1, [test_pattern_1, test_pattern_2], 3, .false.' )
175
+ & ' find : test_string_1, [test_pattern_1, test_pattern_2], 3, .false.' )
176
176
call check(find(test_string_1, test_pattern_1, 7 ) == 0 , &
177
- & ' Find : test_string_1, test_pattern_1, 7' )
177
+ & ' find : test_string_1, test_pattern_1, 7' )
178
178
call check(all (find([test_string_1, test_string_2, test_string_2], [test_pattern_1, &
179
179
& test_pattern_2, test_pattern_2], [7 , 2 , 2 ], [.true. , .false. , .true. ]) == [0 , 0 , 6 ]), &
180
- & ' Find : [test_string_1, test_string_2, test_string_2], [test_pattern_1, &
180
+ & ' find : [test_string_1, test_string_2, test_string_2], [test_pattern_1, &
181
181
& test_pattern_2, test_pattern_2], [7, 2, 2], [.true., .false., .true.]' )
182
182
call check(find(" qwqwqwqwqwqwqw" , test_pattern_1) == 1 , &
183
- & ' Find : "qwqwqwqwqwqwqw", test_pattern_1' )
183
+ & ' find : "qwqwqwqwqwqwqw", test_pattern_1' )
184
184
call check(all (find(test_string_1, [" qwq" , " wqw" ], 2 ) == [3 , 4 ]), &
185
- & ' Find : test_string_1, ["qwq", "wqw"], 2' )
185
+ & ' find : test_string_1, ["qwq", "wqw"], 2' )
186
186
call check(find(" qwqwqwqwqwqwqw" , " qwq" , 2 , .false. ) == 5 , &
187
- & ' Find : "qwqwqwqwqwqwqw", "qwq", 2, .false.' )
187
+ & ' find : "qwqwqwqwqwqwqw", "qwq", 2, .false.' )
188
188
call check(find(" " , " " ) == 0 , &
189
- & ' Find : "", ""' )
189
+ & ' find : "", ""' )
190
190
call check(find(" " , test_pattern_1) == 0 , &
191
- & ' Find : "", test_pattern_1' )
191
+ & ' find : "", test_pattern_1' )
192
192
call check(find(test_string_1, " " ) == 0 , &
193
- & ' Find : test_string_1, ""' )
193
+ & ' find : test_string_1, ""' )
194
194
195
195
end subroutine test_find
196
196
@@ -318,6 +318,66 @@ pure function carray_to_string(carray) result(string)
318
318
string = transfer (carray, string)
319
319
end function carray_to_string
320
320
321
+ subroutine test_replace_all
322
+ type (string_type) :: test_string_1, test_pattern_1, test_replacement_1
323
+ type (string_type) :: test_string_2, test_pattern_2, test_replacement_2
324
+ test_string_1 = " mutate DNA sequence: GTTATCGTATGCCGTAATTAT"
325
+ test_pattern_1 = " TAT"
326
+ test_replacement_1 = " ATA"
327
+ test_string_2 = " mutate DNA sequence: AGAGAGCCTAGAGAGAG"
328
+ test_pattern_2 = " AGA"
329
+ test_replacement_2 = " aga"
330
+
331
+ ! all 3 as string_type
332
+ call check(replace_all(test_string_1, test_pattern_1, test_replacement_1) == &
333
+ & " mutate DNA sequence: GTATACGATAGCCGTAATATA" , &
334
+ & " replace_all: all 3 string_type, test case 1" )
335
+ call check(replace_all(test_string_2, test_pattern_2, test_replacement_2) == &
336
+ & " mutate DNA sequence: agaGAGCCTagaGagaG" , &
337
+ & " replace_all: all 3 string_type, test case 2" )
338
+ call check(replace_all(test_string_2, test_pattern_2, test_replacement_1) == &
339
+ & " mutate DNA sequence: ATAGAGCCTATAGATAG" , &
340
+ & " replace_all: all 3 string_type, test case 3" )
341
+
342
+ ! 2 as string_type and 1 as character scalar
343
+ call check(replace_all(test_string_1, " tat" , test_replacement_1) == &
344
+ & " muATAe DNA sequence: GTTATCGTATGCCGTAATTAT" , &
345
+ & " replace_all: 2 string_type & 1 character scalar, test case 1" )
346
+ call check(replace_all(test_string_2, test_pattern_2, " GC" ) == &
347
+ & " mutate DNA sequence: GCGAGCCTGCGGCG" , &
348
+ & " replace_all: 2 string_type & 1 character scalar, test case 2" )
349
+ call check(replace_all(" mutate DNA sequence: AGAGAGCCTAGAGAGAG" , test_pattern_2, &
350
+ & test_replacement_2) == " mutate DNA sequence: agaGAGCCTagaGagaG" , &
351
+ & " replace_all: 2 string_type & 1 character scalar, test case 3" )
352
+
353
+
354
+ ! 1 as string_type and 2 as character scalar
355
+ call check(replace_all(test_string_1, " TAT" , " ATA" ) == &
356
+ & " mutate DNA sequence: GTATACGATAGCCGTAATATA" , &
357
+ & " replace_all: 1 string_type & 2 character scalar, test case 1" )
358
+ call check(replace_all(" mutate DNA sequence: AGAGAGCCTAGAGAGAG" , test_pattern_2, &
359
+ & " GC" ) == " mutate DNA sequence: GCGAGCCTGCGGCG" , &
360
+ & " replace_all: 1 string_type & 2 character scalar, test case 2" )
361
+ call check(replace_all(" mutate DNA sequence: GTTATCGTATGCCGTAATTAT" , " TA" , &
362
+ & test_replacement_2) == " mutate DNA sequence: GTagaTCGagaTGCCGagaATagaT" , &
363
+ & " replace_all: 1 string_type & 2 character scalar, test case 3" )
364
+ call check(replace_all(" mutate DNA sequence: GTTATCGTATGCCGTAATTAT" , &
365
+ & test_pattern_1, " " ) == " mutate DNA sequence: GTCGGCCGTAAT" , &
366
+ & " replace_all: 1 string_type & 2 character scalar, test case 4" )
367
+ call check(replace_all(test_string_1, " " , " anything here" ) == test_string_1, &
368
+ & " replace_all: 1 string_type & 2 character scalar, test case 5" )
369
+ call check(replace_all(" " , test_pattern_2, " anything here" ) == " " , &
370
+ & " replace_all: 1 string_type & 2 character scalar, test case 6" )
371
+
372
+ ! all 3 as character scalar
373
+ call check(replace_all(" mutate DNA sequence: GTTATCGTATGCCGTAATTAT" , &
374
+ & " GT" , " gct" ) == " mutate DNA sequence: gctTATCgctATGCCgctAATTAT" , &
375
+ & " replace_all: all 3 character scalar, test case 1" )
376
+ call check(replace_all(" " , " anything here" , " anything here" ) == " " , &
377
+ & " replace_all: all 3 character scalar, test case 2" )
378
+
379
+ end subroutine test_replace_all
380
+
321
381
end module test_string_functions
322
382
323
383
@@ -333,5 +393,6 @@ program tester
333
393
call test_slice_string
334
394
call test_slice_gen
335
395
call test_find
396
+ call test_replace_all
336
397
337
398
end program tester
0 commit comments