public static bool IsBinary(byte[] buf) { BufStatistic bufStatistic = GetBufStatistic(buf); if (bufStatistic.cntNul > 0) { return(true); } if ((bufStatistic.cntPrintable / 128) < bufStatistic.cntNonPrintable) { return(true); } return(false); }
public static byte[] ConvertCrLfToWorktree([CanBeNull] byte[] buf) { if (buf == null) { return(buf); } var bufStatistic = new BufStatistic(buf); if (bufStatistic.cntLf == 0) { return(buf); } if (bufStatistic.cntLf == bufStatistic.cntCrlf) { return(buf); } if (bufStatistic.cntCr != bufStatistic.cntCrlf) { return(buf); } if (IsBinary()) { return(buf); } var bytes = new List <byte>((int)(buf.Length * 1.01)); if (buf.Length >= 1) { if (buf[0] == lf) { bytes.Add(cr); bytes.Add(lf); } else { bytes.Add(buf[0]); } } for (var index = 1; index < buf.Length; index++) { if (buf[index] == lf) { if (buf[index - 1] == cr) { bytes.Add(lf); } else { bytes.Add(cr); bytes.Add(lf); } } else { bytes.Add(buf[index]); } } return(bytes.ToArray()); bool IsBinary() { if (bufStatistic.cntNul > 0) { return(true); } if ((bufStatistic.cntPrintable / 128) < bufStatistic.cntNonPrintable) { return(true); } return(false); } }
private static BufStatistic GetBufStatistic(byte[] buf) { BufStatistic bufStatistic = new BufStatistic(); bufStatistic.ResetBufStatistic(); if (buf == null) { return(bufStatistic); } for (long i = 0; i < buf.Length; i++) { if (buf[i] == 0x0D) { bufStatistic.cntCr++; if (i + 1 < buf.Length) { if (buf[i + 1] == 0x0A) { bufStatistic.cntCrlf++; } } continue; } if (buf[i] == 0x0A) { bufStatistic.cntLf++; continue; } if (buf[i] == 0x7F) { bufStatistic.cntNonPrintable++; } else if (buf[i] < 0x20) { switch (buf[i]) { case 0x08: case 0x09: case 0x1B: case 0x0C: bufStatistic.cntPrintable++; break; case 0: bufStatistic.cntNul++; bufStatistic.cntNonPrintable++; break; default: bufStatistic.cntNonPrintable++; break; } } else { bufStatistic.cntPrintable++; } } if (buf.Length >= 1) { if (buf[buf.Length - 1] == 0x1A) { bufStatistic.cntNonPrintable--; } } return(bufStatistic); }
public static byte[] ConvertCrLfToWorktree(byte[] buf) { if (buf == null) { return(buf); } BufStatistic bufStatistic = GetBufStatistic(buf); if (bufStatistic.cntLf == 0) { return(buf); } if (bufStatistic.cntLf == bufStatistic.cntCrlf) { return(buf); } if (bufStatistic.cntCr != bufStatistic.cntCrlf) { return(buf); } if (IsBinary(buf)) { return(buf); } List <byte> byteList = new List <byte>(); if (buf.LongLength >= 1) { if (buf[0] == 0x0A) { byteList.Add(0x0D); byteList.Add(0x0A); } else { byteList.Add(buf[0]); } } for (long index = 1; index < buf.LongLength; index++) { if (buf[index] == 0x0A) { if (buf[index - 1] == 0x0D) { byteList.Add(0x0A); } else { byteList.Add(0x0D); byteList.Add(0x0A); } } else { byteList.Add(buf[index]); } } return(byteList.ToArray()); }
private static BufStatistic GetBufStatistic(byte[] buf) { BufStatistic bufStatistic = new BufStatistic(); bufStatistic.ResetBufStatistic(); if (buf == null) return bufStatistic; for (long i = 0; i < buf.Length; i++) { if (buf[i] == 0x0D) { bufStatistic.cntCr++; if (i + 1 < buf.Length) { if (buf[i + 1] == 0x0A) { bufStatistic.cntCrlf++; } } continue; } if (buf[i] == 0x0A) { bufStatistic.cntLf++; continue; } if (buf[i] == 0x7F) { bufStatistic.cntNonPrintable++; } else if (buf[i] < 0x20) { switch (buf[i]) { case 0x08: case 0x09: case 0x1B: case 0x0C: bufStatistic.cntPrintable++; break; case 0: bufStatistic.cntNul++; bufStatistic.cntNonPrintable++; break; default: bufStatistic.cntNonPrintable++; break; } } else { bufStatistic.cntPrintable++; } } if (buf.Length >= 1) { if (buf[buf.Length - 1] == 0x1A) { bufStatistic.cntNonPrintable--; } } return bufStatistic; }