@Test
public void testNestedBounds() {

    final String prefix = "<tag";
    final String suffix = "/tag>";
    final TextUtils.ReplacementGenerator generator = new TextUtils.ReplacementGenerator() {
        @Override
        public void appendReplacementForMatch(int matchStart, int matchEnd, char[] initialStringAsCharArray, StringBuilder builder, String prefix, String suffix) {
            int firstQuoteIndex = matchStart;
            while (initialStringAsCharArray[firstQuoteIndex++] != '\'') ;
            int secondQuoteIndex = firstQuoteIndex + 1;
            while (initialStringAsCharArray[secondQuoteIndex++] != '\'') ;
            builder.append("<new src='").append(initialStringAsCharArray, firstQuoteIndex, secondQuoteIndex - firstQuoteIndex - 1).append("'></new>");
        }
    };
    String initial = "aaa<tag src='1'>bbb<tag src='2'>ccc<tag src='3'>dddd<tag src='4'>eee</tag>ddd</tag>cccc</tag>bbb</tag>aaa<tag src='5'>foo</tag>";
    String expected = "aaa<new src='1'></new>aaa<new src='5'></new>";
    assertEquals(expected, TextUtils.replaceBoundedString(initial, prefix, suffix, generator));
    initial = "aaa<tag src='1'>bbb<tag src='2'>ccc</tag>  <tag src='3'>dddd<tag src='4'>eee</tag>ddd</tag> bbb</tag> aaa <tag src='5'>foo</tag>    ffff";
    expected = "aaa<new src='1'></new> aaa <new src='5'></new>    ffff";
    assertEquals(expected, TextUtils.replaceBoundedString(initial, prefix, suffix, generator));
    initial = "foo <tag src='1'> aaaa\n\n           <tag src='2'>\n bbbbbb </tag>\n\t    cccccccc    <tag src='3'>\n" +
            " dddd     </tag>\n eee <tag src='4'>\n    ffff </tag>\n g <tag src='5'>\n gggg </tag>\n hhhh </tag> bar";
    expected = "foo <new src='1'></new> bar";
    assertEquals(expected, TextUtils.replaceBoundedString(initial, prefix, suffix, generator));
}