Skip to content

Commit 0a5a5b7

Browse files
committed
Move generic string functions to util.cpp
1 parent 9fcea20 commit 0a5a5b7

File tree

4 files changed

+48
-75
lines changed

4 files changed

+48
-75
lines changed

src/sass_context.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -182,44 +182,6 @@ extern "C" {
182182
type sass_context_take_##option (struct Sass_Context* ctx) \
183183
{ type foo = ctx->option; ctx->option = 0; return foo; }
184184

185-
// helper for safe access to c_ctx
186-
static const char* safe_str (const char* str) {
187-
return str == NULL ? "" : str;
188-
}
189-
190-
static void free_string_array(char ** arr) {
191-
if(!arr)
192-
return;
193-
194-
char **it = arr;
195-
while (it && (*it)) {
196-
free(*it);
197-
++it;
198-
}
199-
200-
free(arr);
201-
}
202-
203-
static char **copy_strings(const std::vector<std::string>& strings, char*** array) {
204-
int num = static_cast<int>(strings.size());
205-
char** arr = (char**) calloc(num + 1, sizeof(char*));
206-
if (arr == 0)
207-
return *array = (char **)NULL;
208-
209-
for(int i = 0; i < num; i++) {
210-
arr[i] = (char*) malloc(sizeof(char) * (strings[i].size() + 1));
211-
if (arr[i] == 0) {
212-
free_string_array(arr);
213-
return *array = (char **)NULL;
214-
}
215-
std::copy(strings[i].begin(), strings[i].end(), arr[i]);
216-
arr[i][strings[i].size()] = '\0';
217-
}
218-
219-
arr[num] = 0;
220-
return *array = arr;
221-
}
222-
223185
static int handle_errors(Sass_Context* c_ctx) {
224186
try {
225187
throw;

src/sass_interface.cpp

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,14 @@ extern "C" {
1919
sass_context* sass_new_context()
2020
{ return (sass_context*) calloc(1, sizeof(sass_context)); }
2121

22-
// helper for safe access to c_ctx
23-
static const char* safe_str (const char* str) {
24-
return str == NULL ? "" : str;
25-
}
26-
27-
static void copy_strings(const std::vector<std::string>& strings, char*** array, int skip = 0) {
28-
int num = static_cast<int>(strings.size());
29-
char** arr = (char**) malloc(sizeof(char*) * (num + 1));
30-
31-
for(int i = skip; i < num; i++) {
32-
arr[i-skip] = (char*) malloc(sizeof(char) * (strings[i].size() + 1));
33-
std::copy(strings[i].begin(), strings[i].end(), arr[i-skip]);
34-
arr[i-skip][strings[i].size()] = '\0';
35-
}
36-
37-
arr[num-skip] = 0;
38-
*array = arr;
39-
}
40-
41-
static void free_string_array(char ** arr) {
42-
if(!arr)
43-
return;
44-
45-
char **it = arr;
46-
while (it && (*it)) {
47-
free(*it);
48-
++it;
49-
}
50-
51-
free(arr);
52-
}
53-
5422
void sass_free_context(sass_context* ctx)
5523
{
5624
if (ctx->output_string) free(ctx->output_string);
5725
if (ctx->source_map_string) free(ctx->source_map_string);
5826
if (ctx->error_message) free(ctx->error_message);
5927
if (ctx->c_functions) free(ctx->c_functions);
6028

61-
free_string_array(ctx->included_files);
29+
Sass::free_string_array(ctx->included_files);
6230

6331
free(ctx);
6432
}
@@ -73,7 +41,7 @@ extern "C" {
7341
if (ctx->error_message) free(ctx->error_message);
7442
if (ctx->c_functions) free(ctx->c_functions);
7543

76-
free_string_array(ctx->included_files);
44+
Sass::free_string_array(ctx->included_files);
7745

7846
free(ctx);
7947
}
@@ -83,7 +51,7 @@ extern "C" {
8351

8452
void sass_free_folder_context(sass_folder_context* ctx)
8553
{
86-
free_string_array(ctx->included_files);
54+
Sass::free_string_array(ctx->included_files);
8755
free(ctx);
8856
}
8957

@@ -135,7 +103,8 @@ extern "C" {
135103
c_ctx->error_message = 0;
136104
c_ctx->error_status = 0;
137105

138-
copy_strings(cpp_ctx.get_included_files(true), &c_ctx->included_files, 1);
106+
if (copy_strings(cpp_ctx.get_included_files(true), &c_ctx->included_files, 1) == NULL)
107+
throw(std::bad_alloc());
139108
}
140109
catch (Error_Invalid& e) {
141110
std::stringstream msg_stream;
@@ -227,7 +196,8 @@ extern "C" {
227196
c_ctx->error_message = 0;
228197
c_ctx->error_status = 0;
229198

230-
copy_strings(cpp_ctx.get_included_files(false), &c_ctx->included_files);
199+
if (copy_strings(cpp_ctx.get_included_files(false), &c_ctx->included_files) == NULL)
200+
throw(std::bad_alloc());
231201
}
232202
catch (Error_Invalid& e) {
233203
std::stringstream msg_stream;

src/util.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ namespace Sass {
4848
return atof(str);
4949
}
5050

51+
// helper for safe access to c_ctx
52+
const char* safe_str (const char* str) {
53+
return str == NULL ? "" : str;
54+
}
55+
56+
void free_string_array(char ** arr) {
57+
if(!arr)
58+
return;
59+
60+
char **it = arr;
61+
while (it && (*it)) {
62+
free(*it);
63+
++it;
64+
}
65+
66+
free(arr);
67+
}
68+
69+
char **copy_strings(const std::vector<std::string>& strings, char*** array, int skip) {
70+
int num = static_cast<int>(strings.size()) - skip;
71+
char** arr = (char**) calloc(num + 1, sizeof(char*));
72+
if (arr == 0)
73+
return *array = (char **)NULL;
74+
75+
for(int i = 0; i < num; i++) {
76+
arr[i] = (char*) malloc(sizeof(char) * (strings[i + skip].size() + 1));
77+
if (arr[i] == 0) {
78+
free_string_array(arr);
79+
return *array = (char **)NULL;
80+
}
81+
std::copy(strings[i + skip].begin(), strings[i + skip].end(), arr[i]);
82+
arr[i][strings[i + skip].size()] = '\0';
83+
}
84+
85+
arr[num] = 0;
86+
return *array = arr;
87+
}
88+
5189
std::string string_eval_escapes(const std::string& s)
5290
{
5391

src/util.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace Sass {
1212

1313
char* sass_strdup(const char* str);
1414
double sass_atof(const char* str);
15+
const char* safe_str(const char *);
16+
void free_string_array(char **);
17+
char **copy_strings(const std::vector<std::string>&, char ***, int = 0);
1518
std::string string_escape(const std::string& str);
1619
std::string string_unescape(const std::string& str);
1720
std::string string_eval_escapes(const std::string& str);

0 commit comments

Comments
 (0)