forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSBufferEncodingType.cpp
More file actions
138 lines (118 loc) · 4.42 KB
/
Copy pathJSBufferEncodingType.cpp
File metadata and controls
138 lines (118 loc) · 4.42 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
This file is part of the WebKit open source project.
This file has been generated by generate-bindings.pl. DO NOT MODIFY!
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
#include "config.h"
#include "JSBufferEncodingType.h"
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/JSString.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
using namespace JSC;
static const NeverDestroyed<String> values[] = {
MAKE_STATIC_STRING_IMPL("utf8"),
MAKE_STATIC_STRING_IMPL("ucs2"),
MAKE_STATIC_STRING_IMPL("utf16le"),
MAKE_STATIC_STRING_IMPL("latin1"),
MAKE_STATIC_STRING_IMPL("ascii"),
MAKE_STATIC_STRING_IMPL("base64"),
MAKE_STATIC_STRING_IMPL("base64url"),
MAKE_STATIC_STRING_IMPL("hex"),
};
String convertEnumerationToString(BufferEncodingType enumerationValue)
{
ASSERT(static_cast<size_t>(enumerationValue) < std::size(values));
return values[static_cast<size_t>(enumerationValue)];
}
template<> JSString* convertEnumerationToJS(JSGlobalObject& lexicalGlobalObject, BufferEncodingType enumerationValue)
{
return jsStringWithCache(lexicalGlobalObject.vm(), convertEnumerationToString(enumerationValue));
}
// this function is mostly copied from node
template<> std::optional<BufferEncodingType> parseEnumeration<BufferEncodingType>(JSGlobalObject& lexicalGlobalObject, JSValue value)
{
// caller must check if value is a string
JSC::JSString* str = asString(value);
if (UNLIKELY(!str))
return std::nullopt;
auto encoding = str->value(&lexicalGlobalObject);
switch (encoding.length()) {
case 0: {
return BufferEncodingType::utf8;
}
case 1:
case 2: {
return std::nullopt;
}
default: {
}
}
switch (encoding[0]) {
case 'u':
case 'U': {
if (WTF::equalIgnoringASCIICase(encoding, "utf8"_s))
return BufferEncodingType::utf8;
if (WTF::equalIgnoringASCIICase(encoding, "utf-8"_s))
return BufferEncodingType::utf8;
if (WTF::equalIgnoringASCIICase(encoding, "ucs2"_s))
return BufferEncodingType::ucs2;
if (WTF::equalIgnoringASCIICase(encoding, "ucs-2"_s))
return BufferEncodingType::ucs2;
if (WTF::equalIgnoringASCIICase(encoding, "utf16le"_s))
return BufferEncodingType::ucs2;
if (WTF::equalIgnoringASCIICase(encoding, "utf-16le"_s))
return BufferEncodingType::ucs2;
break;
}
case 'l':
case 'L': {
if (WTF::equalIgnoringASCIICase(encoding, "latin1"_s))
return BufferEncodingType::latin1;
break;
}
case 'b':
case 'B': {
if (WTF::equalIgnoringASCIICase(encoding, "binary"_s))
return BufferEncodingType::latin1; // BINARY is a deprecated alias of LATIN1.
if (WTF::equalIgnoringASCIICase(encoding, "base64"_s))
return BufferEncodingType::base64;
if (WTF::equalIgnoringASCIICase(encoding, "base64url"_s))
return BufferEncodingType::base64url;
break;
}
case 'a':
case 'A':
// ascii
if (WTF::equalLettersIgnoringASCIICase(encoding, "ascii"_s))
return BufferEncodingType::ascii;
break;
case 'h':
case 'H':
// hex
if (encoding[1] == 'e')
if (encoding[2] == 'x' && encoding[3] == '\0')
return BufferEncodingType::hex;
if (WTF::equalIgnoringASCIICase(encoding, "hex"_s))
return BufferEncodingType::hex;
break;
}
return std::nullopt;
}
template<> const char* expectedEnumerationValues<BufferEncodingType>()
{
return "\"utf8\", \"ucs2\", \"utf16le\", \"latin1\", \"ascii\", \"base64\", \"base64url\", \"hex\"";
}
} // namespace WebCore