@@ -84,7 +84,7 @@ public function parse($value, $flags = 0)
84
84
}
85
85
}
86
86
87
- if (! preg_match ('//u ' , $ value )) {
87
+ if (false === preg_match ('//u ' , $ value )) {
88
88
throw new ParseException ('The YAML value does not appear to be valid UTF-8. ' );
89
89
}
90
90
$ this ->currentLineNb = -1 ;
@@ -115,13 +115,13 @@ public function parse($value, $flags = 0)
115
115
}
116
116
117
117
$ isRef = $ mergeNode = false ;
118
- if (preg_match ('#^\-((?P<leadspaces>\s+)(?P<value>.+? ))?\s* $#u ' , $ this ->currentLine , $ values )) {
118
+ if (self :: preg_match ('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u ' , rtrim ( $ this ->currentLine ) , $ values )) {
119
119
if ($ context && 'mapping ' == $ context ) {
120
120
throw new ParseException ('You cannot define a sequence item when in a mapping ' , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
121
121
}
122
122
$ context = 'sequence ' ;
123
123
124
- if (isset ($ values ['value ' ]) && preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
124
+ if (isset ($ values ['value ' ]) && self :: preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
125
125
$ isRef = $ matches ['ref ' ];
126
126
$ values ['value ' ] = $ matches ['value ' ];
127
127
}
@@ -131,7 +131,7 @@ public function parse($value, $flags = 0)
131
131
$ data [] = $ this ->parseBlock ($ this ->getRealCurrentLineNb () + 1 , $ this ->getNextEmbedBlock (null , true ), $ flags );
132
132
} else {
133
133
if (isset ($ values ['leadspaces ' ])
134
- && preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+? ))?\s* $#u ' , $ values ['value ' ], $ matches )
134
+ && self :: preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+))?$#u ' , rtrim ( $ values ['value ' ]) , $ matches )
135
135
) {
136
136
// this is a compact notation element, add to next block and parse
137
137
$ block = $ values ['value ' ];
@@ -147,7 +147,10 @@ public function parse($value, $flags = 0)
147
147
if ($ isRef ) {
148
148
$ this ->refs [$ isRef ] = end ($ data );
149
149
}
150
- } elseif (preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u ' , $ this ->currentLine , $ values ) && (false === strpos ($ values ['key ' ], ' # ' ) || in_array ($ values ['key ' ][0 ], array ('" ' , "' " )))) {
150
+ } elseif (
151
+ self ::preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+))?$#u ' , rtrim ($ this ->currentLine ), $ values )
152
+ && (false === strpos ($ values ['key ' ], ' # ' ) || in_array ($ values ['key ' ][0 ], array ('" ' , "' " )))
153
+ ) {
151
154
if ($ context && 'sequence ' == $ context ) {
152
155
throw new ParseException ('You cannot define a mapping item when in a sequence ' , $ this ->currentLineNb + 1 , $ this ->currentLine );
153
156
}
@@ -215,7 +218,7 @@ public function parse($value, $flags = 0)
215
218
$ data += $ parsed ; // array union
216
219
}
217
220
}
218
- } elseif (isset ($ values ['value ' ]) && preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
221
+ } elseif (isset ($ values ['value ' ]) && self :: preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
219
222
$ isRef = $ matches ['ref ' ];
220
223
$ values ['value ' ] = $ matches ['value ' ];
221
224
}
@@ -283,27 +286,7 @@ public function parse($value, $flags = 0)
283
286
return $ value ;
284
287
}
285
288
286
- switch (preg_last_error ()) {
287
- case PREG_INTERNAL_ERROR :
288
- $ error = 'Internal PCRE error. ' ;
289
- break ;
290
- case PREG_BACKTRACK_LIMIT_ERROR :
291
- $ error = 'pcre.backtrack_limit reached. ' ;
292
- break ;
293
- case PREG_RECURSION_LIMIT_ERROR :
294
- $ error = 'pcre.recursion_limit reached. ' ;
295
- break ;
296
- case PREG_BAD_UTF8_ERROR :
297
- $ error = 'Malformed UTF-8 data. ' ;
298
- break ;
299
- case PREG_BAD_UTF8_OFFSET_ERROR :
300
- $ error = 'Offset doesn \'t correspond to the begin of a valid UTF-8 code point. ' ;
301
- break ;
302
- default :
303
- $ error = 'Unable to parse. ' ;
304
- }
305
-
306
- throw new ParseException ($ error , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
289
+ throw new ParseException ('Unable to parse. ' , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
307
290
}
308
291
}
309
292
@@ -546,7 +529,7 @@ private function parseValue($value, $flags, $context)
546
529
return $ this ->refs [$ value ];
547
530
}
548
531
549
- if (preg_match ('/^ ' .self ::TAG_PATTERN .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
532
+ if (self :: preg_match ('/^ ' .self ::TAG_PATTERN .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
550
533
$ modifiers = isset ($ matches ['modifiers ' ]) ? $ matches ['modifiers ' ] : '' ;
551
534
552
535
$ data = $ this ->parseBlockScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
@@ -628,7 +611,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
628
611
629
612
// determine indentation if not specified
630
613
if (0 === $ indentation ) {
631
- if (preg_match ('/^ +/ ' , $ this ->currentLine , $ matches )) {
614
+ if (self :: preg_match ('/^ +/ ' , $ this ->currentLine , $ matches )) {
632
615
$ indentation = strlen ($ matches [0 ]);
633
616
}
634
617
}
@@ -639,7 +622,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
639
622
while (
640
623
$ notEOF && (
641
624
$ isCurrentLineBlank ||
642
- preg_match ($ pattern , $ this ->currentLine , $ matches )
625
+ self :: preg_match ($ pattern , $ this ->currentLine , $ matches )
643
626
)
644
627
) {
645
628
if ($ isCurrentLineBlank && strlen ($ this ->currentLine ) > $ indentation ) {
@@ -862,6 +845,49 @@ private function isStringUnIndentedCollectionItem()
862
845
*/
863
846
private function isBlockScalarHeader ()
864
847
{
865
- return (bool ) preg_match ('~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' , $ this ->currentLine );
848
+ return (bool ) self ::preg_match ('~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' , $ this ->currentLine );
849
+ }
850
+
851
+ /**
852
+ * A local wrapper for `preg_match` which will throw a ParseException if there
853
+ * is an internal error in the PCRE engine.
854
+ *
855
+ * This avoids us needing to check for "false" every time PCRE is used
856
+ * in the YAML engine
857
+ *
858
+ * @throws ParseException on a PCRE internal error
859
+ *
860
+ * @see preg_last_error()
861
+ *
862
+ * @internal
863
+ */
864
+ public static function preg_match ($ pattern , $ subject , &$ matches = null , $ flags = 0 , $ offset = 0 )
865
+ {
866
+ $ ret = preg_match ($ pattern , $ subject , $ matches , $ flags , $ offset );
867
+ if ($ ret === false ) {
868
+ switch (preg_last_error ()) {
869
+ case PREG_INTERNAL_ERROR :
870
+ $ error = 'Internal PCRE error. ' ;
871
+ break ;
872
+ case PREG_BACKTRACK_LIMIT_ERROR :
873
+ $ error = 'pcre.backtrack_limit reached. ' ;
874
+ break ;
875
+ case PREG_RECURSION_LIMIT_ERROR :
876
+ $ error = 'pcre.recursion_limit reached. ' ;
877
+ break ;
878
+ case PREG_BAD_UTF8_ERROR :
879
+ $ error = 'Malformed UTF-8 data. ' ;
880
+ break ;
881
+ case PREG_BAD_UTF8_OFFSET_ERROR :
882
+ $ error = 'Offset doesn \'t correspond to the begin of a valid UTF-8 code point. ' ;
883
+ break ;
884
+ default :
885
+ $ error = 'Error. ' ;
886
+ }
887
+
888
+ throw new ParseException ($ error );
889
+ }
890
+
891
+ return $ ret ;
866
892
}
867
893
}
0 commit comments