From 9616e95d865a746c83ea1233aac74a0ac281cd6b Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Mon, 17 Jun 2019 19:56:53 +0100 Subject: [PATCH] Minor fixes / enhancements 1. Initialize static data structures correctly. 2. Close file on read error. 3. Less string creation in path handling routines. --- src/color_maps.cpp | 16 ++++++++-------- src/color_maps.hpp | 8 -------- src/context.cpp | 20 +++++++------------- src/file.cpp | 5 +++-- src/fn_miscs.cpp | 23 ++++++++--------------- src/sass_context.hpp | 2 +- 6 files changed, 27 insertions(+), 47 deletions(-) diff --git a/src/color_maps.cpp b/src/color_maps.cpp index a3de078be..8d147329c 100644 --- a/src/color_maps.cpp +++ b/src/color_maps.cpp @@ -313,7 +313,7 @@ namespace Sass { const Color_RGBA transparent(color_table, 0, 0, 0, 0); } - const std::map colors_to_names { + static const auto* const colors_to_names = new std::unordered_map { { 240 * 0x10000 + 248 * 0x100 + 255, ColorNames::aliceblue }, { 250 * 0x10000 + 235 * 0x100 + 215, ColorNames::antiquewhite }, { 0 * 0x10000 + 255 * 0x100 + 255, ColorNames::cyan }, @@ -455,7 +455,7 @@ namespace Sass { { 102 * 0x10000 + 51 * 0x100 + 153, ColorNames::rebeccapurple } }; - const std::map names_to_colors + static const auto *const names_to_colors = new std::unordered_map { { ColorNames::aliceblue, &Colors::aliceblue }, { ColorNames::antiquewhite, &Colors::antiquewhite }, @@ -619,20 +619,20 @@ namespace Sass { std::string lower{key}; std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); - auto p = names_to_colors.find(lower.c_str()); - if (p != names_to_colors.end()) { + auto p = names_to_colors->find(lower); + if (p != names_to_colors->end()) { return p->second; } - return 0; + return nullptr; } const char* color_to_name(const int key) { - auto p = colors_to_names.find(key); - if (p != colors_to_names.end()) { + auto p = colors_to_names->find(key); + if (p != colors_to_names->end()) { return p->second; } - return 0; + return nullptr; } const char* color_to_name(const double key) diff --git a/src/color_maps.hpp b/src/color_maps.hpp index 9ddc4cff7..2bc4ba5d2 100644 --- a/src/color_maps.hpp +++ b/src/color_maps.hpp @@ -7,14 +7,6 @@ namespace Sass { - struct map_cmp_str - { - bool operator()(char const *a, char const *b) const - { - return std::strcmp(a, b) < 0; - } - }; - namespace ColorNames { extern const char aliceblue[]; diff --git a/src/context.cpp b/src/context.cpp index df5f4a34d..2aa093397 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -28,23 +28,17 @@ namespace Sass { static std::string safe_input(const char* in_path) { - // enforce some safe defaults - // used to create relative file links - std::string safe_path(in_path ? in_path : ""); - return safe_path == "" ? "stdin" : safe_path; + if (in_path == nullptr || in_path[0] == '\0') return "stdin"; + return in_path; } - static std::string safe_output(const char* out_path, const std::string& input_path = "") + static std::string safe_output(const char* out_path, std::string input_path) { - std::string safe_path(out_path ? out_path : ""); - // maybe we can extract an output path from input path - if (safe_path == "" && input_path != "") { - int lastindex = static_cast(input_path.find_last_of(".")); - return (lastindex > -1 ? input_path.substr(0, lastindex) : input_path) + ".css"; + if (out_path == nullptr || out_path[0] == '\0') { + if (input_path.empty()) return "stdout"; + return input_path.substr(0, input_path.find_last_of(".")) + ".css"; } - // enforce some safe defaults - // used to create relative file links - return safe_path == "" ? "stdout" : safe_path; + return out_path; } Context::Context(struct Sass_Context& c_ctx) diff --git a/src/file.cpp b/src/file.cpp index b7084dbc8..3ccdf3a66 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -173,8 +173,8 @@ namespace Sass { while((pos = path.find("/./", pos)) != std::string::npos) path.erase(pos, 2); // remove all leading and trailing self references - while(path.length() > 1 && path.substr(0, 2) == "./") path.erase(0, 2); - while((pos = path.length()) > 1 && path.substr(pos - 2) == "/.") path.erase(pos - 2); + while(path.size() >= 2 && path[0] == '.' && path[1] == '/') path.erase(0, 2); + while((pos = path.length()) > 1 && path[pos - 2] == '/' && path[pos - 1] == '.') path.erase(pos - 2); size_t proto = 0; @@ -474,6 +474,7 @@ namespace Sass { char* contents = static_cast(malloc(st.st_size + 2 * sizeof(char))); if (std::fread(static_cast(contents), 1, size, fd) != size) { free(contents); + std::fclose(fd); return nullptr; } if (std::fclose(fd) != 0) { diff --git a/src/fn_miscs.cpp b/src/fn_miscs.cpp index fb188109b..cb2df5ded 100644 --- a/src/fn_miscs.cpp +++ b/src/fn_miscs.cpp @@ -8,15 +8,6 @@ namespace Sass { namespace Functions { - // features - static std::set features { - "global-variable-shadowing", - "extend-selector-pseudoclass", - "at-error", - "units-level-3", - "custom-property" - }; - ////////////////////////// // INTROSPECTION FUNCTIONS ////////////////////////// @@ -90,12 +81,14 @@ namespace Sass { { std::string s = unquote(ARG("$name", String_Constant)->value()); - if(features.find(s) == features.end()) { - return SASS_MEMORY_NEW(Boolean, pstate, false); - } - else { - return SASS_MEMORY_NEW(Boolean, pstate, true); - } + static const auto *const features = new std::unordered_set { + "global-variable-shadowing", + "extend-selector-pseudoclass", + "at-error", + "units-level-3", + "custom-property" + }; + return SASS_MEMORY_NEW(Boolean, pstate, features->find(s) != features->end()); } Signature call_sig = "call($name, $args...)"; diff --git a/src/sass_context.hpp b/src/sass_context.hpp index 8ae1fb12c..f9632b5c3 100644 --- a/src/sass_context.hpp +++ b/src/sass_context.hpp @@ -126,4 +126,4 @@ struct Sass_Compiler { Sass::Block_Obj root; }; -#endif \ No newline at end of file +#endif