Completed
Branch wip/litedown (377511)
by Josh
03:42
created

AbstractParser::execute()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Litedown\Parser;
9
10
use s9e\TextFormatter\Parser;
11
12
abstract class AbstractParser
13
{
14
	/**
15
	* @var Parser
16
	*/
17
	protected $parser;
18
19
	/**
20
	* @var string Text being parsed
21
	*/
22
	protected $text;
23
24
	/**
25
	* @param Parser $parser
26
	*/
27
	public function __construct(Parser $parser)
28
	{
29
		$this->parser = $parser;
30
	}
31
32
	/**
33
	* Parse given text
34
	*
35
	* @param  string $text
36
	* @return string
37
	*/
38
	public function parse($text)
39
	{
40
		$this->text = $text;
41
		$this->execute();
42
43
		// Remove the local copy of the text to release its memory
44
		$text = $this->text;
45
		unset($this->text);
46
47
		return $text;
48
	}
49
50
	/**
51
	* Execute this parser on stored text
52
	*
53
	* @return void
54
	*/
55
	abstract protected function execute();
56
57
	/**
58
	* Test whether given position is preceded by whitespace
59
	*
60
	* @param  integer $pos
61
	* @return bool
62
	*/
63
	protected function isAfterWhitespace($pos)
64
	{
65
		return ($pos > 0 && $this->isWhitespace($this->text[$pos - 1]));
66
	}
67
68
	/**
69
	* Test whether given character is alphanumeric
70
	*
71
	* @param  string $chr
72
	* @return bool
73
	*/
74
	protected function isAlnum($chr)
75
	{
76
		return (strpos(' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', $chr) > 0);
77
	}
78
79
	/**
80
	* Test whether given position is followed by whitespace
81
	*
82
	* @param  integer $pos
83
	* @return bool
84
	*/
85
	protected function isBeforeWhitespace($pos)
86
	{
87
		return $this->isWhitespace($this->text[$pos + 1]);
88
	}
89
90
	/**
91
	* Test whether a length of text is surrounded by alphanumeric characters
92
	*
93
	* @param  integer $matchPos Start of the text
94
	* @param  integer $matchLen Length of the text
95
	* @return bool
96
	*/
97
	protected function isSurroundedByAlnum($matchPos, $matchLen)
98
	{
99
		return ($matchPos > 0 && $this->isAlnum($this->text[$matchPos - 1]) && $this->isAlnum($this->text[$matchPos + $matchLen]));
100
	}
101
102
	/**
103
	* Test whether given character is an ASCII whitespace character
104
	*
105
	* NOTE: newlines are normalized to LF before parsing so we don't have to check for CR
106
	*
107
	* @param  string $chr
108
	* @return bool
109
	*/
110
	protected function isWhitespace($chr)
111
	{
112
		return (strpos(" \n\t", $chr) !== false);
113
	}
114
115
	/**
116
	* Overwrite part of the text with substitution characters ^Z (0x1A)
117
	*
118
	* @param  integer $pos Start of the range
119
	* @param  integer $len Length of text to overwrite
120
	* @return void
121
	*/
122
	protected function overwrite($pos, $len)
123
	{
124
		if ($len > 0)
125
		{
126
			$this->text = substr($this->text, 0, $pos) . str_repeat("\x1A", $len) . substr($this->text, $pos + $len);
127
		}
128
	}
129
}