Skip to content

Commit

Permalink
Merge pull request #18 from pcholewa/master
Browse files Browse the repository at this point in the history
fix for trimmed values labels multibyte chars
  • Loading branch information
tiamo committed Feb 21, 2019
2 parents cc710d5 + 83ee3e4 commit a17eb3a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Sav/Record/ValueLabel.php
Expand Up @@ -108,16 +108,22 @@ public function write(Buffer $buffer)
$buffer->writeInt(self::TYPE);
$buffer->writeInt(count($this->labels));
foreach ($this->labels as $item) {
$labelLength = mb_strlen($item['label']);
$labelLength = min($labelLength, self::LABEL_MAX_LENGTH);
$labelLength = min(mb_strlen($item['label']), self::LABEL_MAX_LENGTH);
$label = mb_substr($item['label'], 0, $labelLength);
$labelLengthBytes = mb_strlen($label, '8bit');
while ($labelLengthBytes > 255) {
// Strip one char, can be multiple bytes
$label = mb_substr($item['label'], 0, -1);
$labelLengthBytes = mb_strlen($label, '8bit');
}

if ($convertToDouble) {
$item['value'] = Utils::stringToDouble($item['value']);
}

$buffer->writeDouble($item['value']);
$buffer->write(chr($labelLength));
$buffer->writeString($item['label'], Utils::roundUp($labelLength + 1, 8) - 1);
$buffer->write(chr($labelLengthBytes));
$buffer->writeString($label, Utils::roundUp($labelLengthBytes + 1, 8) - 1);
}

// Value label variable record.
Expand Down

0 comments on commit a17eb3a

Please sign in to comment.