Can't you do this in lwes_event:write_attrs and avoid this fold completely?  Actually, yeah looking at it, we should just let write_attrs call write (string,...), then have write (string, ...) do the length check into a temporary and have 2 clauses in the case for the normal form and the long form.  That would be most efficient and mean you wouldn't need another loop here.