Sander Rossel wrote: Also not sure about the nature of your project, but when an employee of mine would use regex, that no one knows or expects in a web API scenario, in favor of JSON, which everyone knows and expects in that scenario, I'd have a very serious talk with him
So would I, because he's working on machines with gobs of RAM and CPU to spare, and hardware is typically cheaper than software, especially software that's written predictably.
Here's my current reality:
RAM: [== ] 20.6% (used 67656 bytes from 327680 bytes)
Flash: [======= ] 65.1% (used 852905 bytes from 1310720 bytes)
I don't have the flash space for a JSON lib.
So with IoT, priorities change. If I had your priorities, my code wouldn't even run.
Sander Rossel wrote: I'm also not sure why JSON would be a dependency while regex is not
Because I am not using a regex engine. I am precomputing a regular expression into an array.
Then I use some simple code to traverse that array in order to match
For example:
int16_t wtime_unix_time_dfa_table[] PROGMEM = {
-1, 1, 6, 1, 34, 34, -1, 1, 12, 1, 117, 117, -1, 1, 18,
1, 110, 110, -1, 1, 24, 1, 105, 105, -1, 1, 30, 1, 120, 120,
-1, 1, 36, 1, 116, 116, -1, 1, 42, 1, 105, 105, -1, 1, 48,
1, 109, 109, -1, 1, 54, 1, 101, 101, -1, 1, 60, 1, 34, 34,
-1, 2, 60, 3, 9, 10, 13, 13, 32, 32, 74, 1, 58, 58, 0,
1, 74, 3, 9, 10, 13, 13, 32, 32
};
In order to match with it, I run this code:
bool wtime_fa_match(const int16_t* dfa, int16_t(read_cb)(void*), void* cb_state = nullptr) {
int tlen;
int tto;
int prlen;
int pmin;
int pmax;
int i;
int j;
int ch;
int state = 0;
bool done;
bool found = false;
int acc = -1;
ch = read_cb(cb_state);
while (ch != -1) {
acc = -1;
done = false;
while (!done) {
start_dfa:
done = true;
acc = (int16_t)pgm_read_word(dfa+(state++));
tlen = (int16_t)pgm_read_word(dfa+(state++));
for (i = 0; i < tlen; ++i) {
tto = (int16_t)pgm_read_word(dfa+(state++));
prlen = (int16_t)pgm_read_word(dfa+(state++));
for (j = 0; j < prlen; ++j) {
pmin = (int16_t)pgm_read_word(dfa+(state++));
pmax = (int16_t)pgm_read_word(dfa+(state++));
if (ch < pmin) break;
if (ch <= pmax) {
found = true;
ch = read_cb(cb_state);
state = tto;
done = false;
goto start_dfa;
}
}
}
}
if (acc != -1) {
return found;
}
ch = read_cb(cb_state);
state = 0;
}
return false;
}
To err is human. Fortune favors the monsters.
modified 10-Jun-22 4:11am.
|