Skip to content

Commit 4e923b4

Browse files
committed
made find function elemental, updated test cases and documentation accordingly
1 parent c63d065 commit 4e923b4

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

doc/specs/stdlib_strings.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Experimental
294294

295295
#### Class
296296

297-
Pure function
297+
Elemental function
298298

299299
#### Argument
300300

@@ -309,7 +309,7 @@ Pure function
309309

310310
#### Result value
311311

312-
The result is of scalar integer type.
312+
The result is a scalar of integer type or integer array of rank equal to the highest rank among all dummy arguments.
313313

314314
#### Example
315315

@@ -320,12 +320,11 @@ program demo_find
320320
implicit none
321321
string_type :: string
322322
323-
string = "needle in this character-stack"
323+
string = "needle in the character-stack"
324324
325-
print *, find(string, "needle") ! 1
326-
print *, find(string, "a", 3) ! 28
327-
print *, find("qwqwqwq", "qwq", 3, .false.) ! 0
328-
print *, find("qwqwqwq", "qwq", 3, .true.) ! 5
325+
print *, find(string, "needle") ! 1
326+
print *, find(string, ["a", "c"], [3, 2]) ! [27, 20]
327+
print *, find("qwqwqwq", "qwq", 3, [.false., .true.]) ! [0, 5]
329328
330329
end program demo_find
331330
```

src/stdlib_strings.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ end function slice_char
379379
!> Returns the starting index of the 'occurrence'th occurrence of substring 'pattern'
380380
!> in input 'string'
381381
!> Returns an integer
382-
pure function find_string_string(string, pattern, occurrence, consider_overlapping) result(res)
382+
elemental function find_string_string(string, pattern, occurrence, consider_overlapping) result(res)
383383
type(string_type), intent(in) :: string
384384
type(string_type), intent(in) :: pattern
385385
integer, intent(in), optional :: occurrence
@@ -393,7 +393,7 @@ end function find_string_string
393393
!> Returns the starting index of the 'occurrence'th occurrence of substring 'pattern'
394394
!> in input 'string'
395395
!> Returns an integer
396-
pure function find_string_char(string, pattern, occurrence, consider_overlapping) result(res)
396+
elemental function find_string_char(string, pattern, occurrence, consider_overlapping) result(res)
397397
type(string_type), intent(in) :: string
398398
character(len=*), intent(in) :: pattern
399399
integer, intent(in), optional :: occurrence
@@ -407,7 +407,7 @@ end function find_string_char
407407
!> Returns the starting index of the 'occurrence'th occurrence of substring 'pattern'
408408
!> in input 'string'
409409
!> Returns an integer
410-
pure function find_char_string(string, pattern, occurrence, consider_overlapping) result(res)
410+
elemental function find_char_string(string, pattern, occurrence, consider_overlapping) result(res)
411411
character(len=*), intent(in) :: string
412412
type(string_type), intent(in) :: pattern
413413
integer, intent(in), optional :: occurrence
@@ -421,7 +421,7 @@ end function find_char_string
421421
!> Returns the starting index of the 'occurrence'th occurrence of substring 'pattern'
422422
!> in input 'string'
423423
!> Returns an integer
424-
pure function find_char_char(string, pattern, occurrence, consider_overlapping) result(res)
424+
elemental function find_char_char(string, pattern, occurrence, consider_overlapping) result(res)
425425
character(len=*), intent(in) :: string
426426
character(len=*), intent(in) :: pattern
427427
integer, intent(in), optional :: occurrence

src/tests/string/test_string_functions.f90

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,34 @@ subroutine test_slice_string
163163
end subroutine test_slice_string
164164

165165
subroutine test_find
166-
type(string_type) :: test_string, test_pattern
167-
test_string = "qwqwqwqwqwqwqw"
168-
test_pattern = "qwq"
169-
call check(find(test_string, test_pattern, 4) == 7, &
170-
& 'Find: test_string, test_pattern, 4')
171-
call check(find(test_string, test_pattern, 3, .false.) == 9, &
172-
& 'Find: test_string, test_pattern, 3')
173-
call check(find(test_string, test_pattern, 7) == 0, &
174-
& 'Find: test_string, test_pattern, 7')
175-
call check(find("qwqwqwqwqwqwqw", test_pattern) == 1, &
176-
& 'Find: "qwqwqwqwqwqwqw", test_pattern')
177-
call check(find(test_string, "qwq", 2) == 3, &
178-
& 'Find: test_string, "qwq", 2')
166+
type(string_type) :: test_string_1, test_string_2, test_pattern_1, test_pattern_2
167+
test_string_1 = "qwqwqwqwqwqwqw"
168+
test_string_2 = "abccbabccbabc"
169+
test_pattern_1 = "qwq"
170+
test_pattern_2 = "abccbabc"
171+
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')
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.')
176+
call check(find(test_string_1, test_pattern_1, 7) == 0, &
177+
& 'Find: test_string_1, test_pattern_1, 7')
178+
call check(all(find([test_string_1, test_string_2, test_string_2], [test_pattern_1, &
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, &
181+
& test_pattern_2, test_pattern_2], [7, 2, 2], [.true., .false., .true.]')
182+
call check(find("qwqwqwqwqwqwqw", test_pattern_1) == 1, &
183+
& 'Find: "qwqwqwqwqwqwqw", test_pattern_1')
184+
call check(all(find(test_string_1, ["qwq", "wqw"], 2) == [3, 4]), &
185+
& 'Find: test_string_1, ["qwq", "wqw"], 2')
179186
call check(find("qwqwqwqwqwqwqw", "qwq", 2, .false.) == 5, &
180187
& 'Find: "qwqwqwqwqwqwqw", "qwq", 2, .false.')
181188
call check(find("", "") == 0, &
182189
& 'Find: "", ""')
183-
call check(find("", test_pattern) == 0, &
184-
& 'Find: "", test_pattern')
185-
call check(find(test_string, "") == 0, &
186-
& 'Find: test_string, ""')
190+
call check(find("", test_pattern_1) == 0, &
191+
& 'Find: "", test_pattern_1')
192+
call check(find(test_string_1, "") == 0, &
193+
& 'Find: test_string_1, ""')
187194

188195
end subroutine test_find
189196

0 commit comments

Comments
 (0)