GenericFixedWidthReader.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 implements Swift_CharacterReader
  17. {
  18. /**
  19. * The number of bytes in a single character.
  20. * @var int
  21. * @access private
  22. */
  23. private $_width;
  24. /**
  25. * Creates a new GenericFixedWidthReader using $width bytes per character.
  26. * @param int $width
  27. */
  28. public function __construct($width)
  29. {
  30. $this->_width = $width;
  31. }
  32. /**
  33. * Returns the complete charactermap
  34. *
  35. * @param string $string
  36. * @param int $startOffset
  37. * @param array $currentMap
  38. * @param mixed $ignoredChars
  39. * @return $int
  40. */
  41. public function getCharPositions($string, $startOffset, &$currentMap, &$ignoredChars)
  42. {
  43. $strlen = strlen($string);
  44. // % and / are CPU intensive, so, maybe find a better way
  45. $ignored = $strlen%$this->_width;
  46. $ignoredChars = substr($string, - $ignored);
  47. $currentMap = $this->_width;
  48. return ($strlen - $ignored)/$this->_width;
  49. }
  50. /**
  51. * Returns mapType
  52. * @return int mapType
  53. */
  54. public function getMapType()
  55. {
  56. return self::MAP_TYPE_FIXED_LEN;
  57. }
  58. /**
  59. * Returns an integer which specifies how many more bytes to read.
  60. * A positive integer indicates the number of more bytes to fetch before invoking
  61. * this method again.
  62. * A value of zero means this is already a valid character.
  63. * A value of -1 means this cannot possibly be a valid character.
  64. * @param string $bytes
  65. * @return int
  66. */
  67. public function validateByteSequence($bytes, $size)
  68. {
  69. $needed = $this->_width - $size;
  70. return ($needed > -1) ? $needed : -1;
  71. }
  72. /**
  73. * Returns the number of bytes which should be read to start each character.
  74. * @return int
  75. */
  76. public function getInitialByteSize()
  77. {
  78. return $this->_width;
  79. }
  80. }