Files
rgbds/include/asm/format.hpp
Sylvie 6b8d33529e Improve string/interpolation formatting (#1491)
- The '#' component for type 's' now escapes the string characters
- The '#' component for type 'f' now prints a precision suffix
- The new 'q' component specifies a precision value
2024-09-01 12:54:26 -04:00

48 lines
1.2 KiB
C++

/* SPDX-License-Identifier: MIT */
#ifndef RGBDS_ASM_FORMAT_HPP
#define RGBDS_ASM_FORMAT_HPP
#include <stddef.h>
#include <stdint.h>
#include <string>
enum FormatState {
FORMAT_SIGN, // expects '+' or ' ' (optional)
FORMAT_EXACT, // expects '#' (optional)
FORMAT_ALIGN, // expects '-' (optional)
FORMAT_WIDTH, // expects '0'-'9', max 255 (optional) (leading '0' indicates pad)
FORMAT_FRAC, // got '.', expects '0'-'9', max 255 (optional)
FORMAT_PREC, // got 'q', expects '0'-'9', range 1-31 (optional)
FORMAT_DONE, // got [duXxbofs] (required)
FORMAT_INVALID, // got unexpected character
};
class FormatSpec {
FormatState state;
int sign;
bool exact;
bool alignLeft;
bool padZero;
size_t width;
bool hasFrac;
size_t fracWidth;
bool hasPrec;
size_t precision;
int type;
bool valid;
public:
bool isEmpty() const { return !state; }
bool isValid() const { return valid || state == FORMAT_DONE; }
bool isFinished() const { return state >= FORMAT_DONE; }
void useCharacter(int c);
void finishCharacters();
void appendString(std::string &str, std::string const &value) const;
void appendNumber(std::string &str, uint32_t value) const;
};
#endif // RGBDS_ASM_FORMAT_HPP