Revert "simplify CellPattern.replace rigid logic"

This reverts commit 19d0e279d9b4723c93a8362c76846de215bcb014.
added unit test
compiled