-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
109 lines (95 loc) · 2.14 KB
/
Copy pathcode.power
File metadata and controls
109 lines (95 loc) · 2.14 KB
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
99
100
101
102
103
104
105
106
107
108
109
/**
* Regex Search Value
*
* @var string
* @since 3.2.0
*/
protected string $regexValue = '';
/**
* Constructor
*
* @param Config|null $config The search config object.
*
* @since 3.2.0
*/
public function __construct(?Config $config = null)
{
parent::__construct($config);
$this->compileRegex();
}
/**
* Search inside a string
*
* @param string $value The string value
*
* @return string|null The marked string if found, else null
* @since 3.2.0
*/
public function string(string $value): ?string
{
// we count every line
$this->lineCounter();
if (empty($this->searchValue) || !$this->match($value))
{
return null;
}
$result = preg_replace(
$this->regexValue,
$this->start . '$1' . $this->end,
$value
);
return is_string($result) ? trim($result) : null;
}
/**
* Replace found instances inside string value
*
* @param string $value The string value to update
*
* @return string The updated string
* @since 3.2.0
*/
public function replace(string $value): string
{
if (empty($this->searchValue) || !$this->match($value))
{
return $value;
}
$result = preg_replace(
$this->regexValue,
(string) $this->replaceValue,
$value
);
return is_string($result) ? $result : $value;
}
/**
* Check if search string exists in the value
*
* @param string $value The string value
*
* @return bool
* @since 3.0.9
*/
public function match(string $value): bool
{
return !empty($this->searchValue) && preg_match($this->regexValue, $value) === 1;
}
/**
* Compile regex pattern based on whole word and match case settings
*
* @return void
* @since 5.1.1
*/
protected function compileRegex(): void
{
if (empty($this->searchValue))
{
$this->regexValue = '//';
return;
}
$quoted = preg_quote($this->searchValue, '/');
$pattern = $this->wholeWord === 1
? '\b' . $quoted . '\b'
: $quoted;
$flags = $this->matchCase === 1 ? 'm' : 'mi';
$this->regexValue = "/($pattern)/$flags";
}