[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/subed 4960c74 098/389: Make timestamp parsing more robust
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/subed 4960c74 098/389: Make timestamp parsing more robust |
Date: |
Fri, 3 Dec 2021 11:00:05 -0500 (EST) |
branch: elpa/subed
commit 4960c747e644c3f40059dc1e1fe6c488513abe4e
Author: Random User <rndusr@posteo.de>
Commit: Random User <rndusr@posteo.de>
Make timestamp parsing more robust
Be more lenient about spaces before and after "-->" and properly handle
milliseconds if less than three digits are given.
---
subed/subed-srt.el | 23 ++++++++++++-----------
tests/test-subed.el | 2 +-
2 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/subed/subed-srt.el b/subed/subed-srt.el
index 841a508..5befb97 100644
--- a/subed/subed-srt.el
+++ b/subed/subed-srt.el
@@ -48,7 +48,7 @@ Return nil if TIME-STRING doesn't match the pattern."
(let ((hours (string-to-number (match-string 1 time-string)))
(mins (string-to-number (match-string 2 time-string)))
(secs (string-to-number (match-string 3 time-string)))
- (msecs (string-to-number (match-string 4 time-string))))
+ (msecs (string-to-number (subed--right-pad (match-string 4
time-string) 3 ?0))))
(+ (* (truncate hours) 3600000)
(* (truncate mins) 60000)
(* (truncate secs) 1000)
@@ -97,7 +97,8 @@ after MSECS if there is one and its start time is >= MSECS +
"Subtitle start time in milliseconds or nil if it can't be found."
(let ((timestamp (save-excursion
(when (subed-srt-jump-to-subtitle-time-start sub-id)
- (buffer-substring (point) (+ (point)
subed-srt--length-timestamp))))))
+ (when (looking-at subed-srt--regexp-timestamp)
+ (match-string 0))))))
(when timestamp
(subed-srt--timestamp-to-msecs timestamp))))
@@ -105,7 +106,8 @@ after MSECS if there is one and its start time is >= MSECS +
"Subtitle stop time in milliseconds or nil if it can't be found."
(let ((timestamp (save-excursion
(when (subed-srt-jump-to-subtitle-time-stop sub-id)
- (buffer-substring (point) (+ (point)
subed-srt--length-timestamp))))))
+ (when (looking-at subed-srt--regexp-timestamp)
+ (match-string 0))))))
(when timestamp
(subed-srt--timestamp-to-msecs timestamp))))
@@ -184,7 +186,7 @@ Return point or nil if no stop time could be found."
(save-match-data
(when (subed-srt-jump-to-subtitle-id sub-id)
(forward-line 1)
- (re-search-forward " +--> +" (point-at-eol) t)
+ (re-search-forward " *--> *" (point-at-eol) t)
(when (looking-at subed-srt--regexp-timestamp)
(point)))))
@@ -302,8 +304,8 @@ Return point or nil if there is no previous subtitle."
(let ((msecs-new (+ (subed-srt--subtitle-msecs-start) msecs)))
(save-excursion
(subed-srt-jump-to-subtitle-time-start)
- (delete-region (point) (+ (point) subed-srt--length-timestamp))
- (insert (subed-srt--msecs-to-timestamp msecs-new)))
+ (when (looking-at subed-srt--regexp-timestamp)
+ (replace-match (subed-srt--msecs-to-timestamp msecs-new))))
(when subed-subtitle-time-adjusted-hook
(let ((sub-id (subed-srt--subtitle-id)))
(run-hook-with-args 'subed-subtitle-time-adjusted-hook sub-id
msecs-new)))))
@@ -313,8 +315,8 @@ Return point or nil if there is no previous subtitle."
(let ((msecs-new (+ (subed-srt--subtitle-msecs-stop) msecs)))
(save-excursion
(subed-srt-jump-to-subtitle-time-stop)
- (delete-region (point) (+ (point) subed-srt--length-timestamp))
- (insert (subed-srt--msecs-to-timestamp msecs-new)))
+ (when (looking-at subed-srt--regexp-timestamp)
+ (replace-match (subed-srt--msecs-to-timestamp msecs-new))))
(when subed-subtitle-time-adjusted-hook
(let ((sub-id (subed-srt--subtitle-id)))
(run-hook-with-args 'subed-subtitle-time-adjusted-hook sub-id
msecs-new)))))
@@ -543,9 +545,8 @@ each subtitle."
;; This regex is stricter than `subed-srt--regexp-timestamp'
(unless (looking-at
"^[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\},[0-9]\\{3\\}")
(error "Found invalid start time: %S" (substring (or
(thing-at-point 'line :no-properties) "\n") 0 -1)))
- (condition-case nil
- (forward-char subed-srt--length-timestamp)
- (error nil))
+ (when (re-search-forward "[[:blank:]]" (point-at-eol) t)
+ (goto-char (match-beginning 0)))
(unless (looking-at " --> ")
(error "Found invalid separator between start and stop time: %S"
(substring (or (thing-at-point 'line :no-properties)
"\n") 0 -1)))
diff --git a/tests/test-subed.el b/tests/test-subed.el
index b84eacb..a2a683e 100644
--- a/tests/test-subed.el
+++ b/tests/test-subed.el
@@ -25,7 +25,7 @@
(forward-line 2)
(kill-line)
(insert "HELlo."))
- (expect (point) :to-equal 100)
+ (expect (point) :to-equal 99)
(expect (subed--subtitle-text 3) :to-equal "HELlo.")))
(it "with only the beginning."
(with-temp-buffer
- [nongnu] elpa/subed a7dda70 137/389: Add subtitle shifting to features, (continued)
- [nongnu] elpa/subed a7dda70 137/389: Add subtitle shifting to features, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed a819a93 139/389: Fix test description, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 06851d8 143/389: Prevent overlapping when adjusting subtitle start/stop time, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 651c6de 031/389: Wrap some function bodies in save-match-data, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 57c902a 068/389: subed-srt-sanitize: Don't change buffer unless it's necessary, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 1571de4 070/389: Use prefix arg to set start/stop time adjustment, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed f832a1d 075/389: Add more tests for subed-srt-move-to-subtitle-end, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 8374498 076/389: README: Split feature list and some rewording, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed d0708e5 080/389: Fix screenshot, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 03cd2bf 097/389: Wrap msecs-to-timestamp in save-match-data, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 4960c74 098/389: Make timestamp parsing more robust,
ELPA Syncer <=
- [nongnu] elpa/subed 9b95137 104/389: Test subed--for-each-subtitle more thoroughly, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed ec03689 105/389: Fix subed--for-each-subtitle, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed a7cbce6 107/389: Test subed-move-subtitle-forward/backward more thoroughly, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 5da6d79 112/389: Add "pause with key" to features, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 868d05c 114/389: Adjust some variable descriptions, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 6cf5df4 036/389: Remove annoying minibuffer messages, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 3927690 037/389: Some rewording, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 62d5d3d 038/389: Use (unless ...) instead of (when (not ...)), ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 166988c 041/389: Special subtitle separator when moving to end of subtitle, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed e471802 046/389: Adjust test description, ELPA Syncer, 2021/12/03