1 //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines utility classes for handling the YAML representation of
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Object/YAML.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace object::yaml;
21 void yaml::ScalarTraits<object::yaml::BinaryRef>::output(
22 const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
26 StringRef yaml::ScalarTraits<object::yaml::BinaryRef>::input(
27 StringRef Scalar, void *, object::yaml::BinaryRef &Val) {
28 if (Scalar.size() % 2 != 0)
29 return "BinaryRef hex string must contain an even number of nybbles.";
30 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
31 // (e.g. a caret pointing to the offending character).
32 for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
33 if (!isxdigit(Scalar[I]))
34 return "BinaryRef hex string must contain only hex digits.";
35 Val = object::yaml::BinaryRef(Scalar);
39 void BinaryRef::writeAsBinary(raw_ostream &OS) const {
40 if (!DataIsHexString) {
41 OS.write((const char *)Data.data(), Data.size());
44 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
46 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
51 void BinaryRef::writeAsHex(raw_ostream &OS) const {
52 if (DataIsHexString) {
53 OS.write((const char *)Data.data(), Data.size());
56 for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
59 OS << hexdigit(Byte >> 4);
60 OS << hexdigit(Byte & 0xf);