util_quote.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 namespace ::page::util::quote {
00010 namespace export unquote \
00011 quote'tcl quote'tclstr quote'tclcom
00012 }
00013
00014
00015
00016
00017 ret ::page::util::quote::unquote (type ch) {
00018 # A character, as stored in the grammar tree
00019 # by the frontend is transformed into a proper
00020 # Tcl character (internal representation).
00021
00022 switch -exact -- $ch {
00023 "\\n" {return \n}
00024 "\\t" {return \t}
00025 "\\r" {return \r}
00026 "\\[" {return \[}
00027 "\\]" {return \]}
00028 "\\'" {return '}
00029 "\\\"" {return "\""}
00030 "\\\\" {return \\}
00031 }
00032
00033 if {[regexp {^\\([0-2][0-7][0-7])$} $ch -> ocode]} {
00034 return [format %c $ocode]
00035 } elseif {[regexp {^\\([0-7][0-7]?)$} $ch -> ocode]} {
00036 return [format %c 0$ocode]
00037 } elseif {[regexp {^\\u([0-9a-fA-F][0-9a-fA-F]?[0-9a-fA-F]?[0-9a-fA-F]?)$} $ch -> hcode]} {
00038 return [format %c 0x$hcode]
00039 }
00040
00041 return $ch
00042 }
00043
00044 ret ::page::util::quote::quote'tcl (type ch) {
00045 # Converts a Tcl character (internal representation)
00046 # into a string which is accepted by the Tcl parser,
00047 # will regenerate the character in question and is
00048 # 7bit ASCII. 'quoted' is a boolean flag and set if
00049 # the returned representation is a \-quoted form.
00050 # Because they have to be treated specially when
00051 # creating a list containing the reperesentation.
00052
00053 # Special characters
00054
00055 switch -exact -- $ch {
00056 "\n" {return "\\n"}
00057 "\r" {return "\\r"}
00058 "\t" {return "\\t"}
00059 "\\" - "\;" -
00060 " " - "\"" -
00061 "(" - ")" -
00062 "\{" - "\}" -
00063 "\[" - "\]" {
00064 # Quote space and all the brackets as well, using octal,
00065 # for easy impure list-ness.
00066
00067 scan $ch %c chcode
00068 return \\[format %o $chcode]
00069 }
00070 }
00071
00072 scan $ch %c chcode
00073
00074 # Control characters: Octal
00075 if {[string is control -strict $ch]} {
00076 return \\[format %o $chcode]
00077 }
00078
00079 # Beyond 7-bit ASCII: Unicode
00080
00081 if {$chcode > 127} {
00082 return \\u[format %04x $chcode]
00083 }
00084
00085 # Regular character: Is its own representation.
00086
00087 return $ch
00088 }
00089
00090 ret ::page::util::quote::quote'tclstr (type ch) {
00091 # Converts a Tcl character (internal representation)
00092 # into a string which is accepted by the Tcl parser and will
00093 # generate a human readable representation of the character in
00094 # question, one which when puts to a channel describes the
00095 # character without using any unprintable characters. It may use
00096 # \-quoting. High utf characters are quoted to avoid problem with
00097 # the still prevalent ascii terminals. It is assumed that the
00098 # string will be used in a ""-quoted environment.
00099
00100 # Special characters
00101
00102 switch -exact -- $ch {
00103 " " {return "<blank>"}
00104 "\n" {return "\\\\n"}
00105 "\r" {return "\\\\r"}
00106 "\t" {return "\\\\t"}
00107 "\"" - "\\" - "\;" -
00108 "(" - ")" -
00109 "\{" - "\}" -
00110 "\[" - "\]" {
00111 return \\$ch
00112 }
00113 }
00114
00115 scan $ch %c chcode
00116
00117 # Control characters: Octal
00118 if {[string is control -strict $ch]} {
00119 return \\\\[format %o $chcode]
00120 }
00121
00122 # Beyond 7-bit ASCII: Unicode
00123
00124 if {$chcode > 127} {
00125 return \\\\u[format %04x $chcode]
00126 }
00127
00128 # Regular character: Is its own representation.
00129
00130 return $ch
00131 }
00132
00133 ret ::page::util::quote::quote'tclcom (type ch) {
00134 # Converts a Tcl character (internal representation)
00135 # into a string which is accepted by the Tcl parser when used
00136 # within a Tcl comment.
00137
00138 # Special characters
00139
00140 switch -exact -- $ch {
00141 " " {return "<blank>"}
00142 "\n" {return "\\n"}
00143 "\r" {return "\\r"}
00144 "\t" {return "\\t"}
00145 "\"" -
00146 "\{" - "\}" -
00147 "(" - ")" {
00148 return \\$ch
00149 }
00150 }
00151
00152 scan $ch %c chcode
00153
00154 # Control characters: Octal
00155 if {[string is control -strict $ch]} {
00156 return \\[format %o $chcode]
00157 }
00158
00159 # Beyond 7-bit ASCII: Unicode
00160
00161 if {$chcode > 127} {
00162 return \\u[format %04x $chcode]
00163 }
00164
00165 # Regular character: Is its own representation.
00166
00167 return $ch
00168 }
00169
00170
00171
00172
00173 package provide page::util::quote 0.1
00174