aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2024-06-06 22:34:59 -0400
committerBen Winston2024-06-06 22:34:59 -0400
commitcbcd1fbb716664be4d784d7e8b359e342cb073c9 (patch)
treed67689b0d6637723fb7712be22cdde11e11e8872
parent624ba368dd919e1b75e30866836a7b3ed5907a57 (diff)
refactor for clarity
-rw-r--r--booki.c47
1 files changed, 24 insertions, 23 deletions
diff --git a/booki.c b/booki.c
index 6840849..a199701 100644
--- a/booki.c
+++ b/booki.c
@@ -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) {