Skip to content

Instantly share code, notes, and snippets.

@yyamasak
Created March 6, 2020 06:27
Show Gist options
  • Save yyamasak/c4eaf14755d65ff776a87018d22b2921 to your computer and use it in GitHub Desktop.
Save yyamasak/c4eaf14755d65ff776a87018d22b2921 to your computer and use it in GitHub Desktop.
Convert string to unicode literal sequence
package require unicode
proc to_unicode_literal {str} {
set cs [lmap c [unicode::fromstring $str] {format \\u%04x $c}]
join $cs ""
}
if {[namespace exists ::tk]} {
proc copy_to_clipboard {str} {
clipboard clear
clipboard append $str
}
proc to_literal {} {
global istr ostr
set ostr [to_unicode_literal $istr]
copy_to_clipboard $ostr
}
proc from_literal {} {
global istr ostr
set istr [subst $ostr]
copy_to_clipboard $istr
}
entry .ei -textvariable istr -width 100
button .bi -text "To Literal" -command "to_literal"
entry .eo -textvariable ostr
button .bo -text "From Literal" -command "from_literal"
grid .ei .bi -sticky news
grid .eo .bo -sticky news
grid columnconfigure . 0 -weight 1
grid columnconfigure . 1 -weight 0
} else {
set str [lindex $argv 0]
to_unicode_literal $str
}
@yyamasak
Copy link
Author

yyamasak commented Mar 6, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment