Skip to content

Commit

Permalink
Show utf-8 strings instead of ascii
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos committed Oct 31, 2023
1 parent c408062 commit 696e6c0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/debugAdapter/debugRequestHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import * as algosdk from 'algosdk';
import {
FileAccessor,
TEALDebuggingAssets,
isAsciiPrintable,
isValidUtf8,
limitArray,
} from './utils';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
11 changes: 4 additions & 7 deletions src/debugAdapter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ export interface FileAccessor {
writeFile(path: string, contents: Uint8Array): Promise<void>;
}

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<T>(
Expand Down

0 comments on commit 696e6c0

Please sign in to comment.