MCPcopy Create free account
hub / github.com/angular/angular / utf8Encode

Function utf8Encode

packages/compiler/src/util.ts:46–82  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

44export type Byte = number;
45
46export function utf8Encode(str: string): Byte[] {
47 let encoded: Byte[] = [];
48 for (let index = 0; index < str.length; index++) {
49 let codePoint = str.charCodeAt(index);
50
51 // decode surrogate
52 // see https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
53 if (codePoint >= 0xd800 && codePoint <= 0xdbff && str.length > index + 1) {
54 const low = str.charCodeAt(index + 1);
55 if (low >= 0xdc00 && low <= 0xdfff) {
56 index++;
57 codePoint = ((codePoint - 0xd800) << 10) + low - 0xdc00 + 0x10000;
58 }
59 }
60
61 if (codePoint <= 0x7f) {
62 encoded.push(codePoint);
63 } else if (codePoint <= 0x7ff) {
64 encoded.push(((codePoint >> 6) & 0x1f) | 0xc0, (codePoint & 0x3f) | 0x80);
65 } else if (codePoint <= 0xffff) {
66 encoded.push(
67 (codePoint >> 12) | 0xe0,
68 ((codePoint >> 6) & 0x3f) | 0x80,
69 (codePoint & 0x3f) | 0x80,
70 );
71 } else if (codePoint <= 0x1fffff) {
72 encoded.push(
73 ((codePoint >> 18) & 0x07) | 0xf0,
74 ((codePoint >> 12) & 0x3f) | 0x80,
75 ((codePoint >> 6) & 0x3f) | 0x80,
76 (codePoint & 0x3f) | 0x80,
77 );
78 }
79 }
80
81 return encoded;
82}
83
84export function stringify(token: any): string {
85 if (typeof token === 'string') {

Callers 2

util_spec.tsFile · 0.90
toBase64StringFunction · 0.90

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected