Skip to content

Commit

Permalink
New parsing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Nov 26, 2024
1 parent b6972d5 commit 4ae6fa3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ struct date {
date local_day();

date date_from_string(std::string_view str);
date dmy_date_from_string(std::string_view str);
year year_from_string(std::string_view str);
month month_from_string(std::string_view str);
day day_from_string(std::string_view str);
Expand Down
1 change: 1 addition & 0 deletions include/money.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ struct money {
// Official money parsing functions
std::string money_to_string(const money& amount);
money money_from_string(std::string_view money_string);
money single_money_from_string(std::string_view money_string);

std::ostream& operator<<(std::ostream& stream, const money& amount);

Expand Down
32 changes: 28 additions & 4 deletions src/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,47 @@ budget::date budget::local_day(){

budget::date budget::date_from_string(std::string_view str){
if (str.size() != 10) {
throw date_exception("Invalid size for date_from_string");
throw date_exception(std::format("Invalid size for date_from_string while parsing {}", str));
}

date_type y = 0;
date_type m = 0;
date_type d = 0;

if (auto [p, ec] = std::from_chars(str.data(), str.data() + 4, y); ec != std::errc() || p != str.data() + 4) {
throw date_exception("Invalid year in date_from_string");
throw date_exception(std::format("Invalid year in date_from_string while parsing {}", str));
}

if (auto [p, ec] = std::from_chars(str.data() + 5, str.data() + 7, m); ec != std::errc() || p != str.data() + 7) {
throw date_exception("Invalid month in date_from_string");
throw date_exception(std::format("Invalid month in date_from_string while parsing {}", str));
}

if (auto [p, ec] = std::from_chars(str.data() + 8, str.data() + 10, d); ec != std::errc() || p != str.data() + 10) {
throw date_exception("Invalid day in date_from_string");
throw date_exception(std::format("Invalid day in date_from_string while parsing {}", str));
}

return {y, m, d};
}

budget::date budget::dmy_date_from_string(std::string_view str){
if (str.size() != 10) {
throw date_exception(std::format("Invalid size for dmy_date_from_string while parsing {}", str));
}

date_type y = 0;
date_type m = 0;
date_type d = 0;

if (auto [p, ec] = std::from_chars(str.data(), str.data() + 2, d); ec != std::errc() || p != str.data() + 2) {
throw date_exception(std::format("Invalid day in dmy_date_from_string while parsing {}", str));
}

if (auto [p, ec] = std::from_chars(str.data() + 3, str.data() + 5, m); ec != std::errc() || p != str.data() + 5) {
throw date_exception(std::format("Invalid month in dmy_date_from_string while parsing {}", str));
}

if (auto [p, ec] = std::from_chars(str.data() + 6, str.data() + 10, y); ec != std::errc() || p != str.data() + 10) {
throw date_exception(std::format("Invalid year in dmy_date_from_string while parsing {}", str));
}

return {y, m, d};
Expand Down
35 changes: 34 additions & 1 deletion src/money.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ money budget::money_from_string(std::string_view money_sv){
if (p == money_string.data() + money_string.size()) {
return {dollars, cents};
}

if (*p == '.') {
++p;

Expand All @@ -45,7 +46,39 @@ money budget::money_from_string(std::string_view money_sv){
}
}

throw budget::budget_exception("\"" + std::string(money_string) + "\" is not a valid amount of money");
throw budget::budget_exception(std::format("\"{}\" is not a valid amount of money", money_sv));
}

money budget::single_money_from_string(std::string_view money_sv){
int dollars = 0;
int cents = 0;

if (auto [p, ec] = std::from_chars(money_sv.data(), money_sv.data() + money_sv.size(), dollars); ec == std::errc()) {
if (p == money_sv.data() + money_sv.size()) {
return {dollars, cents};
}

if (*p == '.') {
++p;

bool zero = *p == '0';

if (auto [p2, ec2] = std::from_chars(p, money_sv.data() + money_sv.size(), cents); ec2 == std::errc()) {
if (p2 == money_sv.data() + money_sv.size()) {
if (cents >= 0 && cents < 100) {
if (!zero && cents < 10) {
return {dollars, 10 * cents};
}

return {dollars, cents};
}
}
}
}
}

throw budget::budget_exception(std::format("\"{}\" is not a valid amount of money", money_sv));

}

std::string budget::money_to_string(const money& amount) {
Expand Down

0 comments on commit 4ae6fa3

Please sign in to comment.