Skip to content

Commit 600d118

Browse files
authored
Update interface.rs
Changes to `encode` * Use arithmetic operators * Reserve String space as done in `/cpp/openlocationcode.cc`
1 parent 77f4a47 commit 600d118

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
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
}

0 commit comments

Comments
 (0)