Skip to content

Commit

Permalink
Ruby: Optimized regex and string patterns (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored May 18, 2020
1 parent b4ed5de commit b526e8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.
76 changes: 16 additions & 60 deletions components/prism-ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,14 @@
Prism.languages.insertBefore('ruby', 'keyword', {
'regex': [
{
pattern: /%r([^a-zA-Z0-9\s{(\[<])(?:(?!\1)[^\\]|\\[\s\S])*\1[gim]{0,3}/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
pattern: /%r\((?:[^()\\]|\\[\s\S])*\)[gim]{0,3}/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
// Here we need to specifically allow interpolation
pattern: /%r\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}[gim]{0,3}/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
pattern: /%r\[(?:[^\[\]\\]|\\[\s\S])*\][gim]{0,3}/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
pattern: /%r<(?:[^<>\\]|\\[\s\S])*>[gim]{0,3}/,
pattern: RegExp(/%r/.source + '(?:' + [
/([^a-zA-Z0-9\s{(\[<])(?:(?!\1)[^\\]|\\[\s\S])*\1[gim]{0,3}/.source,
/\((?:[^()\\]|\\[\s\S])*\)[gim]{0,3}/.source,
// Here we need to specifically allow interpolation
/\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}[gim]{0,3}/.source,
/\[(?:[^\[\]\\]|\\[\s\S])*\][gim]{0,3}/.source,
/<(?:[^<>\\]|\\[\s\S])*>[gim]{0,3}/.source
].join('|') + ')'),
greedy: true,
inside: {
'interpolation': interpolation
Expand Down Expand Up @@ -102,36 +80,14 @@

Prism.languages.ruby.string = [
{
pattern: /%[qQiIwWxs]?([^a-zA-Z0-9\s{(\[<])(?:(?!\1)[^\\]|\\[\s\S])*\1/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
pattern: /%[qQiIwWxs]?\((?:[^()\\]|\\[\s\S])*\)/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
// Here we need to specifically allow interpolation
pattern: /%[qQiIwWxs]?\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
pattern: /%[qQiIwWxs]?\[(?:[^\[\]\\]|\\[\s\S])*\]/,
greedy: true,
inside: {
'interpolation': interpolation
}
},
{
pattern: /%[qQiIwWxs]?<(?:[^<>\\]|\\[\s\S])*>/,
pattern: RegExp(/%[qQiIwWxs]?/.source + '(?:' + [
/([^a-zA-Z0-9\s{(\[<])(?:(?!\1)[^\\]|\\[\s\S])*\1/.source,
/\((?:[^()\\]|\\[\s\S])*\)/.source,
// Here we need to specifically allow interpolation
/\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}/.source,
/\[(?:[^\[\]\\]|\\[\s\S])*\]/.source,
/<(?:[^<>\\]|\\[\s\S])*>/.source
].join('|') + ')'),
greedy: true,
inside: {
'interpolation': interpolation
Expand Down
2 changes: 1 addition & 1 deletion components/prism-ruby.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b526e8c

Please sign in to comment.