Skip to content

Commit

Permalink
Improve string parsing performance
Browse files Browse the repository at this point in the history
  • Loading branch information
BezBIS committed Apr 3, 2024
1 parent 6148db7 commit 45c603c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
9 changes: 3 additions & 6 deletions src/osgb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::_500KM;
use crate::grid::{coords_to_square, square_to_coords};
use crate::utils::trim_string;
use crate::{coordinates::point::Point as GridPoint, Error, Precision};
use geo_types::{LineString, Point, Polygon};
use std::fmt::Display;
Expand Down Expand Up @@ -27,7 +28,7 @@ pub struct OSGB {
impl OSGB {
/// Creates a new grid reference from the given coordinates
/// and precision.
///
///
/// # Errors
/// Returns an error if the given coordinates are out of bounds.
///
Expand Down Expand Up @@ -251,11 +252,7 @@ impl FromStr for OSGB {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let string: String = s
.to_uppercase()
.chars()
.filter(|c| !c.is_whitespace())
.collect();
let string: String = trim_string(s);

match string.chars().next() {
Some(c) => {
Expand Down
9 changes: 3 additions & 6 deletions src/osi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::trim_string;
use crate::{coordinates::point::Point as GridPoint, Error, Precision};
use geo_types::{LineString, Point, Polygon};
use std::fmt::Display;
Expand All @@ -18,7 +19,7 @@ pub struct OSI {
impl OSI {
/// Creates a new grid reference from the given coordinates
/// and precision.
///
///
/// # Errors
/// Returns an error if the given coordinates are out of bounds.
///
Expand Down Expand Up @@ -208,11 +209,7 @@ impl FromStr for OSI {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let string: String = s
.to_uppercase()
.chars()
.filter(|c| !c.is_whitespace())
.collect();
let string: String = trim_string(s);
let point: GridPoint = string.parse()?;

Ok(Self { point })
Expand Down
22 changes: 21 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ pub fn digits(s: &str) -> Result<(u32, u32, Precision), Error> {
))
}

/// Removes all non-alphanumeric characters from string
/// and converts to uppercase for parsing.
pub fn trim_string(s: &str) -> String {
s.chars()
.filter(|c| !c.is_ascii_whitespace())
.map(|c| c.to_ascii_uppercase())
.collect()
}

#[cfg(test)]
mod test {
use crate::{constants::*, utils::digits, Error, Precision};
use crate::{
constants::*,
utils::{digits, trim_string},
Error, Precision,
};

#[test]
fn parse_valid_digits() {
Expand Down Expand Up @@ -81,4 +94,11 @@ mod test {
))
)
}

#[test]
fn trim_strings() {
assert_eq!(trim_string("so 14 5"), "SO145");
assert_eq!(trim_string("So 222"), "SO222");
assert_eq!(trim_string(" @ @ "), "@@");
}
}

0 comments on commit 45c603c

Please sign in to comment.