File: HeaderParserBase.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (98 lines) | stat: -rw-r--r-- 2,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

namespace MediaWiki\Rest\HeaderParser;

/**
 * @internal
 */
class HeaderParserBase {
	/**
	 * @var string The input string being processed
	 */
	protected $input;

	/**
	 * @var int The position within $input
	 */
	protected $pos;

	/**
	 * @var int The length of $input
	 */
	protected $inputLength;

	/**
	 * Set the input, and derived convenience properties
	 *
	 * @param string $input
	 */
	protected function setInput( $input ) {
		$this->input = $input;
		$this->pos = 0;
		$this->inputLength = strlen( $input );
	}

	/**
	 * Consume a specified string, or throw an exception.
	 *
	 * @param string $s
	 * @throws HeaderParserError
	 */
	protected function consumeString( $s ) {
		if ( $this->pos >= $this->inputLength ) {
			$this->error( "Expected \"$s\" but got end of string" );
		}
		if ( substr_compare( $this->input, $s, $this->pos, strlen( $s ) ) === 0 ) {
			$this->pos += strlen( $s );
		} else {
			// (T350852) Give full context for error
			$this->error( "Expected \"$s\" to be a substring of \"$this->input\" from position \"$this->pos\"" );
		}
	}

	/**
	 * Skip whitespace at the input position (OWS)
	 */
	protected function skipWhitespace() {
		$this->pos += strspn( $this->input, " \t", $this->pos );
	}

	/**
	 * Throw an exception to indicate a parse error
	 *
	 * @param string $message
	 * @throws HeaderParserError
	 * @return never
	 */
	protected function error( $message ) {
		throw new HeaderParserError( "$message at {$this->pos}" );
	}

	/**
	 * Consume a specified number of digits, or throw an exception
	 *
	 * @param int $numDigits
	 * @return string
	 * @throws HeaderParserError
	 */
	protected function consumeFixedDigits( $numDigits ) {
		$digits = substr( $this->input, $this->pos, $numDigits );
		if ( strlen( $digits ) !== $numDigits || !preg_match( '/^[0-9]*$/', $digits ) ) {
			$this->error( "expected $numDigits digits" );
		}
		$this->pos += $numDigits;
		return $digits;
	}

	/**
	 * If the position is not at the end of the input string, raise an error,
	 * complaining of trailing characters.
	 *
	 * @throws HeaderParserError
	 */
	protected function assertEnd() {
		if ( $this->pos !== $this->inputLength ) {
			$this->error( "trailing characters" );
		}
	}
}