Skip to content

Commit

Permalink
New fmt (#12)
Browse files Browse the repository at this point in the history
* use lexical to write number

* fix cargo fmt

* regolden
  • Loading branch information
zao111222333 authored Feb 5, 2025
1 parent be93832 commit 403608d
Show file tree
Hide file tree
Showing 10 changed files with 736 additions and 744 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ nom = "8.0"
derivative = "2.2"
ryu = "1.0"
itoa = "1.0"
lexical-core = "1.0"
biodivine-lib-bdd = { version = "0.5.23", features = ["serde"] }
# biodivine-lib-bdd = { git = "https://github.com/zao111222333/biodivine-lib-bdd.git", features = ["serde"] }
mut_set = "0.7.0"
Expand All @@ -82,8 +83,8 @@ foldhash = "0.1"
dev_utils = { path = "dev/utils" }
hashmatch = "0.1.1"
pyo3 = "0.23.4"
pyo3-stub-gen = "0.7"
# pyo3-stub-gen = { path = "../pyo3-stub-gen/pyo3-stub-gen" }
# pyo3-stub-gen = "0.7"
pyo3-stub-gen = { path = "../pyo3-stub-gen/pyo3-stub-gen" }

[dependencies]
liberty-macros = { path = "macros" }
Expand All @@ -92,15 +93,13 @@ foldhash.workspace = true
log.workspace = true
serde.workspace = true
thiserror.workspace = true
lexical-core.workspace = true
strum.workspace = true
strum_macros.workspace = true
itertools.workspace = true
ordered-float.workspace = true
fast-float2.workspace = true
nom.workspace = true
# derivative.workspace = true
ryu.workspace = true
itoa.workspace = true
biodivine-lib-bdd.workspace = true
mut_set.workspace = true
duplicate.workspace = true
Expand Down
784 changes: 392 additions & 392 deletions dev/tech/SAED32_EDK/ccs/saed32hvt_dlvl_ff0p85v25c_i0p85v.golden.lib

Large diffs are not rendered by default.

554 changes: 277 additions & 277 deletions dev/tech/SAED32_EDK/ccs/saed32hvt_pg_ff0p95v125c.golden.lib

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions dev/tech/SAED32_EDK/nldm/saed32hvt_ff0p85v25c.golden.lib

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions src/ast/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ impl Indentation for DefaultIndentation {
pub struct CodeFormatter<'a, F, I> {
f: &'a mut F,
level: usize,
buff_f: ryu::Buffer,
buff_i: itoa::Buffer,
buffer: [u8; lexical_core::BUFFER_SIZE],
__i: PhantomData<I>,
}

Expand All @@ -75,19 +74,14 @@ impl<'a, T: fmt::Write, I: Indentation> CodeFormatter<'a, T, I> {
Self {
f,
level: 0,
buff_f: ryu::Buffer::new(),
buff_i: itoa::Buffer::new(),
buffer: [b'0'; lexical_core::BUFFER_SIZE],
__i: PhantomData,
}
}
#[inline]
pub(crate) fn write_float<F: ryu::Float>(&mut self, float: F) -> fmt::Result {
let s = self.buff_f.format(float);
self.f.write_str(s)
}
#[inline]
pub(crate) fn write_int<Int: itoa::Integer>(&mut self, int: Int) -> fmt::Result {
let s = self.buff_i.format(int);
pub(crate) fn write_num<N: lexical_core::ToLexical>(&mut self, n: N) -> fmt::Result {
let bytes = lexical_core::write(n, &mut self.buffer);
let s = std::str::from_utf8(bytes).unwrap();
self.f.write_str(s)
}
/// Set the indentation level to a specific value
Expand Down
30 changes: 15 additions & 15 deletions src/common/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl SimpleAttri for f64 {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_float(*self)
f.write_num(*self)
}
}
crate::ast::impl_self_builder!(bool);
Expand All @@ -47,7 +47,7 @@ impl SimpleAttri for usize {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_int(*self)
f.write_num(*self)
}
}
crate::ast::impl_self_builder!(isize);
Expand All @@ -61,7 +61,7 @@ impl SimpleAttri for isize {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_int(*self)
f.write_num(*self)
}
}

Expand Down Expand Up @@ -338,7 +338,7 @@ impl<const N: usize> ComplexAttri for [f64; N] {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
ast::join_fmt(self.iter(), f, |float, ff| ff.write_float(*float), ", ")
ast::join_fmt(self.iter(), f, |float, ff| ff.write_num(*float), ", ")
}
}
crate::ast::impl_self_builder!(super::items::IdVector);
Expand All @@ -364,9 +364,9 @@ impl ComplexAttri for super::items::IdVector {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_int(self.id)?;
f.write_num(self.id)?;
write!(f, ", \\\n{}", f.indentation())?;
ast::join_fmt(self.vec.iter(), f, |float, ff| ff.write_float(*float), ", ")
ast::join_fmt(self.vec.iter(), f, |float, ff| ff.write_num(*float), ", ")
}
}
crate::ast::impl_self_builder!(Vec<f64>);
Expand Down Expand Up @@ -395,7 +395,7 @@ impl ComplexAttri for Vec<f64> {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
ast::join_fmt(self.iter(), f, |float, ff| ff.write_float(*float), ", ")
ast::join_fmt(self.iter(), f, |float, ff| ff.write_num(*float), ", ")
}
}
impl ComplexAttri for String {
Expand Down Expand Up @@ -447,7 +447,7 @@ impl ComplexAttri for f64 {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_float(*self)
f.write_num(*self)
}
}
crate::ast::impl_self_builder!(Vec<String>);
Expand Down Expand Up @@ -497,7 +497,7 @@ impl ComplexAttri for Vec<usize> {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
join_fmt_no_quote(self.iter(), f, |i, ff| ff.write_int(*i), ", ")
join_fmt_no_quote(self.iter(), f, |i, ff| ff.write_num(*i), ", ")
}
}
crate::ast::impl_self_builder!((f64, f64, String));
Expand Down Expand Up @@ -530,9 +530,9 @@ impl ComplexAttri for (f64, f64, String) {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_float(self.0)?;
f.write_num(self.0)?;
f.write_str(", ")?;
f.write_float(self.1)?;
f.write_num(self.1)?;
f.write_str(", ")?;
f.write_str(&self.2)
}
Expand Down Expand Up @@ -563,9 +563,9 @@ impl ComplexAttri for (i64, f64) {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_int(self.0)?;
f.write_num(self.0)?;
f.write_str(", ")?;
f.write_float(self.1)
f.write_num(self.1)
}
}
crate::ast::impl_self_builder!((f64, f64));
Expand Down Expand Up @@ -594,9 +594,9 @@ impl ComplexAttri for (f64, f64) {
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_float(self.0)?;
f.write_num(self.0)?;
f.write_str(", ")?;
f.write_float(self.1)
f.write_num(self.1)
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/common/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,21 +562,21 @@ impl ComplexAttri for Vec<CcsPowerValue> {
point: &CcsPowerPoint,
f: &mut ast::CodeFormatter<'_, T, I>,
) -> fmt::Result {
f.write_int(point.bc_id)?;
f.write_num(point.bc_id)?;
f.write_str(", ")?;
f.write_float(point.point_time)?;
f.write_num(point.point_time)?;
f.write_str(", ")?;
f.write_float(point.point_current)
f.write_num(point.point_current)
}
#[inline]
fn fmt_value<T: Write, I: ast::Indentation>(
value: &CcsPowerValue,
f: &mut ast::CodeFormatter<'_, T, I>,
) -> fmt::Result {
write!(f, "\"")?;
f.write_float(value.init_time)?;
f.write_num(value.init_time)?;
f.write_str(", ")?;
f.write_float(value.init_current)?;
f.write_num(value.init_current)?;
if !value.points.is_empty() {
f.write_str(", ")?;
ast::join_fmt_no_quote(
Expand Down Expand Up @@ -908,11 +908,11 @@ impl ComplexAttri for Values {
let indent = f.indentation();
let mut iter = self.inner.chunks(self.size1);
if let Some(v) = iter.next() {
ast::join_fmt(v.iter(), f, |float, ff| ff.write_float(*float), ", ")?;
ast::join_fmt(v.iter(), f, |float, ff| ff.write_num(*float), ", ")?;
}
while let Some(v) = iter.next() {
write!(f, ", \\\n{indent}")?;
ast::join_fmt(v.iter(), f, |float, ff| ff.write_float(*float), ", ")?;
ast::join_fmt(v.iter(), f, |float, ff| ff.write_num(*float), ", ")?;
}
Ok(())
}
Expand All @@ -935,11 +935,11 @@ impl<V: Iterator<Item = f64>> DisplayValues<V> {
let chunks = self.inner.chunks(self.size1);
let mut iter = chunks.into_iter();
if let Some(v) = iter.next() {
ast::join_fmt(v.into_iter(), f, |float, ff| ff.write_float(float), ", ")?;
ast::join_fmt(v.into_iter(), f, |float, ff| ff.write_num(float), ", ")?;
}
while let Some(v) = iter.next() {
write!(f, ", \\\n{indent}")?;
ast::join_fmt(v.into_iter(), f, |float, ff| ff.write_float(float), ", ")?;
ast::join_fmt(v.into_iter(), f, |float, ff| ff.write_num(float), ", ")?;
}
Ok(())
}
Expand Down
Loading

0 comments on commit 403608d

Please sign in to comment.