diff options
| author | Ben Winston | 2024-06-06 22:34:59 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-06-06 22:34:59 -0400 |
| commit | cbcd1fbb716664be4d784d7e8b359e342cb073c9 (patch) | |
| tree | d67689b0d6637723fb7712be22cdde11e11e8872 | |
| parent | 624ba368dd919e1b75e30866836a7b3ed5907a57 (diff) | |
refactor for clarity
| -rw-r--r-- | booki.c | 47 |
1 files changed, 24 insertions, 23 deletions
@@ -14,6 +14,30 @@ #define MAX_SEARCH_OPTS 5 /*** helpers ***/ +char* characters_from_end(char* str, int len_in_bytes, int number_of_chars) { + assert(len_in_bytes >= number_of_chars); + + // let's not zoom past the beginning of the string + if (number_of_chars == 0 || len_in_bytes == 0) + return str; + + // start at the end of the string, locate all code points + char* newptr = str + len_in_bytes; // will update to last character in do..while + unsigned char ch; + do { + // update the pointer, get the character + newptr--; + len_in_bytes--; + ch = (unsigned char) *newptr; + + // if we're at the first byte of a unicode point, we've found a whole character + if (ch < 0x80 || ch >= 0xC3) + number_of_chars--; + } while (number_of_chars > 0 && len_in_bytes > 0); + + return newptr; +} + bool comparable(const char* pattern, const char* candidate, int len) { unsigned char p = *pattern; unsigned char c = *candidate; @@ -632,29 +656,6 @@ char* next_book(char* current_pos) { return current_pos; } -char* characters_from_end(char* str, int len, int number_of_chars) { - assert(len >= number_of_chars); - - // let's not zoom past the beginning of the string - if (number_of_chars == 0) - return str; - - // start at the end of the string, locate all code points - char* newptr = str + len; // will update to last character in do..while - unsigned char ch; - do { - // update the pointer, get the character - newptr--; - len--; - ch = (unsigned char) *newptr; - - // if we're at the first byte of a unicode point, we've found a whole character - if (ch < 0x80 || ch >= 0xC3) - number_of_chars--; - } while (number_of_chars > 0 && len > 0); - - return newptr; -} /*** search ***/ bool match_string(const char* pattern, const struct es text) { |
