MCPcopy Create free account
hub / github.com/QNapi/qnapi / decodeTokenStream

Method decodeTokenStream

libqnapi/src/subconvert/subtitleformat.cpp:18–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16#include <QMap>
17
18QVector<SubToken> SubtitleFormat::decodeTokenStream(QString tokenStream) const {
19 QVector<SubToken> tokens;
20 SubToken tok;
21
22 QMap<QString, SubTokenType> tokenTypes = {
23 {"{y:b}", STT_BOLD}, {"{b}", STT_BOLD},
24 {"<b>", STT_BOLD}, {"{y:i}", STT_ITALIC},
25 {"{i}", STT_ITALIC}, {"<i>", STT_ITALIC},
26 {"{y:u}", STT_UNDERLINE}, {"{u}", STT_UNDERLINE},
27 {"<u>", STT_UNDERLINE}, {"{/y:b}", STT_BOLD_END},
28 {"{/b}", STT_BOLD_END}, {"</b>", STT_BOLD_END},
29 {"{/y:i}", STT_ITALIC_END}, {"{/i}", STT_ITALIC_END},
30 {"</i>", STT_ITALIC_END}, {"{/y:u}", STT_UNDERLINE_END},
31 {"{/u}", STT_UNDERLINE_END}, {"</u>", STT_UNDERLINE_END},
32 {"{/c}", STT_FONTCOLOR_END}, {"</font>", STT_FONTCOLOR_END},
33 {"|", STT_NEWLINE}, {"\r\n", STT_NEWLINE},
34 {"\n", STT_NEWLINE}};
35
36 QString wordBuff;
37
38 while (!tokenStream.isEmpty()) {
39 tok.payload.clear();
40
41 bool matched = false;
42
43 foreach (QString token, tokenTypes.keys()) {
44 if (tokenStream.startsWith(token, Qt::CaseInsensitive)) {
45 tok.type = tokenTypes[token];
46 tokenStream.remove(0, token.size());
47 matched = true;
48 break;
49 }
50 }
51
52 if (!matched) {
53 QRegExp colorR1("^\\{c:(.*)\\}");
54 colorR1.setPatternSyntax(QRegExp::RegExp2);
55 colorR1.setCaseSensitivity(Qt::CaseInsensitive);
56
57 QRegExp colorR2("^<font color=(.*)>");
58 colorR1.setPatternSyntax(QRegExp::RegExp2);
59 colorR1.setCaseSensitivity(Qt::CaseInsensitive);
60
61 if (colorR1.indexIn(tokenStream) == 0) {
62 tok.type = STT_FONTCOLOR;
63 tok.payload = parseColor(colorR1.cap(1));
64 tokenStream.remove(0, colorR1.cap(0).size());
65 matched = true;
66 } else if (colorR2.indexIn(tokenStream) == 0) {
67 tok.type = STT_FONTCOLOR;
68 tok.payload = parseColor(colorR2.cap(1));
69 tokenStream.remove(0, colorR2.cap(0).size());
70 matched = true;
71 } else if (tokenStream[0] == '/' && wordBuff.isEmpty()) {
72 tok.type = STT_ITALIC;
73 tokenStream.remove(0, 1);
74 matched = true;
75 } else if (tokenStream[0].isSpace()) {

Callers 1

convertSubtitlesMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected