@@ -12,7 +12,7 @@ module stdlib_strings
12
12
13
13
public :: strip, chomp
14
14
public :: starts_with, ends_with
15
- public :: slice, find
15
+ public :: slice, find, replace_all
16
16
17
17
18
18
! > Remove leading and trailing whitespace characters.
@@ -79,6 +79,20 @@ module stdlib_strings
79
79
module procedure :: find_char_char
80
80
end interface find
81
81
82
+ ! > Replaces all the occurrences of substring 'pattern' in the input 'string'
83
+ ! > with the replacement 'replacement'
84
+ ! > Version: experimental
85
+ interface replace_all
86
+ module procedure :: replace_all_string_string_string
87
+ module procedure :: replace_all_string_string_char
88
+ module procedure :: replace_all_string_char_string
89
+ module procedure :: replace_all_char_string_string
90
+ module procedure :: replace_all_string_char_char
91
+ module procedure :: replace_all_char_string_char
92
+ module procedure :: replace_all_char_char_string
93
+ module procedure :: replace_all_char_char_char
94
+ end interface replace_all
95
+
82
96
contains
83
97
84
98
@@ -353,7 +367,7 @@ pure function slice_char(string, first, last, stride) result(sliced_string)
353
367
end if
354
368
355
369
if (present (first)) then
356
- first_index = first
370
+ first_index = first
357
371
end if
358
372
if (present (last)) then
359
373
last_index = last
@@ -499,5 +513,140 @@ pure function compute_lps(string) result(lps_array)
499
513
500
514
end function compute_lps
501
515
516
+ ! > Replaces all occurrences of substring 'pattern' in the input 'string'
517
+ ! > with the replacement 'replacement'
518
+ ! > Returns a new string
519
+ pure function replace_all_string_string_string (string , pattern , replacement ) result(res)
520
+ type (string_type), intent (in ) :: string
521
+ type (string_type), intent (in ) :: pattern
522
+ type (string_type), intent (in ) :: replacement
523
+ type (string_type) :: res
524
+
525
+ res = string_type(replace_all(char (string), &
526
+ & char (pattern), char (replacement)))
527
+
528
+ end function replace_all_string_string_string
529
+
530
+ ! > Replaces all occurrences of substring 'pattern' in the input 'string'
531
+ ! > with the replacement 'replacement'
532
+ ! > Returns a new string
533
+ pure function replace_all_string_string_char (string , pattern , replacement ) result(res)
534
+ type (string_type), intent (in ) :: string
535
+ type (string_type), intent (in ) :: pattern
536
+ character (len=* ), intent (in ) :: replacement
537
+ type (string_type) :: res
538
+
539
+ res = string_type(replace_all(char (string), char (pattern), replacement))
540
+
541
+ end function replace_all_string_string_char
542
+
543
+ ! > Replaces all occurrences of substring 'pattern' in the input 'string'
544
+ ! > with the replacement 'replacement'
545
+ ! > Returns a new string
546
+ pure function replace_all_string_char_string (string , pattern , replacement ) result(res)
547
+ type (string_type), intent (in ) :: string
548
+ character (len=* ), intent (in ) :: pattern
549
+ type (string_type), intent (in ) :: replacement
550
+ type (string_type) :: res
551
+
552
+ res = string_type(replace_all(char (string), pattern, char (replacement)))
553
+
554
+ end function replace_all_string_char_string
555
+
556
+ ! > Replaces all occurrences of substring 'pattern' in the input 'string'
557
+ ! > with the replacement 'replacement'
558
+ ! > Returns a new string
559
+ pure function replace_all_char_string_string (string , pattern , replacement ) result(res)
560
+ character (len=* ), intent (in ) :: string
561
+ type (string_type), intent (in ) :: pattern
562
+ type (string_type), intent (in ) :: replacement
563
+ character (len= :), allocatable :: res
564
+
565
+ res = replace_all(string, char (pattern), char (replacement))
566
+
567
+ end function replace_all_char_string_string
568
+
569
+ ! > Replaces all occurrences of substring 'pattern' in the input 'string'
570
+ ! > with the replacement 'replacement'
571
+ ! > Returns a new string
572
+ pure function replace_all_string_char_char (string , pattern , replacement ) result(res)
573
+ type (string_type), intent (in ) :: string
574
+ character (len=* ), intent (in ) :: pattern
575
+ character (len=* ), intent (in ) :: replacement
576
+ type (string_type) :: res
577
+
578
+ res = string_type(replace_all(char (string), pattern, replacement))
579
+
580
+ end function replace_all_string_char_char
581
+
582
+ ! > Replaces all occurrences of substring 'pattern' in the input 'string'
583
+ ! > with the replacement 'replacement'
584
+ ! > Returns a new string
585
+ pure function replace_all_char_string_char (string , pattern , replacement ) result(res)
586
+ character (len=* ), intent (in ) :: string
587
+ type (string_type), intent (in ) :: pattern
588
+ character (len=* ), intent (in ) :: replacement
589
+ character (len= :), allocatable :: res
590
+
591
+ res = replace_all(string, char (pattern), replacement)
592
+
593
+ end function replace_all_char_string_char
594
+
595
+ ! > Replaces all occurrences of substring 'pattern' in the input 'string'
596
+ ! > with the replacement 'replacement'
597
+ ! > Returns a new string
598
+ pure function replace_all_char_char_string (string , pattern , replacement ) result(res)
599
+ character (len=* ), intent (in ) :: string
600
+ character (len=* ), intent (in ) :: pattern
601
+ type (string_type), intent (in ) :: replacement
602
+ character (len= :), allocatable :: res
603
+
604
+ res = replace_all(string, pattern, char (replacement))
605
+
606
+ end function replace_all_char_char_string
607
+
608
+ ! > Replaces all the occurrences of substring 'pattern' in the input 'string'
609
+ ! > with the replacement 'replacement'
610
+ ! > Returns a new string
611
+ pure function replace_all_char_char_char (string , pattern , replacement ) result(res)
612
+ character (len=* ), intent (in ) :: string
613
+ character (len=* ), intent (in ) :: pattern
614
+ character (len=* ), intent (in ) :: replacement
615
+ character (len= :), allocatable :: res
616
+ integer :: lps_array(len (pattern))
617
+ integer :: s_i, p_i, last, length_string, length_pattern
618
+
619
+ res = " "
620
+ length_string = len (string)
621
+ length_pattern = len (pattern)
622
+ last = 1
623
+
624
+ if (length_pattern > 0 .and. length_pattern <= length_string) then
625
+ lps_array = compute_lps(pattern)
626
+
627
+ s_i = 1
628
+ p_i = 1
629
+ do while (s_i <= length_string)
630
+ if (string (s_i:s_i) == pattern(p_i:p_i)) then
631
+ if (p_i == length_pattern) then
632
+ res = res // &
633
+ & string (last : s_i - length_pattern) // &
634
+ & replacement
635
+ last = s_i + 1
636
+ p_i = 0
637
+ end if
638
+ s_i = s_i + 1
639
+ p_i = p_i + 1
640
+ else if (p_i > 1 ) then
641
+ p_i = lps_array(p_i - 1 ) + 1
642
+ else
643
+ s_i = s_i + 1
644
+ end if
645
+ end do
646
+ end if
647
+
648
+ res = res // string (last : length_string)
649
+
650
+ end function replace_all_char_char_char
502
651
503
652
end module stdlib_strings
0 commit comments