public void UnescapeOctalCodePoints_returns_same_string_if_nothing_to_escape() { // If nothing was escaped in the original string, the same string instance is returned. const string s = "Hello, World!"; Assert.AreSame(s, VsrModule.UnescapeOctalCodePoints(s)); }
private static Patch CreatePatchFromString([ItemNotNull, NotNull] string[] lines, [NotNull] Lazy <Encoding> filesContentEncoding, ref int lineIndex) { if (lineIndex >= lines.Length) { return(null); } string header = lines[lineIndex]; var headerMatch = _patchHeaderRegex.Match(header); if (!headerMatch.Success) { return(null); } header = VsrModule.ReEncodeFileNameFromLossless(header); var state = PatchProcessorState.InHeader; string fileNameA, fileNameB; var isCombinedDiff = headerMatch.Groups["type"].Value != "git"; if (!isCombinedDiff) { // diff --git a/GitCommands/CommitInformationTest.cs b/GitCommands/CommitInformationTest.cs // diff --git b/Benchmarks/App.config a/Benchmarks/App.config Match match = Regex.Match(header, "^diff --git [\\\"]?[abiwco12]/(.*)[\\\"]? [\\\"]?[abiwco12]/(.*)[\\\"]?$"); if (!match.Success) { throw new FormatException("Invalid patch header: " + header); } fileNameA = match.Groups[1].Value.Trim(); fileNameB = match.Groups[2].Value.Trim(); } else { Match match = Regex.Match(header, "^diff --(cc|combined) [\\\"]?(?<filenamea>.*)[\\\"]?$"); if (!match.Success) { throw new FormatException("Invalid patch header: " + header); } fileNameA = match.Groups["filenamea"].Value.Trim(); fileNameB = null; } string index = null; var changeType = PatchChangeType.ChangeFile; var fileType = PatchFileType.Text; var patchText = new StringBuilder(); patchText.Append(header); if (lineIndex < lines.Length - 1) { patchText.Append("\n"); } var done = false; var i = lineIndex + 1; for (; i < lines.Length; i++) { var line = lines[i]; if (IsStartOfANewPatch(line)) { lineIndex = i - 1; done = true; break; } if (line.StartsWith("@@")) { // Starting a new chunk state = PatchProcessorState.InBody; break; } // header lines are encoded in VsrModule.SystemEncoding line = VsrModule.ReEncodeStringFromLossless(line, VsrModule.SystemEncoding); if (line.StartsWith("index ")) { // Index line index = line; patchText.Append(line); if (i < lines.Length - 1) { patchText.Append("\n"); } continue; } if (line.StartsWith("new file mode ")) { changeType = PatchChangeType.NewFile; } else if (line.StartsWith("deleted file mode ")) { changeType = PatchChangeType.DeleteFile; } else if (line.StartsWith("old mode ")) { changeType = PatchChangeType.ChangeFileMode; } else if (line.StartsWith("Binary files a/") && line.EndsWith(" and /dev/null differ")) { // Unlisted binary file deletion if (changeType != PatchChangeType.DeleteFile) { throw new FormatException("Change not parsed correctly: " + line); } fileType = PatchFileType.Binary; state = PatchProcessorState.OutsidePatch; break; } else if (line.StartsWith("Binary files /dev/null and b/") && line.EndsWith(" differ")) { // Unlisted binary file addition if (changeType != PatchChangeType.NewFile) { throw new FormatException("Change not parsed correctly: " + line); } fileType = PatchFileType.Binary; state = PatchProcessorState.OutsidePatch; break; } else if (line.StartsWith("GIT binary patch")) { fileType = PatchFileType.Binary; state = PatchProcessorState.OutsidePatch; break; } if (line.StartsWith("--- /dev/null")) { // there is no old file, so this should be a new file if (changeType != PatchChangeType.NewFile) { throw new FormatException("Change not parsed correctly: " + line); } } else if (line.StartsWith("--- ")) { // old file name line = VsrModule.UnescapeOctalCodePoints(line); Match regexMatch = Regex.Match(line, "[-]{3} [\\\"]?[abiwco12]/(.*)[\\\"]?"); if (regexMatch.Success) { fileNameA = regexMatch.Groups[1].Value.Trim(); } else { throw new FormatException("Old filename not parsed correctly: " + line); } } else if (line.StartsWith("+++ /dev/null")) { // there is no new file, so this should be a deleted file if (changeType != PatchChangeType.DeleteFile) { throw new FormatException("Change not parsed correctly: " + line); } } else if (line.StartsWith("+++ ")) { // new file name line = VsrModule.UnescapeOctalCodePoints(line); Match regexMatch = Regex.Match(line, "[+]{3} [\\\"]?[abiwco12]/(.*)[\\\"]?"); if (regexMatch.Success) { fileNameB = regexMatch.Groups[1].Value.Trim(); } else { throw new FormatException("New filename not parsed correctly: " + line); } } patchText.Append(line); if (i < lines.Length - 1) { patchText.Append("\n"); } } // process patch body for (; !done && i < lines.Length; i++) { var line = lines[i]; if (IsStartOfANewPatch(line)) { lineIndex = i - 1; break; } if (state == PatchProcessorState.InBody && line.StartsWithAny(new[] { " ", "-", "+", "@" })) { // diff content line = VsrModule.ReEncodeStringFromLossless(line, filesContentEncoding.Value); } else { // warnings, messages ... line = VsrModule.ReEncodeStringFromLossless(line, VsrModule.SystemEncoding); } if (i < lines.Length - 1) { line += "\n"; } patchText.Append(line); } lineIndex = i - 1; return(new Patch(header, index, fileType, fileNameA, fileNameB, isCombinedDiff, changeType, patchText.ToString())); }
public void UnescapeOctalCodePoints_reverses_EscapeOctalCodePoints(string input) { Assert.AreEqual(input, VsrModule.UnescapeOctalCodePoints(VsrModule.EscapeOctalCodePoints(input))); }
[TestCase(@"\353\221\220\353\213\244\777.txt", @"\353\221\220\353\213\244\777.txt")] // valid and invalid in the same string public void UnescapeOctalCodePoints_handles_octal_codes(string input, string expected) { Assert.AreEqual(expected, VsrModule.UnescapeOctalCodePoints(input)); }