1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/HashMap.h>
29#include <wtf/SixCharacterHash.h>
30#include <wtf/StdLibExtras.h>
31#include <wtf/StringPrintStream.h>
32#include <wtf/text/CString.h>
33
34namespace WTF {
35
36template<typename T>
37class StringHashDumpContext {
38public:
39 StringHashDumpContext() { }
40
41 CString getID(const T* value)
42 {
43 typename HashMap<const T*, CString>::iterator iter = m_forwardMap.find(value);
44 if (iter != m_forwardMap.end())
45 return iter->value;
46
47 for (unsigned hashValue = toCString(*value).hash(); ; hashValue++) {
48 CString fullHash = integerToSixCharacterHashString(hashValue).data();
49
50 for (unsigned length = 2; length < 6; ++length) {
51 CString shortHash = CString(fullHash.data(), length);
52 if (!m_backwardMap.contains(shortHash)) {
53 m_forwardMap.add(value, shortHash);
54 m_backwardMap.add(shortHash, value);
55 return shortHash;
56 }
57 }
58 }
59 }
60
61 void dumpBrief(const T* value, PrintStream& out)
62 {
63 value->dumpBrief(out, getID(value));
64 }
65
66 CString brief(const T* value)
67 {
68 StringPrintStream out;
69 dumpBrief(value, out);
70 return out.toCString();
71 }
72
73 bool isEmpty() const { return m_forwardMap.isEmpty(); }
74
75 void dump(PrintStream& out, const char* prefix = "") const
76 {
77 out.print(prefix);
78 T::dumpContextHeader(out);
79 out.print("\n");
80
81 Vector<CString> keys;
82 unsigned maxKeySize = 0;
83 for (
84 typename HashMap<CString, const T*>::const_iterator iter = m_backwardMap.begin();
85 iter != m_backwardMap.end();
86 ++iter) {
87 keys.append(iter->key);
88 maxKeySize = std::max(maxKeySize, static_cast<unsigned>(brief(iter->value, iter->key).length()));
89 }
90
91 std::sort(keys.begin(), keys.end());
92
93 for (unsigned i = 0; i < keys.size(); ++i) {
94 const T* value = m_backwardMap.get(keys[i]);
95 out.print(prefix, " ");
96 CString briefString = brief(value, keys[i]);
97 out.print(briefString);
98 for (unsigned n = briefString.length(); n < maxKeySize; ++n)
99 out.print(" ");
100 out.print(" = ", *value, "\n");
101 }
102 }
103
104public:
105 static CString brief(const T* value, const CString& string)
106 {
107 StringPrintStream out;
108 value->dumpBrief(out, string);
109 return out.toCString();
110 }
111
112 HashMap<const T*, CString> m_forwardMap;
113 HashMap<CString, const T*> m_backwardMap;
114};
115
116} // namespace WTF
117
118using WTF::StringHashDumpContext;
119