static int dt_parse_fractional_seconds_format()

in src/dt.c [617:667]


static int dt_parse_fractional_seconds_format(const char *str, size_t *format_length, size_t *precision)
{
    typedef enum {
        DT_FORMAT_PARSER_INITIAL_STATE,
        DT_FORMAT_PARSER_PERCENT_FOUND,
        DT_FORMAT_PARSER_DIGIT_FOUND
    } dt_format_parser_state;

    dt_format_parser_state state = DT_FORMAT_PARSER_INITIAL_STATE;
    size_t pos = 0;
    size_t format_start_pos = 0;
    size_t parsed_precision = 0;

    while (str[pos] != '\0') {
        switch (state) {
            case DT_FORMAT_PARSER_INITIAL_STATE:
                if (str[pos] == '%') {
                    format_start_pos = pos;
                    parsed_precision = 0;
                    state = DT_FORMAT_PARSER_PERCENT_FOUND;
                }
                break;
            case DT_FORMAT_PARSER_PERCENT_FOUND:
                if (str[pos] == 'f') {
                    *format_length = 2;
                    *precision = parsed_precision;
                    return format_start_pos;
                } else if (isdigit(str[pos])) {
                    parsed_precision = str[pos] - '0';
                    state = DT_FORMAT_PARSER_DIGIT_FOUND;
                } else {
                    state = DT_FORMAT_PARSER_INITIAL_STATE;
                }
                break;
            case DT_FORMAT_PARSER_DIGIT_FOUND:
                if (str[pos] == 'f') {
                    *format_length = 3;
                    *precision = parsed_precision;
                    return format_start_pos;
                } else {
                    state = DT_FORMAT_PARSER_INITIAL_STATE;
                }
                break;
            default:
                // Invalid parser state
                return -1;
        }
        ++pos;
    }
    return -1;
}