-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
291 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2023 The Fury Authors | ||
* | ||
* 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. | ||
*/ | ||
|
||
import { isNodeEnv } from "./util"; | ||
|
||
export interface PlatformBuffer extends Uint8Array { | ||
latin1Slice(start: number, end: number): string, | ||
utf8Slice(start: number, end: number): string, | ||
latin1Write(v: string, offset: number): void, | ||
utf8Write(v: string, offset: number): void, | ||
copy(target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): Uint8Array | ||
} | ||
|
||
|
||
class BrowserBuffer extends Uint8Array { | ||
static alloc(size: number) { | ||
return new BrowserBuffer(new Uint8Array(size)); | ||
} | ||
|
||
latin1Slice(start: number, end: number) { | ||
if (end - start < 1) { | ||
return ""; | ||
} | ||
let str = ""; | ||
for (let i = start; i < end;) { | ||
str += String.fromCharCode(this[i++]); | ||
} | ||
return str; | ||
} | ||
|
||
utf8Slice(start: number, end: number) { | ||
if (end - start < 1) { | ||
return ""; | ||
} | ||
let str = ""; | ||
for (let i = start; i < end;) { | ||
const t = this[i++]; | ||
if (t <= 0x7F) { | ||
str += String.fromCharCode(t); | ||
} else if (t >= 0xC0 && t < 0xE0) { | ||
str += String.fromCharCode((t & 0x1F) << 6 | this[i++] & 0x3F); | ||
} else if (t >= 0xE0 && t < 0xF0) { | ||
str += String.fromCharCode((t & 0xF) << 12 | (this[i++] & 0x3F) << 6 | this[i++] & 0x3F); | ||
} else if (t >= 0xF0) { | ||
const t2 = ((t & 7) << 18 | (this[i++] & 0x3F) << 12 | (this[i++] & 0x3F) << 6 | this[i++] & 0x3F) - 0x10000; | ||
str += String.fromCharCode(0xD800 + (t2 >> 10)); | ||
str += String.fromCharCode(0xDC00 + (t2 & 0x3FF)); | ||
} | ||
} | ||
|
||
return str; | ||
} | ||
|
||
copy(target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number) { | ||
target.set(this.subarray(sourceStart, sourceEnd), targetStart); | ||
} | ||
} | ||
|
||
export function fromString(str: string) { | ||
if (isNodeEnv) { | ||
return Buffer.from(str); | ||
} else { | ||
return new BrowserBuffer(new TextEncoder().encode(str)) | ||
} | ||
} | ||
|
||
export function fromUint8Array(ab: Buffer | Uint8Array): PlatformBuffer { | ||
if (isNodeEnv) { | ||
if (!Buffer.isBuffer(ab)) { | ||
return (Buffer.from(ab) as unknown as PlatformBuffer) | ||
} else { | ||
return ab as unknown as PlatformBuffer; | ||
} | ||
} else { | ||
const result = new BrowserBuffer(ab); | ||
return result as unknown as PlatformBuffer; | ||
} | ||
} | ||
|
||
|
||
|
||
export function alloc(size: number): PlatformBuffer { | ||
if (isNodeEnv) { | ||
return Buffer.allocUnsafe(size) as unknown as PlatformBuffer; | ||
} else { | ||
const result = BrowserBuffer.alloc(size); | ||
return result as PlatformBuffer; | ||
} | ||
} | ||
|
||
|
||
export function strByteLength(str: string): number { | ||
if (isNodeEnv) { | ||
return Buffer.byteLength(str); | ||
} else { | ||
let len = 0; | ||
let c = 0; | ||
for (let i = 0; i < str.length; ++i) { | ||
c = str.charCodeAt(i); | ||
if (c < 128) | ||
len += 1; | ||
else if (c < 2048) | ||
len += 2; | ||
else if ((c & 0xFC00) === 0xD800 && (str.charCodeAt(i + 1) & 0xFC00) === 0xDC00) { | ||
++i; | ||
len += 4; | ||
} else | ||
len += 3; | ||
} | ||
return len; | ||
} | ||
} |
Oops, something went wrong.