manipulateString method
Manipulates a given string by identifying specific characters and assigning corresponding style types to them.
Returns a list of StyleTypeValueModel objects, where each object represents a segment of the manipulated string with its corresponding style type.
Example Usage:
var result = manipulateString();
Implementation
List<StyleTypeValueModel> manipulateString() {
// Define literals in a map for easy access
//to their corresponding style types.
const literalsMap = <String, StyleTypeEnum>{
TextstylesetReserved.boldChar: StyleTypeEnum.bold,
TextstylesetReserved.italicChar: StyleTypeEnum.italic,
TextstylesetReserved.underlineChar: StyleTypeEnum.underline,
TextstylesetReserved.monospaceChar: StyleTypeEnum.monospace,
TextstylesetReserved.strikethroughChar: StyleTypeEnum.strikethrough,
TextstylesetReserved.linkChar: StyleTypeEnum.link,
};
var currentStyle = StyleTypeEnum.plain;
final currentContent = StringBuffer();
for (var i = 0; i < input.length; i++) {
if (i < input.length - 1 &&
input[i] == TextstylesetReserved.escapeLiteral &&
literalsMap.containsKey(input[i + 1])) {
// Skip literal character and add the next char as normal text.
currentContent.write(input[++i]);
continue;
}
if (literalsMap.containsKey(input[i]) &&
currentStyle == StyleTypeEnum.plain) {
// Add any existing plain text to the list.
if (currentContent.isNotEmpty) {
list.add(
StyleTypeValueModel(
styleType: currentStyle,
value: currentContent.toString(),
),
);
currentContent.clear();
}
// Update the style for the subsequent text.
currentStyle = literalsMap[input[i]]!;
} else if (literalsMap.containsKey(input[i]) &&
literalsMap[input[i]] == currentStyle) {
// End the current styled text and reset style to plain.
list.add(
StyleTypeValueModel(
styleType: currentStyle,
value: currentContent.toString(),
),
);
currentContent.clear();
currentStyle = StyleTypeEnum.plain;
} else {
// Add the current character as part of the current text segment.
currentContent.write(input[i]);
}
}
// Add any leftover text as plain text.
if (currentContent.isNotEmpty) {
list.add(
StyleTypeValueModel(
styleType: currentStyle,
value: currentContent.toString(),
),
);
}
return list;
}