Skip to content

Commit 1be70e9

Browse files
authored
Merge pull request #4344 from oleibman/parseutf8
Writer/Xls/Parser::advance Should Parse by Character
2 parents 18cfe5b + cff4015 commit 1be70e9

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4646

4747
### Fixed
4848

49-
- Xls Writer Parser Mishandling True/False Argument. [Issue #4331](https://github.com/PHPOffice/PhpSpreadsheet/issues/4331) [PR #4333](https://github.com/PHPOffice/PhpSpreadsheet/pull/4333)
49+
- Xls writer Parser Mishandling True/False Argument. [Issue #4331](https://github.com/PHPOffice/PhpSpreadsheet/issues/4331) [PR #4333](https://github.com/PHPOffice/PhpSpreadsheet/pull/4333)
50+
- Xls writer Parser Parse By Character Not Byte. [PR #4344](https://github.com/PHPOffice/PhpSpreadsheet/pull/4344)
5051
- Minor changes to dynamic array calculations exposed by using explicit array return types in some tests. [PR #4328](https://github.com/PHPOffice/PhpSpreadsheet/pull/4328)
5152

5253
## 2025-01-26 - 3.9.0

src/PhpSpreadsheet/Writer/Xls/Parser.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class Parser
6767
. '[$]?[A-Ia-i]?[A-Za-z][$]?(\\d+)'
6868
. '$~u';
6969

70+
private const UTF8 = 'UTF-8';
71+
7072
/**
7173
* The index of the character we are currently looking at.
7274
*/
@@ -991,37 +993,38 @@ private function advance(): void
991993
{
992994
$token = '';
993995
$i = $this->currentCharacter;
994-
$formula_length = strlen($this->formula);
996+
$formula = mb_str_split($this->formula, 1, self::UTF8);
997+
$formula_length = count($formula);
995998
// eat up white spaces
996999
if ($i < $formula_length) {
997-
while ($this->formula[$i] == ' ') {
1000+
while ($formula[$i] === ' ') {
9981001
++$i;
9991002
}
10001003

10011004
if ($i < ($formula_length - 1)) {
1002-
$this->lookAhead = $this->formula[$i + 1];
1005+
$this->lookAhead = $formula[$i + 1];
10031006
}
10041007
$token = '';
10051008
}
10061009

10071010
while ($i < $formula_length) {
1008-
$token .= $this->formula[$i];
1011+
$token .= $formula[$i];
10091012

10101013
if ($i < ($formula_length - 1)) {
1011-
$this->lookAhead = $this->formula[$i + 1];
1014+
$this->lookAhead = $formula[$i + 1];
10121015
} else {
10131016
$this->lookAhead = '';
10141017
}
10151018

1016-
if ($this->match($token) != '') {
1019+
if ($this->match($token) !== '') {
10171020
$this->currentCharacter = $i + 1;
10181021
$this->currentToken = $token;
10191022

10201023
return;
10211024
}
10221025

10231026
if ($i < ($formula_length - 2)) {
1024-
$this->lookAhead = $this->formula[$i + 2];
1027+
$this->lookAhead = $formula[$i + 2];
10251028
} else { // if we run out of characters lookAhead becomes empty
10261029
$this->lookAhead = '';
10271030
}
@@ -1198,8 +1201,8 @@ private function match(string $token): string
11981201
public function parse(string $formula): bool
11991202
{
12001203
$this->currentCharacter = 0;
1201-
$this->formula = (string) $formula;
1202-
$this->lookAhead = $formula[1] ?? '';
1204+
$this->formula = $formula;
1205+
$this->lookAhead = mb_substr($formula, 1, 1, self::UTF8);
12031206
$this->advance();
12041207
$this->parseTree = $this->condition();
12051208

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
6+
use PhpOffice\PhpSpreadsheet\Cell\DataValidator;
7+
use PhpOffice\PhpSpreadsheet\NamedRange;
8+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
10+
11+
class NonLatinFormulasTest extends AbstractFunctional
12+
{
13+
public function testNonLatin(): void
14+
{
15+
$spreadsheet = new Spreadsheet();
16+
$worksheet = $spreadsheet->getActiveSheet();
17+
18+
$validation = $worksheet->getCell('B1')->getDataValidation();
19+
$validation->setType(DataValidation::TYPE_LIST);
20+
$validation->setErrorStyle(DataValidation::STYLE_STOP);
21+
$validation->setAllowBlank(false);
22+
$validation->setShowInputMessage(true);
23+
$validation->setShowErrorMessage(true);
24+
$validation->setShowDropDown(true);
25+
$validation->setFormula1('"слово, сло"');
26+
27+
$dataValidator = new DataValidator();
28+
$worksheet->getCell('B1')->setValue('слово');
29+
self::assertTrue(
30+
$dataValidator->isValid($worksheet->getCell('B1'))
31+
);
32+
$worksheet->getCell('B1')->setValue('слов');
33+
self::assertFalse(
34+
$dataValidator->isValid($worksheet->getCell('B1'))
35+
);
36+
37+
$worksheet->setTitle('словслов');
38+
$worksheet->getCell('A1')->setValue('=словслов!B1');
39+
$worksheet->getCell('A2')->setValue("='словслов'!B1");
40+
$spreadsheet->addNamedRange(new NamedRange('слсл', $worksheet, '$B$1'));
41+
$worksheet->getCell('A3')->setValue('=слсл');
42+
43+
$robj = $this->writeAndReload($spreadsheet, 'Xls');
44+
$spreadsheet->disconnectWorksheets();
45+
$sheet0 = $robj->getActiveSheet();
46+
self::assertSame('словслов', $sheet0->getTitle());
47+
self::assertSame('=словслов!B1', $sheet0->getCell('A1')->getValue());
48+
self::assertSame('слов', $sheet0->getCell('A1')->getCalculatedValue());
49+
// Quotes around sheet name are stripped off - harmless
50+
//self::assertSame("='словслов'!B1", $sheet0->getCell('A2')->getValue());
51+
self::assertSame('слов', $sheet0->getCell('A2')->getCalculatedValue());
52+
// Formulas with defined names don't work in Xls Writer
53+
//self::assertSame('=слсл', $sheet0->getCell('A3')->getValue());
54+
// But result should be accurate
55+
self::assertSame('слов', $sheet0->getCell('A3')->getCalculatedValue());
56+
$names = $robj->getDefinedNames();
57+
self::assertCount(1, $names);
58+
// name has been uppercased
59+
$namedRange = $names['СЛСЛ'] ?? null;
60+
self::assertInstanceOf(NamedRange::class, $namedRange);
61+
self::assertSame('$B$1', $namedRange->getRange());
62+
63+
$robj->disconnectWorksheets();
64+
}
65+
}

0 commit comments

Comments
 (0)