$lines =
[];
$lNo = 0;
$lines[$lNo] = '';
$currentLine = &
$lines[$lNo++
];
$size =
$lineLen = 0;
$charStream =
new CharacterStream($string,
$charset);
// Fetching more than 4 chars at one is slower, as is fetching fewer bytes
// Conveniently 4 chars is the UTF-8 safe number since UTF-8 has up to 6
// bytes per char and (6 * 4 * 3 = 72 chars per line) * =NN is 3 bytes
while (null !==
$bytes =
$charStream->
readBytes(4
)) { $enc =
$this->
encodeByteSequence($bytes,
$size);
$i =
strpos($enc, '=0D=0A'
);
$newLineLength =
$lineLen +
(false ===
$i ?
$size :
$i);
if ($currentLine &&
$newLineLength >=
$thisLineLength) { $lines[$lNo] = '';
$currentLine = &
$lines[$lNo++
];
$thisLineLength =
$maxLineLength;
$lineLen = 0;
}