hhvm/hsl

function to translate characters or replace substrings

azjezz opened this issue · 1 comments

PHP provides strtr function to do this, but in HSL there's no equivalent.
all HSLs replace* functions use the str_replace function internally which have different behavior from strtr

strtr will not replace in parts of the string that already have been replaced - str_replace will replace inside replaces.

example :

<?hh

namespace Issue;

use namespace HH\Lib\Str;
use function strtr;

<<__Entrypoint>>
function main(): void
{
    $str = "ZBB2";
 
    $replace = dict[
         "1" => "A",
         "2" => "B", 
         "3" => "C", 
         "B" => "D"
    ];
  
     // ZDDD
     echo Str\replace_every($str, $replace);

     // ZDDB
     echo strtr($str, $replace);
}

strtr : https://secure.php.net/manual/en/function.strtr.php
str_replace : https://secure.php.net/manual/en/function.str-replace.php

Fixed when Str\replace_every_nonrecursive() was added in June of 2020.