Skip to content

Commit

Permalink
calculate length once
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Mar 18, 2024
1 parent c8a78c8 commit e9a0956
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/Stream/OptimalTransferEncodedPhraseStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ public function __construct(string $text, int $lineLength = 78, string $lineBrea
*/
private function calculateOptimalStream(string $text): StreamInterface
{
if (\strcspn($text, self::NON_7BIT_CHARS) === \strlen($text)) {
$length = \strlen($text);

if (\strcspn($text, self::NON_7BIT_CHARS) === $length) {
$this->encoding = '7bit';
return new AsciiEncodedStream($text, $this->lineLength, $this->lineBreak);
}

if (\strcspn($text, HeaderValueParameter::RFC_822_T_SPECIAL) !== \strlen($text)) {
if (\strcspn($text, HeaderValueParameter::RFC_822_T_SPECIAL) !== $length) {
$this->encoding = 'base64';
return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak);
}

if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > (\strlen($text) / 3)) {
if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > ($length / 3)) {
$this->encoding = 'base64';
return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Stream/OptimalTransferEncodedTextStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ public function __construct(string $text, int $lineLength = 78, string $lineBrea
*/
private function calculateOptimalStream(string $text): StreamInterface
{
if (\strcspn($text, self::NON_7BIT_CHARS) === \strlen($text)) {
$length = \strlen($text);

if (\strcspn($text, self::NON_7BIT_CHARS) === $length) {
$this->encoding = '7bit';
return new AsciiEncodedStream($text, $this->lineLength, $this->lineBreak);
}

if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > (\strlen($text) / 3)) {
if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > ($length / 3)) {
$this->encoding = 'base64';
return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak);
}
Expand Down

0 comments on commit e9a0956

Please sign in to comment.