[ADT] Add an in-place version of toHex()

and use that to simplify MD5's hex string code which was previously
using a string stream, as well as Clang's
CGDebugInfo::computeChecksum().

Differential revision: https://reviews.llvm.org/D116960
This commit is contained in:
Hans Wennborg
2022-01-10 19:45:13 +01:00
parent fe2c4af905
commit 0b48d0fe12
4 changed files with 23 additions and 26 deletions

View File

@@ -354,13 +354,9 @@ CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
if (!MemBuffer)
return None;
llvm::MD5 Hash;
llvm::MD5::MD5Result Result;
Hash.update(MemBuffer->getBuffer());
Hash.final(Result);
Hash.stringifyResult(Result, Checksum);
llvm::toHex(
llvm::MD5::hash(llvm::arrayRefFromStringRef(MemBuffer->getBuffer())),
/*LowerCase*/ true, Checksum);
return llvm::DIFile::CSK_MD5;
}

View File

@@ -29,7 +29,6 @@
namespace llvm {
template<typename T> class SmallVectorImpl;
class raw_ostream;
/// hexdigit - Return the hexadecimal character for the
@@ -166,21 +165,26 @@ inline std::string utohexstr(uint64_t X, bool LowerCase = false) {
/// Convert buffer \p Input to its hexadecimal representation.
/// The returned string is double the size of \p Input.
inline std::string toHex(StringRef Input, bool LowerCase = false) {
size_t Length = Input.size();
inline void toHex(ArrayRef<uint8_t> Input, bool LowerCase,
SmallVectorImpl<char> &Output) {
const size_t Length = Input.size();
Output.resize_for_overwrite(Length * 2);
std::string Output;
Output.reserve(2 * Length);
for (size_t i = 0; i < Length; ++i) {
const unsigned char c = Input[i];
Output.push_back(hexdigit(c >> 4, LowerCase));
Output.push_back(hexdigit(c & 15, LowerCase));
for (size_t i = 0; i < Length; i++) {
const uint8_t c = Input[i];
Output[i * 2 ] = hexdigit(c >> 4, LowerCase);
Output[i * 2 + 1] = hexdigit(c & 15, LowerCase);
}
return Output;
}
inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) {
return toHex(toStringRef(Input), LowerCase);
SmallString<16> Output;
toHex(Input, LowerCase, Output);
return std::string(Output);
}
inline std::string toHex(StringRef Input, bool LowerCase = false) {
return toHex(arrayRefFromStringRef(Input), LowerCase);
}
/// Store the binary representation of the two provided values, \p MSB and

View File

@@ -88,7 +88,7 @@ public:
/// Translates the bytes in \p Res to a hex string that is
/// deposited into \p Str. The result will be of length 32.
static void stringifyResult(MD5Result &Result, SmallString<32> &Str);
static void stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str);
/// Computes the hash for a given bytes.
static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data);

View File

@@ -40,10 +40,9 @@
#include "llvm/Support/MD5.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#include <array>
#include <cstdint>
#include <cstring>
@@ -281,14 +280,12 @@ StringRef MD5::result() {
SmallString<32> MD5::MD5Result::digest() const {
SmallString<32> Str;
raw_svector_ostream Res(Str);
for (int i = 0; i < 16; ++i)
Res << format("%.2x", Bytes[i]);
toHex(Bytes, /*LowerCase*/ true, Str);
return Str;
}
void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) {
Str = Result.digest();
void MD5::stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str) {
toHex(Result.Bytes, /*LowerCase*/ true, Str);
}
std::array<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) {