Posted to tcl by kbk at Sun Aug 03 22:06:37 GMT 2008view raw

  1. #----------------------------------------------------------------------
  2. #
  3. # backslashify --
  4. #
  5. # Converts a UTF-8 string to a plain ASCII one with escapes.
  6. #
  7. # Parameters:
  8. # string -- String to convert
  9. #
  10. # Results:
  11. # Returns the converted string
  12. #
  13. # Side effects:
  14. # None.
  15. #
  16. #----------------------------------------------------------------------
  17.  
  18. proc backslashify { string } {
  19.  
  20. set retval {}
  21. foreach char [split $string {}] {
  22. scan $char %c ccode
  23. if { $ccode >= 0x0020 && $ccode < 0x007f
  24. && $char ne "\"" && $char ne "'"
  25. && $char ne "\{" && $char ne "\}" && $char ne "\["
  26. && $char ne "\]" && $char ne "\\" && $char ne "\$" } {
  27. append retval $char
  28. } elseif {$ccode < 0x100} {
  29. append retval \\ [format %03o $ccode]
  30. } else {
  31. append retval \\u [format %04x $ccode]
  32. }
  33. }
  34. return $retval
  35. }