Skip to content

Commit 9616e95

Browse files
committed
Minor fixes / enhancements
1. Initialize static data structures correctly. 2. Close file on read error. 3. Less string creation in path handling routines.
1 parent 773bba7 commit 9616e95

File tree

6 files changed

+27
-47
lines changed

6 files changed

+27
-47
lines changed

src/color_maps.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ namespace Sass {
313313
const Color_RGBA transparent(color_table, 0, 0, 0, 0);
314314
}
315315

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

458-
const std::map<const char*, const Color_RGBA*, map_cmp_str> names_to_colors
458+
static const auto *const names_to_colors = new std::unordered_map<std::string, const Color_RGBA*>
459459
{
460460
{ ColorNames::aliceblue, &Colors::aliceblue },
461461
{ ColorNames::antiquewhite, &Colors::antiquewhite },
@@ -619,20 +619,20 @@ namespace Sass {
619619
std::string lower{key};
620620
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
621621

622-
auto p = names_to_colors.find(lower.c_str());
623-
if (p != names_to_colors.end()) {
622+
auto p = names_to_colors->find(lower);
623+
if (p != names_to_colors->end()) {
624624
return p->second;
625625
}
626-
return 0;
626+
return nullptr;
627627
}
628628

629629
const char* color_to_name(const int key)
630630
{
631-
auto p = colors_to_names.find(key);
632-
if (p != colors_to_names.end()) {
631+
auto p = colors_to_names->find(key);
632+
if (p != colors_to_names->end()) {
633633
return p->second;
634634
}
635-
return 0;
635+
return nullptr;
636636
}
637637

638638
const char* color_to_name(const double key)

src/color_maps.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77

88
namespace Sass {
99

10-
struct map_cmp_str
11-
{
12-
bool operator()(char const *a, char const *b) const
13-
{
14-
return std::strcmp(a, b) < 0;
15-
}
16-
};
17-
1810
namespace ColorNames
1911
{
2012
extern const char aliceblue[];

src/context.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,17 @@ namespace Sass {
2828

2929
static std::string safe_input(const char* in_path)
3030
{
31-
// enforce some safe defaults
32-
// used to create relative file links
33-
std::string safe_path(in_path ? in_path : "");
34-
return safe_path == "" ? "stdin" : safe_path;
31+
if (in_path == nullptr || in_path[0] == '\0') return "stdin";
32+
return in_path;
3533
}
3634

37-
static std::string safe_output(const char* out_path, const std::string& input_path = "")
35+
static std::string safe_output(const char* out_path, std::string input_path)
3836
{
39-
std::string safe_path(out_path ? out_path : "");
40-
// maybe we can extract an output path from input path
41-
if (safe_path == "" && input_path != "") {
42-
int lastindex = static_cast<int>(input_path.find_last_of("."));
43-
return (lastindex > -1 ? input_path.substr(0, lastindex) : input_path) + ".css";
37+
if (out_path == nullptr || out_path[0] == '\0') {
38+
if (input_path.empty()) return "stdout";
39+
return input_path.substr(0, input_path.find_last_of(".")) + ".css";
4440
}
45-
// enforce some safe defaults
46-
// used to create relative file links
47-
return safe_path == "" ? "stdout" : safe_path;
41+
return out_path;
4842
}
4943

5044
Context::Context(struct Sass_Context& c_ctx)

src/file.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ namespace Sass {
173173
while((pos = path.find("/./", pos)) != std::string::npos) path.erase(pos, 2);
174174

175175
// remove all leading and trailing self references
176-
while(path.length() > 1 && path.substr(0, 2) == "./") path.erase(0, 2);
177-
while((pos = path.length()) > 1 && path.substr(pos - 2) == "/.") path.erase(pos - 2);
176+
while(path.size() >= 2 && path[0] == '.' && path[1] == '/') path.erase(0, 2);
177+
while((pos = path.length()) > 1 && path[pos - 2] == '/' && path[pos - 1] == '.') path.erase(pos - 2);
178178

179179

180180
size_t proto = 0;
@@ -474,6 +474,7 @@ namespace Sass {
474474
char* contents = static_cast<char*>(malloc(st.st_size + 2 * sizeof(char)));
475475
if (std::fread(static_cast<void*>(contents), 1, size, fd) != size) {
476476
free(contents);
477+
std::fclose(fd);
477478
return nullptr;
478479
}
479480
if (std::fclose(fd) != 0) {

src/fn_miscs.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ namespace Sass {
88

99
namespace Functions {
1010

11-
// features
12-
static std::set<std::string> features {
13-
"global-variable-shadowing",
14-
"extend-selector-pseudoclass",
15-
"at-error",
16-
"units-level-3",
17-
"custom-property"
18-
};
19-
2011
//////////////////////////
2112
// INTROSPECTION FUNCTIONS
2213
//////////////////////////
@@ -90,12 +81,14 @@ namespace Sass {
9081
{
9182
std::string s = unquote(ARG("$name", String_Constant)->value());
9283

93-
if(features.find(s) == features.end()) {
94-
return SASS_MEMORY_NEW(Boolean, pstate, false);
95-
}
96-
else {
97-
return SASS_MEMORY_NEW(Boolean, pstate, true);
98-
}
84+
static const auto *const features = new std::unordered_set<std::string> {
85+
"global-variable-shadowing",
86+
"extend-selector-pseudoclass",
87+
"at-error",
88+
"units-level-3",
89+
"custom-property"
90+
};
91+
return SASS_MEMORY_NEW(Boolean, pstate, features->find(s) != features->end());
9992
}
10093

10194
Signature call_sig = "call($name, $args...)";

src/sass_context.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ struct Sass_Compiler {
126126
Sass::Block_Obj root;
127127
};
128128

129-
#endif
129+
#endif

0 commit comments

Comments
 (0)