diff --git a/src/analyze.c b/src/analyze.c index d696b8130..6de1d2874 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -14,6 +14,7 @@ #include "include/util.h" #include "include/util/hb_array.h" #include "include/util/hb_string.h" +#include "include/util/string.h" #include "include/visitor.h" #include @@ -60,8 +61,8 @@ static bool analyze_erb_content(const AST_NODE_T* node, void* data) { const char* opening = erb_content_node->tag_opening->value; - if (strcmp(opening, "<%%") != 0 && strcmp(opening, "<%%=") != 0 && strcmp(opening, "<%#") != 0 - && strcmp(opening, "<%graphql") != 0) { + if (!string_equals(opening, "<%%") && !string_equals(opening, "<%%=") && !string_equals(opening, "<%#") + && !string_equals(opening, "<%graphql")) { analyzed_ruby_T* analyzed = herb_analyze_ruby(hb_string(erb_content_node->content->value)); erb_content_node->parsed = true; diff --git a/src/analyze_helpers.c b/src/analyze_helpers.c index 12aea2f7a..3f8ca0977 100644 --- a/src/analyze_helpers.c +++ b/src/analyze_helpers.c @@ -3,6 +3,7 @@ #include #include "include/analyzed_ruby.h" +#include "include/util/string.h" bool has_if_node(analyzed_ruby_T* analyzed) { return analyzed->if_node_count > 0; @@ -83,7 +84,7 @@ bool has_then_keyword(analyzed_ruby_T* analyzed) { bool has_error_message(analyzed_ruby_T* anlayzed, const char* message) { for (const pm_diagnostic_t* error = (const pm_diagnostic_t*) anlayzed->parser.error_list.head; error != NULL; error = (const pm_diagnostic_t*) error->node.next) { - if (strcmp(error->message, message) == 0) { return true; } + if (string_equals(error->message, message)) { return true; } } return false; diff --git a/src/extract.c b/src/extract.c index 110cebac8..d2c6cc9de 100644 --- a/src/extract.c +++ b/src/extract.c @@ -3,6 +3,7 @@ #include "include/lexer.h" #include "include/util/hb_array.h" #include "include/util/hb_buffer.h" +#include "include/util/string.h" #include #include @@ -22,11 +23,11 @@ void herb_extract_ruby_to_buffer(const char* source, hb_buffer_T* output) { } case TOKEN_ERB_START: { - if (strcmp(token->value, "<%#") == 0) { + if (string_equals(token->value, "<%#")) { skip_erb_content = true; is_comment_tag = true; - } else if (strcmp(token->value, "<%%") == 0 || strcmp(token->value, "<%%=") == 0 - || strcmp(token->value, "<%graphql") == 0) { + } else if (string_equals(token->value, "<%%") || string_equals(token->value, "<%%=") + || string_equals(token->value, "<%graphql")) { skip_erb_content = true; is_comment_tag = false; } else { diff --git a/src/include/util/string.h b/src/include/util/string.h new file mode 100644 index 000000000..ce6824fad --- /dev/null +++ b/src/include/util/string.h @@ -0,0 +1,11 @@ +#ifndef HERB_UTILS_STRING_H +#define HERB_UTILS_STRING_H + +#include +#include + +static inline bool string_equals(const char* a, const char* b) { + return strcmp(a, b) == 0; +} + +#endif diff --git a/src/main.c b/src/main.c index 234c55ee3..e52f1c762 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include "include/io.h" #include "include/ruby_parser.h" #include "include/util/hb_buffer.h" +#include "include/util/string.h" #include #include @@ -61,7 +62,7 @@ int main(const int argc, char* argv[]) { struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); - if (strcmp(argv[1], "lex") == 0) { + if (string_equals(argv[1], "lex")) { herb_lex_to_buffer(source, &output); clock_gettime(CLOCK_MONOTONIC, &end); @@ -74,7 +75,7 @@ int main(const int argc, char* argv[]) { return 0; } - if (strcmp(argv[1], "parse") == 0) { + if (string_equals(argv[1], "parse")) { AST_DOCUMENT_NODE_T* root = herb_parse(source, NULL); herb_analyze_parse_tree(root, source); @@ -82,7 +83,7 @@ int main(const int argc, char* argv[]) { clock_gettime(CLOCK_MONOTONIC, &end); int silent = 0; - if (argc > 3 && strcmp(argv[3], "--silent") == 0) { silent = 1; } + if (argc > 3 && string_equals(argv[3], "--silent")) { silent = 1; } if (!silent) { ast_pretty_print_node((AST_NODE_T*) root, 0, 0, &output); @@ -98,7 +99,7 @@ int main(const int argc, char* argv[]) { return 0; } - if (strcmp(argv[1], "ruby") == 0) { + if (string_equals(argv[1], "ruby")) { herb_extract_ruby_to_buffer(source, &output); clock_gettime(CLOCK_MONOTONIC, &end); @@ -111,7 +112,7 @@ int main(const int argc, char* argv[]) { return 0; } - if (strcmp(argv[1], "html") == 0) { + if (string_equals(argv[1], "html")) { herb_extract_html_to_buffer(source, &output); clock_gettime(CLOCK_MONOTONIC, &end); @@ -124,7 +125,7 @@ int main(const int argc, char* argv[]) { return 0; } - if (strcmp(argv[1], "prism") == 0) { + if (string_equals(argv[1], "prism")) { printf("HTML+ERB File: \n%s\n", source); char* ruby_source = herb_extract(source, HERB_EXTRACT_LANGUAGE_RUBY); diff --git a/src/parser.c b/src/parser.c index 5131c0639..9000bab01 100644 --- a/src/parser.c +++ b/src/parser.c @@ -12,6 +12,7 @@ #include "include/util/hb_array.h" #include "include/util/hb_buffer.h" #include "include/util/hb_string.h" +#include "include/util/string.h" #include "include/visitor.h" #include @@ -346,7 +347,7 @@ static AST_HTML_ATTRIBUTE_VALUE_NODE_T* parser_parse_quoted_html_attribute_value while (!token_is(parser, TOKEN_EOF) && !( token_is(parser, TOKEN_QUOTE) && opening_quote != NULL - && strcmp(parser->current_token->value, opening_quote->value) == 0 + && string_equals(parser->current_token->value, opening_quote->value) )) { if (token_is(parser, TOKEN_ERB_START)) { parser_append_literal_node_from_buffer(parser, &buffer, children, start); @@ -364,7 +365,7 @@ static AST_HTML_ATTRIBUTE_VALUE_NODE_T* parser_parse_quoted_html_attribute_value token_T* next_token = lexer_next_token(parser->lexer); if (next_token && next_token->type == TOKEN_QUOTE && opening_quote != NULL - && strcmp(next_token->value, opening_quote->value) == 0) { + && string_equals(next_token->value, opening_quote->value)) { hb_buffer_append(&buffer, parser->current_token->value); hb_buffer_append(&buffer, next_token->value); @@ -387,7 +388,7 @@ static AST_HTML_ATTRIBUTE_VALUE_NODE_T* parser_parse_quoted_html_attribute_value } if (token_is(parser, TOKEN_QUOTE) && opening_quote != NULL - && strcmp(parser->current_token->value, opening_quote->value) == 0) { + && string_equals(parser->current_token->value, opening_quote->value)) { lexer_state_snapshot_T saved_state = lexer_save_state(parser->lexer); token_T* potential_closing = parser->current_token; @@ -415,7 +416,7 @@ static AST_HTML_ATTRIBUTE_VALUE_NODE_T* parser_parse_quoted_html_attribute_value while (!token_is(parser, TOKEN_EOF) && !( token_is(parser, TOKEN_QUOTE) && opening_quote != NULL - && strcmp(parser->current_token->value, opening_quote->value) == 0 + && string_equals(parser->current_token->value, opening_quote->value) )) { if (token_is(parser, TOKEN_ERB_START)) { parser_append_literal_node_from_buffer(parser, &buffer, children, start); @@ -445,7 +446,7 @@ static AST_HTML_ATTRIBUTE_VALUE_NODE_T* parser_parse_quoted_html_attribute_value token_T* closing_quote = parser_consume_expected(parser, TOKEN_QUOTE, errors); - if (opening_quote != NULL && closing_quote != NULL && strcmp(opening_quote->value, closing_quote->value) != 0) { + if (opening_quote != NULL && closing_quote != NULL && !string_equals(opening_quote->value, closing_quote->value)) { append_quotes_mismatch_error( opening_quote, closing_quote,