diff --git a/QRCoder/QRCodeGenerator/DataSegment.cs b/QRCoder/QRCodeGenerator/DataSegment.cs
index 94d77d51..bf3b1a4f 100644
--- a/QRCoder/QRCodeGenerator/DataSegment.cs
+++ b/QRCoder/QRCodeGenerator/DataSegment.cs
@@ -5,22 +5,27 @@ public partial class QRCodeGenerator
///
/// Represents a data segment for QR code encoding, containing the encoding mode, character count, and encoded data.
///
- private readonly struct DataSegment
+ private class DataSegment
{
///
/// The encoding mode for this segment (Numeric, Alphanumeric, Byte, etc.)
///
- public readonly EncodingMode EncodingMode;
+ public EncodingMode EncodingMode { get; }
///
/// The character count (or byte count for byte mode)
///
- public readonly int CharacterCount;
+ public int CharacterCount { get; }
///
/// The encoded data as a BitArray
///
- public readonly BitArray Data;
+ public BitArray Data { get; }
+
+ ///
+ /// The next data segment in the chain, or null if this is the last segment
+ ///
+ public DataSegment? Next { get; set; }
///
/// Whether this segment includes an ECI mode indicator
@@ -30,7 +35,7 @@ private readonly struct DataSegment
///
/// The ECI mode value (only valid if HasEciMode is true)
///
- public readonly EciMode EciMode;
+ public EciMode EciMode { get; }
///
/// Initializes a new instance of the DataSegment struct.
@@ -49,6 +54,7 @@ public DataSegment(EncodingMode encodingMode, int characterCount, BitArray data,
///
/// Calculates the total bit length for this segment when encoded for a specific QR code version.
+ /// Includes the length of all chained segments.
///
/// The QR code version (1-40, or -1 to -4 for Micro QR)
/// The total number of bits required for this segment including mode indicator, count indicator, and data
@@ -56,7 +62,13 @@ public int GetBitLength(int version)
{
int modeIndicatorLength = HasEciMode ? 16 : 4;
int countIndicatorLength = GetCountIndicatorLength(version, EncodingMode);
- return modeIndicatorLength + countIndicatorLength + Data.Length;
+ int length = modeIndicatorLength + countIndicatorLength + Data.Length;
+
+ // Add length of next segment if present
+ if (Next != null)
+ length += Next.GetBitLength(version);
+
+ return length;
}
///
@@ -73,6 +85,7 @@ public BitArray ToBitArray(int version)
///
/// Writes this data segment to an existing BitArray at the specified index for a specific QR code version.
+ /// Chains to the next segment if present.
///
/// The target BitArray to write to
/// The starting index in the BitArray where writing should begin
@@ -97,8 +110,13 @@ public int WriteTo(BitArray bitArray, int startIndex, int version)
// write data
Data.CopyTo(bitArray, 0, index, Data.Length);
+ index += Data.Length;
+
+ // write next segment if present
+ if (Next != null)
+ index = Next.WriteTo(bitArray, index, version);
- return index + Data.Length;
+ return index;
}
}
}