-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathStdString.cpp
70 lines (53 loc) · 1.95 KB
/
StdString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "ClearScriptV8Native.h"
//-----------------------------------------------------------------------------
// StdString implementation
//-----------------------------------------------------------------------------
size_t StdString::GetDigest() const
{
return (sizeof(size_t) == 4) ? static_cast<size_t>(GetDigestAsUInt32()) : static_cast<size_t>(GetDigestAsUInt64());
}
//-----------------------------------------------------------------------------
uint32_t StdString::GetDigestAsUInt32() const
{
uint32_t digest { 2166136261UL };
const uint32_t prime { 16777619UL };
auto pBytes = reinterpret_cast<const uint8_t*>(m_Value.data());
size_t length { m_Value.length() * sizeof(StdChar) };
for (size_t index = 0; index < length; index++)
{
digest ^= pBytes[index];
digest *= prime;
}
return digest;
}
//-----------------------------------------------------------------------------
uint64_t StdString::GetDigestAsUInt64() const
{
uint64_t digest { 14695981039346656037ULL };
const uint64_t prime { 1099511628211ULL };
auto pBytes = reinterpret_cast<const uint8_t*>(m_Value.data());
size_t length { m_Value.length() * sizeof(StdChar) };
for (size_t index = 0; index < length; index++)
{
digest ^= pBytes[index];
digest *= prime;
}
return digest;
}
//-----------------------------------------------------------------------------
StdString::Value StdString::GetValue(const v8_inspector::StringView& stringView)
{
auto length = stringView.length();
if (length < 1)
{
return Value();
}
if (!stringView.is8Bit())
{
return Value(reinterpret_cast<const StdChar*>(stringView.characters16()), length);
}
auto pFirst = reinterpret_cast<const char*>(stringView.characters8());
return UTF8Converter().from_bytes(pFirst, pFirst + length);
}