Closed ssh352 closed 7 months ago
@jalvesaq would it work if the code required numbers to be either the first character or preceded by a whitespace character?
Each new condition added to the code makes it slower. But we can try the branch https://github.com/jalvesaq/colorout/tree/num_begin The result is not perfect: "64" in "x86_64" will still be highlighted as a number.
Here's a patch for what I came up with. It requires a whitespace character before a digit in order to format the output as a number.
diff --git a/src/colorout.c b/src/colorout.c
index eae9be5..90b80d8 100644
--- a/src/colorout.c
+++ b/src/colorout.c
@@ -76,8 +76,17 @@ static int isword(const char * b, int i, int len)
return !is_letter_preceeding && !is_letter_following;
}
+static int iswhitespace(const char b)
+{
+ /* space or horizontal tab, new line, vertical tab, form feed, carriage return */
+ return b == 32 || (b >= 9 && b <= 13);
+}
+
static int isnumber(const char * b, int i, int len)
{
+ if(i > 0 && !iswhitespace(b[i-1]))
+ return 0;
+
int l = len;
if(l > (i + 5))
l = i + 5;
Here's the result:
Much simpler! I will wait a few days for other people's comments before making the code changes. Since the condition you are proposing is much simpler, I think it will be OK to put it directly at the beginning of isnumber()
.
@joshuaulrich, I update the num_begin
branch to your code.
I think it will be OK to put it directly at the beginning of
isnumber()
I agree. I created the function before I implemented it and knew what the conditions would be. It makes sense to avoid the function call and include the conditions directly, especially since iswhitespace()
would only be called once.
Actually, I tried both ways. I didn't see any difference in speed (possibly, the compiler optimization eliminates the function call), and the code becomes easier to understand with the iswhitespace()
function.
in the commit-hash and host for example. Thanks!