@@ -61,7 +61,7 @@ public function __construct($offset = 0, $totalNumberOfLines = null, array $skip
61
61
*/
62
62
public function parse ($ value , $ exceptionOnInvalidType = false , $ objectSupport = false , $ objectForMap = false )
63
63
{
64
- if (! preg_match ('//u ' , $ value )) {
64
+ if (false === preg_match ('//u ' , $ value )) {
65
65
throw new ParseException ('The YAML value does not appear to be valid UTF-8. ' );
66
66
}
67
67
$ this ->currentLineNb = -1 ;
@@ -92,13 +92,13 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
92
92
}
93
93
94
94
$ isRef = $ mergeNode = false ;
95
- if (preg_match ('#^\-((?P<leadspaces>\s+)(?P<value>.+? ))?\s* $#u ' , $ this ->currentLine , $ values )) {
95
+ if (self :: preg_match ('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u ' , rtrim ( $ this ->currentLine ) , $ values )) {
96
96
if ($ context && 'mapping ' == $ context ) {
97
97
throw new ParseException ('You cannot define a sequence item when in a mapping ' , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
98
98
}
99
99
$ context = 'sequence ' ;
100
100
101
- if (isset ($ values ['value ' ]) && preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
101
+ if (isset ($ values ['value ' ]) && self :: preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
102
102
$ isRef = $ matches ['ref ' ];
103
103
$ values ['value ' ] = $ matches ['value ' ];
104
104
}
@@ -108,7 +108,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
108
108
$ data [] = $ this ->parseBlock ($ this ->getRealCurrentLineNb () + 1 , $ this ->getNextEmbedBlock (null , true ), $ exceptionOnInvalidType , $ objectSupport , $ objectForMap );
109
109
} else {
110
110
if (isset ($ values ['leadspaces ' ])
111
- && preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+? ))?\s* $#u ' , $ values ['value ' ], $ matches )
111
+ && self :: preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+))?$#u ' , rtrim ( $ values ['value ' ]) , $ matches )
112
112
) {
113
113
// this is a compact notation element, add to next block and parse
114
114
$ block = $ values ['value ' ];
@@ -124,7 +124,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
124
124
if ($ isRef ) {
125
125
$ this ->refs [$ isRef ] = end ($ data );
126
126
}
127
- } 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 ('" ' , "' " )))) {
127
+ } elseif (
128
+ self ::preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+))?$#u ' , rtrim ($ this ->currentLine ), $ values )
129
+ && (false === strpos ($ values ['key ' ], ' # ' ) || in_array ($ values ['key ' ][0 ], array ('" ' , "' " )))
130
+ ) {
128
131
if ($ context && 'sequence ' == $ context ) {
129
132
throw new ParseException ('You cannot define a mapping item when in a sequence ' , $ this ->currentLineNb + 1 , $ this ->currentLine );
130
133
}
@@ -203,7 +206,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
203
206
}
204
207
}
205
208
}
206
- } elseif (isset ($ values ['value ' ]) && preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
209
+ } elseif (isset ($ values ['value ' ]) && self :: preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
207
210
$ isRef = $ matches ['ref ' ];
208
211
$ values ['value ' ] = $ matches ['value ' ];
209
212
}
@@ -266,27 +269,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
266
269
return $ value ;
267
270
}
268
271
269
- switch (preg_last_error ()) {
270
- case PREG_INTERNAL_ERROR :
271
- $ error = 'Internal PCRE error. ' ;
272
- break ;
273
- case PREG_BACKTRACK_LIMIT_ERROR :
274
- $ error = 'pcre.backtrack_limit reached. ' ;
275
- break ;
276
- case PREG_RECURSION_LIMIT_ERROR :
277
- $ error = 'pcre.recursion_limit reached. ' ;
278
- break ;
279
- case PREG_BAD_UTF8_ERROR :
280
- $ error = 'Malformed UTF-8 data. ' ;
281
- break ;
282
- case PREG_BAD_UTF8_OFFSET_ERROR :
283
- $ error = 'Offset doesn \'t correspond to the begin of a valid UTF-8 code point. ' ;
284
- break ;
285
- default :
286
- $ error = 'Unable to parse. ' ;
287
- }
288
-
289
- throw new ParseException ($ error , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
272
+ throw new ParseException ('Unable to parse ' , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
290
273
}
291
274
}
292
275
@@ -520,7 +503,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
520
503
return $ this ->refs [$ value ];
521
504
}
522
505
523
- if (preg_match ('/^ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
506
+ if (self :: preg_match ('/^ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
524
507
$ modifiers = isset ($ matches ['modifiers ' ]) ? $ matches ['modifiers ' ] : '' ;
525
508
526
509
return $ this ->parseBlockScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
@@ -566,7 +549,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
566
549
567
550
// determine indentation if not specified
568
551
if (0 === $ indentation ) {
569
- if (preg_match ('/^ +/ ' , $ this ->currentLine , $ matches )) {
552
+ if (self :: preg_match ('/^ +/ ' , $ this ->currentLine , $ matches )) {
570
553
$ indentation = strlen ($ matches [0 ]);
571
554
}
572
555
}
@@ -577,7 +560,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
577
560
while (
578
561
$ notEOF && (
579
562
$ isCurrentLineBlank ||
580
- preg_match ($ pattern , $ this ->currentLine , $ matches )
563
+ self :: preg_match ($ pattern , $ this ->currentLine , $ matches )
581
564
)
582
565
) {
583
566
if ($ isCurrentLineBlank && strlen ($ this ->currentLine ) > $ indentation ) {
@@ -800,6 +783,49 @@ private function isStringUnIndentedCollectionItem()
800
783
*/
801
784
private function isBlockScalarHeader ()
802
785
{
803
- return (bool ) preg_match ('~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' , $ this ->currentLine );
786
+ return (bool ) self ::preg_match ('~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' , $ this ->currentLine );
787
+ }
788
+
789
+ /**
790
+ * A local wrapper for `preg_match` which will throw a ParseException if there
791
+ * is an internal error in the PCRE engine.
792
+ *
793
+ * This avoids us needing to check for "false" every time PCRE is used
794
+ * in the YAML engine
795
+ *
796
+ * @throws ParseException on a PCRE internal error
797
+ *
798
+ * @see preg_last_error()
799
+ *
800
+ * @internal
801
+ */
802
+ public static function preg_match ($ pattern , $ subject , &$ matches = null , $ flags = 0 , $ offset = 0 )
803
+ {
804
+ $ ret = preg_match ($ pattern , $ subject , $ matches , $ flags , $ offset );
805
+ if ($ ret === false ) {
806
+ switch (preg_last_error ()) {
807
+ case PREG_INTERNAL_ERROR :
808
+ $ error = 'Internal PCRE error. ' ;
809
+ break ;
810
+ case PREG_BACKTRACK_LIMIT_ERROR :
811
+ $ error = 'pcre.backtrack_limit reached. ' ;
812
+ break ;
813
+ case PREG_RECURSION_LIMIT_ERROR :
814
+ $ error = 'pcre.recursion_limit reached. ' ;
815
+ break ;
816
+ case PREG_BAD_UTF8_ERROR :
817
+ $ error = 'Malformed UTF-8 data. ' ;
818
+ break ;
819
+ case PREG_BAD_UTF8_OFFSET_ERROR :
820
+ $ error = 'Offset doesn \'t correspond to the begin of a valid UTF-8 code point. ' ;
821
+ break ;
822
+ default :
823
+ $ error = 'Error. ' ;
824
+ }
825
+
826
+ throw new ParseException ($ error );
827
+ }
828
+
829
+ return $ ret ;
804
830
}
805
831
}
0 commit comments