1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:57:35 +00:00

LibDiff: Port Diff::parse_hunks from DeprecatedString to StringView

This commit is contained in:
Shannon Booth 2023-06-24 13:13:50 +12:00 committed by Andreas Kling
parent c60150da15
commit 9dc92f19b8
2 changed files with 6 additions and 5 deletions

View file

@ -9,9 +9,9 @@
namespace Diff { namespace Diff {
ErrorOr<Vector<Hunk>> parse_hunks(DeprecatedString const& diff) ErrorOr<Vector<Hunk>> parse_hunks(StringView diff)
{ {
Vector<DeprecatedString> diff_lines = diff.split('\n'); Vector<StringView> diff_lines = diff.split_view('\n');
if (diff_lines.is_empty()) if (diff_lines.is_empty())
return Vector<Hunk> {}; return Vector<Hunk> {};
@ -41,12 +41,12 @@ ErrorOr<Vector<Hunk>> parse_hunks(DeprecatedString const& diff)
hunk.target_start_line = current_location.target_start_line; hunk.target_start_line = current_location.target_start_line;
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '-') { while (line_index < diff_lines.size() && diff_lines[line_index][0] == '-') {
TRY(hunk.removed_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1))); TRY(hunk.removed_lines.try_append(diff_lines[line_index].substring_view(1, diff_lines[line_index].length() - 1)));
current_location.apply_offset(1, HunkLocation::LocationType::Original); current_location.apply_offset(1, HunkLocation::LocationType::Original);
++line_index; ++line_index;
} }
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '+') { while (line_index < diff_lines.size() && diff_lines[line_index][0] == '+') {
TRY(hunk.added_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1))); TRY(hunk.added_lines.try_append(diff_lines[line_index].substring_view(1, diff_lines[line_index].length() - 1)));
current_location.apply_offset(1, HunkLocation::LocationType::Target); current_location.apply_offset(1, HunkLocation::LocationType::Target);
++line_index; ++line_index;
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/DeprecatedString.h> #include <AK/DeprecatedString.h>
#include <AK/StringView.h>
#include <AK/Vector.h> #include <AK/Vector.h>
namespace Diff { namespace Diff {
@ -32,6 +33,6 @@ struct Hunk {
Vector<DeprecatedString> added_lines; Vector<DeprecatedString> added_lines;
}; };
ErrorOr<Vector<Hunk>> parse_hunks(DeprecatedString const& diff); ErrorOr<Vector<Hunk>> parse_hunks(StringView diff);
HunkLocation parse_hunk_location(StringView location_line); HunkLocation parse_hunk_location(StringView location_line);
}; };