/* * 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 * * 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 zero.Rendering.QrCode; /// /// Extension methods for the class. /// public static class BitArrayExtensions { /// /// Appends the specified number bits of the specified value to this bit array. /// /// 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 len-1 /// are appended in the order len-1, len-2 ... 1, 0. /// /// /// Requires 0 ≤ len ≤ 31, and 0 ≤ val < 2len. /// /// /// The BitArray instance that this method extends. /// The value to append. /// The number of low-order bits in the value to append. /// Value or number of bits is out of range. public static void AppendBits(this BitArray bitArray, uint val, int len) { if (len < 0 || len > 31) { throw new ArgumentOutOfRangeException(nameof(len), "'len' out of range"); } if (val >> len != 0) { throw new ArgumentOutOfRangeException(nameof(val), "'val' out of range"); } var bitLength = bitArray.Length; bitArray.Length = bitLength + len; var mask = 1U << (len - 1); for (var i = bitLength; i < bitLength + len; i++) // Append bit by bit { if ((val & mask) != 0) { bitArray.Set(i, true); } mask >>= 1; } } /// /// Appends the content of the specified bit array to the end of this array. /// /// The BitArray instance that this method extends. /// The bit array to append /// If bitArray is null. public static void AppendData(this BitArray bitArray, BitArray otherArray) { Objects.RequireNonNull(otherArray); var bitLength = bitArray.Length; bitArray.Length = bitLength + otherArray.Length; for (var i = 0; i < otherArray.Length; i++, bitLength++) // Append bit by bit { if (otherArray[i]) { bitArray.Set(bitLength, true); } } } }