Word Unperfect
public
Read
Owner: themaster
Branch: main
Commits: 0
Git CLI clone URL
git clone https://www.xt-emporium.com/git/word-unperfect.git
Fullscreen desktop URL
Code
Commits
History
Branches
Bug Reports
Discussions
Compare
Settings
word-unperfect
/
unix-word-unperfect
/
libc_format_table.c
File editor
#include "wp_format_table.h" #include <stdio.h> #include <string.h> static uint16_t format_table_le16(const uint8_t *p) { return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8)); } void wp_format_table_clear(WpFormatTableInfo *table) { if (table != NULL) { memset(table, 0, sizeof(*table)); } } bool wp_format_table_decode_payload(const uint8_t *payload, uint16_t payload_length, uint8_t sub_code, WpFormatTableInfo *out_table) { WpFormatTableInfo table; size_t i; bool in_group = false; size_t current_group_words = 0U; bool have_previous_in_group = false; uint16_t previous_in_group = 0U; wp_format_table_clear(out_table); if (payload == NULL || out_table == NULL) { return false; } memset(&table, 0, sizeof(table)); table.is_present = true; table.sub_code = sub_code; table.payload_bytes = payload_length; table.word_count = payload_length / 2U; table.odd_payload_byte = (payload_length & 1U) != 0U; if (table.odd_payload_byte) { table.odd_payload_tail = payload[payload_length - 1U]; } for (i = 0U; i < table.word_count; ++i) { uint16_t value = format_table_le16(payload + i * 2U); if (table.stored_word_count < WP_FORMAT_TABLE_MAX_STORED_WORDS) { table.stored_words[table.stored_word_count++] = value; } if (value == 0xffffU) { table.sentinel_word_count++; if (in_group) { if (current_group_words > table.longest_group_words) { table.longest_group_words = current_group_words; } current_group_words = 0U; in_group = false; have_previous_in_group = false; } continue; } table.usable_word_count++; if (value == 0U) { table.zero_word_count++; } if (!in_group) { table.group_count++; in_group = true; current_group_words = 0U; have_previous_in_group = false; } current_group_words++; if (!table.has_values) { table.has_values = true; table.first_value_index = (uint16_t)i; table.first_value = value; table.min_value = value; table.max_value = value; } else { if (value < table.min_value) { table.min_value = value; } if (value > table.max_value) { table.max_value = value; } } table.last_value_index = (uint16_t)i; table.last_value = value; if (have_previous_in_group) { if (value < previous_in_group) { table.nonmonotonic_step_count++; } else if (value > previous_in_group) { uint16_t step = (uint16_t)(value - previous_in_group); if (!table.has_positive_step) { table.has_positive_step = true; table.first_positive_step = step; table.min_positive_step = step; table.max_positive_step = step; } else { if (step < table.min_positive_step) { table.min_positive_step = step; } if (step > table.max_positive_step) { table.max_positive_step = step; } } table.positive_step_count++; } } previous_in_group = value; have_previous_in_group = true; } if (in_group && current_group_words > table.longest_group_words) { table.longest_group_words = current_group_words; } *out_table = table; return true; } bool wp_format_table_decode_record(const WpRecord *rec, WpFormatTableInfo *out_table) { wp_format_table_clear(out_table); if (rec == NULL || rec->type != WP_CODE_VARIABLE_LENGTH || rec->code != 0xd0U) { return false; } if (rec->data == NULL && rec->data_length != 0U) { return false; } return wp_format_table_decode_payload(rec->data, rec->data_length, rec->sub_code, out_table); } uint16_t wp_format_table_recommended_span_step(const WpFormatTableInfo *table) { if (table == NULL || !table->has_positive_step) { return 0U; } return table->first_positive_step; } bool wp_format_table_describe(const WpFormatTableInfo *table, char *out, size_t out_size) { if (out == NULL || out_size == 0U) { return false; } out[0] = '\0'; if (table == NULL || !table->is_present) { return false; } if (table->has_values) { snprintf(out, out_size, "format-table words=%lu groups=%lu sentinels=%lu zeros=%lu first[%u]=%u last[%u]=%u max=%u step=%u nonmono=%lu odd=%s", (unsigned long)table->word_count, (unsigned long)table->group_count, (unsigned long)table->sentinel_word_count, (unsigned long)table->zero_word_count, (unsigned)table->first_value_index, (unsigned)table->first_value, (unsigned)table->last_value_index, (unsigned)table->last_value, (unsigned)table->max_value, (unsigned)wp_format_table_recommended_span_step(table), (unsigned long)table->nonmonotonic_step_count, table->odd_payload_byte ? "yes" : "no"); } else { snprintf(out, out_size, "format-table words=%lu groups=0 sentinels=%lu odd=%s", (unsigned long)table->word_count, (unsigned long)table->sentinel_word_count, table->odd_payload_byte ? "yes" : "no"); } return true; }
Commit message
This repository is read-only for this account.
Repository snapshot
Current branch
main
Visibility
public
Your access
Read
Remote
None
File activity
View file history