| public static byte[] BytesShift(ref byte[] toShift, byte[] splitter) { byte[] bytesShift, bytesTemp; if (toShift.Length > 0) { BytesTrim(ref toShift, splitter); int offset = BytesIndexOf(ref toShift, splitter, 0); if (offset > -1) { bytesShift = new byte[offset]; Buffer.BlockCopy(toShift, 0, bytesShift, 0, bytesShift.Length); if ((toShift.Length - bytesShift.Length) > splitter.Length) { bytesTemp = new byte[toShift.Length - bytesShift.Length - splitter.Length]; Buffer.BlockCopy(toShift, bytesShift.Length + splitter.Length, bytesTemp, 0, bytesTemp.Length); } else bytesTemp = new byte[0]; toShift = bytesTemp; return bytesShift; } else { bytesTemp = toShift; toShift = new byte[0]; return bytesTemp; } } else return new byte[0]; } public static int BytesIndexOf(ref byte[] toSearch, byte[] searchFor, int startOffset) { byte[] temp = new byte[searchFor.Length]; int rv = -1; for (int i = startOffset; i < toSearch.Length - searchFor.Length; i++) { Buffer.BlockCopy(toSearch, i, temp, 0, searchFor.Length); if (BytesEqual(temp, searchFor)) { rv = i; break; } } temp = null; return rv; } public static int BytesLastIndexOf(ref byte[] toSearch, byte[] searchFor) { byte[] temp = new byte[searchFor.Length]; int rv = -1; for (int i = (toSearch.Length - searchFor.Length); i > rv; i—) { Buffer.BlockCopy(toSearch, i, temp, 0, searchFor.Length); if (BytesEqual(temp, searchFor)) { rv = i; break; } } temp = null; return rv; } public static bool BytesEqual(byte[] bytesA, byte[] bytesB) { if (bytesA.Length != bytesB.Length) return false; int pos = 0; foreach (byte a in bytesA) { if (!a.Equals(bytesB[pos])) return false; pos++; } return true; } public static void BytesTrim(ref byte[] toTrim, byte[] trim) { int offset = 0; byte[] bytesTemp; bool moreTrim = false; while (true) { offset = BytesIndexOf(ref toTrim, trim, 0); if (offset == 0) { bytesTemp = new byte[toTrim.Length - trim.Length]; Buffer.BlockCopy(toTrim, trim.Length, bytesTemp, 0, bytesTemp.Length); toTrim = bytesTemp; moreTrim = true; } else moreTrim = false; offset = BytesLastIndexOf(ref toTrim, trim); if (offset == (toTrim.Length - trim.Length)) { bytesTemp = new byte[toTrim.Length - trim.Length]; Buffer.BlockCopy(toTrim, 0, bytesTemp, 0, bytesTemp.Length); toTrim = bytesTemp; moreTrim = moreTrim || true; } else moreTrim = moreTrim || false; if (!moreTrim) break; } } |
Tidak ada komentar:
Posting Komentar
Jika ada kritik dan saran, komentari Artikel ini.