Skip to content

Commit

Permalink
replace number with var, formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
drinckes committed Dec 27, 2024
1 parent eaee196 commit 0743751
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
45 changes: 28 additions & 17 deletions rust/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ pub const PADDING_CHAR_STR: &str = "0";

// The character set used to encode the values.
pub const CODE_ALPHABET: [char; 20] = [
'2', '3', '4', '5', '6', '7', '8', '9', 'C', 'F', 'G', 'H', 'J', 'M', 'P', 'Q', 'R', 'V', 'W',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'C',
'F',
'G',
'H',
'J',
'M',
'P',
'Q',
'R',
'V',
'W',
'X',
];

Expand All @@ -23,6 +41,9 @@ pub const LATITUDE_MAX: f64 = 90f64;
// The maximum value for longitude in degrees.
pub const LONGITUDE_MAX: f64 = 180f64;

// Minimum number of digits to process for plus codes.
pub const MIN_CODE_LENGTH: usize = 2;

// Maximum number of digits to process for plus codes.
pub const MAX_CODE_LENGTH: usize = 15;

Expand Down Expand Up @@ -50,22 +71,12 @@ pub const MIN_TRIMMABLE_CODE_LEN: usize = 6;

// What to multiply latitude degrees by to get an integer value. There are three pairs representing
// decimal digits, and five digits in the grid.
pub const LAT_INTEGER_MULTIPLIER: i64 = (ENCODING_BASE
* ENCODING_BASE
* ENCODING_BASE
* GRID_ROWS
* GRID_ROWS
* GRID_ROWS
* GRID_ROWS
* GRID_ROWS) as i64;
pub const LAT_INTEGER_MULTIPLIER: i64 =
(ENCODING_BASE * ENCODING_BASE * ENCODING_BASE * GRID_ROWS * GRID_ROWS * GRID_ROWS *
GRID_ROWS * GRID_ROWS) as i64;

// What to multiply longitude degrees by to get an integer value. There are three pairs representing
// decimal digits, and five digits in the grid.
pub const LNG_INTEGER_MULTIPLIER: i64 = (ENCODING_BASE
* ENCODING_BASE
* ENCODING_BASE
* GRID_COLUMNS
* GRID_COLUMNS
* GRID_COLUMNS
* GRID_COLUMNS
* GRID_COLUMNS) as i64;
pub const LNG_INTEGER_MULTIPLIER: i64 =
(ENCODING_BASE * ENCODING_BASE * ENCODING_BASE * GRID_COLUMNS * GRID_COLUMNS *
GRID_COLUMNS * GRID_COLUMNS * GRID_COLUMNS) as i64;
46 changes: 22 additions & 24 deletions rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ use std::cmp;

use codearea::CodeArea;

use consts::{
CODE_ALPHABET, ENCODING_BASE, GRID_CODE_LENGTH, GRID_COLUMNS, GRID_ROWS, LATITUDE_MAX,
LAT_INTEGER_MULTIPLIER, LNG_INTEGER_MULTIPLIER, LONGITUDE_MAX, MAX_CODE_LENGTH,
MIN_TRIMMABLE_CODE_LEN, PADDING_CHAR, PADDING_CHAR_STR, PAIR_CODE_LENGTH, PAIR_RESOLUTIONS,
SEPARATOR, SEPARATOR_POSITION,
};
use consts::{CODE_ALPHABET, ENCODING_BASE, GRID_CODE_LENGTH, GRID_COLUMNS, GRID_ROWS,
LATITUDE_MAX, LAT_INTEGER_MULTIPLIER, LNG_INTEGER_MULTIPLIER, LONGITUDE_MAX,
MIN_CODE_LENGTH, MAX_CODE_LENGTH, MIN_TRIMMABLE_CODE_LEN, PADDING_CHAR,
PADDING_CHAR_STR, PAIR_CODE_LENGTH, PAIR_RESOLUTIONS, SEPARATOR, SEPARATOR_POSITION};

use private::{
clip_latitude, code_value, compute_latitude_precision, normalize_longitude, prefix_by_reference,
};
use private::{clip_latitude, code_value, compute_latitude_precision, normalize_longitude,
prefix_by_reference};

/// Determines if a code is a valid Open Location Code.
pub fn is_valid(code: &str) -> bool {
Expand Down Expand Up @@ -70,9 +67,9 @@ pub fn is_valid(code: &str) -> bool {
}

// Validate all characters are permissible
code.chars()
.map(|c| c.to_ascii_uppercase())
.all(|c| c == SEPARATOR || CODE_ALPHABET.contains(&c))
code.chars().map(|c| c.to_ascii_uppercase()).all(|c| {
c == SEPARATOR || CODE_ALPHABET.contains(&c)
})
}

/// Determines if a code is a valid short code.
Expand Down Expand Up @@ -106,7 +103,7 @@ pub fn encode(pt: Point<f64>, code_length: usize) -> String {
let mut lat = clip_latitude(pt.lat());
let lng = normalize_longitude(pt.lng());

let trimmed_code_length = cmp::min(code_length, MAX_CODE_LENGTH);
let trimmed_code_length = cmp::min(cmp::max(code_length, MIN_CODE_LENGTH), MAX_CODE_LENGTH);

// Latitude 90 needs to be adjusted to be just less, so the returned code
// can also be decoded.
Expand All @@ -115,10 +112,10 @@ pub fn encode(pt: Point<f64>, code_length: usize) -> String {
}

// Convert to integers.
let mut lat_val =
(((lat + LATITUDE_MAX) * LAT_INTEGER_MULTIPLIER as f64 * 1e6).round() / 1e6f64) as i64;
let mut lng_val =
(((lng + LONGITUDE_MAX) * LNG_INTEGER_MULTIPLIER as f64 * 1e6).round() / 1e6f64) as i64;
let mut lat_val = (((lat + LATITUDE_MAX) * LAT_INTEGER_MULTIPLIER as f64 * 1e6).round() /
1e6f64) as i64;
let mut lng_val = (((lng + LONGITUDE_MAX) * LNG_INTEGER_MULTIPLIER as f64 * 1e6).round() /
1e6f64) as i64;

// Compute the code digits. This largely ignores the requested length - it
// generates either a 10 digit code, or a 15 digit code, and then truncates
Expand Down Expand Up @@ -178,8 +175,7 @@ pub fn decode(code: &str) -> Result<CodeArea, String> {
if !is_full(code) {
return Err(format!("Code must be a valid full code: {}", code));
}
let mut code = code
.to_string()
let mut code = code.to_string()
.replace(SEPARATOR, "")
.replace(PADDING_CHAR_STR, "")
.to_uppercase();
Expand Down Expand Up @@ -212,10 +208,10 @@ pub fn decode(code: &str) -> Result<CodeArea, String> {
// Convert to floating point values.
let lat_lo: f64 = lat as f64 / LAT_INTEGER_MULTIPLIER as f64;
let lng_lo: f64 = lng as f64 / LNG_INTEGER_MULTIPLIER as f64;
let lat_hi: f64 =
(lat + lat_place_val) as f64 / (ENCODING_BASE.pow(3) * GRID_ROWS.pow(5)) as f64;
let lng_hi: f64 =
(lng + lng_place_val) as f64 / (ENCODING_BASE.pow(3) * GRID_COLUMNS.pow(5)) as f64;
let lat_hi: f64 = (lat + lat_place_val) as f64 /
(ENCODING_BASE.pow(3) * GRID_ROWS.pow(5)) as f64;
let lng_hi: f64 = (lng + lng_place_val) as f64 /
(ENCODING_BASE.pow(3) * GRID_COLUMNS.pow(5)) as f64;
Ok(CodeArea::new(lat_lo, lng_lo, lat_hi, lng_hi, code.len()))
}

Expand Down Expand Up @@ -254,7 +250,9 @@ pub fn shorten(code: &str, ref_pt: Point<f64>) -> Result<String, String> {
// How close are the latitude and longitude to the code center.
let range = (codearea.center.lat() - clip_latitude(ref_pt.lat()))
.abs()
.max((codearea.center.lng() - normalize_longitude(ref_pt.lng())).abs());
.max(
(codearea.center.lng() - normalize_longitude(ref_pt.lng())).abs(),
);

for i in 0..PAIR_RESOLUTIONS.len() - 2 {
// Check if we're close enough to shorten. The range must be less than 1/2
Expand Down

0 comments on commit 0743751

Please sign in to comment.