Please review.
LGTM, committed (with slight modification of renaming the function and putting in an anonymous namespace) as r4271.