Cleanup: extract implementation of SEARCH into gutils.c

We need it for conditional formats.