From 16f17ee030bf51207e810c852a98f1452d7dd6d0 Mon Sep 17 00:00:00 2001 From: ffranr Date: Wed, 20 Sep 2023 20:12:02 +0100 Subject: [PATCH] asset: add function for sanitizing and asset name for display purposes --- asset/asset.go | 26 ++++++++++++++++++++++++++ tapgarden/seedling.go | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/asset/asset.go b/asset/asset.go index 3221e830d..580a7ae5f 100644 --- a/asset/asset.go +++ b/asset/asset.go @@ -1064,3 +1064,29 @@ func ValidateAssetName(name string) error { } return nil } + +// DisplayAssetName sanitizes an asset name for display purposes. +func DisplayAssetName(name string) string { + var displayName string + + for _, char := range name { + switch { + case char > 127: + // Convert non-ASCII characters to hexadecimal + // representation. + hexValue := fmt.Sprintf("\\x%X", char) + displayName += hexValue + case char == '\n': + // Represent newline character as \n + displayName += "\\n" + case char == '\t': + // Represent tab character as \t + displayName += "\\t" + default: + // Add ASCII characters as is + displayName += string(char) + } + } + + return displayName +} diff --git a/tapgarden/seedling.go b/tapgarden/seedling.go index f93a920ca..af7e95146 100644 --- a/tapgarden/seedling.go +++ b/tapgarden/seedling.go @@ -146,6 +146,7 @@ func (c Seedling) HasGroupKey() bool { // String returns a human-readable representation for the AssetSeedling. func (c Seedling) String() string { + assetDisplayName := asset.DisplayAssetName(c.AssetName) return fmt.Sprintf("AssetSeedling(name=%v, type=%v, amt=%v) received", - c.AssetName, c.AssetType, c.Amount) + assetDisplayName, c.AssetType, c.Amount) }