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