C# Search array in an array

mail@pastecode.io avatar
unknown
csharp
2 years ago
4.1 kB
21
Indexable
Never
 Func<byte[], byte[], int> indexOf = (where, what) =>
                {
                    Console.WriteLine("where: {0} what: {1}", string.Join(",", where ?? new byte[] { }), string.Join(",", what ?? new byte[] { }));
                    if (where == null || where.Length == 0 || what == null || what.Length == 0) return -1;
                    int wherePos = 0;
                    int whatPos = 0;
                    while (wherePos < where.Length)
                    {
                        Console.WriteLine("outer wherePos: {0} whatPos: {1}", wherePos, whatPos);

                        while (what[whatPos] == where[wherePos + whatPos])
                        {
                            Console.WriteLine("inner wherePos: {0} whatPos: {1}", wherePos, whatPos);

                            if (whatPos >= what.Length -1) return wherePos;
                            if (wherePos + whatPos >= where.Length) return -1;
                            whatPos++;
                        }
                        whatPos = 0;
                        wherePos++;
                    }

                    return -1;
                };
                /**/
                Console.WriteLine("" + indexOf(null, null));
                Console.WriteLine("" + indexOf(new byte[] { }, new byte[] { }));
                Console.WriteLine("" + indexOf(new byte[] {1,2,3,4,5,6,7,8 }, new byte[] { 1,2,3}));
                Console.WriteLine("" + indexOf(new byte[] {1,2,3,4,5,6,7,8 }, new byte[] { 1 }));
                Console.WriteLine("" + indexOf(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] {  2, 3 }));
                Console.WriteLine("" + indexOf(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 7,8 }));
                Console.WriteLine("" + indexOf(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 4,5 }));
                Console.WriteLine("" + indexOf(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 5 }));
                Console.WriteLine("" + indexOf(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 5,3 }));
                Console.WriteLine("" + indexOf(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 8 }));
                
                
                output
                
                
where:  what: 
-1
where:  what: 
-1
where: 1,2,3,4,5,6,7,8 what: 1,2,3
outer wherePos: 0 whatPos: 0
inner wherePos: 0 whatPos: 0
inner wherePos: 0 whatPos: 1
inner wherePos: 0 whatPos: 2
0
where: 1,2,3,4,5,6,7,8 what: 1
outer wherePos: 0 whatPos: 0
inner wherePos: 0 whatPos: 0
0
where: 1,2,3,4,5,6,7,8 what: 2,3
outer wherePos: 0 whatPos: 0
outer wherePos: 1 whatPos: 0
inner wherePos: 1 whatPos: 0
inner wherePos: 1 whatPos: 1
1
where: 1,2,3,4,5,6,7,8 what: 7,8
outer wherePos: 0 whatPos: 0
outer wherePos: 1 whatPos: 0
outer wherePos: 2 whatPos: 0
outer wherePos: 3 whatPos: 0
outer wherePos: 4 whatPos: 0
outer wherePos: 5 whatPos: 0
outer wherePos: 6 whatPos: 0
inner wherePos: 6 whatPos: 0
inner wherePos: 6 whatPos: 1
6
where: 1,2,3,4,5,6,7,8 what: 4,5
outer wherePos: 0 whatPos: 0
outer wherePos: 1 whatPos: 0
outer wherePos: 2 whatPos: 0
outer wherePos: 3 whatPos: 0
inner wherePos: 3 whatPos: 0
inner wherePos: 3 whatPos: 1
3
where: 1,2,3,4,5,6,7,8 what: 5
outer wherePos: 0 whatPos: 0
outer wherePos: 1 whatPos: 0
outer wherePos: 2 whatPos: 0
outer wherePos: 3 whatPos: 0
outer wherePos: 4 whatPos: 0
inner wherePos: 4 whatPos: 0
4
where: 1,2,3,4,5,6,7,8 what: 5,3
outer wherePos: 0 whatPos: 0
outer wherePos: 1 whatPos: 0
outer wherePos: 2 whatPos: 0
outer wherePos: 3 whatPos: 0
outer wherePos: 4 whatPos: 0
inner wherePos: 4 whatPos: 0
outer wherePos: 5 whatPos: 0
outer wherePos: 6 whatPos: 0
outer wherePos: 7 whatPos: 0
-1
where: 1,2,3,4,5,6,7,8 what: 8
outer wherePos: 0 whatPos: 0
outer wherePos: 1 whatPos: 0
outer wherePos: 2 whatPos: 0
outer wherePos: 3 whatPos: 0
outer wherePos: 4 whatPos: 0
outer wherePos: 5 whatPos: 0
outer wherePos: 6 whatPos: 0
outer wherePos: 7 whatPos: 0
inner wherePos: 7 whatPos: 0
7
>