Skip to content

Commit 637654b

Browse files
Update cstring.h
1 parent 9d1097f commit 637654b

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

code/logic/fossil/io/cstring.h

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,62 +1130,48 @@ namespace fossil {
11301130
}
11311131

11321132
/**
1133-
* @brief Converts a double amount into a formatted money string.
1134-
*
1135-
* Example: 1234.56 -> "$1,234.56"
1136-
*
1137-
* @param amount Numeric amount to convert.
1138-
* @return Formatted money string.
1133+
* Convert numeric amount to string.
1134+
* Uses default currency "$".
11391135
*/
11401136
static std::string money_to_string(double amount) {
1141-
char buf[128];
1142-
if (fossil_io_cstring_money_to_string(amount, buf, sizeof(buf)) != 0) {
1143-
return "";
1137+
char buffer[128];
1138+
if (fossil_io_cstring_money_to_string(amount, buffer, sizeof(buffer)) != 0) {
1139+
throw std::runtime_error("Failed to convert amount to string");
11441140
}
1145-
return std::string(buf);
1141+
return std::string(buffer);
11461142
}
1147-
1143+
11481144
/**
1149-
* @brief Parses a money string into a numeric double value.
1150-
*
1151-
* Example: "$1,234.56" -> 1234.56
1152-
*
1153-
* @param input Formatted money string.
1154-
* @param amount Reference to double to store numeric value.
1155-
* @return true on success, false on failure.
1145+
* Convert numeric amount to string with currency symbol.
11561146
*/
1157-
static bool string_to_money(const std::string &input, double &amount) {
1158-
return fossil_io_cstring_string_to_money(input.c_str(), &amount) == 0;
1147+
static std::string currency_to_string(double amount, const std::string &currency) {
1148+
char buffer[128];
1149+
if (fossil_io_cstring_money_to_string_currency(amount, buffer, sizeof(buffer), currency.c_str()) != 0) {
1150+
throw std::runtime_error("Failed to convert amount to string with currency");
1151+
}
1152+
return std::string(buffer);
11591153
}
1160-
1154+
11611155
/**
1162-
* @brief Converts a double amount into a formatted money string with optional currency symbol.
1163-
*
1164-
* Example: 1234.56 -> "$1,234.56" (USD default)
1165-
*
1166-
* @param amount The numeric amount to convert.
1167-
* @param output Buffer to store the formatted string.
1168-
* @param size Size of the output buffer.
1169-
* @param currency Currency symbol to prepend (e.g., "$", "€", "¥"); NULL defaults to "$".
1170-
* @return 0 on success, -1 if the buffer is too small or invalid.
1156+
* Convert string to numeric amount.
11711157
*/
1172-
static int to_string_currency(double amount, std::string &output, size_t size, const std::string &currency){
1173-
return fossil_io_cstring_money_to_string_currency(amount, output.c_str(), currency.c_str());
1158+
static double from_money(const std::string &str) {
1159+
double value = 0.0;
1160+
if (fossil_io_cstring_string_to_money(str.c_str(), &value) != 0) {
1161+
throw std::runtime_error("Failed to parse money string");
1162+
}
1163+
return value;
11741164
}
1175-
1165+
11761166
/**
1177-
* @brief Parses a money string into a numeric double value.
1178-
*
1179-
* Detects and ignores a currency symbol at the start.
1180-
*
1181-
* Example: "$1,234.56" -> 1234.56
1182-
*
1183-
* @param input Input string representing money.
1184-
* @param amount Pointer to store the parsed numeric value.
1185-
* @return 0 on success, -1 on failure (invalid format).
1167+
* Convert string to numeric amount with currency detection.
11861168
*/
1187-
static int to_money_currency(const std::string &input, double *amount) {
1188-
return fossil_io_cstring_string_to_money_currency(input.c_str(), amount);
1169+
static double from_currency(const std::string &str) {
1170+
double value = 0.0;
1171+
if (fossil_io_cstring_string_to_money_currency(str.c_str(), &value) != 0) {
1172+
throw std::runtime_error("Failed to parse money string with currency");
1173+
}
1174+
return value;
11891175
}
11901176

11911177
/**

0 commit comments

Comments
 (0)