From b2327abb138c6e650deba9638604c42c9078b53d Mon Sep 17 00:00:00 2001
From: terencehill <piuntn@gmail.com>
Date: Thu, 1 Sep 2016 15:09:31 +0200
Subject: [PATCH] Add IS_HEXDIGIT macro and make use of it

---
 qcsrc/common/util.qc              | 12 ++++++------
 qcsrc/lib/string.qh               |  4 +++-
 qcsrc/menu/xonotic/colorpicker.qc |  6 +++---
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/qcsrc/common/util.qc b/qcsrc/common/util.qc
index 1c1278bfd5..4afc151f32 100644
--- a/qcsrc/common/util.qc
+++ b/qcsrc/common/util.qc
@@ -703,9 +703,9 @@ int skipIncompleteTag(string theText, float pos, int len)
 	if(tag_start >= 0)
 	{
 		if(tag_start + 5 < len)
-		if(strstrofs("0123456789abcdefABCDEF", substring(theText, tag_start + 2, 1), 0) >= 0)
-		if(strstrofs("0123456789abcdefABCDEF", substring(theText, tag_start + 3, 1), 0) >= 0)
-		if(strstrofs("0123456789abcdefABCDEF", substring(theText, tag_start + 4, 1), 0) >= 0)
+		if(IS_HEXDIGIT(substring(theText, tag_start + 2, 1)))
+		if(IS_HEXDIGIT(substring(theText, tag_start + 3, 1)))
+		if(IS_HEXDIGIT(substring(theText, tag_start + 4, 1)))
 		{
 			if(!isCaretEscaped(theText, tag_start))
 				return 5 - (pos - tag_start); // ^xRGB color code found
@@ -803,9 +803,9 @@ string find_last_color_code(string s)
 
 			if(i+4 <= len)
 			if(substring(s, i+1, 1) == "x")
-			if(strstrofs("0123456789abcdefABCDEF", substring(s, i+2, 1), 0) >= 0)
-			if(strstrofs("0123456789abcdefABCDEF", substring(s, i+3, 1), 0) >= 0)
-			if(strstrofs("0123456789abcdefABCDEF", substring(s, i+4, 1), 0) >= 0)
+			if(IS_HEXDIGIT(substring(s, i + 2, 1)))
+			if(IS_HEXDIGIT(substring(s, i + 3, 1)))
+			if(IS_HEXDIGIT(substring(s, i + 4, 1)))
 				return substring(s, i, 5);
 		}
 		i -= carets; // this also skips one char before the carets
diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh
index 2ade296912..3207296a7d 100644
--- a/qcsrc/lib/string.qh
+++ b/qcsrc/lib/string.qh
@@ -339,7 +339,9 @@ int vercmp(string v1, string v2)
 	return vercmp_recursive(v1, v2);
 }
 
+const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
 const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
-#define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
+#define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
+#define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
diff --git a/qcsrc/menu/xonotic/colorpicker.qc b/qcsrc/menu/xonotic/colorpicker.qc
index b88d5d9a7d..b1e231c8bf 100644
--- a/qcsrc/menu/xonotic/colorpicker.qc
+++ b/qcsrc/menu/xonotic/colorpicker.qc
@@ -87,9 +87,9 @@ float XonoticColorpicker_mouseDrag(entity me, vector coords)
 				while (i - 5 - carets >= 0 && substring(me.controlledTextbox.text, i - 5 - carets, 1) == "^")
 					++carets;
 				if (carets & 1)
-					if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-3, 1), 0) >= 0)
-						if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-2, 1), 0) >= 0)
-							if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
+					if(IS_HEXDIGIT(substring(me.controlledTextbox.text, i - 3, 1)))
+						if(IS_HEXDIGIT(substring(me.controlledTextbox.text, i - 2, 1)))
+							if(IS_HEXDIGIT(substring(me.controlledTextbox.text, i - 1, 1)))
 							{
 								me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
 								me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
-- 
2.39.5