Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions QRCoder/QRCodeGenerator/DataSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ public partial class QRCodeGenerator
/// <summary>
/// Represents a data segment for QR code encoding, containing the encoding mode, character count, and encoded data.
/// </summary>
private readonly struct DataSegment
private class DataSegment
{
/// <summary>
/// The encoding mode for this segment (Numeric, Alphanumeric, Byte, etc.)
/// </summary>
public readonly EncodingMode EncodingMode;
public EncodingMode EncodingMode { get; }

/// <summary>
/// The character count (or byte count for byte mode)
/// </summary>
public readonly int CharacterCount;
public int CharacterCount { get; }

/// <summary>
/// The encoded data as a BitArray
/// </summary>
public readonly BitArray Data;
public BitArray Data { get; }

/// <summary>
/// The next data segment in the chain, or null if this is the last segment
/// </summary>
public DataSegment? Next { get; set; }

/// <summary>
/// Whether this segment includes an ECI mode indicator
Expand All @@ -30,7 +35,7 @@ private readonly struct DataSegment
/// <summary>
/// The ECI mode value (only valid if HasEciMode is true)
/// </summary>
public readonly EciMode EciMode;
public EciMode EciMode { get; }

/// <summary>
/// Initializes a new instance of the DataSegment struct.
Expand All @@ -49,14 +54,21 @@ public DataSegment(EncodingMode encodingMode, int characterCount, BitArray data,

/// <summary>
/// Calculates the total bit length for this segment when encoded for a specific QR code version.
/// Includes the length of all chained segments.
/// </summary>
/// <param name="version">The QR code version (1-40, or -1 to -4 for Micro QR)</param>
/// <returns>The total number of bits required for this segment including mode indicator, count indicator, and data</returns>
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;
}

/// <summary>
Expand All @@ -73,6 +85,7 @@ public BitArray ToBitArray(int version)

/// <summary>
/// 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.
/// </summary>
/// <param name="bitArray">The target BitArray to write to</param>
/// <param name="startIndex">The starting index in the BitArray where writing should begin</param>
Expand All @@ -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;
}
}
}