aboutsummaryrefslogtreecommitdiff
path: root/util/stringprintf.h
diff options
context:
space:
mode:
Diffstat (limited to 'util/stringprintf.h')
-rw-r--r--util/stringprintf.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/util/stringprintf.h b/util/stringprintf.h
new file mode 100644
index 00000000..93e5eb93
--- /dev/null
+++ b/util/stringprintf.h
@@ -0,0 +1,41 @@
+// 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 <ostream>
+
+namespace openscreen {
+
+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');
+ }
+ }
+ }
+}
+
+} // namespace openscreen
+
+#endif // UTIL_STRINGPRINTF_H_