Skip to content

Commit

Permalink
Merge pull request #3 from drashna/progmem
Browse files Browse the repository at this point in the history
Autocorrection: Convert to PROGMEM.
  • Loading branch information
getreuer authored Dec 20, 2021
2 parents f24d779 + f51d27c commit 7b672ed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
15 changes: 7 additions & 8 deletions features/autocorrection.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,33 @@ bool process_autocorrection(uint16_t keycode, keyrecord_t* record) {
// Check whether the buffer ends in a typo. This is done using a trie
// stored in `autocorrection_data`.
uint16_t state = 0;
uint8_t code = autocorrection_data[state];
uint8_t code = pgm_read_byte(autocorrection_data + state);
for (int i = typo_buffer_size - 1; i >= 0; --i) {
const uint8_t key_i = typo_buffer[i];

if (code & 64) { // Check for match in node with multiple children.
code &= 63;
for (; code != key_i; code = autocorrection_data[state += 3]) {
for (; code != key_i; code = pgm_read_byte(autocorrection_data + (state += 3))) {
if (!code) { return true; }
}

// Follow link to child node.
state = (uint16_t)((uint_fast16_t)autocorrection_data[state + 1]
| (uint_fast16_t)autocorrection_data[state + 2] << 8);
state = (uint16_t)((uint_fast16_t)pgm_read_byte(autocorrection_data + state + 1)
| (uint_fast16_t)pgm_read_byte(autocorrection_data + state + 2) << 8);
// Otherwise check for match in node with a single child.
} else if (code != key_i) {
return true;
} else if (!(code = autocorrection_data[++state])) {
} else if (!(code = pgm_read_byte(autocorrection_data + (++state)))) {
++state;
}

// Read first byte of the next node.
code = autocorrection_data[state];
code = pgm_read_byte(autocorrection_data + state);

if (code & 128) { // A typo was found! Apply autocorrection.
const int backspaces = code & 63;
for (int i = 0; i < backspaces; ++i) { tap_code(KC_BSPC); }
send_string((const char*)(autocorrection_data + state + 1));
send_string_P((char const*)(autocorrection_data + state + 1));

if (keycode == KC_SPC) {
typo_buffer[0] = KC_SPC;
Expand All @@ -127,4 +127,3 @@ bool process_autocorrection(uint16_t keycode, keyrecord_t* record) {

return true;
}

3 changes: 1 addition & 2 deletions features/autocorrection_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#define AUTOCORRECTION_MIN_LENGTH 5 // ":ture"
#define AUTOCORRECTION_MAX_LENGTH 10 // "accomodate"

static const uint8_t autocorrection_data[1104] = {108, 43, 0, 6, 71, 0, 7, 81,
static const uint8_t autocorrection_data[1104] PROGMEM = {108, 43, 0, 6, 71, 0, 7, 81,
0, 8, 199, 0, 9, 240, 1, 10, 250, 1, 11, 26, 2, 17, 53, 2, 18, 190, 2, 19,
202, 2, 21, 212, 2, 22, 20, 3, 23, 67, 3, 28, 16, 4, 0, 72, 50, 0, 22, 60, 0,
0, 11, 23, 44, 8, 11, 23, 44, 0, 132, 0, 8, 22, 18, 18, 15, 0, 132, 115, 101,
Expand Down Expand Up @@ -133,4 +133,3 @@ static const uint8_t autocorrection_data[1104] = {108, 43, 0, 6, 71, 0, 7, 81,
21, 9, 0, 129, 110, 99, 121, 0, 23, 9, 4, 22, 0, 130, 101, 116, 121, 0, 6, 21,
4, 21, 12, 8, 11, 0, 135, 105, 101, 114, 97, 114, 99, 104, 121, 0, 4, 5, 12,
15, 0, 130, 114, 97, 114, 121, 0};

2 changes: 1 addition & 1 deletion features/make_autocorrection_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def write_generated_code(autocorrections: List[Tuple[str, str]],
for typo, correction in autocorrections)),
f'\n#define AUTOCORRECTION_MIN_LENGTH {len(min_typo)} // "{min_typo}"\n',
f'#define AUTOCORRECTION_MAX_LENGTH {len(max_typo)} // "{max_typo}"\n\n',
textwrap.fill('static const uint8_t autocorrection_data[%d] = {%s};' % (
textwrap.fill('static const uint8_t autocorrection_data[%d] PROGMEM = {%s};' % (
len(data), ', '.join(map(str, data))), width=80, subsequent_indent=' '),
'\n\n'])

Expand Down

0 comments on commit 7b672ed

Please sign in to comment.