Skip to content

Commit adee4e2

Browse files
authored
Merge pull request #80 from tcyrus/tcyrus-patch-1
Changes to Rust implementation
2 parents 871c12a + 4caef73 commit adee4e2

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

rust/src/interface.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ pub fn encode(pt: Point<f64>, code_length: usize) -> String {
109109
// Latitude 90 needs to be adjusted to be just less, so the returned code
110110
// can also be decoded.
111111
if lat > LATITUDE_MAX || (LATITUDE_MAX - lat) < 1e-10f64 {
112-
lat = lat - compute_latitude_precision(code_length);
112+
lat -= compute_latitude_precision(code_length);
113113
}
114114

115115
lat += LATITUDE_MAX;
116116
lng += LONGITUDE_MAX;
117117

118-
let mut code = String::new();
118+
let mut code = String::with_capacity(code_length + 1);
119119
let mut digit = 0;
120120
while digit < code_length {
121121
narrow_region(digit, &mut lat, &mut lng);
@@ -130,8 +130,8 @@ pub fn encode(pt: Point<f64>, code_length: usize) -> String {
130130
code.push(CODE_ALPHABET[4 * lat_digit + lng_digit]);
131131
digit += 1;
132132
}
133-
lat = lat - lat_digit as f64;
134-
lng = lng - lng_digit as f64;
133+
lat -= lat_digit as f64;
134+
lng -= lng_digit as f64;
135135
if digit == SEPARATOR_POSITION {
136136
code.push(SEPARATOR);
137137
}

rust/src/private.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,19 @@ use interface::encode;
88
use geo::Point;
99

1010
pub fn code_value(chr: char) -> usize {
11-
for (i, c) in CODE_ALPHABET.iter().enumerate() {
12-
if chr == *c {
13-
return i;
14-
}
15-
}
1611
// We assume this function is only called by other functions that have
1712
// already ensured that the characters in the passed-in code are all valid
1813
// and have all been "treated" (upper-cased, padding and '+' stripped)
19-
//
20-
// If that is the case, we will always return above.
21-
unreachable!();
14+
CODE_ALPHABET.iter().position(|&x| x == char).unwrap()
2215
}
2316

2417
pub fn normalize_longitude(value: f64) -> f64 {
2518
let mut result: f64 = value;
2619
while result >= LONGITUDE_MAX {
27-
result = result - LONGITUDE_MAX * 2f64;
20+
result -= LONGITUDE_MAX * 2f64;
2821
}
2922
while result < -LONGITUDE_MAX{
30-
result = result + LONGITUDE_MAX * 2f64;
23+
result += LONGITUDE_MAX * 2f64;
3124
}
3225
result
3326
}
@@ -40,7 +33,7 @@ pub fn compute_latitude_precision(code_length: usize) -> f64 {
4033
if code_length <= PAIR_CODE_LENGTH {
4134
return ENCODING_BASE.powf((code_length as f64 / -2f64 + 2f64).floor())
4235
}
43-
ENCODING_BASE.powf(-3f64) / GRID_ROWS.powf(code_length as f64 - PAIR_CODE_LENGTH as f64)
36+
ENCODING_BASE.powi(-3i32) / GRID_ROWS.powf(code_length as f64 - PAIR_CODE_LENGTH as f64)
4437
}
4538

4639
pub fn prefix_by_reference(pt: Point<f64>, code_length: usize) -> String {

0 commit comments

Comments
 (0)