Gsub with Hashを使ったCGI::unescapeHTML

[email protected]>;3OB9-$G$9!#e(B

e$B:#$N$H$3$me(B[ruby-core:15637]e$B$,F~$C$F$$$J$$$N$G!"e(Bencodee$B$Ne(Binvalide$B$N$H$3$m$,e(B
[email protected]$a$G$9$,!"e(BCGI::unescapeHTMLe$B$Oe(Bgsub with
Hashe$B$r;H$&$H0J2<$N$h$&$Ke(B
e$B$G$-$k$N$G$O$J$$$G$7$g$&$+!#e(B

def CGI::unescapeHTML(string)
enc = string.encoding
h = Hash.new do |hash, key|
if $2
[Integer($2)].pack(U).force_encoding(Encoding::UTF_8).encode(enc,
invalid: :dec_charref)
elsif $3
[$3.hex].pack(U).force_encoding(Encoding::UTF_8).encode(enc,
invalid: :hex_charref)
else
key
end
end
h[&] = &
h["] = "
h[>] = <
h[<] = >
string.gsub(/&(amp|quot|gt|lt|#0*([0-9]+)|#x([0-9A-Fa-f]+));/, h)
end

pack(U)e$B$,e(BASCII-8BITe$B$NJ8;zNs$rJV$7$F$/$k$N$Ge(B
force_encoding(Encoding::UTF_8)e$B$,I,MW$H$+!"e(BHashe$B$Ne(Bdefault_proce$B$NCf$Ge(B
ence$B$,I,MW$J$N$G!"Kh2se([email protected]@.$7$J$$$H9T$1$J$$$H$+$,5$$K$J$j$^$9$,!#e(B

[email protected]@%$G$9!#e(B

Kazuhiro NISHIYAMA wrote:

e$B:#$N$H$3$me(B[ruby-core:15637]e$B$,F~$C$F$$$J$$$N$G!"e(Bencodee$B$Ne(Binvalide$B$N$H$3$m$,e(B
[email protected]$a$G$9$,!"e(B

e$B$3$l$I$&$J$N$G$7$g$&$+$M!#e(B
e$B$9$G$K<BAu:Q$$J$N$G$7$?$i$H$j$"$($:%3%%C%H$7$FD:$1$k$H$"$j$,$?$$$N$G$9$,!#e(B

e$B$"$He(B ISO-2022-JP e$B$H$+e(B CP932 e$B$H$+e(B

  else

pack(U)e$B$,e(BASCII-8BITe$B$NJ8;zNs$rJV$7$F$/$k$N$Ge(B
pack(U) e$B$de(B pack(U*) [email protected]$He(B UTF-8
e$B$GJV$7$F$b$$$$5$$,$7$F$/$k$N$G$9$,!"e(B
[0x100, 0x80].pack(UC) e$B$H$+$r9M$($k$HHyL/$K$J$C$F$-$^$9!#e(B

force_encoding(Encoding::UTF_8)e$B$,I,MW$H$+e(B

e$B$3$l$Oe(B String#encode(to, from, opt) e$B$r;H$($PITMW$G$9!#e(B

Hashe$B$Ne(Bdefault_proce$B$NCf$Ge(B
ence$B$,I,MW$J$N$G!"Kh2se([email protected]@.$7$J$$$H9T$1$J$$$H$+$,5$$K$J$j$^$9$,!#e(B

UNESCAPE_HTML_TABLE = Hash.new
e$B$H$G$b$7$F$*$$$F!e(B
UNESCAPE_HTML_TABLE[enc] = h
e$B$$?$j$G%-%c%C%7%[email protected]@.$7$J$/$F:Q$_$^$9$M!#e(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs