/// \brief Write the contents (regardless of whether it is binary or a
/// hex string) as binary to the given raw_ostream.
void writeAsBinary(raw_ostream &OS) const;
+ /// \brief Write the contents (regardless of whether it is binary or a
+ /// hex string) as hex to the given raw_ostream.
+ ///
+ /// For example, a possible output could be `DEADBEEFCAFEBABE`.
+ void writeAsHex(raw_ostream &OS) const;
};
}
void yaml::ScalarTraits<object::yaml::BinaryRef>::output(
const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
- ArrayRef<uint8_t> Data = Val.getBinary();
- for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
- ++I) {
- uint8_t Byte = *I;
- Out << hexdigit(Byte >> 4);
- Out << hexdigit(Byte & 0xf);
- }
+ Val.writeAsHex(Out);
}
// Can't find this anywhere else in the codebase (clang has one, but it has
OS.write(Byte);
}
}
+
+void BinaryRef::writeAsHex(raw_ostream &OS) const {
+ if (DataIsHexString) {
+ OS.write((const char *)Data.data(), Data.size());
+ return;
+ }
+ for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
+ ++I) {
+ uint8_t Byte = *I;
+ OS << hexdigit(Byte >> 4);
+ OS << hexdigit(Byte & 0xf);
+ }
+}