deno.land / std@0.166.0 / node / internal_binding / _utils.ts

نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import * as base64 from "../../encoding/base64.ts";import * as base64url from "../../encoding/base64url.ts";
export function asciiToBytes(str: string) { const byteArray = []; for (let i = 0; i < str.length; ++i) { byteArray.push(str.charCodeAt(i) & 255); } return new Uint8Array(byteArray);}
export function base64ToBytes(str: string) { str = base64clean(str); str = str.replaceAll("-", "+").replaceAll("_", "/"); return base64.decode(str);}
const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;function base64clean(str: string) { // Node takes equal signs as end of the Base64 encoding str = str.split("=")[0]; // Node strips out invalid characters like \n and \t from the string, std/base64 does not str = str.trim().replace(INVALID_BASE64_RE, ""); // Node converts strings with length < 2 to '' if (str.length < 2) return ""; // Node allows for non-padded base64 strings (missing trailing ===), std/base64 does not while (str.length % 4 !== 0) { str = str + "="; } return str;}
export function base64UrlToBytes(str: string) { str = base64clean(str); str = str.replaceAll("+", "-").replaceAll("/", "_"); return base64url.decode(str);}
export function hexToBytes(str: string) { const byteArray = new Uint8Array(Math.floor((str || "").length / 2)); let i; for (i = 0; i < byteArray.length; i++) { const a = Number.parseInt(str[i * 2], 16); const b = Number.parseInt(str[i * 2 + 1], 16); if (Number.isNaN(a) && Number.isNaN(b)) { break; } byteArray[i] = (a << 4) | b; } return new Uint8Array( i === byteArray.length ? byteArray : byteArray.slice(0, i), );}
export function utf16leToBytes(str: string, units: number) { let c, hi, lo; const byteArray = []; for (let i = 0; i < str.length; ++i) { if ((units -= 2) < 0) { break; } c = str.charCodeAt(i); hi = c >> 8; lo = c % 256; byteArray.push(lo); byteArray.push(hi); } return new Uint8Array(byteArray);}
export function bytesToAscii(bytes: Uint8Array) { let ret = ""; for (let i = 0; i < bytes.length; ++i) { ret += String.fromCharCode(bytes[i] & 127); } return ret;}
export function bytesToUtf16le(bytes: Uint8Array) { let res = ""; for (let i = 0; i < bytes.length - 1; i += 2) { res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256); } return res;}
std

Version Info

Tagged at
a year ago