shithub: cstory

Download patch

ref: b0974c3def80bf7cec6dffcbc4be7653eb03cecb
parent: 4763aecdbdbb6802d2b30ea93f62377dc7dd3f1f
author: Clownacy <Clownacy@users.noreply.github.com>
date: Thu Sep 17 22:03:05 EDT 2020

Binary-search optimisations and fixes

--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -1073,20 +1073,12 @@
 	size_t left = 0;
 	size_t right = font->total_local_glyphs;
 
-	while (right - left >= 2)
+	for (;;)
 	{
-		size_t index = left + (right - left) / 2;
+		size_t index = (left + right) / 2;
 
-		if (font->local_glyphs[index].unicode_value < unicode_value)
+		if (font->local_glyphs[index].unicode_value == unicode_value)
 		{
-			left = index;
-		}
-		else if (font->local_glyphs[index].unicode_value > unicode_value)
-		{
-			right = index;
-		}
-		else
-		{
 			const Glyph *local_glyph = &font->local_glyphs[index];
 
 			glyph->unicode_value = local_glyph->unicode_value;
@@ -1104,6 +1096,14 @@
 
 			return glyph;
 		}
+
+		if (index == left)
+			break;
+
+		if (font->local_glyphs[index].unicode_value < unicode_value)
+			left = index;
+		else //if (font->local_glyphs[index].unicode_value > unicode_value)
+			right = index;
 	}
 #endif