aboutsummaryrefslogtreecommitdiff
path: root/util/stringprintf.h
blob: 23f07fea94c141812ad003f1147f8808d1b6377a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UTIL_STRINGPRINTF_H_
#define UTIL_STRINGPRINTF_H_

#include <stdint.h>

#include <ostream>
#include <string>

namespace openscreen {

// Enable compile-time checking of the printf format argument, if available.
#if defined(__GNUC__) || defined(__clang__)
#define OSP_CHECK_PRINTF_ARGS(format_param, dots_param) \
  __attribute__((format(printf, format_param, dots_param)))
#else
#define OSP_CHECK_PRINTF_ARGS(format_param, dots_param)
#endif

// Returns a std::string containing the results of a std::sprintf() call. This
// is an efficient, zero-copy wrapper.
[[nodiscard]] std::string StringPrintf(const char* format, ...)
    OSP_CHECK_PRINTF_ARGS(1, 2);

template <typename It>
void PrettyPrintAsciiHex(std::ostream& os, It first, It last) {
  auto it = first;
  while (it != last) {
    uint8_t c = *it++;
    if (c >= ' ' && c <= '~') {
      os.put(c);
    } else {
      // Output a hex escape sequence for non-printable values.
      os.put('\\');
      os.put('x');
      char digit = (c >> 4) & 0xf;
      if (digit >= 0 && digit <= 9) {
        os.put(digit + '0');
      } else {
        os.put(digit - 10 + 'a');
      }
      digit = c & 0xf;
      if (digit >= 0 && digit <= 9) {
        os.put(digit + '0');
      } else {
        os.put(digit - 10 + 'a');
      }
    }
  }
}

// Returns a hex string representation of the given |bytes|.
std::string HexEncode(const uint8_t* bytes, std::size_t len);

}  // namespace openscreen

#endif  // UTIL_STRINGPRINTF_H_