(a) Except as otherwise provided in subsection (b) of this section, if a particular charitable purpose becomes unlawful, impracticable, impossible to achieve, or wasteful:
(1) The charitable trust does not fail, in whole or in part;
(2) The charitable trust property does not revert to the grantor or the grantor's successors in interest; and
(3) Upon petition by a trustee or a person having a special interest in the trust, the court shall apply cy pres to fulfill as nearly as possible the grantor's charitable intention, whether it be general or specific.
(b) A provision in the terms of a charitable trust that would result in distribution of the trust property to a noncharitable beneficiary prevails over the power of the court under subsection (a) of this section to apply cy pres to modify or terminate the trust only if, when the provision takes effect:
(1) The charitable trust property is to revert to the grantor and the grantor is still living; or
(2) Fewer than twenty-one years have elapsed since the date of the trust's creation.