public
anonymous / gist:1420708
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
diff --git a/lib/rexml/encoding.rb b/lib/rexml/encoding.rb
index a01763b..b74b124 100644
--- a/lib/rexml/encoding.rb
+++ b/lib/rexml/encoding.rb
@@ -17,6 +17,8 @@ module REXML
UTF_8 = 'UTF-8'
UTF_16 = 'UTF-16'
UNILE = 'UNILE'
+
+ attr_accessor :encoder
# ID ---> Encoding name
attr_reader :encoding
diff --git a/lib/rexml/encodings/CP-1252.rb b/lib/rexml/encodings/CP-1252.rb
index 8675f9f..2b5f74d 100644
--- a/lib/rexml/encodings/CP-1252.rb
+++ b/lib/rexml/encodings/CP-1252.rb
@@ -3,101 +3,101 @@
#
module REXML
module Encoding
- register( "CP-1252" ) do |o|
- class << o
- alias encode encode_cp1252
- alias decode decode_cp1252
- end
- end
-
# Convert from UTF-8
- def encode_cp1252(content)
- array_utf8 = content.unpack('U*')
- array_enc = []
- array_utf8.each do |num|
- case num
- # shortcut first bunch basic characters
- when 0..0xFF; array_enc << num
- # characters added compared to iso-8859-1
- when 0x20AC; array_enc << 0x80 # 0xe2 0x82 0xac
- when 0x201A; array_enc << 0x82 # 0xe2 0x82 0x9a
- when 0x0192; array_enc << 0x83 # 0xc6 0x92
- when 0x201E; array_enc << 0x84 # 0xe2 0x82 0x9e
- when 0x2026; array_enc << 0x85 # 0xe2 0x80 0xa6
- when 0x2020; array_enc << 0x86 # 0xe2 0x80 0xa0
- when 0x2021; array_enc << 0x87 # 0xe2 0x80 0xa1
- when 0x02C6; array_enc << 0x88 # 0xcb 0x86
- when 0x2030; array_enc << 0x89 # 0xe2 0x80 0xb0
- when 0x0160; array_enc << 0x8A # 0xc5 0xa0
- when 0x2039; array_enc << 0x8B # 0xe2 0x80 0xb9
- when 0x0152; array_enc << 0x8C # 0xc5 0x92
- when 0x017D; array_enc << 0x8E # 0xc5 0xbd
- when 0x2018; array_enc << 0x91 # 0xe2 0x80 0x98
- when 0x2019; array_enc << 0x92 # 0xe2 0x80 0x99
- when 0x201C; array_enc << 0x93 # 0xe2 0x80 0x9c
- when 0x201D; array_enc << 0x94 # 0xe2 0x80 0x9d
- when 0x2022; array_enc << 0x95 # 0xe2 0x80 0xa2
- when 0x2013; array_enc << 0x96 # 0xe2 0x80 0x93
- when 0x2014; array_enc << 0x97 # 0xe2 0x80 0x94
- when 0x02DC; array_enc << 0x98 # 0xcb 0x9c
- when 0x2122; array_enc << 0x99 # 0xe2 0x84 0xa2
- when 0x0161; array_enc << 0x9A # 0xc5 0xa1
- when 0x203A; array_enc << 0x9B # 0xe2 0x80 0xba
- when 0x0152; array_enc << 0x9C # 0xc5 0x93
- when 0x017E; array_enc << 0x9E # 0xc5 0xbe
- when 0x0178; array_enc << 0x9F # 0xc5 0xb8
- else
- # all remaining basic characters can be used directly
- if num <= 0xFF
- array_enc << num
+ class CP_1252Encoder
+ def encode(content)
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ case num
+ # shortcut first bunch basic characters
+ when 0..0xFF; array_enc << num
+ # characters added compared to iso-8859-1
+ when 0x20AC; array_enc << 0x80 # 0xe2 0x82 0xac
+ when 0x201A; array_enc << 0x82 # 0xe2 0x82 0x9a
+ when 0x0192; array_enc << 0x83 # 0xc6 0x92
+ when 0x201E; array_enc << 0x84 # 0xe2 0x82 0x9e
+ when 0x2026; array_enc << 0x85 # 0xe2 0x80 0xa6
+ when 0x2020; array_enc << 0x86 # 0xe2 0x80 0xa0
+ when 0x2021; array_enc << 0x87 # 0xe2 0x80 0xa1
+ when 0x02C6; array_enc << 0x88 # 0xcb 0x86
+ when 0x2030; array_enc << 0x89 # 0xe2 0x80 0xb0
+ when 0x0160; array_enc << 0x8A # 0xc5 0xa0
+ when 0x2039; array_enc << 0x8B # 0xe2 0x80 0xb9
+ when 0x0152; array_enc << 0x8C # 0xc5 0x92
+ when 0x017D; array_enc << 0x8E # 0xc5 0xbd
+ when 0x2018; array_enc << 0x91 # 0xe2 0x80 0x98
+ when 0x2019; array_enc << 0x92 # 0xe2 0x80 0x99
+ when 0x201C; array_enc << 0x93 # 0xe2 0x80 0x9c
+ when 0x201D; array_enc << 0x94 # 0xe2 0x80 0x9d
+ when 0x2022; array_enc << 0x95 # 0xe2 0x80 0xa2
+ when 0x2013; array_enc << 0x96 # 0xe2 0x80 0x93
+ when 0x2014; array_enc << 0x97 # 0xe2 0x80 0x94
+ when 0x02DC; array_enc << 0x98 # 0xcb 0x9c
+ when 0x2122; array_enc << 0x99 # 0xe2 0x84 0xa2
+ when 0x0161; array_enc << 0x9A # 0xc5 0xa1
+ when 0x203A; array_enc << 0x9B # 0xe2 0x80 0xba
+ when 0x0152; array_enc << 0x9C # 0xc5 0x93
+ when 0x017E; array_enc << 0x9E # 0xc5 0xbe
+ when 0x0178; array_enc << 0x9F # 0xc5 0xb8
else
- # Numeric entity (&#nnnn;); shard by Stefan Scholl
- array_enc.concat "&\##{num};".unpack('C*')
+ # all remaining basic characters can be used directly
+ if num <= 0xFF
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
end
end
+ array_enc.pack('C*')
end
- array_enc.pack('C*')
- end
-
- # Convert to UTF-8
- def decode_cp1252(str)
- array_latin9 = str.unpack('C*')
- array_enc = []
- array_latin9.each do |num|
- case num
- # characters that added compared to iso-8859-1
- when 0x80; array_enc << 0x20AC # 0xe2 0x82 0xac
- when 0x82; array_enc << 0x201A # 0xe2 0x82 0x9a
- when 0x83; array_enc << 0x0192 # 0xc6 0x92
- when 0x84; array_enc << 0x201E # 0xe2 0x82 0x9e
- when 0x85; array_enc << 0x2026 # 0xe2 0x80 0xa6
- when 0x86; array_enc << 0x2020 # 0xe2 0x80 0xa0
- when 0x87; array_enc << 0x2021 # 0xe2 0x80 0xa1
- when 0x88; array_enc << 0x02C6 # 0xcb 0x86
- when 0x89; array_enc << 0x2030 # 0xe2 0x80 0xb0
- when 0x8A; array_enc << 0x0160 # 0xc5 0xa0
- when 0x8B; array_enc << 0x2039 # 0xe2 0x80 0xb9
- when 0x8C; array_enc << 0x0152 # 0xc5 0x92
- when 0x8E; array_enc << 0x017D # 0xc5 0xbd
- when 0x91; array_enc << 0x2018 # 0xe2 0x80 0x98
- when 0x92; array_enc << 0x2019 # 0xe2 0x80 0x99
- when 0x93; array_enc << 0x201C # 0xe2 0x80 0x9c
- when 0x94; array_enc << 0x201D # 0xe2 0x80 0x9d
- when 0x95; array_enc << 0x2022 # 0xe2 0x80 0xa2
- when 0x96; array_enc << 0x2013 # 0xe2 0x80 0x93
- when 0x97; array_enc << 0x2014 # 0xe2 0x80 0x94
- when 0x98; array_enc << 0x02DC # 0xcb 0x9c
- when 0x99; array_enc << 0x2122 # 0xe2 0x84 0xa2
- when 0x9A; array_enc << 0x0161 # 0xc5 0xa1
- when 0x9B; array_enc << 0x203A # 0xe2 0x80 0xba
- when 0x9C; array_enc << 0x0152 # 0xc5 0x93
- when 0x9E; array_enc << 0x017E # 0xc5 0xbe
- when 0x9F; array_enc << 0x0178 # 0xc5 0xb8
- else
- array_enc << num
+
+ # Convert to UTF-8
+ def decode(str)
+ array_latin9 = str.unpack('C*')
+ array_enc = []
+ array_latin9.each do |num|
+ case num
+ # characters that added compared to iso-8859-1
+ when 0x80; array_enc << 0x20AC # 0xe2 0x82 0xac
+ when 0x82; array_enc << 0x201A # 0xe2 0x82 0x9a
+ when 0x83; array_enc << 0x0192 # 0xc6 0x92
+ when 0x84; array_enc << 0x201E # 0xe2 0x82 0x9e
+ when 0x85; array_enc << 0x2026 # 0xe2 0x80 0xa6
+ when 0x86; array_enc << 0x2020 # 0xe2 0x80 0xa0
+ when 0x87; array_enc << 0x2021 # 0xe2 0x80 0xa1
+ when 0x88; array_enc << 0x02C6 # 0xcb 0x86
+ when 0x89; array_enc << 0x2030 # 0xe2 0x80 0xb0
+ when 0x8A; array_enc << 0x0160 # 0xc5 0xa0
+ when 0x8B; array_enc << 0x2039 # 0xe2 0x80 0xb9
+ when 0x8C; array_enc << 0x0152 # 0xc5 0x92
+ when 0x8E; array_enc << 0x017D # 0xc5 0xbd
+ when 0x91; array_enc << 0x2018 # 0xe2 0x80 0x98
+ when 0x92; array_enc << 0x2019 # 0xe2 0x80 0x99
+ when 0x93; array_enc << 0x201C # 0xe2 0x80 0x9c
+ when 0x94; array_enc << 0x201D # 0xe2 0x80 0x9d
+ when 0x95; array_enc << 0x2022 # 0xe2 0x80 0xa2
+ when 0x96; array_enc << 0x2013 # 0xe2 0x80 0x93
+ when 0x97; array_enc << 0x2014 # 0xe2 0x80 0x94
+ when 0x98; array_enc << 0x02DC # 0xcb 0x9c
+ when 0x99; array_enc << 0x2122 # 0xe2 0x84 0xa2
+ when 0x9A; array_enc << 0x0161 # 0xc5 0xa1
+ when 0x9B; array_enc << 0x203A # 0xe2 0x80 0xba
+ when 0x9C; array_enc << 0x0152 # 0xc5 0x93
+ when 0x9E; array_enc << 0x017E # 0xc5 0xbe
+ when 0x9F; array_enc << 0x0178 # 0xc5 0xb8
+ else
+ array_enc << num
+ end
end
+ array_enc.pack('U*')
end
- array_enc.pack('U*')
+ end
+
+ cp_1252 = CP_1252Encoder.new
+ register( "CP-1252" ) do |o|
+ o.encoder = cp_1252
end
end
end
diff --git a/lib/rexml/encodings/EUC-JP.rb b/lib/rexml/encodings/EUC-JP.rb
index db37b6b..15063c0 100644
--- a/lib/rexml/encodings/EUC-JP.rb
+++ b/lib/rexml/encodings/EUC-JP.rb
@@ -3,12 +3,14 @@ module REXML
begin
require 'uconv'
- def decode_eucjp(str)
- Uconv::euctou8(str)
- end
-
- def encode_eucjp content
- Uconv::u8toeuc(content)
+ class EUC_JP
+ def decode(str)
+ Uconv::euctou8(str)
+ end
+
+ def encode content
+ Uconv::u8toeuc(content)
+ end
end
rescue LoadError
require 'nkf'
@@ -16,20 +18,20 @@ module REXML
EUCTOU8 = '-Ewm0'
U8TOEUC = '-Wem0'
- def decode_eucjp(str)
- NKF.nkf(EUCTOU8, str)
- end
-
- def encode_eucjp content
- NKF.nkf(U8TOEUC, content)
+ class EUC_JPEncoder
+ def decode(str)
+ NKF.nkf(EUCTOU8, str)
+ end
+
+ def encode content
+ NKF.nkf(U8TOEUC, content)
+ end
end
end
+ euc_jp = EUC_JPEncoder.new
register("EUC-JP") do |obj|
- class << obj
- alias decode decode_eucjp
- alias encode encode_eucjp
- end
+ obj.encoder = euc_jp
end
end
end
diff --git a/lib/rexml/encodings/ICONV.rb b/lib/rexml/encodings/ICONV.rb
index 172fba7..297738b 100644
--- a/lib/rexml/encodings/ICONV.rb
+++ b/lib/rexml/encodings/ICONV.rb
@@ -3,20 +3,20 @@ raise LoadError unless defined? Iconv
module REXML
module Encoding
- def decode_iconv(str)
- Iconv.conv(UTF_8, @encoding, str)
- end
-
- def encode_iconv(content)
- Iconv.conv(@encoding, UTF_8, content)
+ class ICONVEncoder
+ def decode(str)
+ Iconv.conv(UTF_8, @encoding, str)
+ end
+
+ def encode(content)
+ Iconv.conv(@encoding, UTF_8, content)
+ end
end
+ iconv = ICONVEncoder.new
register("ICONV") do |obj|
Iconv.conv(UTF_8, obj.encoding, nil)
- class << obj
- alias decode decode_iconv
- alias encode encode_iconv
- end
+ obj.encoder = iconv
end
end
end
diff --git a/lib/rexml/encodings/ISO-8859-15.rb b/lib/rexml/encodings/ISO-8859-15.rb
index 8dea0d3..23632d8 100644
--- a/lib/rexml/encodings/ISO-8859-15.rb
+++ b/lib/rexml/encodings/ISO-8859-15.rb
@@ -3,70 +3,72 @@
#
module REXML
module Encoding
- register("ISO-8859-15") do |o|
- alias encode to_iso_8859_15
- alias decode from_iso_8859_15
- end
-
- # Convert from UTF-8
- def to_iso_8859_15(content)
- array_utf8 = content.unpack('U*')
- array_enc = []
- array_utf8.each do |num|
- case num
- # shortcut first bunch basic characters
- when 0..0xA3; array_enc << num
- # characters removed compared to iso-8859-1
- when 0xA4; array_enc << '&#164;'
- when 0xA6; array_enc << '&#166;'
- when 0xA8; array_enc << '&#168;'
- when 0xB4; array_enc << '&#180;'
- when 0xB8; array_enc << '&#184;'
- when 0xBC; array_enc << '&#188;'
- when 0xBD; array_enc << '&#189;'
- when 0xBE; array_enc << '&#190;'
- # characters added compared to iso-8859-1
- when 0x20AC; array_enc << 0xA4 # 0xe2 0x82 0xac
- when 0x0160; array_enc << 0xA6 # 0xc5 0xa0
- when 0x0161; array_enc << 0xA8 # 0xc5 0xa1
- when 0x017D; array_enc << 0xB4 # 0xc5 0xbd
- when 0x017E; array_enc << 0xB8 # 0xc5 0xbe
- when 0x0152; array_enc << 0xBC # 0xc5 0x92
- when 0x0153; array_enc << 0xBD # 0xc5 0x93
- when 0x0178; array_enc << 0xBE # 0xc5 0xb8
- else
- # all remaining basic characters can be used directly
- if num <= 0xFF
- array_enc << num
+ class ISO_8859_15Encoder
+ # Convert from UTF-8
+ def encode(content)
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ case num
+ # shortcut first bunch basic characters
+ when 0..0xA3; array_enc << num
+ # characters removed compared to iso-8859-1
+ when 0xA4; array_enc << '&#164;'
+ when 0xA6; array_enc << '&#166;'
+ when 0xA8; array_enc << '&#168;'
+ when 0xB4; array_enc << '&#180;'
+ when 0xB8; array_enc << '&#184;'
+ when 0xBC; array_enc << '&#188;'
+ when 0xBD; array_enc << '&#189;'
+ when 0xBE; array_enc << '&#190;'
+ # characters added compared to iso-8859-1
+ when 0x20AC; array_enc << 0xA4 # 0xe2 0x82 0xac
+ when 0x0160; array_enc << 0xA6 # 0xc5 0xa0
+ when 0x0161; array_enc << 0xA8 # 0xc5 0xa1
+ when 0x017D; array_enc << 0xB4 # 0xc5 0xbd
+ when 0x017E; array_enc << 0xB8 # 0xc5 0xbe
+ when 0x0152; array_enc << 0xBC # 0xc5 0x92
+ when 0x0153; array_enc << 0xBD # 0xc5 0x93
+ when 0x0178; array_enc << 0xBE # 0xc5 0xb8
else
- # Numeric entity (&#nnnn;); shard by Stefan Scholl
- array_enc.concat "&\##{num};".unpack('C*')
+ # all remaining basic characters can be used directly
+ if num <= 0xFF
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
end
end
+ array_enc.pack('C*')
end
- array_enc.pack('C*')
- end
-
- # Convert to UTF-8
- def from_iso_8859_15(str)
- array_latin9 = str.unpack('C*')
- array_enc = []
- array_latin9.each do |num|
- case num
- # characters that differ compared to iso-8859-1
- when 0xA4; array_enc << 0x20AC
- when 0xA6; array_enc << 0x0160
- when 0xA8; array_enc << 0x0161
- when 0xB4; array_enc << 0x017D
- when 0xB8; array_enc << 0x017E
- when 0xBC; array_enc << 0x0152
- when 0xBD; array_enc << 0x0153
- when 0xBE; array_enc << 0x0178
- else
- array_enc << num
+
+ # Convert to UTF-8
+ def decode(str)
+ array_latin9 = str.unpack('C*')
+ array_enc = []
+ array_latin9.each do |num|
+ case num
+ # characters that differ compared to iso-8859-1
+ when 0xA4; array_enc << 0x20AC
+ when 0xA6; array_enc << 0x0160
+ when 0xA8; array_enc << 0x0161
+ when 0xB4; array_enc << 0x017D
+ when 0xB8; array_enc << 0x017E
+ when 0xBC; array_enc << 0x0152
+ when 0xBD; array_enc << 0x0153
+ when 0xBE; array_enc << 0x0178
+ else
+ array_enc << num
+ end
end
+ array_enc.pack('U*')
end
- array_enc.pack('U*')
+ end
+
+ iso_8859_15 = ISO_8859_15Encoder.new
+ register("ISO-8859-15") do |o|
+ o.encoder = iso_8859_15
end
end
end
diff --git a/lib/rexml/encodings/SHIFT-JIS.rb b/lib/rexml/encodings/SHIFT-JIS.rb
index 9e0f4af..313d426 100644
--- a/lib/rexml/encodings/SHIFT-JIS.rb
+++ b/lib/rexml/encodings/SHIFT-JIS.rb
@@ -2,34 +2,36 @@ module REXML
module Encoding
begin
require 'uconv'
-
- def decode_sjis content
- Uconv::sjistou8(content)
- end
-
- def encode_sjis(str)
- Uconv::u8tosjis(str)
+
+ class SHIFT_JISEncoder
+ def decode content
+ Uconv::sjistou8(content)
+ end
+
+ def encode(str)
+ Uconv::u8tosjis(str)
+ end
end
rescue LoadError
require 'nkf'
SJISTOU8 = '-Swm0x'
U8TOSJIS = '-Wsm0x'
-
- def decode_sjis(str)
- NKF.nkf(SJISTOU8, str)
- end
-
- def encode_sjis content
- NKF.nkf(U8TOSJIS, content)
+
+ class SHIFT_JISEncoder
+ def decode(str)
+ NKF.nkf(SJISTOU8, str)
+ end
+
+ def encode content
+ NKF.nkf(U8TOSJIS, content)
+ end
end
end
+ shift_jis = SHIFT_JISEncoder.new
b = proc do |obj|
- class << obj
- alias decode decode_sjis
- alias encode encode_sjis
- end
+ obj.encoder = shift_jis
end
register("SHIFT-JIS", &b)
register("SHIFT_JIS", &b)
diff --git a/lib/rexml/encodings/UNILE.rb b/lib/rexml/encodings/UNILE.rb
index d054140..6e360b1 100644
--- a/lib/rexml/encodings/UNILE.rb
+++ b/lib/rexml/encodings/UNILE.rb
@@ -1,34 +1,34 @@
module REXML
module Encoding
- def encode_unile content
- array_utf8 = content.unpack("U*")
- array_enc = []
- array_utf8.each do |num|
- if ((num>>16) > 0)
- array_enc << ??
- array_enc << 0
- else
- array_enc << (num & 0xFF)
- array_enc << (num >> 8)
+ class UNILEEncoder
+ def encode content
+ array_utf8 = content.unpack("U*")
+ array_enc = []
+ array_utf8.each do |num|
+ if ((num>>16) > 0)
+ array_enc << ??
+ array_enc << 0
+ else
+ array_enc << (num & 0xFF)
+ array_enc << (num >> 8)
+ end
end
+ array_enc.pack('C*')
+ end
+
+ def decode(str)
+ array_enc=str.unpack('C*')
+ array_utf8 = []
+ 0.step(array_enc.size-1, 2){|i|
+ array_utf8 << (array_enc.at(i) + array_enc.at(i+1)*0x100)
+ }
+ array_utf8.pack('U*')
end
- array_enc.pack('C*')
- end
-
- def decode_unile(str)
- array_enc=str.unpack('C*')
- array_utf8 = []
- 0.step(array_enc.size-1, 2){|i|
- array_utf8 << (array_enc.at(i) + array_enc.at(i+1)*0x100)
- }
- array_utf8.pack('U*')
end
+ unile = UNILEEncoder.new
register(UNILE) do |obj|
- class << obj
- alias decode decode_unile
- alias encode encode_unile
- end
+ obj.encoder = unile
end
end
end
diff --git a/lib/rexml/encodings/US-ASCII.rb b/lib/rexml/encodings/US-ASCII.rb
index fb4c217..5aea358 100644
--- a/lib/rexml/encodings/US-ASCII.rb
+++ b/lib/rexml/encodings/US-ASCII.rb
@@ -1,30 +1,30 @@
module REXML
module Encoding
- # Convert from UTF-8
- def encode_ascii content
- array_utf8 = content.unpack('U*')
- array_enc = []
- array_utf8.each do |num|
- if num <= 0x7F
- array_enc << num
- else
- # Numeric entity (&#nnnn;); shard by Stefan Scholl
- array_enc.concat "&\##{num};".unpack('C*')
+ class US_ASCIIEncoder
+ # Convert from UTF-8
+ def encode content
+ array_utf8 = content.unpack('U*')
+ array_enc = []
+ array_utf8.each do |num|
+ if num <= 0x7F
+ array_enc << num
+ else
+ # Numeric entity (&#nnnn;); shard by Stefan Scholl
+ array_enc.concat "&\##{num};".unpack('C*')
+ end
end
+ array_enc.pack('C*')
+ end
+
+ # Convert to UTF-8
+ def decode(str)
+ str.unpack('C*').pack('U*')
end
- array_enc.pack('C*')
- end
-
- # Convert to UTF-8
- def decode_ascii(str)
- str.unpack('C*').pack('U*')
end
+ us_ascii = US_ASCIIEncoder.new
register("US-ASCII") do |obj|
- class << obj
- alias decode decode_ascii
- alias encode encode_ascii
- end
+ us_ascii.encoder = us_ascii
end
end
end
diff --git a/lib/rexml/encodings/UTF-16.rb b/lib/rexml/encodings/UTF-16.rb
index 007c493..640fcc3 100644
--- a/lib/rexml/encodings/UTF-16.rb
+++ b/lib/rexml/encodings/UTF-16.rb
@@ -1,35 +1,35 @@
module REXML
module Encoding
- def encode_utf16 content
- array_utf8 = content.unpack("U*")
- array_enc = []
- array_utf8.each do |num|
- if ((num>>16) > 0)
- array_enc << 0
- array_enc << ??
- else
- array_enc << (num >> 8)
- array_enc << (num & 0xFF)
+ class UTF_16Encoder
+ def encode content
+ array_utf8 = content.unpack("U*")
+ array_enc = []
+ array_utf8.each do |num|
+ if ((num>>16) > 0)
+ array_enc << 0
+ array_enc << ??
+ else
+ array_enc << (num >> 8)
+ array_enc << (num & 0xFF)
+ end
end
+ array_enc.pack('C*')
+ end
+
+ def decode(str)
+ str = str[2..-1] if /^\376\377/n =~ str
+ array_enc=str.unpack('C*')
+ array_utf8 = []
+ 0.step(array_enc.size-1, 2){|i|
+ array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100)
+ }
+ array_utf8.pack('U*')
end
- array_enc.pack('C*')
- end
-
- def decode_utf16(str)
- str = str[2..-1] if /^\376\377/n =~ str
- array_enc=str.unpack('C*')
- array_utf8 = []
- 0.step(array_enc.size-1, 2){|i|
- array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100)
- }
- array_utf8.pack('U*')
end
+ utf_16 = UTF_16Encoder.new
register(UTF_16) do |obj|
- class << obj
- alias decode decode_utf16
- alias encode encode_utf16
- end
+ obj.encoder = utf_16
end
end
end
diff --git a/lib/rexml/encodings/UTF-8.rb b/lib/rexml/encodings/UTF-8.rb
index bb08f44..42f7b38 100644
--- a/lib/rexml/encodings/UTF-8.rb
+++ b/lib/rexml/encodings/UTF-8.rb
@@ -1,18 +1,18 @@
module REXML
module Encoding
- def encode_utf8 content
- content
- end
-
- def decode_utf8(str)
- str
+ class UTF_8Encoder
+ def encode content
+ content
+ end
+
+ def decode(str)
+ str
+ end
end
+ utf_8 = UTF_8Encoder.new
register(UTF_8) do |obj|
- class << obj
- alias decode decode_utf8
- alias encode encode_utf8
- end
+ obj.encoder = utf_8
end
end
end
diff --git a/lib/rexml/parsers/baseparser.rb b/lib/rexml/parsers/baseparser.rb
index fc2354a..50a7a81 100644
--- a/lib/rexml/parsers/baseparser.rb
+++ b/lib/rexml/parsers/baseparser.rb
@@ -108,22 +108,10 @@ module REXML
def initialize( source )
self.stream = source
+ @listeners = []
end
def add_listener( listener )
- if !defined?(@listeners) or !@listeners
- @listeners = []
- instance_eval <<-EOL
- alias :_old_pull :pull
- def pull
- event = _old_pull
- @listeners.each do |listener|
- listener.receive event
- end
- event
- end
- EOL
- end
@listeners << listener
end
@@ -186,6 +174,14 @@ module REXML
# Returns the next event. This is a +PullEvent+ object.
def pull
+ _pull_inner.tap do |event|
+ @listeners.each do |listener|
+ listener.receive event
+ end
+ end
+ end
+
+ def _pull_inner
if @closed
x, @closed = @closed, nil
return [ :end_element, x ]
diff --git a/lib/rexml/source.rb b/lib/rexml/source.rb
index ce7a2c9..aa9f20f 100644
--- a/lib/rexml/source.rb
+++ b/lib/rexml/source.rb
@@ -52,9 +52,9 @@ module REXML
# Overridden to support optimized en/decoding
def encoding=(enc)
return unless super
- @line_break = encode( '>' )
+ @line_break = @encoder.encode( '>' )
if enc != UTF_8
- @buffer = decode(@buffer)
+ @buffer = @encoder.decode(@buffer)
@to_utf = true
else
@to_utf = false

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.