GenericFixedWidthReader.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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) ? $needed : -1;
  72. }
  73. /**
  74. * Returns the number of bytes which should be read to start each character.
  75. * @return int
  76. */
  77. public function getInitialByteSize()
  78. {
  79. return $this->_width;
  80. }
  81. }