Skip to content

Commit 39a90d6

Browse files
committed
Use strlen() for numeric and alphanumeric modes to avoid iconv overhead
For NUMERIC and ALPHANUMERIC modes, strlen() is sufficient since these modes only operate on single-byte characters. strlen() runs in O(1) time and avoids the overhead of iconv_strlen().
1 parent a7e2d7b commit 39a90d6

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/Encoder/Encoder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ public static function encode(
115115
$headerAndDataBits->appendBitArray($headerBits);
116116

117117
// Find "length" of main segment and write it.
118-
$numLetters = (Mode::BYTE() === $mode ? $dataBits->getSizeInBytes() : iconv_strlen($content, 'utf-8'));
118+
$numLetters = match ($mode) {
119+
Mode::BYTE() => $dataBits->getSizeInBytes(),
120+
Mode::NUMERIC(), Mode::ALPHANUMERIC() => strlen($content),
121+
Mode::KANJI(), default => iconv_strlen($content, 'utf-8'),
122+
};
119123
self::appendLengthInfo($numLetters, $version, $mode, $headerAndDataBits);
120124

121125
// Put data together into the overall payload.

0 commit comments

Comments
 (0)