-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitArrayExtensions.cs
112 lines (101 loc) · 4.43 KB
/
BitArrayExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* QR code generator library (.NET)
*
* Copyright (c) Manuel Bleichenbacher (MIT License)
* https://github.com/manuelbl/QrCodeGenerator
* Copyright (c) Project Nayuki (MIT License)
* https://www.nayuki.io/page/qr-code-generator-library
* Copyright (c) ArdeshirV@protonmail.com (MIT License)
* https://ardeshirv.github.io/ArdeshirV.Utility.QrCode/
* Downgrade to C# 2.0 by ArdeshirV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
using System;
using System.Collections;
namespace ArdeshirV.Tools.QrCode
{
/// <summary>
/// Extension methods for the <see cref="BitArray"/> class.
/// </summary>
public static class BitArrayExtensions
{
/// <summary>
/// Appends the specified number bits of the specified value to this bit array.
/// <para>
/// The least significant bits of the specified value are added. They are appended in reverse order,
/// from the most significant to the least significant one, i.e. bits 0 to <i>len-1</i>
/// are appended in the order <i>len-1</i>, <i>len-2</i> ... 1, 0.
/// </para>
/// <para>
/// Requires 0 ≤ len ≤ 31, and 0 ≤ val < 2<sup>len</sup>.
/// </para>
/// </summary>
/// <param name="bitArray">The BitArray instance that this method extends.</param>
/// <param name="val">The value to append.</param>
/// <param name="len">The number of low-order bits in the value to append.</param>
/// <exception cref="ArgumentOutOfRangeException">Value or number of bits is out of range.</exception>
public static BitArray AppendBits(BitArray bitArray, uint val, int len)
{
if (len < 0 || len > 31 || val >> len != 0)
{
throw new ArgumentOutOfRangeException(nameof(len), "'len' out of range");
}
if (len < 0 || len > 31 || val >> len != 0)
{
throw new ArgumentOutOfRangeException(nameof(val), "'val' out of range");
}
int bitLength = bitArray.Length;
bitArray.Length = bitLength + len;
uint mask = 1U << (len - 1);
for (int i = bitLength; i < bitLength + len; i++) // Append bit by bit
{
if ((val & mask) != 0)
{
bitArray.Set(i, true);
}
mask >>= 1;
}
return bitArray;
}
private static string nameof(object obj) {
return obj.GetType().Name;
}
/// <summary>
/// Appends the content of the specified bit array to the end of this array.
/// </summary>
/// <param name="bitArray">The BitArray instance that this method extends.</param>
/// <param name="otherArray">The bit array to append</param>
/// <exception cref="ArgumentNullException">If <c>bitArray</c> is <c>null</c>.</exception>
public static BitArray AppendData(BitArray bitArray, BitArray otherArray)
{
Objects.RequireNonNull(otherArray);
int bitLength = bitArray.Length;
bitArray.Length = bitLength + otherArray.Length;
for (int i = 0; i < otherArray.Length; i++, bitLength++) // Append bit by bit
{
if (otherArray[i])
{
bitArray.Set(bitLength, true);
}
}
return bitArray;
}
}
}