[m-rev.] for review: Avoid intermediate strings in string.replace.

Mark Brown mark at mercurylang.org
Thu Nov 7 20:52:23 AEDT 2019


This looks fine.

On Thu, Nov 7, 2019 at 3:21 PM Peter Wang <novalazy at gmail.com> wrote:
>
> library/string.m:
>     Implement string.replace using unsafe_append_string_pieces.
> ---
>  library/string.m | 16 +++++++---------
>  1 file changed, 7 insertions(+), 9 deletions(-)
>
> diff --git a/library/string.m b/library/string.m
> index 8e7802e3c..95d944926 100644
> --- a/library/string.m
> +++ b/library/string.m
> @@ -4818,15 +4818,13 @@ rstrip_pred(P, S0) = S :-
>  %---------------------%
>
>  replace(Str, Pat, Subst, Result) :-
> -    sub_string_search(Str, Pat, Index),
> -
> -    Initial = unsafe_between(Str, 0, Index),
> -
> -    BeginAt = Index + length(Pat),
> -    EndAt = length(Str),
> -    Final = unsafe_between(Str, BeginAt, EndAt),
> -
> -    Result = append_list([Initial, Subst, Final]).
> +    sub_string_search(Str, Pat, PatStart),
> +    Pieces = [
> +        substring(Str, 0, PatStart),
> +        substring(Subst, 0, length(Subst)),
> +        substring(Str, PatStart + length(Pat), length(Str))
> +    ],
> +    unsafe_append_string_pieces(Pieces, Result).
>
>  replace_all(S1, S2, S3) = S4 :-
>      replace_all(S1, S2, S3, S4).
> --
> 2.23.0
>
> _______________________________________________
> reviews mailing list
> reviews at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/reviews


More information about the reviews mailing list