Wouldn't it be better to change `i` to a `size_t` -- that way it's unsigned and you can drop the later `size_t` casts? The `length()`s should be cached as well... entire thing is pretty ugly.