From 1388dc8f9dbe092e7c4937c531d01430aca278f0 Mon Sep 17 00:00:00 2001 From: atymic Date: Wed, 14 Jul 2021 13:21:33 +1000 Subject: [PATCH 1/4] fix: straddle message bug --- src/SmsLength.php | 34 ++++++++++++++++++++++++++++++++-- tests/SmsLengthTest.php | 15 +++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/SmsLength.php b/src/SmsLength.php index 93da5a1..1aeb39c 100644 --- a/src/SmsLength.php +++ b/src/SmsLength.php @@ -77,6 +77,11 @@ class SmsLength */ private $size; + /** + * @var int + */ + private $padding; + /** * @var int */ @@ -108,6 +113,16 @@ public function getSize(): int return $this->size; } + /** + * Get size of message padding used in the determined encoding + * + * @return int + */ + public function getPadding() + { + return $this->padding; + } + /** * Get number of messages that would be used to send the given content size */ @@ -162,12 +177,18 @@ private function inspect(string $messageContent): void // Any character outside the 7-bit alphabet switches the entire encoding to UCS-2 $this->encoding = '7-bit'; $this->size = 0; + $this->padding = 0; + $mbLength = mb_strlen($messageContent, 'UTF-8'); for ($i = 0; $i < $mbLength; $i++) { $char = mb_substr($messageContent, $i, 1, 'UTF-8'); if (in_array($char, self::GSM0338_BASIC, true)) { $this->size++; } elseif (in_array($char, self::GSM0338_EXTENDED, true)) { + // In cases where a double counted char straddles two messages, add padding to push it to the next part + if (($this->size + 2) % self::MAXIMUM_CHARACTERS_7BIT_CONCATENATED === 1) { + $this->padding++; + } $this->size += 2; } else { $this->encoding = 'ucs-2'; @@ -182,7 +203,14 @@ private function inspect(string $messageContent): void for ($i = 0; $i < $mbLength; $i++) { $char = mb_substr($messageContent, $i, 1, 'UTF-8'); $utf16Hex = bin2hex(mb_convert_encoding($char, 'UTF-16', 'UTF-8')); - $this->size += strlen($utf16Hex) / 4; + $charSize = strlen($utf16Hex) / 4; + + // In cases where a double counted char straddles two messages, add padding to push it to the next part + if ($charSize > 1 && ($this->size + $charSize) % self::MAXIMUM_CHARACTERS_UCS2_CONCATENATED === 1) { + $this->padding++; + } + + $this->size += $charSize; } } @@ -194,9 +222,11 @@ private function inspect(string $messageContent): void $concatSize = self::MAXIMUM_CHARACTERS_UCS2_CONCATENATED; } + $sizeIncludingPadding = $this->size + $this->padding; + $this->messageCount = 1; if ($this->size > $singleSize) { - $this->messageCount = (int)ceil($this->size / $concatSize); + $this->messageCount = (int)ceil($sizeIncludingPadding / $concatSize); } } } diff --git a/tests/SmsLengthTest.php b/tests/SmsLengthTest.php index 011bf83..7e4792c 100644 --- a/tests/SmsLengthTest.php +++ b/tests/SmsLengthTest.php @@ -88,6 +88,21 @@ public function providerSize(): array // empty 'empty' => ['', '7-bit', 0, 1, 160], + + [ + 'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown f[x jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the.', + '7-bit', + 306, + 3, + 459, + ], + [ + str_repeat('🌐', 67), + 'ucs-2', + 134, + 3, + 201 + ], ]; } From e861cb37d39fe81ebb762642cfdd56bed2dd1bbc Mon Sep 17 00:00:00 2001 From: atymic Date: Tue, 20 Jul 2021 08:52:47 +1000 Subject: [PATCH 2/4] fix: test naming --- tests/SmsLengthTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/SmsLengthTest.php b/tests/SmsLengthTest.php index 7e4792c..4b3cc2d 100644 --- a/tests/SmsLengthTest.php +++ b/tests/SmsLengthTest.php @@ -89,19 +89,19 @@ public function providerSize(): array // empty 'empty' => ['', '7-bit', 0, 1, 160], - [ + 'test length calculates correctly when a GSM extended char straddles two messages' => [ 'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown f[x jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the.', '7-bit', 306, 3, 459, ], - [ + 'test length calculates correctly when a unicode char straddles two messages' => [ str_repeat('🌐', 67), 'ucs-2', 134, 3, - 201 + 201, ], ]; } From e3649d2b238142267654f2519334d3e46d607256 Mon Sep 17 00:00:00 2001 From: atymic Date: Mon, 19 Jul 2021 09:41:51 +1000 Subject: [PATCH 3/4] fix: name test cases --- tests/SmsLengthTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/SmsLengthTest.php b/tests/SmsLengthTest.php index 4b3cc2d..49389a7 100644 --- a/tests/SmsLengthTest.php +++ b/tests/SmsLengthTest.php @@ -87,7 +87,7 @@ public function providerSize(): array 'long-ucs-exact' => [str_repeat('exact•max', 67), 'ucs-2', 603, 9, 603], // empty - 'empty' => ['', '7-bit', 0, 1, 160], + 'empty messages' => ['', '7-bit', 0, 1, 160], 'test length calculates correctly when a GSM extended char straddles two messages' => [ 'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown f[x jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the.', From e57514f1f6aff0308b548fbe9e50de9a33049983 Mon Sep 17 00:00:00 2001 From: atymic Date: Tue, 1 Feb 2022 09:50:27 +1100 Subject: [PATCH 4/4] fix: remove padding var --- src/SmsLength.php | 24 +++--------------------- tests/SmsLengthTest.php | 8 ++++---- 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/src/SmsLength.php b/src/SmsLength.php index 1aeb39c..89902d9 100644 --- a/src/SmsLength.php +++ b/src/SmsLength.php @@ -77,11 +77,6 @@ class SmsLength */ private $size; - /** - * @var int - */ - private $padding; - /** * @var int */ @@ -113,16 +108,6 @@ public function getSize(): int return $this->size; } - /** - * Get size of message padding used in the determined encoding - * - * @return int - */ - public function getPadding() - { - return $this->padding; - } - /** * Get number of messages that would be used to send the given content size */ @@ -177,7 +162,6 @@ private function inspect(string $messageContent): void // Any character outside the 7-bit alphabet switches the entire encoding to UCS-2 $this->encoding = '7-bit'; $this->size = 0; - $this->padding = 0; $mbLength = mb_strlen($messageContent, 'UTF-8'); for ($i = 0; $i < $mbLength; $i++) { @@ -187,7 +171,7 @@ private function inspect(string $messageContent): void } elseif (in_array($char, self::GSM0338_EXTENDED, true)) { // In cases where a double counted char straddles two messages, add padding to push it to the next part if (($this->size + 2) % self::MAXIMUM_CHARACTERS_7BIT_CONCATENATED === 1) { - $this->padding++; + $this->size++; } $this->size += 2; } else { @@ -207,7 +191,7 @@ private function inspect(string $messageContent): void // In cases where a double counted char straddles two messages, add padding to push it to the next part if ($charSize > 1 && ($this->size + $charSize) % self::MAXIMUM_CHARACTERS_UCS2_CONCATENATED === 1) { - $this->padding++; + $this->size++; } $this->size += $charSize; @@ -222,11 +206,9 @@ private function inspect(string $messageContent): void $concatSize = self::MAXIMUM_CHARACTERS_UCS2_CONCATENATED; } - $sizeIncludingPadding = $this->size + $this->padding; - $this->messageCount = 1; if ($this->size > $singleSize) { - $this->messageCount = (int)ceil($sizeIncludingPadding / $concatSize); + $this->messageCount = (int) ceil($this->size / $concatSize); } } } diff --git a/tests/SmsLengthTest.php b/tests/SmsLengthTest.php index 49389a7..0766bfe 100644 --- a/tests/SmsLengthTest.php +++ b/tests/SmsLengthTest.php @@ -78,8 +78,8 @@ public function providerSize(): array 'long-gsm-exact' => [str_repeat('exact max', 153), '7-bit', 1377, 9, 1377], // long 7-bit extended - 'long-gsm-ex-1' => [str_repeat(self::GSM0338_EXTENDED, 40), '7-bit', 720, 5, 765], - 'long-gsm-ex-2' => [str_repeat(self::GSM0338_EXTENDED, 76), '7-bit', 1368, 9, 1377], + 'long-gsm-ex-1' => [str_repeat(self::GSM0338_EXTENDED, 40), '7-bit', 724, 5, 765], + 'long-gsm-ex-2' => [str_repeat(self::GSM0338_EXTENDED, 76), '7-bit', 1376, 9, 1377], // long UCS-2 'long-ucs-1' => [str_repeat('simple msg plus •', 20), 'ucs-2', 340, 6, 402], @@ -92,14 +92,14 @@ public function providerSize(): array 'test length calculates correctly when a GSM extended char straddles two messages' => [ 'The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown f[x jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the.', '7-bit', - 306, + 307, 3, 459, ], 'test length calculates correctly when a unicode char straddles two messages' => [ str_repeat('🌐', 67), 'ucs-2', - 134, + 136, 3, 201, ],