From d82102dad1b6993c0ed09f80cdb6cb444740161b Mon Sep 17 00:00:00 2001 From: rhl120 <rhl120@protonmail.com> Date: Sun, 12 Feb 2023 02:18:32 +0100 Subject: [PATCH 1/3] Simplify the lexer_starts_with function --- src/lexer.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/lexer.c b/src/lexer.c index cfd9022c..09f4ff5b 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -77,18 +77,9 @@ Lexer lexer_new(Free_Glyph_Atlas *atlas, const char *content, size_t content_len bool lexer_starts_with(Lexer *l, const char *prefix) { size_t prefix_len = strlen(prefix); - if (prefix_len == 0) { - return true; - } - if (l->cursor + prefix_len - 1 >= l->content_len) { - return false; - } - for (size_t i = 0; i < prefix_len; ++i) { - if (prefix[i] != l->content[l->cursor + i]) { - return false; - } - } - return true; + return prefix_len == 0 || ( + l->cursor + prefix_len - 1 < l->content_len && + !strncmp(prefix, l->content + l->cursor, prefix_len)); } void lexer_chop_char(Lexer *l, size_t len) From 9af636bc0fac1e8e955d4c19d005e6e2e86f2862 Mon Sep 17 00:00:00 2001 From: rhl120 <rhl120@protonmail.com> Date: Sun, 12 Feb 2023 10:47:39 +0100 Subject: [PATCH 2/3] No need to check if the preifix is empty in has_prefix --- src/lexer.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lexer.c b/src/lexer.c index 09f4ff5b..c3880365 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -77,9 +77,8 @@ Lexer lexer_new(Free_Glyph_Atlas *atlas, const char *content, size_t content_len bool lexer_starts_with(Lexer *l, const char *prefix) { size_t prefix_len = strlen(prefix); - return prefix_len == 0 || ( - l->cursor + prefix_len - 1 < l->content_len && - !strncmp(prefix, l->content + l->cursor, prefix_len)); + return l->cursor + prefix_len - 1 < l->content_len && + !strncmp(prefix, l->content + l->cursor, prefix_len); } void lexer_chop_char(Lexer *l, size_t len) From d24b15061c017520f5ac23ffa019aa747b794929 Mon Sep 17 00:00:00 2001 From: rhl120 <rhl120@protonmail.com> Date: Sun, 12 Feb 2023 10:59:14 +0100 Subject: [PATCH 3/3] Revert "No need to check if the preifix is empty in has_prefix" This reverts commit 9af636bc0fac1e8e955d4c19d005e6e2e86f2862. --- src/lexer.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lexer.c b/src/lexer.c index c3880365..09f4ff5b 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -77,8 +77,9 @@ Lexer lexer_new(Free_Glyph_Atlas *atlas, const char *content, size_t content_len bool lexer_starts_with(Lexer *l, const char *prefix) { size_t prefix_len = strlen(prefix); - return l->cursor + prefix_len - 1 < l->content_len && - !strncmp(prefix, l->content + l->cursor, prefix_len); + return prefix_len == 0 || ( + l->cursor + prefix_len - 1 < l->content_len && + !strncmp(prefix, l->content + l->cursor, prefix_len)); } void lexer_chop_char(Lexer *l, size_t len)