mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 20:26:08 +00:00
re-use text encoder
This commit is contained in:
@@ -27,6 +27,7 @@ export function getPreferredLanguage(metadata?: nbformat.INotebookMetadata) {
|
||||
return translateKernelLanguageToMonaco(jupyterLanguage || defaultLanguage);
|
||||
}
|
||||
|
||||
const textDecoder = new TextDecoder();
|
||||
function translateKernelLanguageToMonaco(language: string): string {
|
||||
language = language.toLowerCase();
|
||||
if (language.length === 2 && language.endsWith('#')) {
|
||||
@@ -336,7 +337,7 @@ function translateCellErrorOutput(output: NotebookCellOutput): nbformat.IError {
|
||||
};
|
||||
}
|
||||
const originalError: undefined | nbformat.IError = output.metadata?.originalError;
|
||||
const value: Error = JSON.parse(new TextDecoder().decode(firstItem.data.buffer.slice(firstItem.data.byteOffset)));
|
||||
const value: Error = JSON.parse(textDecoder.decode(firstItem.data.buffer.slice(firstItem.data.byteOffset)));
|
||||
return {
|
||||
output_type: 'error',
|
||||
ename: value.name,
|
||||
@@ -385,10 +386,11 @@ function convertOutputMimeToJupyterOutput(mime: string, value: Uint8Array) {
|
||||
return '';
|
||||
}
|
||||
try {
|
||||
const stringValue = new TextDecoder().decode(value.buffer.slice(value.byteOffset));
|
||||
if (mime === CellOutputMimeTypes.error) {
|
||||
const stringValue = textDecoder.decode(value.buffer.slice(value.byteOffset));
|
||||
return JSON.parse(stringValue);
|
||||
} else if (mime.startsWith('text/') || textMimeTypes.includes(mime)) {
|
||||
const stringValue = textDecoder.decode(value.buffer.slice(value.byteOffset));
|
||||
return splitMultilineString(stringValue);
|
||||
} else if (mime.startsWith('image/') && mime !== 'image/svg+xml') {
|
||||
// Images in Jupyter are stored in base64 encoded format.
|
||||
@@ -397,13 +399,16 @@ function convertOutputMimeToJupyterOutput(mime: string, value: Uint8Array) {
|
||||
return Buffer.from(value).toString('base64');
|
||||
} else {
|
||||
// https://developer.mozilla.org/en-US/docs/Glossary/Base64#solution_1_%E2%80%93_escaping_the_string_before_encoding_it
|
||||
const stringValue = textDecoder.decode(value.buffer.slice(value.byteOffset));
|
||||
return btoa(encodeURIComponent(stringValue).replace(/%([0-9A-F]{2})/g, function (_match, p1) {
|
||||
return String.fromCharCode(Number.parseInt('0x' + p1));
|
||||
}));
|
||||
}
|
||||
} else if (mime.toLowerCase().includes('json')) {
|
||||
const stringValue = textDecoder.decode(value.buffer.slice(value.byteOffset));
|
||||
return stringValue.length > 0 ? JSON.parse(stringValue) : stringValue;
|
||||
} else {
|
||||
const stringValue = textDecoder.decode(value.buffer.slice(value.byteOffset));
|
||||
return stringValue;
|
||||
}
|
||||
} catch (ex) {
|
||||
|
||||
Reference in New Issue
Block a user