Skip to content

Commit

Permalink
JavaScript: added support for Unicode-aware regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Sep 18, 2016
1 parent 6e284fa commit f2c2203
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
23 changes: 22 additions & 1 deletion README.md
Expand Up @@ -72,15 +72,36 @@ echo $builder->build(['☺', '☹']);

### UTF-8 input with JavaScript output

For JavaScript regular expressions that do not use the `u` flag and need the higher codepoints to be split into surrogates.

```php
$builder = new s9e\RegexpBuilder\Builder([
'input' => 'Utf8ToSurrogates',
'output' => 'JavaScript'
]);
echo $builder->build(['☺', '☹']);
echo $builder->build(['☺', '☹']), "\n";
echo $builder->build(['😁', '😂']);
```
```
[\u2639\u263A]
\uD83D[\uDE01\uDE02]
```

### UTF-8 input with Unicode-aware JavaScript output

For JavaScript regular expressions that use the `u` flag introduced in ECMAScript 6.

```php
$builder = new s9e\RegexpBuilder\Builder([
'input' => 'Utf8',
'output' => 'JavaScript'
]);
echo $builder->build(['☺', '☹']), "\n";
echo $builder->build(['😁', '😂']);
```
```
[\u2639\u263A]
[\u{1F601}\u{1F602}]
```

### Custom delimiters
Expand Down
2 changes: 1 addition & 1 deletion composer.json
@@ -1,6 +1,6 @@
{
"name": "s9e/regexp-builder",
"version": "1.0.1",
"version": "1.1.0",
"type": "library",
"description": "Single-purpose library that generates regular expressions that match a list of literal strings.",
"homepage": "https://github.com/s9e/RegexpBuilder/",
Expand Down
4 changes: 2 additions & 2 deletions src/Output/JavaScript.php
Expand Up @@ -10,13 +10,13 @@
class JavaScript extends PrintableAscii
{
/** {@inheritdoc} */
protected $maxValue = 0xFFFF;
protected $maxValue = 0x10FFFF;

/**
* {@inheritdoc}
*/
protected function escapeUnicode($cp)
{
return sprintf('\\u%04X', $cp);
return sprintf(($cp > 0xFFFF) ? '\\u{%X}' : '\\u%04X', $cp);
}
}
3 changes: 2 additions & 1 deletion tests/Output/JavaScriptTest.php
Expand Up @@ -22,7 +22,8 @@ public function getOutputTests()
[102, 'f'],
[0xC3, '\\xC3'],
[0x2026, '\\u2026'],
[0x1F600, new InvalidArgumentException('Value 128512 is out of bounds (0..65535)')]
[0x1F600, '\\u{1F600}'],
[0x110000, new InvalidArgumentException('Value 1114112 is out of bounds (0..1114111)')]
];
}
}

0 comments on commit f2c2203

Please sign in to comment.