Skip to content

Commit e875e72

Browse files
authored
Refactor DataSegment from struct to class and add chained segment capability (#673)
1 parent 4cb1f83 commit e875e72

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

QRCoder/QRCodeGenerator/DataSegment.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@ public partial class QRCodeGenerator
55
/// <summary>
66
/// Represents a data segment for QR code encoding, containing the encoding mode, character count, and encoded data.
77
/// </summary>
8-
private readonly struct DataSegment
8+
private class DataSegment
99
{
1010
/// <summary>
1111
/// The encoding mode for this segment (Numeric, Alphanumeric, Byte, etc.)
1212
/// </summary>
13-
public readonly EncodingMode EncodingMode;
13+
public EncodingMode EncodingMode { get; }
1414

1515
/// <summary>
1616
/// The character count (or byte count for byte mode)
1717
/// </summary>
18-
public readonly int CharacterCount;
18+
public int CharacterCount { get; }
1919

2020
/// <summary>
2121
/// The encoded data as a BitArray
2222
/// </summary>
23-
public readonly BitArray Data;
23+
public BitArray Data { get; }
24+
25+
/// <summary>
26+
/// The next data segment in the chain, or null if this is the last segment
27+
/// </summary>
28+
public DataSegment? Next { get; set; }
2429

2530
/// <summary>
2631
/// Whether this segment includes an ECI mode indicator
@@ -30,7 +35,7 @@ private readonly struct DataSegment
3035
/// <summary>
3136
/// The ECI mode value (only valid if HasEciMode is true)
3237
/// </summary>
33-
public readonly EciMode EciMode;
38+
public EciMode EciMode { get; }
3439

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

5055
/// <summary>
5156
/// Calculates the total bit length for this segment when encoded for a specific QR code version.
57+
/// Includes the length of all chained segments.
5258
/// </summary>
5359
/// <param name="version">The QR code version (1-40, or -1 to -4 for Micro QR)</param>
5460
/// <returns>The total number of bits required for this segment including mode indicator, count indicator, and data</returns>
5561
public int GetBitLength(int version)
5662
{
5763
int modeIndicatorLength = HasEciMode ? 16 : 4;
5864
int countIndicatorLength = GetCountIndicatorLength(version, EncodingMode);
59-
return modeIndicatorLength + countIndicatorLength + Data.Length;
65+
int length = modeIndicatorLength + countIndicatorLength + Data.Length;
66+
67+
// Add length of next segment if present
68+
if (Next != null)
69+
length += Next.GetBitLength(version);
70+
71+
return length;
6072
}
6173

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

7486
/// <summary>
7587
/// Writes this data segment to an existing BitArray at the specified index for a specific QR code version.
88+
/// Chains to the next segment if present.
7689
/// </summary>
7790
/// <param name="bitArray">The target BitArray to write to</param>
7891
/// <param name="startIndex">The starting index in the BitArray where writing should begin</param>
@@ -97,8 +110,13 @@ public int WriteTo(BitArray bitArray, int startIndex, int version)
97110

98111
// write data
99112
Data.CopyTo(bitArray, 0, index, Data.Length);
113+
index += Data.Length;
114+
115+
// write next segment if present
116+
if (Next != null)
117+
index = Next.WriteTo(bitArray, index, version);
100118

101-
return index + Data.Length;
119+
return index;
102120
}
103121
}
104122
}

0 commit comments

Comments
 (0)