GenericFixedWidthReader.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Provides fixed-width byte sizes for reading fixed-width character sets.
  11. * @package Swift
  12. * @subpackage Encoder
  13. * @author Chris Corbyn
  14. * @author Xavier De Cock <xdecock@gmail.com>
  15. */
  16. class Swift_CharacterReader_GenericFixedWidthReader
  17. implements Swift_CharacterReader
  18. {
  19. /**
  20. * The number of bytes in a single character.
  21. * @var int
  22. * @access private
  23. */
  24. private $_width;
  25. /**
  26. * Creates a new GenericFixedWidthReader using $width bytes per character.
  27. * @param int $width
  28. */
  29. public function __construct($width)
  30. {
  31. $this->_width = $width;
  32. }
  33. /**
  34. * Returns the complete charactermap
  35. *
  36. * @param string $string
  37. * @param int $startOffset
  38. * @param array $currentMap
  39. * @param mixed $ignoredChars
  40. * @return $int
  41. */
  42. public function getCharPositions($string, $startOffset, &$currentMap, &$ignoredChars)
  43. {
  44. $strlen = strlen($string);
  45. // % and / are CPU intensive, so, maybe find a better way
  46. $ignored = $strlen%$this->_width;
  47. $ignoredChars = substr($string, - $ignored);
  48. $currentMap = $this->_width;
  49. return ($strlen - $ignored)/$this->_width;
  50. }
  51. /**
  52. * Returns mapType
  53. * @int mapType
  54. */
  55. public function getMapType()
  56. {
  57. return self::MAP_TYPE_FIXED_LEN;
  58. }
  59. /**
  60. * Returns an integer which specifies how many more bytes to read.
  61. * A positive integer indicates the number of more bytes to fetch before invoking
  62. * this method again.
  63. * A value of zero means this is already a valid character.
  64. * A value of -1 means this cannot possibly be a valid character.
  65. * @param string $bytes
  66. * @return int
  67. */
  68. public function validateByteSequence($bytes, $size)
  69. {
  70. $needed = $this->_width - $size;
  71. return ($needed > -1)
  72. ? $needed
  73. : -1
  74. ;
  75. }
  76. /**
  77. * Returns the number of bytes which should be read to start each character.
  78. * @return int
  79. */
  80. public function getInitialByteSize()
  81. {
  82. return $this->_width;
  83. }
  84. }