1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-24 15:52:25 +00:00

sub/sd_ass: rewrite is_animated()

Using strchr should be faster as it is optimized for the job. Also
ensure that tags are not escaped and are within {} correctly. This also
is important for performance as it allows to scan only needed areas of
the string.

Co-authored-by: Avi Halachmi (:avih) <avihpit@yahoo.com>
Co-authored-by: rcombs <rcombs@rcombs.me>
This commit is contained in:
Kacper Michajłow 2024-06-05 01:21:04 +02:00
parent 0fd4c84a5e
commit b0aa088bc4

View File

@ -289,45 +289,34 @@ static int init(struct sd *sd)
}
// Check if subtitle has events that would cause it to be animated inside {}
static bool is_animated(char *s)
static bool is_animated(const char *str)
{
bool in_tag = false;
bool valid_event = false;
bool valid_tag = false;
while (*s) {
if (!in_tag && s[0] == '{')
in_tag = true;
if (s[0] == '\\') {
s++;
if (!s[0])
break;
if (s[0] == 'k' || s[0] == 'K' || s[0] == 't') {
valid_event = true;
continue;
// just bruteforce the multi-letter ones
} else if (s[0] == 'f') {
if (!strncmp(s, "fad", 3)) {
valid_event = true;
continue;
}
} else if (s[0] == 'm') {
if (!strncmp(s, "move", 4)) {
valid_event = true;
continue;
}
const char *begin = str;
while ((str = strchr(str, '{'))) {
if (str++ > begin && str[-2] == '\\')
continue;
const char *end = strchr(str, '}');
if (!end)
return false;
while ((str = memchr(str, '\\', end - str))) {
while (str[0] == '\\')
++str;
while (str[0] == ' ' || str[0] == '\t')
++str;
if (str[0] == 'k' || str[0] == 'K' || str[0] == 't' ||
(str[0] == 'f' && str[1] == 'a' && str[2] == 'd') ||
(str[0] == 'm' && str[1] == 'o' && str[2] == 'v' && str[3] == 'e'))
{
return true;
}
}
if (in_tag && valid_event && s[0] == '}') {
valid_tag = true;
break;
} else if (s[0] == '}') {
in_tag = false;
valid_event = false;
valid_tag = false;
}
s++;
str = end + 1;
}
return valid_tag;
return false;
}
// Note: pkt is not necessarily a fully valid refcounted packet.