Skip to content

Commit e532fc2

Browse files
committed
replacing special characters in subtokens
1 parent c2c623f commit e532fc2

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Paths/Subtokens.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ final class Subtokens
1212
*/
1313
public static function fromString(string $string): array
1414
{
15-
$string = str_replace([',', '|', '\\'], ['comma', 'pipe', 'slash'], $string);
15+
$string = str_replace([',', '|', '\\', '"', "'"], ['comma', 'pipe', 'slash', 'quote', "quote"], $string);
16+
17+
// Collapse whitespace
18+
$string = trim(preg_replace('/\s+/', ' ', $string));
1619

1720
// Replace all-caps `FOO` with `Foo`.
1821
$string = preg_replace_callback('/([A-Z])([A-Z]+)/', function ($matches) {
@@ -28,4 +31,4 @@ public static function fromString(string $string): array
2831

2932
return $tokens;
3033
}
31-
}
34+
}

tests/SubtokensTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,43 @@ public function testAllCaps(): void
7474
['foo', 'bar']
7575
);
7676
}
77+
78+
public function testWhitespaceCollapse(): void {
79+
$this->assertEquals(
80+
Subtokens::fromString('foo bar'),
81+
['foo', 'bar']
82+
);
83+
84+
$this->assertEquals(
85+
Subtokens::fromString("foo\tbar"),
86+
['foo', 'bar']
87+
);
88+
89+
$this->assertEquals(
90+
Subtokens::fromString("foo\t\tbar"),
91+
['foo', 'bar']
92+
);
93+
94+
$this->assertEquals(
95+
Subtokens::fromString(" foo\t\tbar "),
96+
['foo', 'bar']
97+
);
98+
}
99+
100+
public function testSpecialCharacters(): void {
101+
$this->assertEquals(
102+
Subtokens::fromString("' foo"),
103+
['quote', 'foo']
104+
);
105+
106+
$this->assertEquals(
107+
Subtokens::fromString("'foo' bar"),
108+
['quotefooquote', 'bar']
109+
);
110+
111+
$this->assertEquals(
112+
Subtokens::fromString(", | \\ \" '"),
113+
['comma', 'pipe', 'slash', 'quote', 'quote']
114+
);
115+
}
77116
}

0 commit comments

Comments
 (0)