Skip to content

Commit

Permalink
use lexical to parse integer (#14)
Browse files Browse the repository at this point in the history
* use lexical to parse integer

* bump version to 0.9.2
  • Loading branch information
zao111222333 authored Feb 5, 2025
1 parent 3918536 commit e211975
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
members = ["macros", "dev", "dev/utils", "dev/include/liberty2json"]

[workspace.package]
version = "0.9.1"
version = "0.9.2"
license = "MIT"
edition = "2021"
authors = ["Junzhuo <[email protected]>"]
Expand Down
3 changes: 2 additions & 1 deletion src/ast/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ impl<'a, T: fmt::Write, I: Indentation> CodeFormatter<'a, T, I> {
}
}
#[inline]
#[expect(clippy::unwrap_used, clippy::unwrap_in_result)]
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();
let s = core::str::from_utf8(bytes).unwrap();
self.f.write_str(s)
}
/// Set the indentation level to a specific value
Expand Down
28 changes: 14 additions & 14 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
mod fmt;
pub mod parser;
use crate::{common::f64_into_hash_ord_fn, library::AttributeType};
use crate::{
common::{f64_into_hash_ord_fn, parse_f64},
library::AttributeType,
};
use core::{
cmp::Ordering,
fmt::Write,
hash::{BuildHasher as _, Hash as _, Hasher as _},
num::ParseIntError,
str::FromStr,
};
pub use fmt::{CodeFormatter, DefaultCodeFormatter, DefaultIndentation, Indentation};
Expand Down Expand Up @@ -323,7 +325,7 @@ pub(crate) fn attributs_set_undefined_attri(
}
AttributeType::Integer => {
if let AttriValues::Simple(SimpleDefined::Integer(v)) = value {
v.push(u.parse().map_or(Err(u), Ok));
v.push(lexical_core::parse(u.as_bytes()).map_or(Err(u), Ok));
} else {
log::error!(
"Line={}; Key={key}, the old attribute do NOT meet new one",
Expand All @@ -333,7 +335,7 @@ pub(crate) fn attributs_set_undefined_attri(
}
AttributeType::Float => {
if let AttriValues::Simple(SimpleDefined::Float(v)) = value {
v.push(u.parse().map_or(Err(u), Ok));
v.push(parse_f64(&u).map_or(Err(u), Ok));
} else {
log::error!(
"Line={}; Key={key}, the old attribute do NOT meet new one",
Expand All @@ -354,14 +356,12 @@ pub(crate) fn attributs_set_undefined_attri(
AttributeType::String => {
AttriValues::Simple(SimpleDefined::String(vec![u]))
}
AttributeType::Integer => {
AttriValues::Simple(SimpleDefined::Integer(vec![u
.parse()
.map_or(Err(u), Ok)]))
}
AttributeType::Float => AttriValues::Simple(SimpleDefined::Float(vec![u
.parse()
.map_or(Err(u), Ok)])),
AttributeType::Integer => AttriValues::Simple(SimpleDefined::Integer(
vec![lexical_core::parse(u.as_bytes()).map_or(Err(u), Ok)],
)),
AttributeType::Float => AttriValues::Simple(SimpleDefined::Float(vec![
parse_f64(&u).map_or(Err(u), Ok),
])),
},
);
}
Expand Down Expand Up @@ -528,7 +528,7 @@ pub(crate) enum ComplexParseError {
// Float(#[from] ParseNotNanError<ParseFloatError>),
/// `ParseIntError`
#[error("{0}")]
Int(#[from] ParseIntError),
Int(#[from] lexical_core::Error),
/// title length mismatch
#[error("title length mismatch")]
LengthDismatch,
Expand Down Expand Up @@ -695,7 +695,7 @@ pub enum IdError {
RepeatAttri,
/// Int Error
#[error("{0}")]
Int(ParseIntError),
Int(#[from] lexical_core::Error),
/// something else
#[error("{0}")]
Other(String),
Expand Down
20 changes: 16 additions & 4 deletions src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::ast::GroupWrapper;
use nom::{
branch::alt,
bytes::complete::{escaped, is_not, tag, take, take_until, take_while},
character::complete::{char, digit1, one_of},
character::complete::{char, one_of},
combinator::{map, map_opt, opt},
error::{Error, ErrorKind},
multi::{many0, separated_list0},
Expand Down Expand Up @@ -366,9 +366,21 @@ fn float_vec(i: &str) -> IResult<&str, Vec<f64>> {
}

#[inline]
fn int_usize(i: &str) -> IResult<&str, usize> {
#[expect(clippy::unwrap_used)]
map(digit1, |s: &str| s.parse().unwrap()).parse(i)
pub(crate) fn int_isize(i: &str) -> IResult<&str, isize> {
#[expect(clippy::string_slice)]
match lexical_core::parse_partial(i.as_bytes()) {
Ok((n, pos)) => Ok((&i[pos..], n)),
Err(_) => Err(nom::Err::Error(Error::new(i, ErrorKind::Digit))),
}
}

#[inline]
pub(crate) fn int_usize(i: &str) -> IResult<&str, usize> {
#[expect(clippy::string_slice)]
match lexical_core::parse_partial(i.as_bytes()) {
Ok((n, pos)) => Ok((&i[pos..], n)),
Err(_) => Err(nom::Err::Error(Error::new(i, ErrorKind::Digit))),
}
}

#[inline]
Expand Down
8 changes: 4 additions & 4 deletions src/common/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ crate::ast::impl_self_builder!(usize);
impl SimpleAttri for usize {
#[inline]
fn nom_parse<'a>(i: &'a str, scope: &mut ParseScope) -> ast::SimpleParseRes<'a, Self> {
ast::nom_parse_from_str(i, scope)
ast::parser::simple_custom(i, &mut scope.line_num, ast::parser::int_usize)
}
#[inline]
fn fmt_self<T: Write, I: Indentation>(
Expand All @@ -54,7 +54,7 @@ crate::ast::impl_self_builder!(isize);
impl SimpleAttri for isize {
#[inline]
fn nom_parse<'a>(i: &'a str, scope: &mut ParseScope) -> ast::SimpleParseRes<'a, Self> {
ast::nom_parse_from_str(i, scope)
ast::parser::simple_custom(i, &mut scope.line_num, ast::parser::int_isize)
}
#[inline]
fn fmt_self<T: Write, I: Indentation>(
Expand Down Expand Up @@ -484,7 +484,7 @@ impl ComplexAttri for Vec<usize> {
_scope: &mut ParseScope,
) -> Result<Self, ComplexParseError> {
iter
.map(|&s| s.parse())
.map(|&s| lexical_core::parse(s.as_bytes()))
.collect::<Result<Self, _>>()
.map_err(ComplexParseError::Int)
}
Expand Down Expand Up @@ -546,7 +546,7 @@ impl ComplexAttri for (i64, f64) {
) -> Result<Self, ComplexParseError> {
let mut i = iter;
let v1 = match i.next() {
Some(s) => s.parse::<i64>()?,
Some(s) => lexical_core::parse(s.as_bytes())?,
None => return Err(ComplexParseError::LengthDismatch),
};
let v2 = match i.next() {
Expand Down
2 changes: 1 addition & 1 deletion src/expression/boolean_expression/latch_ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ impl<C: Ctx> NamedGroup for LatchFFBank_type<C> {
}
v.pop()
.map_or(Err(IdError::Other("Unkown pop error".into())), |bits_str| {
match bits_str.parse::<usize>() {
match lexical_core::parse(bits_str.as_bytes()) {
Ok(bits) => {
v.pop()
.map_or(Err(IdError::Other("Unkown pop error".into())), |variable2| {
Expand Down
22 changes: 11 additions & 11 deletions src/library/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ impl ComplexAttri for SensitizationVector {
_scope: &mut ParseScope,
) -> Result<Self, ComplexParseError> {
let id: usize = match iter.next() {
Some(&s) => match s.parse() {
Ok(f) => f,
Err(e) => return Err(ComplexParseError::Int(e)),
},
Some(&s) => lexical_core::parse(s.as_bytes())?,
None => return Err(ComplexParseError::LengthDismatch),
};
let states = match iter.next() {
Expand Down Expand Up @@ -1126,19 +1123,22 @@ impl ComplexAttri for FanoutLength {
_scope: &mut ParseScope,
) -> Result<Self, ComplexParseError> {
let fanout = match iter.next() {
Some(&s) => match s.parse() {
Ok(f) => f,
Err(e) => return Err(ComplexParseError::Int(e)),
},
Some(&s) => lexical_core::parse(s.as_bytes())?,
None => return Err(ComplexParseError::LengthDismatch),
};
let length = match iter.next() {
Some(s) => parse_f64(s)?,
None => return Err(ComplexParseError::LengthDismatch),
};
let average_capacitance = iter.next().and_then(|s| s.parse().ok());
let standard_deviation = iter.next().and_then(|s| s.parse().ok());
let number_of_nets = iter.next().and_then(|s| s.parse().ok());
let average_capacitance =
if let Some(s) = iter.next() { Some(parse_f64(s)?) } else { None };
let standard_deviation =
if let Some(s) = iter.next() { Some(parse_f64(s)?) } else { None };
let number_of_nets = if let Some(s) = iter.next() {
Some(lexical_core::parse(s.as_bytes())?)
} else {
None
};

if iter.next().is_some() {
return Err(ComplexParseError::LengthDismatch);
Expand Down

0 comments on commit e211975

Please sign in to comment.