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

Function utf8Encode

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

Source from the content-addressed store, hash-verified

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