aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2024-06-06 22:31:35 -0400
committerBen Winston2024-06-06 22:31:35 -0400
commit624ba368dd919e1b75e30866836a7b3ed5907a57 (patch)
tree4bb6a0c4809412df965628ec0729a5e0385bd36d
parent5bfbde81501a2060b92b061d651d8337ae5fd64b (diff)
refactor for less looping
-rw-r--r--booki.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/booki.c b/booki.c
index 2bd30bf..6840849 100644
--- a/booki.c
+++ b/booki.c
@@ -634,19 +634,26 @@ char* next_book(char* current_pos) {
char* characters_from_end(char* str, int len, int number_of_chars) {
assert(len >= number_of_chars);
- int retchars = number_of_chars;
- char* newptr = str + len - 1;
+
+ // 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;
- int total_bytes = 0;
- while (number_of_chars > 0) {
+ do {
+ // update the pointer, get the character
+ newptr--;
+ len--;
ch = (unsigned char) *newptr;
- // if we're at the first byte of a unicode point, adjust
+
+ // 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--;
- newptr--;
- total_bytes++;
- }
- return newptr + 1;
+ } while (number_of_chars > 0 && len > 0);
+
+ return newptr;
}
/*** search ***/