fix -Wsign-compare in core

There were a few places that were doing

    unsigned_var = cond ? signed_val : unsigned_val;

or similar. Fixed by suitable casts etc.

The four in utf8.c were fixed by assigning to an intermediate
unsigned var; this has the happy side-effect of collapsing
a large macro expansion, where toUPPER_LC() etc evaluate their arg
multiple times.