Skip to content
  • Jeff King's avatar
    write_file: add pointer+len variant · 52563d7e
    Jeff King authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    There are many callsites which could use write_file, but for
    which it is a little awkward because they have a strbuf or
    other pointer/len combo. Specifically:
    
     1. write_file() takes a format string, so we have to use
        "%s" or "%.*s", which are ugly.
    
     2. Using any form of "%s" does not handle embedded NULs in
        the output. That probably doesn't matter for our
        call-sites, but it's nicer not to have to worry.
    
     3. It's less efficient; we format into another strbuf
        just to do the write. That's probably not measurably
        slow for our uses, but it's simply inelegant.
    
    We can fix this by providing a helper to write out the
    formatted buffer, and just calling it from write_file().
    
    Note that we don't do the usual "complete with a newline"
    that write_file does. If the caller has their own buffer,
    there's a reasonable chance they're doing something more
    complicated than a single line, and they can call
    strbuf_complete_line() themselves.
    
    We could go even further and add strbuf_write_file(), but it
    doesn't save much:
    
      -  write_file_buf(path, sb.buf, sb.len);
      +  strbuf_write_file(&sb, path);
    
    It would also be somewhat asymmetric with strbuf_read_file,
    which actually returns errors rather than dying (and the
    error handling is most of the benefit of write_file() in the
    first place).
    
    Signed-off-by: default avatarJeff King <peff@peff.net>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    52563d7e