@@ -649,17 +649,17 @@ replace_err(repl) = error("Bad replacement string: $repl")
649649function _write_capture (io:: IO , group:: Int , str, r, re:: RegexAndMatchData )
650650 len = PCRE. substring_length_bynumber (re. match_data, group)
651651 # in the case of an optional group that doesn't match, len == 0
652- len == 0 && return
652+ len == 0 && return len
653653 ensureroom (io, len+ 1 )
654654 PCRE. substring_copy_bynumber (re. match_data, group,
655655 pointer (io. data, io. ptr), len+ 1 )
656656 io. ptr += len
657657 io. size = max (io. size, io. ptr - 1 )
658- nothing
658+ return len
659659end
660660function _write_capture (io:: IO , group:: Int , str, r, re)
661661 group == 0 || replace_err (" pattern is not a Regex" )
662- return print (io, SubString (str, r))
662+ return write (io, SubString (str, r))
663663end
664664
665665
@@ -673,12 +673,13 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
673673 repl = unescape_string (repl_s. string, KEEP_ESC)
674674 i = firstindex (repl)
675675 e = lastindex (repl)
676+ nb = 0
676677 while i <= e
677678 if repl[i] == SUB_CHAR
678679 next_i = nextind (repl, i)
679680 next_i > e && replace_err (repl)
680681 if repl[next_i] == SUB_CHAR
681- write (io, SUB_CHAR)
682+ nb += write (io, SUB_CHAR)
682683 i = nextind (repl, next_i)
683684 elseif isdigit (repl[next_i])
684685 group = parse (Int, repl[next_i])
@@ -691,7 +692,7 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
691692 break
692693 end
693694 end
694- _write_capture (io, group, str, r, re)
695+ nb += _write_capture (io, group, str, r, re)
695696 elseif repl[next_i] == GROUP_CHAR
696697 i = nextind (repl, next_i)
697698 if i > e || repl[i] != LBRACKET
@@ -713,16 +714,17 @@ function _replace(io, repl_s::SubstitutionString, str, r, re)
713714 else
714715 group = - 1
715716 end
716- _write_capture (io, group, str, r, re)
717+ nb += _write_capture (io, group, str, r, re)
717718 i = nextind (repl, i)
718719 else
719720 replace_err (repl)
720721 end
721722 else
722- write (io, repl[i])
723+ nb += write (io, repl[i])
723724 i = nextind (repl, i)
724725 end
725726 end
727+ nb
726728end
727729
728730struct RegexMatchIterator{S <: AbstractString }
0 commit comments