Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Minor fixes / enhancements
Browse files Browse the repository at this point in the history
1. Initialize static data structures correctly.
2. Close file on read error.
3. Less string creation in path handling routines.
  • Loading branch information
glebm committed Jun 17, 2019
1 parent 773bba7 commit 9616e95
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 47 deletions.
16 changes: 8 additions & 8 deletions src/color_maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ namespace Sass {
const Color_RGBA transparent(color_table, 0, 0, 0, 0);
}

const std::map<const int, const char*> colors_to_names {
static const auto* const colors_to_names = new std::unordered_map<int, const char*> {
{ 240 * 0x10000 + 248 * 0x100 + 255, ColorNames::aliceblue },
{ 250 * 0x10000 + 235 * 0x100 + 215, ColorNames::antiquewhite },
{ 0 * 0x10000 + 255 * 0x100 + 255, ColorNames::cyan },
Expand Down Expand Up @@ -455,7 +455,7 @@ namespace Sass {
{ 102 * 0x10000 + 51 * 0x100 + 153, ColorNames::rebeccapurple }
};

const std::map<const char*, const Color_RGBA*, map_cmp_str> names_to_colors
static const auto *const names_to_colors = new std::unordered_map<std::string, const Color_RGBA*>
{
{ ColorNames::aliceblue, &Colors::aliceblue },
{ ColorNames::antiquewhite, &Colors::antiquewhite },
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions src/color_maps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
20 changes: 7 additions & 13 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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)
Expand Down
5 changes: 3 additions & 2 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -474,6 +474,7 @@ namespace Sass {
char* contents = static_cast<char*>(malloc(st.st_size + 2 * sizeof(char)));
if (std::fread(static_cast<void*>(contents), 1, size, fd) != size) {
free(contents);
std::fclose(fd);
return nullptr;
}
if (std::fclose(fd) != 0) {
Expand Down
23 changes: 8 additions & 15 deletions src/fn_miscs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ namespace Sass {

namespace Functions {

// features
static std::set<std::string> features {
"global-variable-shadowing",
"extend-selector-pseudoclass",
"at-error",
"units-level-3",
"custom-property"
};

//////////////////////////
// INTROSPECTION FUNCTIONS
//////////////////////////
Expand Down Expand Up @@ -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<std::string> {
"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...)";
Expand Down
2 changes: 1 addition & 1 deletion src/sass_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ struct Sass_Compiler {
Sass::Block_Obj root;
};

#endif
#endif

0 comments on commit 9616e95

Please sign in to comment.