Skip to content

Commit 6b9558e

Browse files
committed
Fixed bug for text generator.
closes #1
1 parent fba62c5 commit 6b9558e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Tests/TextGeneratorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public function testGenerator()
5858
$textGen->generate("{|}"),
5959
''
6060
);
61+
62+
for ($i = 0; $i < 100; $i++) {
63+
$this->assertContains(
64+
$textGen->generate("{aaa {bbb|bbb|bbb}|aaa|aaa}, {ccc|ccc}! {ddd|ddd}?"),
65+
[
66+
'aaa, ccc! ddd?',
67+
'aaa bbb, ccc! ddd?',
68+
]
69+
);
70+
}
6171
}
6272

6373
public function testCustomConfig()

TextGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ private function getRandomPart($text)
173173
$lastPos = 0;
174174
$isIgnore = false;
175175
$parts = [];
176-
for ($i = 0; $i < mb_strlen($text, $this->encoding); $i++) {
176+
$textLen = mb_strlen($text, $this->encoding);
177+
for ($i = 0; $i < $textLen; $i++) {
177178
$currentChar = mb_substr($text, $i, 1, $this->encoding);
178179
if ($currentChar === $this->startTag) {
179180
$openLevel++;
@@ -195,7 +196,7 @@ private function getRandomPart($text)
195196
}
196197

197198
if ($currentChar === $this->separator) {
198-
$parts[] = mb_substr($text, $lastPos, $i, $this->encoding);
199+
$parts[] = mb_substr($text, $lastPos, ($i - $lastPos), $this->encoding);
199200
$lastPos = $i + 1;
200201
}
201202
}

0 commit comments

Comments
 (0)