Skip to content

Commit d55ae90

Browse files
committed
Add more missing const qualifiers
Eliminate unnecessary autos where direct instantiation can be used.
1 parent 1727d97 commit d55ae90

2 files changed

Lines changed: 24 additions & 25 deletions

File tree

cpp/openlocationcode.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ double compute_precision_for_length(const int code_length) {
148148
}
149149

150150
// Returns the position of a char in the encoding alphabet, or -1 if invalid.
151-
int get_alphabet_position(char c) {
151+
int get_alphabet_position(const char c) {
152152
// We use a lookup table for performance reasons (e.g. over std::find).
153153
if (c >= 'C' && c <= 'X') return internal::kPositionLUT[c - 'C'];
154154
if (c >= 'c' && c <= 'x') return internal::kPositionLUT[c - 'c'];
@@ -206,8 +206,8 @@ std::string Encode(const LatLng &location, size_t code_length) {
206206
code_length = code_length + 1;
207207
}
208208
// Convert latitude and longitude into integer values.
209-
int64_t lat_val = internal::latitudeToInteger(location.latitude);
210-
int64_t lng_val = internal::longitudeToInteger(location.longitude);
209+
const int64_t lat_val = internal::latitudeToInteger(location.latitude);
210+
const int64_t lng_val = internal::longitudeToInteger(location.longitude);
211211
return internal::encodeIntegers(lat_val, lng_val, code_length);
212212
}
213213

@@ -369,15 +369,15 @@ std::string RecoverNearest(const std::string &short_code,
369369
} else if (longitude - half_res > center_lng) {
370370
center_lng += resolution;
371371
}
372-
LatLng center_latlng = {center_lat, center_lng};
372+
const LatLng center_latlng = {center_lat, center_lng};
373373
return Encode(center_latlng, CodeLength(short_code) + padding_length);
374374
}
375375

376376
bool IsValid(const std::string &code) {
377377
if (code.empty()) {
378378
return false;
379379
}
380-
size_t separatorPos = code.find(internal::kSeparator);
380+
const size_t separatorPos = code.find(internal::kSeparator);
381381
// The separator is required.
382382
if (separatorPos == std::string::npos) {
383383
return false;
@@ -397,7 +397,7 @@ bool IsValid(const std::string &code) {
397397
}
398398
// We can have an even number of padding characters before the separator,
399399
// but then it must be the final character.
400-
std::size_t paddingStart = code.find_first_of(internal::kPaddingCharacter);
400+
const std::size_t paddingStart = code.find_first_of(internal::kPaddingCharacter);
401401
if (paddingStart != std::string::npos) {
402402
// Short codes cannot have padding
403403
if (separatorPos < internal::kSeparatorPosition) {
@@ -475,7 +475,7 @@ bool IsFull(const std::string &code) {
475475
}
476476

477477
size_t CodeLength(const std::string &code) {
478-
std::string clean_code = clean_code_chars(code);
478+
const std::string clean_code = clean_code_chars(code);
479479
return clean_code.size();
480480
}
481481

cpp/openlocationcode_test.cc

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const std::string kDecodingTestsFile = "test_data/decoding.csv";
9191

9292
std::vector<DecodingTestData> GetDecodingDataFromCsv() {
9393
std::vector<DecodingTestData> data_results;
94-
std::vector<std::vector<std::string>> csv_records =
94+
const std::vector<std::vector<std::string>> csv_records =
9595
ParseCsv(kDecodingTestsFile);
9696
for (auto& csv_record : csv_records) {
9797
DecodingTestData test_data = {};
@@ -108,9 +108,9 @@ std::vector<DecodingTestData> GetDecodingDataFromCsv() {
108108

109109
TEST_P(DecodingChecks, Decode) {
110110
const DecodingTestData& test_data = GetParam();
111-
const auto expected_rect =
112-
CodeArea(test_data.lo_lat_deg, test_data.lo_lng_deg, test_data.hi_lat_deg,
113-
test_data.hi_lng_deg, test_data.length);
111+
const CodeArea expected_rect{test_data.lo_lat_deg, test_data.lo_lng_deg,
112+
test_data.hi_lat_deg, test_data.hi_lng_deg,
113+
test_data.length};
114114
// Decode the code and check we get the correct coordinates.
115115
const CodeArea actual_rect = Decode(test_data.code);
116116
EXPECT_EQ(expected_rect.GetCodeLength(), actual_rect.GetCodeLength());
@@ -173,7 +173,7 @@ TEST_P(TolerantEncodingChecks, EncodeDegrees) {
173173
int failure_count = 0;
174174

175175
for (const EncodingTestData& tc : test_params.test_data) {
176-
const auto lat_lng = LatLng{tc.lat_deg, tc.lng_deg};
176+
const LatLng lat_lng{tc.lat_deg, tc.lng_deg};
177177
// Encode the test location and make sure we get the expected code.
178178
std::string got_code = Encode(lat_lng, tc.length);
179179
if (tc.code != got_code) {
@@ -234,7 +234,7 @@ const std::string kValidityTestsFile = "test_data/validityTests.csv";
234234

235235
std::vector<ValidityTestData> GetValidityDataFromCsv() {
236236
std::vector<ValidityTestData> data_results;
237-
std::vector<std::vector<std::string>> csv_records =
237+
const std::vector<std::vector<std::string>> csv_records =
238238
ParseCsv(kValidityTestsFile);
239239
for (auto& csv_record : csv_records) {
240240
ValidityTestData test_data = {};
@@ -271,7 +271,7 @@ const std::string kShortCodeTestsFile = "test_data/shortCodeTests.csv";
271271

272272
std::vector<ShortCodeTestData> GetShortCodeDataFromCsv() {
273273
std::vector<ShortCodeTestData> data_results;
274-
std::vector<std::vector<std::string>> csv_records =
274+
const std::vector<std::vector<std::string>> csv_records =
275275
ParseCsv(kShortCodeTestsFile);
276276
for (auto& csv_record : csv_records) {
277277
ShortCodeTestData test_data = {};
@@ -286,17 +286,17 @@ std::vector<ShortCodeTestData> GetShortCodeDataFromCsv() {
286286
}
287287

288288
TEST_P(ShortCodeChecks, ShortCode) {
289-
ShortCodeTestData test_data = GetParam();
290-
LatLng reference_loc =
291-
LatLng{test_data.reference_lat, test_data.reference_lng};
289+
const ShortCodeTestData& test_data = GetParam();
290+
const LatLng reference_loc{test_data.reference_lat, test_data.reference_lng};
292291
// Shorten the code using the reference location and check.
293292
if (test_data.test_type == "B" || test_data.test_type == "S") {
294-
std::string actual_short = Shorten(test_data.full_code, reference_loc);
293+
const std::string actual_short =
294+
Shorten(test_data.full_code, reference_loc);
295295
EXPECT_EQ(test_data.short_code, actual_short);
296296
}
297297
// Now extend the code using the reference location and check.
298298
if (test_data.test_type == "B" || test_data.test_type == "R") {
299-
std::string actual_full =
299+
const std::string actual_full =
300300
RecoverNearest(test_data.short_code, reference_loc);
301301
EXPECT_EQ(test_data.full_code, actual_full);
302302
}
@@ -306,9 +306,9 @@ INSTANTIATE_TEST_SUITE_P(OLC_Tests, ShortCodeChecks,
306306
::testing::ValuesIn(GetShortCodeDataFromCsv()));
307307

308308
TEST(MaxCodeLengthChecks, MaxCodeLength) {
309-
LatLng loc = LatLng{51.3701125, -10.202665625};
309+
constexpr LatLng loc{51.3701125, -10.202665625};
310310
// Check we do not return a code longer than is valid.
311-
std::string long_code = Encode(loc, 1000000);
311+
const std::string long_code = Encode(loc, 1000000);
312312
// The code length is the maximum digit count plus one for the separator.
313313
EXPECT_EQ(long_code.size(), 1 + internal::kMaximumDigitCount);
314314
EXPECT_TRUE(IsValid(long_code));
@@ -336,20 +336,19 @@ TEST(BenchmarkChecks, BenchmarkEncodeDecode) {
336336
std::uniform_int_distribution<size_t> len_dist(0, 15);
337337

338338
std::vector<BenchmarkTestData> tests;
339-
constexpr size_t loops = 1000000;
339+
constexpr size_t loops = 1000000;
340340
for (size_t i = 0; i < loops; i++) {
341341
BenchmarkTestData test_data = {};
342342
double lat = lat_dist(rng);
343343
double lng = lng_dist(rng);
344-
const double rounding =
345-
pow(10, round(rounding_dist(rng)));
344+
const double rounding = pow(10, round(rounding_dist(rng)));
346345
lat = round(lat * rounding) / rounding;
347346
lng = round(lng * rounding) / rounding;
348347
auto len = len_dist(rng);
349348
if (len < 10 && len % 2 == 1) {
350349
len += 1;
351350
}
352-
const auto lat_lng = LatLng{lat, lng};
351+
const LatLng lat_lng{lat, lng};
353352
const std::string code = Encode(lat_lng, len);
354353
test_data.lat_lng = lat_lng;
355354
test_data.len = len;

0 commit comments

Comments
 (0)