|
| 1 | +#include "match.hpp" |
| 2 | +#include <print> |
| 3 | +#include <sstream> |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | +#include <charconv> |
| 7 | +#include <string_view> |
| 8 | +using namespace cppmatch; |
| 9 | + |
| 10 | +// ANSI escape codes for colors |
| 11 | +inline constexpr std::string_view RESET = "\033[0m"; |
| 12 | +inline constexpr std::string_view RED = "\033[31m"; |
| 13 | +inline constexpr std::string_view GREEN = "\033[32m"; |
| 14 | +inline constexpr std::string_view YELLOW = "\033[33m"; |
| 15 | +inline constexpr std::string_view BLUE = "\033[34m"; |
| 16 | +inline constexpr std::string_view CYAN = "\033[36m"; |
| 17 | + |
| 18 | +// Define a simple structure to hold latitude & longitude |
| 19 | +struct Coordinate { |
| 20 | + double latitude; |
| 21 | + double longitude; |
| 22 | +}; |
| 23 | + |
| 24 | + |
| 25 | +struct InvalidDoubleConversion { std::string_view message; }; |
| 26 | +struct InvalidCoordinate { std::string_view message; }; |
| 27 | +struct InvalidCoordinateFormat { std::string_view message; }; |
| 28 | + |
| 29 | +// Exception-free string to double conversion using std::from_chars |
| 30 | +auto safe_str_to_double(std::string_view str) -> Result<double, InvalidDoubleConversion> { |
| 31 | + double value = 0.0; |
| 32 | + auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value); |
| 33 | + |
| 34 | + if (ec == std::errc::invalid_argument) return InvalidDoubleConversion{"Invalid number format"}; |
| 35 | + if (ec == std::errc::result_out_of_range) return InvalidDoubleConversion{"Number out of range"}; |
| 36 | + if (ptr != str.data() + str.size()) return InvalidDoubleConversion{"Extra characters found in number"}; |
| 37 | + |
| 38 | + return value; |
| 39 | +} |
| 40 | + |
| 41 | +// Parses a single latitude,longitude pair safely |
| 42 | +auto parse_coordinate(const std::string& input) -> Result<Coordinate, Error<InvalidDoubleConversion, InvalidCoordinate, InvalidCoordinateFormat>> { |
| 43 | + std::istringstream ss(input); |
| 44 | + std::string lat_str, lon_str; |
| 45 | + |
| 46 | + if (!std::getline(ss, lat_str, ',') || !std::getline(ss, lon_str)) { |
| 47 | + return InvalidCoordinateFormat{"Invalid format (expected 'latitude,longitude')"}; |
| 48 | + } |
| 49 | + |
| 50 | + // Convert latitude and longitude safely |
| 51 | + double latitude = expect(safe_str_to_double(lat_str)); |
| 52 | + double longitude = expect(safe_str_to_double(lon_str)); |
| 53 | + |
| 54 | + // Validate ranges |
| 55 | + if (latitude < -90 || latitude > 90) return InvalidCoordinate{"Latitude out of range (-90 to 90)"}; |
| 56 | + if (longitude < -180 || longitude > 180) return InvalidCoordinate{"Longitude out of range (-180 to 180)"}; |
| 57 | + |
| 58 | + return Coordinate{latitude, longitude}; |
| 59 | +} |
| 60 | + |
| 61 | +// Parses a string containing multiple coordinates separated by semicolons |
| 62 | +auto parse_coordinates(const std::string& input) -> Result<std::vector<Coordinate>, Error<InvalidDoubleConversion, InvalidCoordinate, InvalidCoordinateFormat>> { |
| 63 | + std::vector<Coordinate> coordinates; |
| 64 | + std::istringstream ss(input); |
| 65 | + std::string token; |
| 66 | + |
| 67 | + while (std::getline(ss, token, ';')) { |
| 68 | + token.erase(0, token.find_first_not_of(" \t")); // Trim left |
| 69 | + token.erase(token.find_last_not_of(" \t") + 1); // Trim right |
| 70 | + |
| 71 | + Coordinate coord = expect(parse_coordinate(token)); |
| 72 | + coordinates.push_back(coord); |
| 73 | + } |
| 74 | + |
| 75 | + return coordinates; |
| 76 | +} |
| 77 | + |
| 78 | +// Pretty-printing a coordinate |
| 79 | +void print_coordinate(const Coordinate& coord) { |
| 80 | + std::print("{}Parsed Coordinate -> Latitude: {}, Longitude: {}{}\n", |
| 81 | + GREEN, coord.latitude, coord.longitude, RESET); |
| 82 | +} |
| 83 | + |
| 84 | +// Testing our parser |
| 85 | +int main() { |
| 86 | + std::vector<std::string> test_cases = { |
| 87 | + "40.7128,-74.0060; 34.0522,-118.2437; 48.8566,2.3522", // Valid |
| 88 | + "91.0000,45.0000; 50.0000,-30.0000", // Invalid latitude |
| 89 | + "50.0000,190.0000; -20.0000,120.0000", // Invalid longitude |
| 90 | + "abcd,efgh; 10.0,20.0", // Invalid number format |
| 91 | + "10.5,20.5", // Single valid coordinate |
| 92 | + " 40.0 , -75.0 ; 50.0 , -45.0 ", // With extra spaces |
| 93 | + ";;" // Empty inputs |
| 94 | + }; |
| 95 | + |
| 96 | + for (const auto& test : test_cases) { |
| 97 | + std::print("{}\nInput: \"{}\"{}\n", CYAN, test, RESET); |
| 98 | + auto result = parse_coordinates(test); |
| 99 | + |
| 100 | + match(result, |
| 101 | + [](const std::vector<Coordinate>& coords) { |
| 102 | + std::print("{}Successfully parsed {} coordinates:\n{}", GREEN, coords.size(), RESET); |
| 103 | + for (const auto& coord : coords) { |
| 104 | + print_coordinate(coord); |
| 105 | + } |
| 106 | + }, |
| 107 | + [](const InvalidCoordinate err) { |
| 108 | + std::print("{}Error: {}{}\n", RED, err.message, RESET); |
| 109 | + }, |
| 110 | + [](const InvalidCoordinateFormat err) { |
| 111 | + std::print("{}Error: {}{}\n", RED, err.message, RESET); |
| 112 | + }, |
| 113 | + [](const InvalidDoubleConversion err) { |
| 114 | + std::print("{}Error: {}{}\n", RED, err.message, RESET); |
| 115 | + } |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments