diff --git a/src/debugAdapter/debugRequestHandlers.ts b/src/debugAdapter/debugRequestHandlers.ts index 9c8f28f..ec14dbf 100644 --- a/src/debugAdapter/debugRequestHandlers.ts +++ b/src/debugAdapter/debugRequestHandlers.ts @@ -26,7 +26,7 @@ import * as algosdk from 'algosdk'; import { FileAccessor, TEALDebuggingAssets, - isAsciiPrintable, + isValidUtf8, limitArray, } from './utils'; @@ -991,7 +991,7 @@ export class TxnGroupDebugSession extends LoggingDebugSession { // byte array const bytes = avmValue.bytes || new Uint8Array(); namedVariables = 2; - if (isAsciiPrintable(bytes)) { + if (isValidUtf8(bytes)) { namedVariables++; } indexedVariables = bytes.length; @@ -1032,9 +1032,8 @@ export class TxnGroupDebugSession extends LoggingDebugSession { if (filter !== 'indexed') { let formats: BufferEncoding[] = ['hex', 'base64']; - if (isAsciiPrintable(bytes)) { - // TODO: perhaps do this with UTF-8 instead, see https://stackoverflow.com/questions/75108373/how-to-check-if-a-node-js-buffer-contains-valid-utf-8 - formats.push('ascii'); + if (isValidUtf8(bytes)) { + formats.push('utf-8'); } if (bytes.length === 0) { formats = []; diff --git a/src/debugAdapter/utils.ts b/src/debugAdapter/utils.ts index c8fc837..c9bd616 100644 --- a/src/debugAdapter/utils.ts +++ b/src/debugAdapter/utils.ts @@ -8,13 +8,10 @@ export interface FileAccessor { writeFile(path: string, contents: Uint8Array): Promise; } -export function isAsciiPrintable(data: Uint8Array): boolean { - for (let i = 0; i < data.length; i++) { - if (data[i] < 32 || data[i] > 126) { - return false; - } - } - return true; +export function isValidUtf8(data: Uint8Array): boolean { + const dataBuffer = Buffer.from(data); + const decoded = dataBuffer.toString('utf-8'); + return Buffer.from(decoded).equals(dataBuffer); } export function limitArray(