startMatch method
Implementation
List<XHMatchInfo> startMatch(String target) {
StringScanner scanner = StringScanner(target);
List<XHHighlightMatch> rules = _toMatchMap.values.toList();
List<int> types = _toMatchMap.keys.toList();
List<XHMatchInfo> tmpMatchList = [];
while (!scanner.isDone) {
bool hasMatch = false;
for (int i = 0; i < rules.length; i++) {
if (scanner.scan(rules[i].matchReg()!)) {
Match? match = scanner.lastMatch;
if (match != null) {
tmpMatchList.add(XHMatchInfo(
matchType: types[i],
start: match.start,
end: match.end,
value: match.group(0) ?? '',
));
}
hasMatch = true;
break;
}
}
if (scanner.isDone) break;
if (!hasMatch) scanner.position++;
}
List<XHMatchInfo> allMatchList = [];
int cursor = 0;
if (tmpMatchList.isEmpty) {
return [
XHMatchInfo(
matchType: unMatch.matchType,
start: cursor,
end: target.length,
value: target.substring(cursor))
];
}
for (int i = 0; i < tmpMatchList.length; i++) {
XHMatchInfo match = tmpMatchList[i];
if (cursor != match.start) {
allMatchList.add(XHMatchInfo(
matchType: unMatch.matchType,
start: cursor,
end: match.start,
value: target.substring(cursor, match.start)));
}
allMatchList.add(match);
cursor = match.end;
}
if (cursor != target.length) {
allMatchList.add(XHMatchInfo(
matchType: unMatch.matchType,
start: cursor,
end: target.length,
value: target.substring(cursor)));
}
return allMatchList;
}