Skip to content

Commit

Permalink
Cleanup internal API
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Jan 9, 2025
1 parent 58fb1b0 commit cae655e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/cli/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default_envs =

[env]
lib_deps =
libcli@1.3.0
libcli@1.4.0

[env:promicro16]
platform = atmelavr
Expand Down
12 changes: 6 additions & 6 deletions src/libcli/libcli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,28 @@ void Cli::readLine(
}

void Cli::readHex(NumberCallback callback, uintptr_t context, uint32_t limit) {
_impl.setCallback(callback, context, limit, 16);
_impl.setCallback(callback, context, 16, limit);
}

void Cli::readHex(NumberCallback callback, uintptr_t context, uint32_t limit, uint32_t defval) {
_impl.setCallback(callback, context, limit, defval, 16);
_impl.setCallback(callback, context, 16, limit, defval);
}

void Cli::readDec(NumberCallback callback, uintptr_t context, uint32_t limit) {
_impl.setCallback(callback, context, limit, 10);
_impl.setCallback(callback, context, 10, limit);
}

void Cli::readDec(NumberCallback callback, uintptr_t context, uint32_t limit, uint32_t defval) {
_impl.setCallback(callback, context, limit, defval, 10);
_impl.setCallback(callback, context, 10, limit, defval);
}

void Cli::readNum(NumberCallback callback, uintptr_t context, uint8_t radix, uint32_t limit) {
_impl.setCallback(callback, context, limit, radix);
_impl.setCallback(callback, context, radix, limit);
}

void Cli::readNum(NumberCallback callback, uintptr_t context, uint8_t radix, uint32_t limit,
uint32_t defval) {
_impl.setCallback(callback, context, limit, defval, radix);
_impl.setCallback(callback, context, radix, limit, defval);
}

} // namespace libcli
Expand Down
43 changes: 22 additions & 21 deletions src/libcli/libcli_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ bool isNewline(char c) {
}

/** Returns number of digits of |number| in |radix|. */
int8_t getDigits(uint32_t number, uint8_t radix) {
int8_t n = 0;
int_fast8_t getDigits(uint32_t number, uint_fast8_t radix) {
int_fast8_t n = 0;
do {
n++;
number /= radix;
Expand All @@ -47,53 +47,53 @@ int8_t getDigits(uint32_t number, uint8_t radix) {

} // namespace

size_t Impl::pad_left(int8_t len, int8_t width, char pad) {
size_t Impl::pad_left(int_fast8_t len, int_fast8_t width, char pad) {
size_t size = 0;
for (auto n = width - len; n > 0; n--)
size += console->print(pad);
return size;
}

size_t Impl::pad_right(int8_t len, int8_t width, char pad) {
size_t Impl::pad_right(int_fast8_t len, int_fast8_t width, char pad) {
size_t size = 0;
for (auto n = width + len; n < 0; n++)
size += console->print(pad);
return size;
}

size_t Impl::printNum(uint32_t number, int8_t width, uint8_t radix, bool newline) {
size_t Impl::printNum(uint32_t number, int_fast8_t width, uint_fast8_t radix, bool newline) {
const auto len = getDigits(number, radix);
size_t size = pad_left(len, width, radix == 10 ? ' ' : '0');
auto size = pad_left(len, width, radix == 10 ? ' ' : '0');
size += console->print(number, radix);
size += pad_right(len, width, ' ');
if (newline)
size += console->println();
return size;
}

size_t Impl::printStr(const __FlashStringHelper *text, int8_t width, bool newline) {
size_t Impl::printStr(const __FlashStringHelper *text, int_fast8_t width, bool newline) {
const auto l = strlen_P(reinterpret_cast<const char *>(text));
const int8_t len = (l < INT8_MAX) ? l : INT8_MAX;
size_t size = pad_left(len, width, ' ');
const auto len = (l < INT8_MAX) ? l : INT8_MAX;
auto size = pad_left(len, width, ' ');
size += console->print(text);
size += pad_right(len, width, ' ');
if (newline)
size += console->println();
return size;
}

size_t Impl::printStr(const char *text, int8_t width, bool newline) {
size_t Impl::printStr(const char *text, int_fast8_t width, bool newline) {
const auto l = strlen(text);
const int8_t len = (l < INT8_MAX) ? l : INT8_MAX;
size_t size = pad_left(len, width, ' ');
const auto len = (l < INT8_MAX) ? l : INT8_MAX;
auto size = pad_left(len, width, ' ');
size += console->print(text);
size += pad_right(len, width, ' ');
if (newline)
size += console->println();
return size;
}

size_t Impl::backspace(int8_t n) {
size_t Impl::backspace(int_fast8_t n) {
size_t s = 0;
while (n--)
s += console->print(F("\b \b"));
Expand Down Expand Up @@ -149,42 +149,43 @@ void Impl::processString(char c) {
}
}

void Impl::setCallback(NumberCallback callback, uintptr_t context, uint32_t limit, uint8_t radix) {
void Impl::setCallback(
NumberCallback callback, uintptr_t context, uint_fast8_t radix, uint32_t limit) {
this->callback.number = callback;
num_width = getDigits(num_limit = limit, num_radix = radix);
num_value = 0;
num_len = 0;
setProcessor(&Impl::processNumber, context);
}

void Impl::setCallback(NumberCallback callback, uintptr_t context, uint32_t limit, uint32_t defval,
uint8_t radix) {
void Impl::setCallback(NumberCallback callback, uintptr_t context, uint_fast8_t radix,
uint32_t limit, uint32_t defval) {
setCallback(callback, context, limit, radix);
backspace(num_width);
num_value = defval;
num_len = num_width;
printNum(num_value, num_len, radix, false);
}

bool Impl::checkLimit(char c, uint8_t &n) const {
bool Impl::checkLimit(char c, uint_fast8_t &n) const {
if (num_len >= num_width)
return false;
c = toUpperCase(c);
if (num_radix <= 10 && isDigit(c) && c < num_radix + '0') {
if (num_radix <= 10 && c >= '0' && c < num_radix + '0') {
n = c - '0';
const uint32_t limit = num_limit / num_radix;
const auto limit = num_limit / num_radix;
return num_value < limit || (num_value == limit && n <= (num_limit % num_radix));
}
if (num_radix == 16 && isHexadecimalDigit(c)) {
n = isDigit(c) ? c - '0' : c - 'A' + 10;
const uint32_t limit = num_limit / 16;
const auto limit = num_limit / 16;
return num_value < limit || (num_value == limit && n <= (num_limit % 16));
}
return false;
}

void Impl::processNumber(char c) {
uint8_t n;
uint_fast8_t n;
if (checkLimit(c, n)) {
num_value *= num_radix;
num_value += n;
Expand Down
21 changes: 10 additions & 11 deletions src/libcli/libcli_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,16 @@ struct Impl final {
void setCallback(LetterCallback callback, uintptr_t context);
void setCallback(StringCallback callback, uintptr_t context, char *buffer, size_t size,
bool hasDefval, bool word);
void setCallback(NumberCallback callback, uintptr_t context, uint32_t limit, uint8_t radix);
void setCallback(NumberCallback callback, uintptr_t context, uint32_t limit, uint32_t defval,
uint8_t radix);
void setCallback(NumberCallback callback, uintptr_t context, uint_fast8_t radix, uint32_t limit);
void setCallback(NumberCallback callback, uintptr_t context, uint_fast8_t radix, uint32_t limit, uint32_t defval);

size_t backspace(int8_t n);
size_t printNum(uint32_t number, int8_t width, uint8_t radix, bool newline);
size_t printStr(const __FlashStringHelper *str, int8_t width, bool newline);
size_t printStr(const char *str, int8_t width, bool newline);
size_t backspace(int_fast8_t n);
size_t printNum(uint32_t number, int_fast8_t width, uint_fast8_t radix, bool newline);
size_t printStr(const __FlashStringHelper *str, int_fast8_t width, bool newline);
size_t printStr(const char *str, int_fast8_t width, bool newline);

/** Delegate methods for Print. */
size_t write(uint8_t val) { return console->write(val); }
size_t write(uint_fast8_t val) { return console->write(val); }
size_t write(const uint8_t *buf, size_t size) { return console->write(buf, size); }
int availableForWrite() { return console->availableForWrite(); }

Expand Down Expand Up @@ -93,9 +92,9 @@ struct Impl final {
void processLetter(char c);
void processString(char c);
void processNumber(char c);
bool checkLimit(char c, uint8_t &n) const;
size_t pad_left(int8_t len, int8_t width, char pad);
size_t pad_right(int8_t len, int8_t width, char pad);
bool checkLimit(char c, uint_fast8_t &n) const;
size_t pad_left(int_fast8_t len, int_fast8_t width, char pad);
size_t pad_right(int_fast8_t len, int_fast8_t width, char pad);

/** No copy constructor. */
Impl(Impl const &) = delete;
Expand Down

0 comments on commit cae655e

Please sign in to comment.