/[MorrowindCustomClothingSlotCreator]/trunk/libTES3FileFormat/ByteExtension.cs
ViewVC logotype

Contents of /trunk/libTES3FileFormat/ByteExtension.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download)
Fri Jul 29 16:09:53 2011 UTC (8 years, 6 months ago) by william
File size: 2036 byte(s)
*** overhaul of code:
    +++ forget trying to parse the entire TES3 ESM/ESP plugin format
        just parse the clothing section

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace libTES3FileFormat
7 {
8 public static class ByteExtension
9 {
10 public static string ToStr(this byte[] bytes)
11 {
12 StringBuilder builder = new StringBuilder();
13 for (int i = 0; i < bytes.Length - 1; i++) { builder.Append(bytes[i].ToString("X2") + " "); }
14 builder.Append(bytes[bytes.Length - 1].ToString("X2"));
15 return builder.ToString();
16 }
17 public static string ToAscii(this byte[] bytes)
18 {
19 return ASCIIEncoding.ASCII.GetString(bytes);
20 }
21 public static string ToAscii(this byte bytes)
22 {
23 return ASCIIEncoding.ASCII.GetString(new byte[] { bytes });
24 }
25
26 static readonly int[] Empty = new int[0];
27
28 public static int[] Locate(this byte[] self, byte[] candidate)
29 {
30 if (IsEmptyLocate(self, candidate))
31 return Empty;
32
33 var list = new List<int>();
34
35 for (int i = 0; i < self.Length; i++)
36 {
37 if (!IsMatch(self, i, candidate))
38 continue;
39
40 list.Add(i);
41 }
42
43 return list.Count == 0 ? Empty : list.ToArray();
44 }
45
46 static bool IsMatch(byte[] array, int position, byte[] candidate)
47 {
48 if (candidate.Length > (array.Length - position))
49 return false;
50
51 for (int i = 0; i < candidate.Length; i++)
52 if (array[position + i] != candidate[i])
53 return false;
54
55 return true;
56 }
57 static bool IsEmptyLocate(byte[] array, byte[] candidate)
58 {
59 return array == null
60 || candidate == null
61 || array.Length == 0
62 || candidate.Length == 0
63 || candidate.Length > array.Length;
64 }
65 }
66 }

  ViewVC Help
Powered by ViewVC 1.1.22