SegmentedMessage constructor
SegmentedMessage(
- String message, [
- SmsEncodingMode encodingMode = SmsEncodingMode.auto,
- bool smartEncoding = false
Constructor for the SegmentedMessage class.
message : The message content to be segmented and encoded.
encodingMode : The desired encoding format (defaults to auto-detection).
smartEncoding : Whether to use smart encoding for character replacement.
Implementation
SegmentedMessage(String message,
[this.encodingMode = SmsEncodingMode.auto, bool smartEncoding = false]) {
// Apply smart encoding if enabled
if (smartEncoding) {
message = message
.split('')
.map((char) => smartEncodingMap[char] ?? char)
.join('');
}
// Split message into graphemes and process line breaks
graphemes = message.characters
.expand(
(grapheme) => grapheme == '\r\n' ? grapheme.split('') : [grapheme])
.toList(growable: false);
// Count the number of Unicode scalars in the message
numberOfUnicodeScalars = message.runes.length;
// Determine the encoding type for the message
if (encodingMode == SmsEncodingMode.auto) {
encoding =
_hasAnyUCSCharacters(graphemes) ? SmsEncoding.ucs2 : SmsEncoding.gsm7;
} else {
if (encodingMode == SmsEncodingMode.gsm7 &&
_hasAnyUCSCharacters(graphemes)) {
throw Exception(
'The string provided is incompatible with GSM-7 encoding');
}
encoding = switch (encodingMode) {
SmsEncodingMode.gsm7 => SmsEncoding.gsm7,
SmsEncodingMode.ucs2 => SmsEncoding.ucs2,
_ => throw ('Unsupported encoding mode: $encodingMode'),
};
}
// Encode the characters based on the determined encoding
encodedChars = _encodeChars(graphemes, encoding);
// Count the number of characters based on encoding
numberOfCharacters = encoding == SmsEncoding.ucs2
? graphemes.length
: _countCodeUnits(encodedChars);
// Build segments from encoded characters
segments = _buildSegments(encodedChars);
// Detect the line break style in the message
lineBreakStyle = _detectLineBreakStyle(message);
// Check for any warnings in the message content
warnings = _checkForWarnings(lineBreakStyle);
}