public static String replaceAll(String s, final String what, final String withWhat)
  {
    StringBuilder sb = null;
    int fromIndex = 0;     //index of last found '$' char + 1
    boolean found = false;
    int length = s.length();
    do
    {
      final int index = s.indexOf(what, fromIndex);
      found = index >= 0;
      if (found)
      {
        if (sb == null)
          sb = new StringBuilder(s.length() * 2);

        sb.append(s, fromIndex, index);
        sb.append(withWhat);

        fromIndex = index + what.length();
      }
    } while (found && fromIndex < length);

    if (sb != null)
    {

      if (fromIndex < length)
        sb.append(s, fromIndex, length);

      return sb.toString();
    } else
    {
      return s;
    }
  }
@Test
  public void testUnescaping()
  {
    assertEquals(Escaper.replaceAll("", "$$", "$"), "");
    assertEquals(Escaper.replaceAll("$$", "$$", "$"), "$");
    assertEquals(Escaper.replaceAll(" $$", "$$", "$"), " $");
    assertEquals(Escaper.replaceAll(" $$ ", "$$", "$"), " $ ");
    assertEquals(Escaper.replaceAll(" $$$$ ", "$$", "$"), " $$ ");
    assertEquals(Escaper.replaceAll("$$ $$ ", "$$", "$"), "$ $ ");
    assertEquals(Escaper.replaceAll("$$ $$ $$$$", "$$", "$"), "$ $ $$");
    assertEquals(Escaper.replaceAll("$$start", "$$", "$"), "$start");
    assertEquals(Escaper.replaceAll("$$*$$", "$$", "$"), "$*$");

    assertEquals(Escaper.replaceAll(" $$$ ", "$$", "$"), " $$ ");  //first two dollars are unescaped
  }