| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | namespace GDX |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// <see cref="byte" /> Based Extension Methods |
| | 9 | | /// </summary> |
| | 10 | | [VisualScriptingCompatible(2)] |
| | 11 | | public static class ByteExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// <para>Get the stable hash code of <paramref name="targetBytes" />, an array of <see cref="System.Byte" / |
| | 15 | | /// </summary> |
| | 16 | | /// <remarks>Does NOT get the object's hashcode.</remarks> |
| | 17 | | /// <param name="targetBytes">The target array of <see cref="byte" />.</param> |
| | 18 | | /// <returns>A <see cref="System.Int32" /> value.</returns> |
| | 19 | | public static int GetStableHashCode(this byte[] targetBytes) |
| 1 | 20 | | { |
| | 21 | | unchecked |
| 1 | 22 | | { |
| | 23 | | const int k_P = 16777619; |
| 1 | 24 | | int hash = (int)2166136261; |
| 1 | 25 | | int length = targetBytes.Length; |
| | 26 | |
|
| 24 | 27 | | for (int i = 0; i < length; i++) |
| 11 | 28 | | { |
| 11 | 29 | | hash = (hash ^ targetBytes[i]) * k_P; |
| 11 | 30 | | } |
| | 31 | |
|
| 1 | 32 | | hash += hash << 13; |
| 1 | 33 | | hash ^= hash >> 7; |
| 1 | 34 | | hash += hash << 3; |
| 1 | 35 | | hash ^= hash >> 17; |
| 1 | 36 | | hash += hash << 5; |
| | 37 | |
|
| 1 | 38 | | return hash; |
| | 39 | | } |
| 1 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Are the two provided <see cref="byte" /> arrays the same. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="sourceBytes">The left hand side <see cref="byte" /> array to compare.</param> |
| | 46 | | /// <param name="targetBytes">The right hand side <see cref="byte" /> array to compare.</param> |
| | 47 | | /// <returns>true if they are identical, will also return true if both are null.</returns> |
| | 48 | | public static bool IsSame(this byte[] sourceBytes, byte[] targetBytes) |
| 5 | 49 | | { |
| 5 | 50 | | if (sourceBytes == null) |
| 1 | 51 | | { |
| 1 | 52 | | return targetBytes == null; |
| | 53 | | } |
| | 54 | |
|
| 4 | 55 | | if (targetBytes == null) |
| 1 | 56 | | { |
| 1 | 57 | | return false; |
| | 58 | | } |
| | 59 | |
|
| 3 | 60 | | if (sourceBytes.Length != targetBytes.Length) |
| 1 | 61 | | { |
| 1 | 62 | | return false; |
| | 63 | | } |
| | 64 | |
|
| 2 | 65 | | int count = sourceBytes.Length; |
| 20 | 66 | | for (int i = 0; i < count; i++) |
| 9 | 67 | | { |
| 9 | 68 | | if (sourceBytes[i] != targetBytes[i]) |
| 1 | 69 | | { |
| 1 | 70 | | return false; |
| | 71 | | } |
| 8 | 72 | | } |
| | 73 | |
|
| 1 | 74 | | return true; |
| 5 | 75 | | } |
| | 76 | | } |
| | 77 | | } |