remainingCharsInSegment property
int
get
remainingCharsInSegment
The number of characters that can still be added to the current (last) segment before a new segment would be created.
This is calculated by taking the free bits remaining in the last segment and dividing by the bits-per-character of the current encoding (7 for GSM-7, 16 for UCS-2).
How it works:
- The last segment has a certain number of free bits left.
- Divide free bits by bits-per-character → remaining characters.
Practical examples:
// GSM-7: 5 chars typed → 160 - 5 = 155 remaining
final msg1 = SegmentedMessage('Hello');
print(msg1.remainingCharsInSegment); // 155
// UCS-2: 5 Japanese chars → 70 - 5 = 65 remaining
final msg2 = SegmentedMessage('こんにちは');
print(msg2.remainingCharsInSegment); // 65
// Exactly 160 GSM-7 chars → segment full, 0 remaining
final msg3 = SegmentedMessage('A' * 160);
print(msg3.remainingCharsInSegment); // 0
// 161 GSM-7 chars → spills into segment 2, remaining in segment 2
final msg4 = SegmentedMessage('A' * 161);
print(msg4.remainingCharsInSegment); // 145 (153 - 8)
Returns 0 when the last segment is completely full.
Note: Extended GSM-7 characters (e.g. \, €) consume 2 code units
(14 bits), so they reduce the remaining count by 2 instead of 1.
Implementation
int get remainingCharsInSegment {
if (segments.isEmpty) return maxCharsPerSegment;
return segments.last.freeSizeInBits() ~/ _bitsPerCharacter;
}