Skip to content

Commit

Permalink
asset: add function for sanitizing and asset name for display purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Sep 20, 2023
1 parent de9d24c commit f2eb42e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
26 changes: 26 additions & 0 deletions asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion tapgarden/seedling.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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)
}

0 comments on commit f2eb42e

Please sign in to comment.