From b626c1f575e2c8fd1345ef264795d2a6f5f2af4f Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 3 Oct 2024 11:06:39 -0700
Subject: [PATCH] Added a quick speedup to TTF_GetTextSubString() for ASCII
text
---
src/SDL_ttf.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/SDL_ttf.c b/src/SDL_ttf.c
index 12ff7d6e..549a2bb9 100644
--- a/src/SDL_ttf.c
+++ b/src/SDL_ttf.c
@@ -4206,11 +4206,17 @@ bool SDLCALL TTF_GetTextSubString(TTF_Text *text, int offset, TTF_SubString *sub
return true;
}
- // Do a binary search to find the cluster
+ // Make a quick guess that works for ASCII text with no line breaks
int num_clusters = text->internal->num_clusters;
const TTF_SubString *clusters = text->internal->clusters;
- const TTF_SubString *closest = NULL;
const TTF_SubString *cluster = NULL;
+ if (offset < num_clusters && clusters[offset].offset == offset) {
+ SDL_copyp(substring, &clusters[offset]);
+ return true;
+ }
+
+ // Do a binary search to find the cluster
+ const TTF_SubString *closest = NULL;
int low = 0;
int high = num_clusters - 1;
while (low <= high) {