LCOV - code coverage report
Current view: top level - src - buffer.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 42 42 100.0 %
Date: 1970-01-01 00:00:01 Functions: 5 5 100.0 %
Branches: 4 4 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2016 Andrew Kelley
       3                 :            :  *
       4                 :            :  * This file is part of zig, which is MIT licensed.
       5                 :            :  * See http://opensource.org/licenses/MIT
       6                 :            :  */
       7                 :            : 
       8                 :            : #include "buffer.hpp"
       9                 :            : #include <stdarg.h>
      10                 :            : #include <stdlib.h>
      11                 :            : #include <stdio.h>
      12                 :            : 
      13                 :      16188 : Buf *buf_vprintf(const char *format, va_list ap) {
      14                 :            :     va_list ap2;
      15                 :      16188 :     va_copy(ap2, ap);
      16                 :            : 
      17                 :      16188 :     int len1 = vsnprintf(nullptr, 0, format, ap);
      18                 :      16188 :     assert(len1 >= 0);
      19                 :            : 
      20                 :      16188 :     size_t required_size = len1 + 1;
      21                 :            : 
      22                 :      16188 :     Buf *buf = buf_alloc_fixed(len1);
      23                 :            : 
      24                 :      16188 :     int len2 = vsnprintf(buf_ptr(buf), required_size, format, ap2);
      25                 :      16188 :     assert(len2 == len1);
      26                 :            : 
      27                 :      16188 :     va_end(ap2);
      28                 :            : 
      29                 :      16188 :     return buf;
      30                 :            : }
      31                 :            : 
      32                 :      16188 : Buf *buf_sprintf(const char *format, ...) {
      33                 :            :     va_list ap;
      34                 :      16188 :     va_start(ap, format);
      35                 :      16188 :     Buf *result = buf_vprintf(format, ap);
      36                 :      16188 :     va_end(ap);
      37                 :      16188 :     return result;
      38                 :            : }
      39                 :            : 
      40                 :     137045 : void buf_appendf(Buf *buf, const char *format, ...) {
      41                 :     137045 :     assert(buf->list.length);
      42                 :            :     va_list ap, ap2;
      43                 :     137045 :     va_start(ap, format);
      44                 :     137045 :     va_copy(ap2, ap);
      45                 :            : 
      46                 :     137045 :     int len1 = vsnprintf(nullptr, 0, format, ap);
      47                 :     137045 :     assert(len1 >= 0);
      48                 :            : 
      49                 :     137045 :     size_t required_size = len1 + 1;
      50                 :            : 
      51                 :     137045 :     size_t orig_len = buf_len(buf);
      52                 :            : 
      53                 :     137045 :     buf_resize(buf, orig_len + len1);
      54                 :            : 
      55                 :     137045 :     int len2 = vsnprintf(buf_ptr(buf) + orig_len, required_size, format, ap2);
      56                 :     137045 :     assert(len2 == len1);
      57                 :            : 
      58                 :     137045 :     va_end(ap2);
      59                 :     137045 :     va_end(ap);
      60                 :     137045 : }
      61                 :            : 
      62                 :            : // these functions are not static inline so they can be better used as template parameters
      63                 :    9632190 : bool buf_eql_buf(Buf *buf, Buf *other) {
      64                 :    9632190 :     assert(buf->list.length);
      65                 :    9632190 :     return buf_eql_mem(buf, buf_ptr(other), buf_len(other));
      66                 :            : }
      67                 :            : 
      68                 :    3924642 : uint32_t buf_hash(Buf *buf) {
      69                 :    3924642 :     assert(buf->list.length);
      70                 :    3924642 :     size_t interval = buf->list.length / 256;
      71         [ +  + ]:    3924642 :     if (interval == 0)
      72                 :    3924087 :         interval = 1;
      73                 :            :     // FNV 32-bit hash
      74                 :    3924642 :     uint32_t h = 2166136261;
      75         [ +  + ]:   33711541 :     for (size_t i = 0; i < buf_len(buf); i += interval) {
      76                 :   29786899 :         h = h ^ ((uint8_t)buf->list.at(i));
      77                 :   29786899 :         h = h * 16777619;
      78                 :            :     }
      79                 :    3924642 :     return h;
      80                 :            : }

Generated by: LCOV version 1.14