Effort free video editing!
Match words that start or end with punctuation
WyattBluedeeb0901653b
diff --git a/src/edit.nim b/src/edit.nimindex 409814ea..8bd9be65 100644--- a/src/edit.nim+++ b/src/edit.nim@@ -1,7 +1,7 @@import std/[math, options, os, sequtils, strformat, strutils]import ./[av, editexpr, editlexer, editparse, ffmpeg, log]import ./analyze/[audio, blackdetect, motion, subtitle]-import ./lib/[audioutil, dnorm16]+import ./lib/[audioutil, dnorm16, wordre]import ./util/[bar, fun, rational, suggest]import ./vendor/tinyre/tinyre@@ -394,12 +394,9 @@ proc evalEdit(e: EditExpr, container: InputContainer, input: string,m.stream.int16, m.pixelBlack), m.threshold)of ekSubtitle, ekWord:let name = if m.kind == ekWord: "word" else: "regex"- var flags: set[ReFlag]- if m.kind == ekSubtitle: flags.incl reUtf8- if m.ignoreCase: flags.incl reIgnoreCaselet regexPattern =- if m.kind == ekWord: re("\\b" & escapeRe(m.pattern) & "\\b", flags)- else: re(m.pattern, flags)+ if m.kind == ekWord: wordRe(m.pattern, m.ignoreCase)+ else: re(m.pattern, (if m.ignoreCase: {reUtf8, reIgnoreCase} else: {reUtf8}))let stream = m.stream.int16let (ret, val) = subtitle(container, tb, regexPattern, stream)if ret != -1:diff --git a/src/lib/wordre.nim b/src/lib/wordre.nimnew file mode 100644index 00000000..db003f62--- /dev/null+++ b/src/lib/wordre.nim@@ -0,0 +1,22 @@+## The pattern the `word` edit method matches subtitles with. Kept apart from+## the CLI so other programs can match exactly as auto-editor does.++import ../vendor/tinyre/tinyre++func lastCharStart(s: string): int =+ ## The byte where the last UTF-8 character of `s` starts.+ result = s.high+ while result > 0 and (s[result].uint8 and 0xC0) == 0x80:+ dec result++proc wordRe*(word: string, ignoreCase: bool): Re =+ ## Matches `word` (a word or phrase, taken literally) as a whole word: a+ ## side of it that begins or ends with a word character, as `\b` counts+ ## them, can't run into another word character. So `really?` matches+ ## "really?" but not "unreally?", and `(sic)` needs no boundary at all.+ var pattern = escapeRe(word)+ if word.isWordChar(0):+ pattern = "\\b" & pattern+ if word.len > 0 and word.isWordChar(word.lastCharStart):+ pattern.add "\\b"+ re(pattern, (if ignoreCase: {reUtf8, reIgnoreCase} else: {reUtf8}))diff --git a/src/vendor/tinyre/re.c b/src/vendor/tinyre/re.cindex 5e2ae6ea..d913e67c 100644--- a/src/vendor/tinyre/re.c+++ b/src/vendor/tinyre/re.c@@ -64,10 +64,36 @@ static int uc_code(const char * s, int utf8) {return dst;}-static int isword(const char *s)+/* Whether code point u is a space or punctuation from the blocks subtitles+ * use: Latin-1's (but not its ª, µ, º), General and Supplemental+ * Punctuation, CJK punctuation, and the fullwidth ASCII punctuation. */+static int isunipunct(int u)+{+ if (u >= 0xA0 && u <= 0xBF) return u != 0xAA && u != 0xB5 && u != 0xBA;+ if (u == 0xD7 || u == 0xF7) return 1;+ if (u >= 0x2000 && u <= 0x206F) return 1;+ if (u >= 0x2E00 && u <= 0x2E7F) return 1;+ if (u >= 0x3000 && u <= 0x303F) return u < 0x3005 || u > 0x3007;+ return (u >= 0xFF01 && u <= 0xFF0F) || (u >= 0xFF1A && u <= 0xFF20) ||+ (u >= 0xFF3B && u <= 0xFF40) || (u >= 0xFF5B && u <= 0xFF65);+}++/* What \b counts as a word character: ASCII letters, digits and `_`, and any+ * other character but Unicode spaces and punctuation. A byte inside a UTF-8+ * character counts as one, so there's never a boundary within it. */+int re_isword(const char *s){int c = (unsigned char) s[0];- return isalnum(c) || c == '_' || c > 127;+ if (c < 128) return isalnum(c) || c == '_';+ if (c < 0xC0) return 1;+ return !isunipunct(uc_code(s, 1));+}+#define isword re_isword++/* tolower for ASCII only: past 255, C's tolower is undefined. */+static int fold(int c)+{+ return c < 128 ? tolower(c) : c;}static int isasciiword(int c)@@ -156,8 +182,8 @@ static int re_classmatch(const int *pc, int c, int insensitive)} else if (!insensitive) {if (c >= *pc && c <= pc[1]) return is_positive;} else {- c = tolower(c);- if (c >= tolower(*pc) && c <= tolower(pc[1])) return is_positive;+ c = fold(c);+ if (c >= fold(*pc) && c <= fold(pc[1])) return is_positive;}pc += 2;}@@ -743,7 +769,7 @@ int re_pikevm(rcode *prog, const char *s, int len, const char **subp, int nsubp,if (!insensitive) {if (c != *(npc+1)) deccont()} else {- if (tolower(c) != tolower(*(npc+1))) deccont()+ if (fold(c) != fold(*(npc+1))) deccont()}npc += 2;} else if (spc == CLASS) {diff --git a/src/vendor/tinyre/tinyre.nim b/src/vendor/tinyre/tinyre.nimindex e78db2c2..f80b0392 100644--- a/src/vendor/tinyre/tinyre.nim+++ b/src/vendor/tinyre/tinyre.nim@@ -109,6 +109,18 @@ proc re_dup(re: ReRaw): ReRaw {.importc, cdecl.}proc re_match(re: ReRaw, text: cstring, L: cint, cont: cstring): cstringArray {.importc, cdecl.}proc re_max_matches(re: ReRaw): cint {.importc, cdecl.}proc re_uc_len(re: ReRaw, s: cstring): cint {.importc, cdecl.}+proc re_isword(s: cstring): cint {.importc, cdecl, noSideEffect.}++func isWordChar*(s: string, i: int): bool =+ ## Whether the character starting at byte `i` of `s` is one `\b` counts as+ ## part of a word: ASCII letters, digits and `_`, and any other character+ ## but Unicode spaces and punctuation.+ if i notin 0 ..< s.len: return false+ let c = s[i].uint8+ # re_isword decodes the whole character; a cut-off one counts as a word.+ let size = (if c < 0xC0: 1 elif c < 0xE0: 2 elif c < 0xF0: 3 else: 4)+ if i + size > s.len: return true+ re_isword(cast[cstring](unsafeAddr s[i])) != 0proc `=destroy`(re: Re) =if not re.raw.isNil:@@ -170,7 +182,11 @@ iterator matchRaw(s: cstring, L0: int, re: ReRaw, global: ReGlobalKind, sub: booelse:L -= cast[int](matches[1]) -% cast[int](p)p = matches[1]- cont = cast[cstring](cast[int](p) -% 1)+ # The character before `p`, from its first byte, so `\b` sees all of it.+ var before = cast[int](p) -% 1+ while before >% cast[int](s) and (cast[ptr uint8](before)[] and 0xC0) == 0x80:+ before = before -% 1+ cont = cast[cstring](before)lastMatch1 = matches[1]case global