Completed
Push — master ( b24285...12f2ad )
by Steve
18:04 queued 15:44
created

TextLineLCS::getTextLines()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 3
nop 1
dl 0
loc 22
rs 9.5222
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) Steve Nebes <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace SN\RangeDifferencer;
12
13
/**
14
 * TextLineLCS
15
 */
16
class TextLineLCS extends AbstractLCS
17
{
18
    /** @var TextLine[] */
19
    private $lines1 = [];
20
21
    /** @var TextLine[] */
22
    private $lines2 = [];
23
24
    /** @var TextLine[][] */
25
    private $lcs = [];
26
27
    /**
28
     * Default values.
29
     *
30
     * @param TextLine[] $lines1
31
     * @param TextLine[] $lines2
32
     */
33
    public function __construct(array $lines1, array $lines2)
34
    {
35
        $this->lines1 = $lines1;
36
        $this->lines2 = $lines2;
37
    }
38
39
    /**
40
     * @return TextLine[][]
41
     */
42
    public function getResult(): array
43
    {
44
        $length = $this->getLength();
45
        $result = array_fill(0, 2, null);
46
47
        if (0 === $length) {
48
            return $result;
49
        }
50
51
        $result[0] = $this->compactAndShiftLCS($this->lcs[0], $length, $this->lines1);
52
        $result[1] = $this->compactAndShiftLCS($this->lcs[1], $length, $this->lines2);
53
54
        return $result;
55
    }
56
57
    /**
58
     * @return int
59
     */
60
    protected function getLength1(): int
61
    {
62
        return \count($this->lines1);
63
    }
64
65
    /**
66
     * @return int
67
     */
68
    protected function getLength2(): int
69
    {
70
        return \count($this->lines2);
71
    }
72
73
    /**
74
     * @param int $i1
75
     * @param int $i2
76
     * @return bool
77
     */
78
    protected function isRangeEqual(int $i1, int $i2): bool
79
    {
80
        return $this->lines1[$i1]->isSameText($this->lines2[$i2]);
81
    }
82
83
    /**
84
     * @param int $sl1
85
     * @param int $sl2
86
     */
87
    protected function setLcs(int $sl1, int $sl2): void
88
    {
89
        $this->lcs[0][$sl1] = $this->lines1[$sl1];
90
        $this->lcs[1][$sl1] = $this->lines2[$sl2];
91
    }
92
93
    /**
94
     * @param int $lcsLength
95
     */
96
    protected function initializeLcs(int $lcsLength): void
97
    {
98
        $this->lcs = array_fill(0, 2, array_fill(0, $lcsLength, null));
99
    }
100
101
    /**
102
     * @param TextLine[] $lcsSide
103
     * @param int        $len
104
     * @param TextLine[] $original
105
     * @return array
106
     */
107
    private function compactAndShiftLCS(array &$lcsSide, int $len, array &$original): array
108
    {
109
        /** @var TextLine[] $result */
110
        $result = array_fill(0, $len, null);
111
112
        if (0 === $len) {
113
            return $result;
114
        }
115
116
        $j = 0;
117
118
        while (null === $lcsSide[$j]) {
119
            $j++;
120
        }
121
122
        $result[0] = $lcsSide[$j];
123
        $j++;
124
125
        for ($i = 1; $i < $len; $i++) {
126
            while (null === $lcsSide[$j]) {
127
                $j++;
128
            }
129
130
            if ($original[$result[$i - 1]->getLineNumber() + 1]->isSameText($lcsSide[$j])) {
131
                $result[$i] = $original[$result[$i - 1]->getLineNumber() + 1];
132
            } else {
133
                $result[$i] = $lcsSide[$j];
134
            }
135
136
            $j++;
137
        }
138
139
        return $result;
140
    }
141
142
    /**
143
     * @param string $text
144
     * @return TextLine[]
145
     */
146
    public static function getTextLines(string $text): array
147
    {
148
        $lines = [];
149
        $begin = 0;
150
        $end = static::getEOL($text, 0);
151
        $lineNum = 0;
152
153
        while (-1 !== $end) {
154
            $lines[] = new TextLine($lineNum++, \mb_substr($text, $begin, $end - $begin));
155
            $begin = $end + 1;
156
            $end = static::getEOL($text, $begin);
157
158
            if ($end === $begin && "\r" === \mb_substr($text, $begin - 1, 1)  && "\n" === \mb_substr($text, $begin, 1)) {
159
                // We have \r followed by \n, skip it.
160
                $begin = $end + 1;
161
                $end = static::getEOL($text, $begin);
162
            }
163
        }
164
165
        $lines[] = new TextLine($lineNum, \mb_substr($text, $begin));
166
167
        return $lines;
168
    }
169
170
    /**
171
     * @param string $text
172
     * @param int    $start
173
     * @return int
174
     */
175
    private static function getEOL(string $text, int $start): int
176
    {
177
        $max = \mb_strlen($text);
178
179
        for ($i = $start; $i < $max; $i++) {
180
            $c = \mb_substr($text, $i, 1);
181
182
            if ("\n" === $c || "\r" === $c) {
183
                return $i;
184
            }
185
        }
186
187
        return -1;
188
    }
189
}
190