Skip to content

Commit 847c347

Browse files
authored
♻️ refactor: rustify filter_bad_chars (#309)
1 parent 942b143 commit 847c347

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

packages/biliass/rust/src/reader/xml.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,26 @@ fn parse_comment(
145145
Ok(parsed_p)
146146
}
147147

148+
fn filter_bad_chars(string: &str) -> String {
149+
string
150+
.chars()
151+
.map(|c| {
152+
if ('\u{00}'..='\u{08}').contains(&c)
153+
|| c == '\u{0b}'
154+
|| c == '\u{0c}'
155+
|| ('\u{0e}'..='\u{1f}').contains(&c)
156+
{
157+
'\u{fffd}'
158+
} else {
159+
c
160+
}
161+
})
162+
.collect()
163+
}
164+
148165
pub fn read_comments_from_xml(text: &str, fontsize: f32) -> Result<Vec<Comment>, BiliassError> {
149-
let mut reader = Reader::from_str(text);
166+
let filtered_text = filter_bad_chars(text);
167+
let mut reader = Reader::from_str(&filtered_text);
150168

151169
let mut buf = Vec::new();
152170
let mut comments: Vec<Comment> = Vec::new();

packages/biliass/src/biliass/biliass.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import math
88
import random
99
import re
10-
import xml.dom.minidom
1110
from typing import TYPE_CHECKING, NamedTuple, TypeVar
1211

1312
from biliass._core import CommentPosition, DmSegMobileReply, read_comments_from_xml
@@ -49,7 +48,6 @@ class Comment(NamedTuple):
4948
def read_comments_bilibili_xml(text: str | bytes, fontsize: float) -> Generator[Comment, None, None]:
5049
if isinstance(text, bytes):
5150
text = text.decode()
52-
text = filter_bad_chars(text)
5351
res_rs = read_comments_from_xml(text, fontsize)
5452
return (
5553
Comment(
@@ -588,10 +586,6 @@ def convert_type2(row, height, bottom_reserved):
588586
return height - bottom_reserved - row
589587

590588

591-
def filter_bad_chars(string: str) -> str:
592-
return re.sub("[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]", "\ufffd", string)
593-
594-
595589
class safe_list(list):
596590
def get(self, index, default=None):
597591
try:

0 commit comments

Comments
 (0)