Skip to content

Commit f74843a

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 f74843a

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/Encoder/Encoder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ 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(),
121+
Mode::ALPHANUMERIC() => strlen($content),
122+
Mode::KANJI(),
123+
default => iconv_strlen($content, 'utf-8'),
124+
};
119125
self::appendLengthInfo($numLetters, $version, $mode, $headerAndDataBits);
120126

121127
// Put data together into the overall payload.

0 commit comments

Comments
 (0)