- fennec::format refactor. Strings partially implemented. Integers and bools fully implemented.

This commit is contained in:
2025-12-04 20:39:10 -05:00
parent 6d58105734
commit 7f1dd245dc
6 changed files with 403 additions and 49 deletions

View File

@@ -43,6 +43,8 @@ struct _format_argimpl {
_format_argimpl() {};
virtual ~_format_argimpl() {};
virtual string format(const format_arg& fmt) = 0;
virtual bool is_integer() = 0;
virtual int64_t int_value() = 0;
};
// Polymorphic template specialization
@@ -53,21 +55,28 @@ struct _format_arg : _format_argimpl {
_format_arg(const T& arg) : val(arg) {
}
virtual ~_format_arg() = default;
virtual string format(const format_arg& fmt) {
string format(const format_arg& fmt) override {
return fennec::forward<string>(fmtr(fmt, val));
}
bool is_integer() override {
return is_integral_v<T> or is_convertible_v<T, int64_t>;
}
virtual int64_t int_value() override {
if constexpr(is_integral_v<T>) {
return val;
} else if constexpr(is_convertible_v<T, int64_t>) {
return val;
} else {
return -1;
}
}
};
// Polymorphic template specialization for rvalue_references
// Polymorphic template specialization for x/r/xr value references
template<typename T>
struct _format_arg<T&> : _format_argimpl {
formatter<T> fmtr;
const T& val;
_format_arg(const T& arg) : val(arg) {
}
virtual ~_format_arg() = default;
virtual string format(const format_arg& fmt) {
return fennec::forward<string>(fmtr(fmt, val));
struct _format_arg<T&> : _format_arg<T> {
_format_arg(const T& arg) : _format_arg<T>(arg) {
}
};
@@ -83,8 +92,59 @@ struct _format_argarray {
string format(size_t i, const format_arg& fmt) {
return args[i]->format(fmt);
}
bool is_integer(size_t i) {
return args[i]->is_integer();
}
int64_t int_value(size_t i) {
return args[i]->int_value();
}
};
// checks if character is a valid format type
constexpr bool _isfmt_t(char c) {
switch (c) {
default: return false;
case 's': case '?': // strings
case 'c': // char
case 'd': // decimal
case 'b': case 'B': // binary
case 'o': // octal
case 'x': case 'X': // hex
case 'a': case 'A': // float hex
case 'e': case 'E': // scientific notation
case 'f': case 'F': // fixed precision
case 'g': case 'G': // general precision
return true;
}
}
// checks if character is a valid format int type
constexpr bool _isfmt_i(char c) {
switch (c) {
default: return false;
case 'd': // decimal
case 'b': case 'B': // binary
case 'o': // octal
case 'x': case 'X': // hex
return true;
}
}
// checks if character is a valid format float type
constexpr bool _isfmt_f(char c) {
switch (c) {
default: return false;
case 'a': case 'A': // float hex
case 'e': case 'E': // scientific notation
case 'f': case 'F': // fixed precision
case 'g': case 'G': // general precision
return true;
}
}
}
#endif // FENNEC_LANG_STRINGS_DETAIL_FORMAT_H