Skip to content

Commit 0b48d0f

Browse files
committed
[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
1 parent fe2c4af commit 0b48d0f

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,9 @@ CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
354354
if (!MemBuffer)
355355
return None;
356356

357-
llvm::MD5 Hash;
358-
llvm::MD5::MD5Result Result;
359-
360-
Hash.update(MemBuffer->getBuffer());
361-
Hash.final(Result);
362-
363-
Hash.stringifyResult(Result, Checksum);
357+
llvm::toHex(
358+
llvm::MD5::hash(llvm::arrayRefFromStringRef(MemBuffer->getBuffer())),
359+
/*LowerCase*/ true, Checksum);
364360
return llvm::DIFile::CSK_MD5;
365361
}
366362

llvm/include/llvm/ADT/StringExtras.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
namespace llvm {
3131

32-
template<typename T> class SmallVectorImpl;
3332
class raw_ostream;
3433

3534
/// hexdigit - Return the hexadecimal character for the
@@ -166,21 +165,26 @@ inline std::string utohexstr(uint64_t X, bool LowerCase = false) {
166165

167166
/// Convert buffer \p Input to its hexadecimal representation.
168167
/// The returned string is double the size of \p Input.
169-
inline std::string toHex(StringRef Input, bool LowerCase = false) {
170-
size_t Length = Input.size();
171-
172-
std::string Output;
173-
Output.reserve(2 * Length);
174-
for (size_t i = 0; i < Length; ++i) {
175-
const unsigned char c = Input[i];
176-
Output.push_back(hexdigit(c >> 4, LowerCase));
177-
Output.push_back(hexdigit(c & 15, LowerCase));
168+
inline void toHex(ArrayRef<uint8_t> Input, bool LowerCase,
169+
SmallVectorImpl<char> &Output) {
170+
const size_t Length = Input.size();
171+
Output.resize_for_overwrite(Length * 2);
172+
173+
for (size_t i = 0; i < Length; i++) {
174+
const uint8_t c = Input[i];
175+
Output[i * 2 ] = hexdigit(c >> 4, LowerCase);
176+
Output[i * 2 + 1] = hexdigit(c & 15, LowerCase);
178177
}
179-
return Output;
180178
}
181179

182180
inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) {
183-
return toHex(toStringRef(Input), LowerCase);
181+
SmallString<16> Output;
182+
toHex(Input, LowerCase, Output);
183+
return std::string(Output);
184+
}
185+
186+
inline std::string toHex(StringRef Input, bool LowerCase = false) {
187+
return toHex(arrayRefFromStringRef(Input), LowerCase);
184188
}
185189

186190
/// Store the binary representation of the two provided values, \p MSB and

llvm/include/llvm/Support/MD5.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class MD5 {
8888

8989
/// Translates the bytes in \p Res to a hex string that is
9090
/// deposited into \p Str. The result will be of length 32.
91-
static void stringifyResult(MD5Result &Result, SmallString<32> &Str);
91+
static void stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str);
9292

9393
/// Computes the hash for a given bytes.
9494
static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data);

llvm/lib/Support/MD5.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@
4040
#include "llvm/Support/MD5.h"
4141
#include "llvm/ADT/ArrayRef.h"
4242
#include "llvm/ADT/SmallString.h"
43+
#include "llvm/ADT/StringExtras.h"
4344
#include "llvm/ADT/StringRef.h"
4445
#include "llvm/Support/Endian.h"
45-
#include "llvm/Support/Format.h"
46-
#include "llvm/Support/raw_ostream.h"
4746
#include <array>
4847
#include <cstdint>
4948
#include <cstring>
@@ -281,14 +280,12 @@ StringRef MD5::result() {
281280

282281
SmallString<32> MD5::MD5Result::digest() const {
283282
SmallString<32> Str;
284-
raw_svector_ostream Res(Str);
285-
for (int i = 0; i < 16; ++i)
286-
Res << format("%.2x", Bytes[i]);
283+
toHex(Bytes, /*LowerCase*/ true, Str);
287284
return Str;
288285
}
289286

290-
void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) {
291-
Str = Result.digest();
287+
void MD5::stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str) {
288+
toHex(Result.Bytes, /*LowerCase*/ true, Str);
292289
}
293290

294291
std::array<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) {

0 commit comments

Comments
 (0)