Exemplo n.º 1
0
        internal virtual string GetScriptText(Encoding[] charsetGuess)
        {
            if (GetHunks().IsEmpty())
            {
                // If we have no hunks then we can safely assume the entire
                // patch is a binary style patch, or a meta-data only style
                // patch. Either way the encoding of the headers should be
                // strictly 7-bit US-ASCII and the body is either 7-bit ASCII
                // (due to the base 85 encoding used for a BinaryHunk) or is
                // arbitrary noise we have chosen to ignore and not understand
                // (e.g. the message "Binary files ... differ").
                //
                return(RawParseUtils.ExtractBinaryString(buf, startOffset, endOffset));
            }
            if (charsetGuess != null && charsetGuess.Length != GetParentCount() + 1)
            {
                throw new ArgumentException(MessageFormat.Format(JGitText.Get().expectedCharacterEncodingGuesses
                                                                 , Sharpen.Extensions.ValueOf(GetParentCount() + 1)));
            }
            if (TrySimpleConversion(charsetGuess))
            {
                Encoding cs = charsetGuess != null ? charsetGuess[0] : null;
                if (cs == null)
                {
                    cs = Constants.CHARSET;
                }
                try
                {
                    return(RawParseUtils.DecodeNoFallback(cs, buf, startOffset, endOffset));
                }
                catch (CharacterCodingException)
                {
                }
            }
            // Try the much slower, more-memory intensive version which
            // can handle a character set conversion patch.
            StringBuilder r = new StringBuilder(endOffset - startOffset);
            // Always treat the headers as US-ASCII; Git file names are encoded
            // in a C style escape if any character has the high-bit set.
            //
            int hdrEnd = GetHunks()[0].GetStartOffset();

            for (int ptr = startOffset; ptr < hdrEnd;)
            {
                int eol = Math.Min(hdrEnd, RawParseUtils.NextLF(buf, ptr));
                r.Append(RawParseUtils.ExtractBinaryString(buf, ptr, eol));
                ptr = eol;
            }
            string[] files   = ExtractFileLines(charsetGuess);
            int[]    offsets = new int[files.Length];
            foreach (HunkHeader h in GetHunks())
            {
                h.ExtractFileLines(r, files, offsets);
            }
            return(r.ToString());
        }