mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-31 05:09:12 +00:00
LibDiff: Make Diff::parse_hunks fallible
Currently the only error that can happen is an OOM. However, in the future there may be other errors that this function may throw, such as detecting an invalid patch.
This commit is contained in:
parent
dbd838efdf
commit
23df5748f6
Notes:
sideshowbarker
2024-07-17 17:49:11 +09:00
Author: https://github.com/shannonbooth
Commit: 23df5748f6
Pull-request: https://github.com/SerenityOS/serenity/pull/19592
Reviewed-by: https://github.com/kennethmyhra ✅
4 changed files with 10 additions and 9 deletions
|
@ -8,11 +8,12 @@
|
|||
#include <AK/Debug.h>
|
||||
|
||||
namespace Diff {
|
||||
Vector<Hunk> parse_hunks(DeprecatedString const& diff)
|
||||
|
||||
ErrorOr<Vector<Hunk>> parse_hunks(DeprecatedString const& diff)
|
||||
{
|
||||
Vector<DeprecatedString> diff_lines = diff.split('\n');
|
||||
if (diff_lines.is_empty())
|
||||
return {};
|
||||
return Vector<Hunk> {};
|
||||
|
||||
Vector<Hunk> hunks;
|
||||
|
||||
|
@ -40,12 +41,12 @@ Vector<Hunk> parse_hunks(DeprecatedString const& diff)
|
|||
hunk.target_start_line = current_location.target_start_line;
|
||||
|
||||
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '-') {
|
||||
hunk.removed_lines.append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1));
|
||||
TRY(hunk.removed_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)));
|
||||
current_location.apply_offset(1, HunkLocation::LocationType::Original);
|
||||
++line_index;
|
||||
}
|
||||
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '+') {
|
||||
hunk.added_lines.append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1));
|
||||
TRY(hunk.added_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)));
|
||||
current_location.apply_offset(1, HunkLocation::LocationType::Target);
|
||||
++line_index;
|
||||
}
|
||||
|
@ -54,7 +55,7 @@ Vector<Hunk> parse_hunks(DeprecatedString const& diff)
|
|||
current_location.apply_offset(1, HunkLocation::LocationType::Both);
|
||||
++line_index;
|
||||
}
|
||||
hunks.append(hunk);
|
||||
TRY(hunks.try_append(hunk));
|
||||
}
|
||||
|
||||
if constexpr (HUNKS_DEBUG) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue