@@ -86,7 +86,7 @@ public function parse($value, $flags = 0)
86
86
}
87
87
}
88
88
89
- if (! preg_match ('//u ' , $ value )) {
89
+ if (false === preg_match ('//u ' , $ value )) {
90
90
throw new ParseException ('The YAML value does not appear to be valid UTF-8. ' );
91
91
}
92
92
$ this ->currentLineNb = -1 ;
@@ -133,13 +133,13 @@ public function parse($value, $flags = 0)
133
133
}
134
134
135
135
$ isRef = $ mergeNode = false ;
136
- if (preg_match ('#^\-((?P<leadspaces>\s+)(?P<value>.+? ))?\s* $#u ' , $ this ->currentLine , $ values )) {
136
+ if (self :: preg_match ('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u ' , rtrim ( $ this ->currentLine ) , $ values )) {
137
137
if ($ context && 'mapping ' == $ context ) {
138
138
throw new ParseException ('You cannot define a sequence item when in a mapping ' , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
139
139
}
140
140
$ context = 'sequence ' ;
141
141
142
- if (isset ($ values ['value ' ]) && preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
142
+ if (isset ($ values ['value ' ]) && self :: preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
143
143
$ isRef = $ matches ['ref ' ];
144
144
$ values ['value ' ] = $ matches ['value ' ];
145
145
}
@@ -154,7 +154,7 @@ public function parse($value, $flags = 0)
154
154
);
155
155
} else {
156
156
if (isset ($ values ['leadspaces ' ])
157
- && preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u ' , $ this ->trimTag ($ values ['value ' ]), $ matches )
157
+ && self :: preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u ' , $ this ->trimTag ($ values ['value ' ]), $ matches )
158
158
) {
159
159
// this is a compact notation element, add to next block and parse
160
160
$ block = $ values ['value ' ];
@@ -170,7 +170,10 @@ public function parse($value, $flags = 0)
170
170
if ($ isRef ) {
171
171
$ this ->refs [$ isRef ] = end ($ data );
172
172
}
173
- } elseif (preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|(?:![^\s]+\s+)?[^ \'"\[\{!].*?) *\:(\s+(?P<value>.+?))?\s*$#u ' , $ this ->currentLine , $ values ) && (false === strpos ($ values ['key ' ], ' # ' ) || in_array ($ values ['key ' ][0 ], array ('" ' , "' " )))) {
173
+ } elseif (
174
+ self ::preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|(?:![^\s]+\s+)?[^ \'"\[\{!].*?) *\:(\s+(?P<value>.+?))?\s*$#u ' , $ this ->currentLine , $ values )
175
+ && (false === strpos ($ values ['key ' ], ' # ' ) || in_array ($ values ['key ' ][0 ], array ('" ' , "' " )))
176
+ ) {
174
177
if ($ context && 'sequence ' == $ context ) {
175
178
throw new ParseException ('You cannot define a mapping item when in a sequence ' , $ this ->currentLineNb + 1 , $ this ->currentLine );
176
179
}
@@ -243,7 +246,7 @@ public function parse($value, $flags = 0)
243
246
$ data += $ parsed ; // array union
244
247
}
245
248
}
246
- } elseif (isset ($ values ['value ' ]) && preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
249
+ } elseif (isset ($ values ['value ' ]) && self :: preg_match ('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u ' , $ values ['value ' ], $ matches )) {
247
250
$ isRef = $ matches ['ref ' ];
248
251
$ values ['value ' ] = $ matches ['value ' ];
249
252
}
@@ -356,27 +359,7 @@ public function parse($value, $flags = 0)
356
359
}
357
360
}
358
361
359
- switch (preg_last_error ()) {
360
- case PREG_INTERNAL_ERROR :
361
- $ error = 'Internal PCRE error. ' ;
362
- break ;
363
- case PREG_BACKTRACK_LIMIT_ERROR :
364
- $ error = 'pcre.backtrack_limit reached. ' ;
365
- break ;
366
- case PREG_RECURSION_LIMIT_ERROR :
367
- $ error = 'pcre.recursion_limit reached. ' ;
368
- break ;
369
- case PREG_BAD_UTF8_ERROR :
370
- $ error = 'Malformed UTF-8 data. ' ;
371
- break ;
372
- case PREG_BAD_UTF8_OFFSET_ERROR :
373
- $ error = 'Offset doesn \'t correspond to the begin of a valid UTF-8 code point. ' ;
374
- break ;
375
- default :
376
- $ error = 'Unable to parse. ' ;
377
- }
378
-
379
- throw new ParseException ($ error , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
362
+ throw new ParseException ('Unable to parse. ' , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
380
363
}
381
364
} while ($ this ->moveToNextLine ());
382
365
@@ -627,7 +610,7 @@ private function parseValue($value, $flags, $context)
627
610
return $ this ->refs [$ value ];
628
611
}
629
612
630
- if (preg_match ('/^(?: ' .self ::TAG_PATTERN .' +)? ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
613
+ if (self :: preg_match ('/^(?: ' .self ::TAG_PATTERN .' +)? ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
631
614
$ modifiers = isset ($ matches ['modifiers ' ]) ? $ matches ['modifiers ' ] : '' ;
632
615
633
616
$ data = $ this ->parseBlockScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
@@ -647,7 +630,7 @@ private function parseValue($value, $flags, $context)
647
630
$ quotation = '' !== $ value && ('" ' === $ value [0 ] || "' " === $ value [0 ]) ? $ value [0 ] : null ;
648
631
649
632
// do not take following lines into account when the current line is a quoted single line value
650
- if (null !== $ quotation && preg_match ('/^ ' .$ quotation .'.* ' .$ quotation .'(\s*#.*)?$/ ' , $ value )) {
633
+ if (null !== $ quotation && self :: preg_match ('/^ ' .$ quotation .'.* ' .$ quotation .'(\s*#.*)?$/ ' , $ value )) {
651
634
return Inline::parse ($ value , $ flags , $ this ->refs );
652
635
}
653
636
@@ -713,7 +696,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
713
696
714
697
// determine indentation if not specified
715
698
if (0 === $ indentation ) {
716
- if (preg_match ('/^ +/ ' , $ this ->currentLine , $ matches )) {
699
+ if (self :: preg_match ('/^ +/ ' , $ this ->currentLine , $ matches )) {
717
700
$ indentation = strlen ($ matches [0 ]);
718
701
}
719
702
}
@@ -724,7 +707,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
724
707
while (
725
708
$ notEOF && (
726
709
$ isCurrentLineBlank ||
727
- preg_match ($ pattern , $ this ->currentLine , $ matches )
710
+ self :: preg_match ($ pattern , $ this ->currentLine , $ matches )
728
711
)
729
712
) {
730
713
if ($ isCurrentLineBlank && strlen ($ this ->currentLine ) > $ indentation ) {
@@ -812,10 +795,7 @@ private function isNextLineIndented()
812
795
return false ;
813
796
}
814
797
815
- $ ret = false ;
816
- if ($ this ->getCurrentLineIndentation () > $ currentIndentation ) {
817
- $ ret = true ;
818
- }
798
+ $ ret = $ this ->getCurrentLineIndentation () > $ currentIndentation ;
819
799
820
800
$ this ->moveToPreviousLine ();
821
801
@@ -916,14 +896,7 @@ private function isNextLineUnIndentedCollection()
916
896
return false ;
917
897
}
918
898
919
- $ ret = false ;
920
- if (
921
- $ this ->getCurrentLineIndentation () == $ currentIndentation
922
- &&
923
- $ this ->isStringUnIndentedCollectionItem ()
924
- ) {
925
- $ ret = true ;
926
- }
899
+ $ ret = $ this ->getCurrentLineIndentation () === $ currentIndentation && $ this ->isStringUnIndentedCollectionItem ();
927
900
928
901
$ this ->moveToPreviousLine ();
929
902
@@ -947,7 +920,49 @@ private function isStringUnIndentedCollectionItem()
947
920
*/
948
921
private function isBlockScalarHeader ()
949
922
{
950
- return (bool ) preg_match ('~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' , $ this ->currentLine );
923
+ return (bool ) self ::preg_match ('~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' , $ this ->currentLine );
924
+ }
925
+
926
+ /**
927
+ * A local wrapper for `preg_match` which will throw a ParseException if there
928
+ * is an internal error in the PCRE engine.
929
+ *
930
+ * This avoids us needing to check for "false" every time PCRE is used
931
+ * in the YAML engine
932
+ *
933
+ * @throws ParseException on a PCRE internal error
934
+ *
935
+ * @see preg_last_error()
936
+ *
937
+ * @internal
938
+ */
939
+ public static function preg_match ($ pattern , $ subject , &$ matches = null , $ flags = 0 , $ offset = 0 )
940
+ {
941
+ if (false === $ ret = preg_match ($ pattern , $ subject , $ matches , $ flags , $ offset )) {
942
+ switch (preg_last_error ()) {
943
+ case PREG_INTERNAL_ERROR :
944
+ $ error = 'Internal PCRE error. ' ;
945
+ break ;
946
+ case PREG_BACKTRACK_LIMIT_ERROR :
947
+ $ error = 'pcre.backtrack_limit reached. ' ;
948
+ break ;
949
+ case PREG_RECURSION_LIMIT_ERROR :
950
+ $ error = 'pcre.recursion_limit reached. ' ;
951
+ break ;
952
+ case PREG_BAD_UTF8_ERROR :
953
+ $ error = 'Malformed UTF-8 data. ' ;
954
+ break ;
955
+ case PREG_BAD_UTF8_OFFSET_ERROR :
956
+ $ error = 'Offset doesn \'t correspond to the begin of a valid UTF-8 code point. ' ;
957
+ break ;
958
+ default :
959
+ $ error = 'Error. ' ;
960
+ }
961
+
962
+ throw new ParseException ($ error );
963
+ }
964
+
965
+ return $ ret ;
951
966
}
952
967
953
968
/**
@@ -967,7 +982,7 @@ private function trimTag($value)
967
982
968
983
private function getLineTag ($ value , $ flags , $ nextLineCheck = true )
969
984
{
970
- if ('' === $ value || '! ' !== $ value [0 ] || 1 !== preg_match ('/^ ' .self ::TAG_PATTERN .' *( +#.*)?$/ ' , $ value , $ matches )) {
985
+ if ('' === $ value || '! ' !== $ value [0 ] || 1 !== self :: preg_match ('/^ ' .self ::TAG_PATTERN .' *( +#.*)?$/ ' , $ value , $ matches )) {
971
986
return ;
972
987
}
973
988
0 commit comments