Skip to content

Commit 44efe6b

Browse files
committed
feat: 替换正则库,删除无用代码,提升性能
1 parent 613d09f commit 44efe6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+143
-3713
lines changed

Cargo.lock

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ once_cell = "1.18.0"
2020
selectors = "0.25.0"
2121
smallvec = "1.11.0"
2222
style = "0.1.0"
23-
regex = "1.10.3"
23+
pcre2 = "0.2.6"
2424
swc_core = "0.90.11"
2525
swc_common = {version = "0.33.0", features = ["tty-emitter", "sourcemap"]}
2626
swc_ecma_ast = {version = "0.110.0"}

src/style_propetries/background_size.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
use lightningcss::{
22
properties::{background::BackgroundSize as LNBackgroundSize, Property},
3-
stylesheet::PrinterOptions,
4-
traits::ToCss,
53
values::length::LengthPercentageOrAuto,
64
};
75
use smallvec::SmallVec;
86
use swc_common::DUMMY_SP;
97
use swc_ecma_ast::{
10-
ArrayLit, Expr, Ident, KeyValueProp, MemberExpr, MemberProp, ObjectLit, Prop, PropName,
8+
Expr, Ident, KeyValueProp, MemberExpr, MemberProp, ObjectLit, Prop, PropName,
119
PropOrSpread,
1210
};
1311

1412
use crate::{generate_expr_by_length_percentage_or_auto, generate_invalid_expr};
1513

16-
use super::{traits::ToExpr, unit::{generate_expr_with_css_input, Platform, PropertyTuple}};
14+
use super::{traits::ToExpr, unit::{Platform, PropertyTuple}};
1715

1816
pub fn parse_background_size_item(size_item: &LNBackgroundSize) -> Option<ImageSize> {
1917
match size_item {

src/style_propetries/unit.rs

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use lightningcss::{values::length::LengthValue, traits::ToCss, stylesheet::PrinterOptions};
2-
use regex::Regex;
2+
use pcre2::bytes::Regex;
33
use swc_common::DUMMY_SP;
44
use swc_ecma_ast::{ExprOrSpread, Expr, Callee, Ident, CallExpr, Lit, Number};
55
use crate::{constants::{CONVERT_STYLE_PX_FN, RN_CONVERT_STYLE_PX_FN, RN_CONVERT_STYLE_VU_FN}, generate_expr_lit_num, generate_expr_lit_str, utils::fix_rgba};
@@ -125,63 +125,69 @@ pub fn generate_expr_by_length_value(length_value: &LengthValue, platform: Platf
125125

126126
pub fn generate_expr_with_css_input(input: String, platform: Platform) -> Expr {
127127
// 定义匹配 '16px' 的正则表达式
128-
let re: Regex = Regex::new(r"(-?(\d+(\.\d*)?|\.\d+))((px)|(vw)|(vh))").unwrap();
128+
let re = Regex::new(r"(-?(?P<num>\d+(\.\d*)?|\.\d+))(?P<unit>(px)|(vw)|(vh))").unwrap();
129+
let bytes = input.as_bytes();
129130
// 使用正则表达式进行匹配
130-
if let Some(captures) = re.captures(&input) {
131-
// 提取匹配到的数字部分
132-
let input_str = captures.get(1).unwrap().as_str();
133-
let unit = match captures.get(4) {
134-
Some(m) => m.as_str(),
135-
None => "vp"
136-
};
131+
if let Ok(caps) = re.captures(bytes) {
132+
if let Some(caps) = caps {
133+
// 提取匹配到的数字部分
134+
let input_str = std::str::from_utf8(&caps["num"]);
135+
let unit = match std::str::from_utf8(&caps["unit"]) {
136+
Ok(s) => s,
137+
Err(_) => "vp",
138+
};
139+
if let Ok(input_str) = input_str {
140+
if let Ok(number) = input_str.parse::<f64>() {
137141

138-
if let Ok(number) = input_str.parse::<f64>() {
139-
140-
let mut args: Vec<Expr> = vec![];
141-
let mut handler: Option<String> = None;
142-
143-
match unit {
144-
"vw" | "vh" | "vmin" | "vmax" => {
145-
handler = match platform {
146-
Platform::ReactNative => Some(RN_CONVERT_STYLE_VU_FN.to_string()),
147-
Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string())
148-
};
149-
args.push(generate_expr_lit_num!(number));
150-
args.push(generate_expr_lit_str!(unit));
151-
},
152-
"px" => {
153-
handler = match platform {
154-
Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()),
155-
Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string())
156-
};
157-
args.push(generate_expr_lit_num!(number));
158-
},
159-
"rem" => {
160-
handler = match platform {
161-
Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()),
162-
Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string())
163-
};
164-
args.push(generate_expr_lit_num!(number * 16.0));
165-
},
166-
_ => {}
142+
let mut args: Vec<Expr> = vec![];
143+
let mut handler: Option<String> = None;
144+
145+
match unit {
146+
"vw" | "vh" | "vmin" | "vmax" => {
147+
handler = match platform {
148+
Platform::ReactNative => Some(RN_CONVERT_STYLE_VU_FN.to_string()),
149+
Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string())
150+
};
151+
args.push(generate_expr_lit_num!(number));
152+
args.push(generate_expr_lit_str!(unit));
153+
},
154+
"px" => {
155+
handler = match platform {
156+
Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()),
157+
Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string())
158+
};
159+
args.push(generate_expr_lit_num!(number));
160+
},
161+
"rem" => {
162+
handler = match platform {
163+
Platform::ReactNative => Some(RN_CONVERT_STYLE_PX_FN.to_string()),
164+
Platform::Harmony => Some(CONVERT_STYLE_PX_FN.to_string())
165+
};
166+
args.push(generate_expr_lit_num!(number * 16.0));
167+
},
168+
_ => {}
169+
}
170+
171+
// 替换原始字符串
172+
if let Some(handler_name) = handler {
173+
return Expr::Call(CallExpr {
174+
span: DUMMY_SP,
175+
callee: Callee::Expr(Box::new(Expr::Ident(Ident::new(
176+
handler_name.into(),
177+
DUMMY_SP
178+
)))),
179+
args: args.into_iter().map(|arg| ExprOrSpread {
180+
spread: None,
181+
expr: Box::new(arg),
182+
}).collect(),
183+
type_args: None,
184+
})
185+
}
186+
}
167187
}
188+
}
168189

169-
// 替换原始字符串
170-
if let Some(handler_name) = handler {
171-
return Expr::Call(CallExpr {
172-
span: DUMMY_SP,
173-
callee: Callee::Expr(Box::new(Expr::Ident(Ident::new(
174-
handler_name.into(),
175-
DUMMY_SP
176-
)))),
177-
args: args.into_iter().map(|arg| ExprOrSpread {
178-
spread: None,
179-
expr: Box::new(arg),
180-
}).collect(),
181-
type_args: None,
182-
})
183-
}
184-
}
190+
185191
}
186192

187193

src/style_transform_backend/background/background.rs

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/style_transform_backend/background/background_color.rs

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)