rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
text_hl.c File Reference

Syntax highlighting of one line of text (text_hl.h): the rules of a bench text, JSON and CSV. More...

#include "text_hl.h"
#include <stdbool.h>
#include <string.h>

Data Structures

struct  spans_t
 Where the spans of a line are written. More...

Macros

#define CHECKSUM_DIGITS   8u
 Digits of the checksum in a KST file's "checksum" line.
#define HEADING_MIN   3u
 Characters a line has at least to be a heading.
#define CLASS_CSS(name, css, role)
 Expands a row of TEXT_HL_CLASS_ROWS to its style sheet name.

Enumerations

enum  letter_t { LETTER_NONE = 0 , LETTER_UPPER , LETTER_LOWER }
 The case of a letter. More...

Functions

static bool is_word (unsigned char b)
 Whether b belongs to a word: an ASCII letter or digit, "_", or any byte of a UTF-8 sequence.
static bool is_digit (unsigned char b)
 Whether b is an ASCII digit.
static bool is_hex (unsigned char b)
 Whether b is a hexadecimal digit.
static bool is_blank (unsigned char b)
 Whether b is a space or a tab.
static letter_t letter (const unsigned char *s, size_t n, size_t *len)
 The case of the letter at s: ASCII and the 7 German letters.
static void emit (spans_t *sp, size_t start, size_t len, text_hl_class_t cls)
 Add bytes start to start + len as class cls, joined to the last span when it has that class.
static void trim (const char *s, size_t *a, size_t *b)
 Narrow [*a, *b) of s past the blanks at both ends.
const char * text_hl_word_core (const char *s, size_t *len)
 The word a writer's string gives: spaces taken off both ends, a "Label:" and the spaces after it taken off the front, then one pair of parentheses around it and a full stop behind it.
static bool apart (unsigned char e, unsigned char nb)
 Whether the edge byte e of a word stands alone against its neighbour nb: a word byte against no word byte, a mark against anything but the same mark.
static size_t word_at (const unsigned char *line, size_t len, size_t i, const char *const *list, size_t n)
 The longest verdict word of list at byte i of the line.
text_hl_class_t text_hl_unit_class (const char *unit, size_t len)
 The class of a unit of the table.
const char * text_hl_class_css (text_hl_class_t cls)
 The style sheet's name of a class (TEXT_HL_CLASS_ROWS).
static size_t unit_end (const unsigned char *line, size_t len, size_t k)
 The end of a unit's bytes from byte k: a "%"; or a "/" and letters, or letters; the ohm sign counts as a letter.
static text_hl_class_t unit_class_at (const unsigned char *line, size_t len, size_t k, size_t u)
 The class of the unit from byte k to u, when it ends at no word byte.
static size_t number_at (const unsigned char *line, size_t len, size_t i, text_hl_class_t *cls)
 A number at byte i, with the unit behind it: joined to it, as in "6.00V", or after one space, as the older reports write it.
static bool hex_line (const unsigned char *s, size_t len)
 Whether a line is a register line "NN: XX XX ...", a checksum line "checksum XXXXXXXX" or a journal line "key=hexdigits", trailing spaces allowed.
static bool heading (const unsigned char *s, size_t len)
 Whether a line is a heading: its first character an upper-case letter, at least HEADING_MIN characters, and no lower-case letter before the first "(".
static size_t label_end (const unsigned char *s, size_t len)
 Where a label ends: after the first ":" or "=" in the first TEXT_HL_LABEL_CELLS characters, when a blank or the line's end follows it and the line starts with no blank.
static void rest (spans_t *sp, const unsigned char *s, size_t len, size_t i, const text_hl_words_t *words)
 The spans of a bench text's line from byte i on: comments, verdict words and numbers with units.
static void text_line (spans_t *sp, const unsigned char *s, size_t len, size_t line_no, const text_hl_words_t *words)
 The spans of a bench text's line.
static size_t json_number (const unsigned char *s, size_t len, size_t i)
 Bytes of a JSON number at i.
static void json_line (spans_t *sp, const unsigned char *s, size_t len)
 The spans of a JSON line.
static void csv_line (spans_t *sp, const unsigned char *s, size_t len, size_t line_no)
 The spans of a CSV line.
size_t text_hl_line (const char *line, size_t len, size_t line_no, text_hl_kind_t kind, const text_hl_words_t *words, text_hl_span_t *out, size_t cap)
 The spans of one line.
static bool same_ext (const char *s, size_t n, const char *ext)
 Whether the n bytes at s are ext without regard to case.
static size_t ext_at (const char *name, size_t n)
 Where the extension of the first n bytes of name starts.
text_hl_kind_t text_hl_kind_of (const char *name)
 How a file is classed, by its name: ".json" JSON, ".csv" CSV, ".txt" TEXT, in any case, a ".NEW" behind any of them taken off first; PLAIN for any other name.

Variables

const text_hl_unit_t text_hl_units [TEXT_HL_UNIT_COUNT]
 The units a number may carry, case as written: V mV A mA W rpm Hz us ms s /s deg Grad % mOhm B kB MB mΩ Ω; mOhm for the reports of 0.22 and older. test_text_hl holds it to every unit the report formats of ui_text.def put behind a number.
static const char *const k_css [TEXT_HL_CLASSES]
 The style sheet's name of each class, by class.

Detailed Description

Syntax highlighting of one line of text (text_hl.h): the rules of a bench text, JSON and CSV.

SPDX-License-Identifier: MIT

Macro Definition Documentation

◆ CLASS_CSS

#define CLASS_CSS ( name,
css,
role )
Value:
css,

Expands a row of TEXT_HL_CLASS_ROWS to its style sheet name.

Enumeration Type Documentation

◆ letter_t

enum letter_t

The case of a letter.

Enumerator
LETTER_NONE 

no letter

LETTER_UPPER 

A to Z, Ä Ö Ü

LETTER_LOWER 

a to z, ä ö ü ß

Function Documentation

◆ apart()

bool apart ( unsigned char e,
unsigned char nb )
static

Whether the edge byte e of a word stands alone against its neighbour nb: a word byte against no word byte, a mark against anything but the same mark.

Parameters
eThe word's first or last byte.
nbThe byte beside it in the line.
Returns
true when the word ends there.

◆ csv_line()

void csv_line ( spans_t * sp,
const unsigned char * s,
size_t len,
size_t line_no )
static

The spans of a CSV line.

Parameters
spThe spans.
sThe line.
lenIts bytes.
line_noIts index in the file: 0 is the header.

◆ emit()

void emit ( spans_t * sp,
size_t start,
size_t len,
text_hl_class_t cls )
static

Add bytes start to start + len as class cls, joined to the last span when it has that class.

Parameters
spThe spans.
startFirst byte; the end of the last span.
lenBytes; 0 adds nothing.
clsThe class.

◆ ext_at()

size_t ext_at ( const char * name,
size_t n )
static

Where the extension of the first n bytes of name starts.

Parameters
nameThe name.
nBytes to look at.
Returns
The offset of its last ".", or n without one.

◆ heading()

bool heading ( const unsigned char * s,
size_t len )
static

Whether a line is a heading: its first character an upper-case letter, at least HEADING_MIN characters, and no lower-case letter before the first "(".

Parameters
sThe line.
lenIts bytes.
Returns
true for a heading.

◆ hex_line()

bool hex_line ( const unsigned char * s,
size_t len )
static

Whether a line is a register line "NN: XX XX ...", a checksum line "checksum XXXXXXXX" or a journal line "key=hexdigits", trailing spaces allowed.

Parameters
sThe line.
lenIts bytes.
Returns
true for a hex line.

◆ is_blank()

bool is_blank ( unsigned char b)
static

Whether b is a space or a tab.

Parameters
bThe byte.
Returns
true for 0x20 and 0x09.

◆ is_digit()

bool is_digit ( unsigned char b)
static

Whether b is an ASCII digit.

Parameters
bThe byte.
Returns
true for 0 to 9.

◆ is_hex()

bool is_hex ( unsigned char b)
static

Whether b is a hexadecimal digit.

Parameters
bThe byte.
Returns
true for 0 to 9, A to F and a to f.

◆ is_word()

bool is_word ( unsigned char b)
static

Whether b belongs to a word: an ASCII letter or digit, "_", or any byte of a UTF-8 sequence.

Parameters
bThe byte.
Returns
true for a word byte.

◆ json_line()

void json_line ( spans_t * sp,
const unsigned char * s,
size_t len )
static

The spans of a JSON line.

Parameters
spThe spans.
sThe line.
lenIts bytes.

◆ json_number()

size_t json_number ( const unsigned char * s,
size_t len,
size_t i )
static

Bytes of a JSON number at i.

Parameters
sThe line.
lenIts bytes.
iWhere it would start.
Returns
Bytes; 0 when no number starts here.

◆ label_end()

size_t label_end ( const unsigned char * s,
size_t len )
static

Where a label ends: after the first ":" or "=" in the first TEXT_HL_LABEL_CELLS characters, when a blank or the line's end follows it and the line starts with no blank.

Parameters
sThe line.
lenIts bytes.
Returns
Bytes of the label with its mark; 0 for none.

◆ letter()

letter_t letter ( const unsigned char * s,
size_t n,
size_t * len )
static

The case of the letter at s: ASCII and the 7 German letters.

Parameters
sThe bytes.
nTheir count from s, at least 1.
lenOut: bytes of the character, 1 or 2.
Returns
The case; LETTER_NONE for any other character.

◆ number_at()

size_t number_at ( const unsigned char * line,
size_t len,
size_t i,
text_hl_class_t * cls )
static

A number at byte i, with the unit behind it: joined to it, as in "6.00V", or after one space, as the older reports write it.

A word joined to the number that is no unit of the table ("6.00Volt", "5x") makes the whole no number.

Parameters
lineThe line.
lenIts bytes.
iWhere the number would start.
clsOut: the unit's class; TEXT_HL_TEXT for a bare number.
Returns
Bytes of the number and its unit; 0 when no number starts here.

◆ rest()

void rest ( spans_t * sp,
const unsigned char * s,
size_t len,
size_t i,
const text_hl_words_t * words )
static

The spans of a bench text's line from byte i on: comments, verdict words and numbers with units.

Parameters
spThe spans.
sThe line.
lenIts bytes.
iWhere to start.
wordsThe verdict words; NULL for none.

◆ same_ext()

bool same_ext ( const char * s,
size_t n,
const char * ext )
static

Whether the n bytes at s are ext without regard to case.

Parameters
sThe bytes.
nTheir count.
extAn extension in lower case, with its dot.
Returns
true when they are the same.

◆ text_hl_class_css()

const char * text_hl_class_css ( text_hl_class_t cls)

The style sheet's name of a class (TEXT_HL_CLASS_ROWS).

Parameters
clsThe class.
Returns
Its short name; "tx" for a value out of range.

◆ text_hl_kind_of()

text_hl_kind_t text_hl_kind_of ( const char * name)

How a file is classed, by its name: ".json" JSON, ".csv" CSV, ".txt" TEXT, in any case, a ".NEW" behind any of them taken off first; PLAIN for any other name.

Parameters
nameThe file name; NULL is PLAIN.
Returns
The kind.

◆ text_hl_line()

size_t text_hl_line ( const char * line,
size_t len,
size_t line_no,
text_hl_kind_t kind,
const text_hl_words_t * words,
text_hl_span_t * out,
size_t cap )

The spans of one line.

With room for fewer spans than the line has, the last span the room holds is TEXT and reaches to the line's end.

Parameters
lineThe line's bytes, without its line end.
lenTheir count.
line_noThe line's index in the file, from 0.
kindHow the file is classed.
wordsThe verdict words; NULL for none.
outOut: the spans.
capRoom in out.
Returns
Spans written; 0 for an empty line, a NULL or cap 0.

◆ text_hl_unit_class()

text_hl_class_t text_hl_unit_class ( const char * unit,
size_t len )

The class of a unit of the table.

Parameters
unitThe unit's bytes.
lenTheir count.
Returns
TEXT_HL_VOLT, TEXT_HL_CURR, TEXT_HL_POWER, TEXT_HL_RATE or TEXT_HL_UNIT; TEXT_HL_TEXT for a word that is no unit.

◆ text_hl_word_core()

const char * text_hl_word_core ( const char * s,
size_t * len )

The word a writer's string gives: spaces taken off both ends, a "Label:" and the spaces after it taken off the front, then one pair of parentheses around it and a full stop behind it.

"Brown-out: not run" gives "not run", " (cut short)" "cut short", "Not run." "Not run".

Parameters
sThe string; NULL gives an empty word.
lenOut: bytes of the word.
Returns
Its first byte, inside s.

◆ text_line()

void text_line ( spans_t * sp,
const unsigned char * s,
size_t len,
size_t line_no,
const text_hl_words_t * words )
static

The spans of a bench text's line.

Parameters
spThe spans.
sThe line.
lenIts bytes.
line_noIts index in the file.
wordsThe verdict words; NULL for none.

◆ trim()

void trim ( const char * s,
size_t * a,
size_t * b )
static

Narrow [*a, *b) of s past the blanks at both ends.

Parameters
sThe string.
aIn and out: the first byte.
bIn and out: the byte after the last.

◆ unit_class_at()

text_hl_class_t unit_class_at ( const unsigned char * line,
size_t len,
size_t k,
size_t u )
static

The class of the unit from byte k to u, when it ends at no word byte.

Parameters
lineThe line.
lenIts bytes.
kThe unit's first byte.
uThe byte after its last (unit_end()).
Returns
The unit's class; TEXT_HL_TEXT for none, a word that is no unit, or one a word byte runs on from.

◆ unit_end()

size_t unit_end ( const unsigned char * line,
size_t len,
size_t k )
static

The end of a unit's bytes from byte k: a "%"; or a "/" and letters, or letters; the ohm sign counts as a letter.

Parameters
lineThe line.
lenIts bytes.
kWhere the unit would start.
Returns
The byte after the unit's last; k for none.

◆ word_at()

size_t word_at ( const unsigned char * line,
size_t len,
size_t i,
const char *const * list,
size_t n )
static

The longest verdict word of list at byte i of the line.

Parameters
lineThe line.
lenIts bytes.
iWhere the word would start.
listThe words.
nTheir count.
Returns
Bytes of the longest word that matches; 0 for none.

Variable Documentation

◆ k_css

const char* const k_css[TEXT_HL_CLASSES]
static
Initial value:
= {
}

The style sheet's name of each class, by class.

◆ text_hl_units

const text_hl_unit_t text_hl_units[TEXT_HL_UNIT_COUNT]
Initial value:
= {
{"V", TEXT_HL_VOLT}, {"mV", TEXT_HL_VOLT}, {"A", TEXT_HL_CURR},
{"mA", TEXT_HL_CURR}, {"W", TEXT_HL_POWER}, {"rpm", TEXT_HL_RATE},
{"Hz", TEXT_HL_RATE}, {"us", TEXT_HL_UNIT}, {"ms", TEXT_HL_UNIT},
{"s", TEXT_HL_UNIT}, {"/s", TEXT_HL_UNIT}, {"deg", TEXT_HL_UNIT},
{"Grad", TEXT_HL_UNIT}, {"%", TEXT_HL_UNIT}, {"mOhm", TEXT_HL_UNIT},
{"B", TEXT_HL_UNIT}, {"kB", TEXT_HL_UNIT}, {"MB", TEXT_HL_UNIT},
{"mΩ", TEXT_HL_UNIT}, {"Ω", TEXT_HL_UNIT},
}

The units a number may carry, case as written: V mV A mA W rpm Hz us ms s /s deg Grad % mOhm B kB MB mΩ Ω; mOhm for the reports of 0.22 and older. test_text_hl holds it to every unit the report formats of ui_text.def put behind a number.