| 61 | * See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html |
| 62 | */ |
| 63 | export class DiffParser { |
| 64 | /** |
| 65 | * Line start pointer. |
| 66 | * |
| 67 | * The offset into the text property where the current line starts (ie either zero |
| 68 | * or one character ahead of the last newline character). |
| 69 | */ |
| 70 | private ls!: number |
| 71 | |
| 72 | /** |
| 73 | * Line end pointer. |
| 74 | * |
| 75 | * The offset into the text property where the current line ends (ie it points to |
| 76 | * the newline character) or -1 if the line boundary hasn't been determined yet |
| 77 | */ |
| 78 | private le!: number |
| 79 | |
| 80 | /** |
| 81 | * The text buffer containing the raw, unified diff output to be parsed |
| 82 | */ |
| 83 | private text!: string |
| 84 | |
| 85 | public constructor() { |
| 86 | this.reset() |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Resets the internal parser state so that it can be reused. |
| 91 | * |
| 92 | * This is done automatically at the end of each parse run. |
| 93 | */ |
| 94 | private reset() { |
| 95 | this.ls = 0 |
| 96 | this.le = -1 |
| 97 | this.text = '' |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Aligns the internal character pointers at the boundaries of |
| 102 | * the next line. |
| 103 | * |
| 104 | * Returns true if successful or false if the end of the diff |
| 105 | * has been reached. |
| 106 | */ |
| 107 | private nextLine(): boolean { |
| 108 | this.ls = this.le + 1 |
| 109 | |
| 110 | // We've reached the end of the diff |
| 111 | if (this.ls >= this.text.length) { |
| 112 | return false |
| 113 | } |
| 114 | |
| 115 | this.le = this.text.indexOf('\n', this.ls) |
| 116 | |
| 117 | // If we can't find the next newline character we'll put our |
| 118 | // end pointer at the end of the diff string |
| 119 | if (this.le === -1) { |
| 120 | this.le = this.text.length |
nothing calls this directly
no outgoing calls
no test coverage detected