Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
c41280a0
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c41280a0
编写于
1月 04, 2020
作者:
L
Luca Casonato
提交者:
Ry Dahl
1月 04, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
perf: TextEncoder.encode improvement (#3596)
上级
0a900949
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
87 addition
and
0 deletion
+87
-0
cli/js/encode_utf8.ts
cli/js/encode_utf8.ts
+80
-0
cli/js/text_encoding.ts
cli/js/text_encoding.ts
+7
-0
未找到文件。
cli/js/encode_utf8.ts
0 → 100644
浏览文件 @
c41280a0
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
// The following code is based off:
// https://github.com/samthor/fast-text-encoding
//
// Copyright 2017 Sam Thorogood. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//
export
function
encodeUtf8
(
input
:
string
):
Uint8Array
{
let
pos
=
0
;
const
len
=
input
.
length
;
let
at
=
0
;
// output position
let
tlen
=
Math
.
max
(
32
,
len
+
(
len
>>
1
)
+
7
);
// 1.5x size
let
target
=
new
Uint8Array
((
tlen
>>
3
)
<<
3
);
// ... but at 8 byte offset
while
(
pos
<
len
)
{
let
value
=
input
.
charCodeAt
(
pos
++
);
if
(
value
>=
0xd800
&&
value
<=
0xdbff
)
{
// high surrogate
if
(
pos
<
len
)
{
const
extra
=
input
.
charCodeAt
(
pos
);
if
((
extra
&
0xfc00
)
===
0xdc00
)
{
++
pos
;
value
=
((
value
&
0x3ff
)
<<
10
)
+
(
extra
&
0x3ff
)
+
0x10000
;
}
}
if
(
value
>=
0xd800
&&
value
<=
0xdbff
)
{
continue
;
// drop lone surrogate
}
}
// expand the buffer if we couldn't write 4 bytes
if
(
at
+
4
>
target
.
length
)
{
tlen
+=
8
;
// minimum extra
tlen
*=
1.0
+
(
pos
/
input
.
length
)
*
2
;
// take 2x the remaining
tlen
=
(
tlen
>>
3
)
<<
3
;
// 8 byte offset
const
update
=
new
Uint8Array
(
tlen
);
update
.
set
(
target
);
target
=
update
;
}
if
((
value
&
0xffffff80
)
===
0
)
{
// 1-byte
target
[
at
++
]
=
value
;
// ASCII
continue
;
}
else
if
((
value
&
0xfffff800
)
===
0
)
{
// 2-byte
target
[
at
++
]
=
((
value
>>
6
)
&
0x1f
)
|
0xc0
;
}
else
if
((
value
&
0xffff0000
)
===
0
)
{
// 3-byte
target
[
at
++
]
=
((
value
>>
12
)
&
0x0f
)
|
0xe0
;
target
[
at
++
]
=
((
value
>>
6
)
&
0x3f
)
|
0x80
;
}
else
if
((
value
&
0xffe00000
)
===
0
)
{
// 4-byte
target
[
at
++
]
=
((
value
>>
18
)
&
0x07
)
|
0xf0
;
target
[
at
++
]
=
((
value
>>
12
)
&
0x3f
)
|
0x80
;
target
[
at
++
]
=
((
value
>>
6
)
&
0x3f
)
|
0x80
;
}
else
{
// FIXME: do we care
continue
;
}
target
[
at
++
]
=
(
value
&
0x3f
)
|
0x80
;
}
return
target
.
slice
(
0
,
at
);
}
cli/js/text_encoding.ts
浏览文件 @
c41280a0
...
...
@@ -26,6 +26,7 @@
import
*
as
base64
from
"
./base64.ts
"
;
import
{
decodeUtf8
}
from
"
./decode_utf8.ts
"
;
import
*
as
domTypes
from
"
./dom_types.ts
"
;
import
{
encodeUtf8
}
from
"
./encode_utf8.ts
"
;
import
{
DenoError
,
ErrorKind
}
from
"
./errors.ts
"
;
const
CONTINUE
=
null
;
...
...
@@ -405,6 +406,12 @@ export class TextEncoder {
readonly
encoding
=
"
utf-8
"
;
/** Returns the result of running UTF-8's encoder. */
encode
(
input
=
""
):
Uint8Array
{
// For performance reasons we utilise a highly optimised decoder instead of
// the general decoder.
if
(
this
.
encoding
===
"
utf-8
"
)
{
return
encodeUtf8
(
input
);
}
const
encoder
=
new
UTF8Encoder
();
const
inputStream
=
new
Stream
(
stringToCodePoints
(
input
));
const
output
:
number
[]
=
[];
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录