MediaWiki master
HeaderParserBase.php
Go to the documentation of this file.
1<?php
2
4
12 protected $input;
13
17 protected $pos;
18
22 protected $inputLength;
23
29 protected function setInput( $input ) {
30 $this->input = $input;
31 $this->pos = 0;
32 $this->inputLength = strlen( $input );
33 }
34
41 protected function consumeString( $s ) {
42 if ( $this->pos >= $this->inputLength ) {
43 $this->error( "Expected \"$s\" but got end of string" );
44 }
45 if ( substr_compare( $this->input, $s, $this->pos, strlen( $s ) ) === 0 ) {
46 $this->pos += strlen( $s );
47 } else {
48 // (T350852) Give full context for error
49 $this->error( "Expected \"$s\" to be a substring of \"$this->input\" from position \"$this->pos\"" );
50 }
51 }
52
56 protected function skipWhitespace() {
57 $this->pos += strspn( $this->input, " \t", $this->pos );
58 }
59
67 protected function error( $message ) {
68 throw new HeaderParserError( "$message at {$this->pos}" );
69 }
70
78 protected function consumeFixedDigits( $numDigits ) {
79 $digits = substr( $this->input, $this->pos, $numDigits );
80 if ( strlen( $digits ) !== $numDigits || !preg_match( '/^[0-9]*$/', $digits ) ) {
81 $this->error( "expected $numDigits digits" );
82 }
83 $this->pos += $numDigits;
84 return $digits;
85 }
86
93 protected function assertEnd() {
94 if ( $this->pos !== $this->inputLength ) {
95 $this->error( "trailing characters" );
96 }
97 }
98}
string $input
The input string being processed.
setInput( $input)
Set the input, and derived convenience properties.
assertEnd()
If the position is not at the end of the input string, raise an error, complaining of trailing charac...
consumeFixedDigits( $numDigits)
Consume a specified number of digits, or throw an exception.
consumeString( $s)
Consume a specified string, or throw an exception.
skipWhitespace()
Skip whitespace at the input position (OWS)
error( $message)
Throw an exception to indicate a parse error.