Click here to Skip to main content
16,020,182 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: My uncle fell down the stairs... Pin
Nelek13-Sep-24 6:01
protectorNelek13-Sep-24 6:01 
GeneralRe: My uncle fell down the stairs... Pin
CPallini13-Sep-24 7:31
mveCPallini13-Sep-24 7:31 
GeneralRe: My uncle fell down the stairs... Pin
Sander Rossel15-Sep-24 22:20
professionalSander Rossel15-Sep-24 22:20 
GeneralRe: My uncle fell down the stairs... Pin
jmaida13-Sep-24 17:09
jmaida13-Sep-24 17:09 
GeneralRe: My uncle fell down the stairs... Pin
Craig Robbins15-Sep-24 3:24
Craig Robbins15-Sep-24 3:24 
GeneralWoohoo, peephole parsing big content Pin
honey the codewitch12-Sep-24 23:16
mvahoney the codewitch12-Sep-24 23:16 
GeneralRe: Woohoo, peephole parsing big content Pin
BernardIE531713-Sep-24 1:06
BernardIE531713-Sep-24 1:06 
GeneralRe: Woohoo, peephole parsing big content Pin
honey the codewitch13-Sep-24 3:42
mvahoney the codewitch13-Sep-24 3:42 
Here's my float routine. It uses my ml_reader markup peephole parser

Basically I keep a running cursor over the current buffer (**current) as well as the rdr for when I need to fetch the next string. The rest is just state machine stuff.

C++
result_t parse_float(ml_reader_base& rdr, const char** current, float* result) {
    char* end = NULL;
    double res = 0.0, sign = 1.0;
    long long intPart = 0, fracPart = 0;
    int fracCount = 0;
    long expPart = 0;
    char expNeg = 0;
    char hasIntPart = 0, hasFracPart = 0, hasExpPart = 0;
    int state = 0;
    // Parse optional sign
    if (**current == '+') {
        (*current)++;
    } else if (**current == '-') {
        sign = -1;
        (*current)++;
    }
    
    while (state<7) {
        if (**current) {
            switch (state) {
                case 0: // int part
                    if (!isdigit(**current)) {
                        state = 1;
                        break;
                    }
                    hasIntPart=1;
                    intPart = (intPart*10)+(**current-'0');
                    ++(*current);
                    break;
                case 1:
                    *result = (float)intPart;
                    if(**current!='.') {
                        state = 3;
                        break;
                    }
                    ++(*current);
                    state = 2;
                    break;
                case 2: // frac part
                    if (!isdigit(**current)) {
                        state = 3;
                        break;
                    }
                    ++fracCount;
                    hasFracPart=1;
                    fracPart = (fracPart*10)+(**current-'0');
                    ++(*current);
                    break;
                case 3:
                    if(hasFracPart) {
                        *result += (double)fracPart/pow(10.0,(double)fracCount);
                    }
                    if(**current=='E' || **current=='e') {
                        ++(*current);
                        state = 4;
                    } else {
                        state = 6;
                    }
                    break;
                case 4:
                    if(**current=='+') {
                        ++(*current);
                    }
                    if(**current=='-') {
                        expNeg = 1;
                        ++(*current);
                    }
                    state = 5;
                    break;
                case 5: 
                    if (!isdigit(**current)) {
                        state = 6;
                        break;
                    }
                    hasExpPart=1;
                    expPart = (expPart*10)+(**current-'0');
                    ++(*current);
                    break;
                case 6:
                    if(hasExpPart) {
                        if(expNeg) {
                            expPart = -expPart;
                        }
                        *result *= pow(10.0,(double)expPart);
                    }
                    *result*=sign;
                    state = 7;
                    break;
            }

        } else {
            if (!rdr.read()) {
                return IO_ERROR;
            }
            if (!rdr.has_value()) {
                *current = nullptr;
                break;
            }
            *current = rdr.value();
        }
    }
    if (rdr.node_type() == ml_node_type::attribute_end) {
        if (!rdr.read()) {
            return IO_ERROR;
        }
    }
    if(!hasIntPart&&!hasFracPart) {
        return FMT_ERROR;
    }
    return SUCCESS;
}

Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix

GeneralRe: Woohoo, peephole parsing big content Pin
Matthew Dennis13-Sep-24 3:14
sysadminMatthew Dennis13-Sep-24 3:14 
GeneralThe Daily insider, things are getting really bad ... Pin
Richard MacCutchan12-Sep-24 22:50
mveRichard MacCutchan12-Sep-24 22:50 
GeneralRe: The Daily insider, things are getting really bad ... PinPopular
CodeProject13-Sep-24 3:36
CodeProject13-Sep-24 3:36 
GeneralRe: The Daily insider, things are getting really bad ... Pin
dandy7213-Sep-24 3:59
dandy7213-Sep-24 3:59 
GeneralRe: The Daily insider, things are getting really bad ... Pin
TNCaver13-Sep-24 4:28
TNCaver13-Sep-24 4:28 
JokeRe: The Daily insider, things are getting really bad ... Pin
Nelek13-Sep-24 6:04
protectorNelek13-Sep-24 6:04 
GeneralI threw a boomerang ... Pin
BernardIE531712-Sep-24 21:00
BernardIE531712-Sep-24 21:00 
GeneralRe: I threw a boomerang ... Pin
CPallini12-Sep-24 21:30
mveCPallini12-Sep-24 21:30 
GeneralRe: I threw a boomerang ... Pin
Sander Rossel12-Sep-24 23:33
professionalSander Rossel12-Sep-24 23:33 
GeneralRe: I threw a boomerang ... Pin
MarkTJohnson13-Sep-24 3:48
professionalMarkTJohnson13-Sep-24 3:48 
GeneralRe: I threw a boomerang ... Pin
obermd13-Sep-24 3:52
obermd13-Sep-24 3:52 
GeneralRe: I threw a boomerang ... Pin
Gary Stachelski 202113-Sep-24 6:03
Gary Stachelski 202113-Sep-24 6:03 
GeneralRe: I threw a boomerang ... Pin
Nelek13-Sep-24 10:01
protectorNelek13-Sep-24 10:01 
GeneralRe: I threw a boomerang ... Pin
Gary Stachelski 202113-Sep-24 12:04
Gary Stachelski 202113-Sep-24 12:04 
GeneralRe: I threw a boomerang ... Pin
FreedMalloc13-Sep-24 12:27
FreedMalloc13-Sep-24 12:27 
GeneralWordle 1,182 Pin
Shane010312-Sep-24 17:54
Shane010312-Sep-24 17:54 
GeneralRe: Wordle 1,182 Pin
GKP199212-Sep-24 18:05
professionalGKP199212-Sep-24 18:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.