Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sass
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
楚学文
node-sass
Commits
6c1b5301
Commit
6c1b5301
authored
Feb 23, 2012
by
Aaron Leung
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generic resizable array templates.
parent
10d5c72a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
0 deletions
+97
-0
array.h
array.h
+49
-0
test_array.c
test_array.c
+44
-0
test_array.h
test_array.h
+4
-0
No files found.
array.h
0 → 100644
View file @
6c1b5301
#define DECLARE_SASS_ARRAY_OF(type) \
typedef struct { \
size_t length; \
size_t capacity; \
type *contents; \
} sass_ ## type ## _array; \
sass_ ## type ## _array sass_make_ ## type ## _array(size_t cap); \
void sass_ ## type ## _array_push(sass_ ## type ## _array *a, type elem); \
void sass_ ## type ## _array_cat(sass_ ## type ## _array *a, sass_ ## type ## _array *b); \
void sass_free_ ## type ## _array(sass_ ## type ## _array *a)
#define DEFINE_SASS_ARRAY_OF(type) \
sass_ ## type ## _array sass_make_ ## type ## _array(size_t cap) { \
sass_ ## type ## _array a; \
a.contents = (type *) malloc(cap * sizeof(type)); \
if (!a.contents) { \
printf("ERROR: unable to allocate array of %s\n", #type); \
abort(); \
} \
a.capacity = cap; \
a.length = 0; \
return a; \
} \
void sass_ ## type ## _array_push(sass_ ## type ## _array *a, type elem) { \
size_t len = a->length, cap = a->capacity; \
if (len < cap) { \
a->contents[len] = elem; \
a->length++; \
} \
else { \
type *new = (type *) realloc(a->contents, len * 2); \
if (!new) { \
printf("ERROR: unable to resize array of %s\n", #type); \
abort(); \
} \
else { \
new[len] = elem; \
a->contents = new; \
a->length++; \
a->capacity = len * 2; \
} \
} \
} \
void sass_ ## type ## _array_cat(sass_ ## type ## _array *a, sass_ ## type ## _array *b) { \
size_t b_len = b->length; \
type *b_cont = b->contents; \
int i; \
for (i = 0; i < b_len; i++) sass_ ## type ## _array_push(a, b_cont[i]); \
}
test_array.c
0 → 100644
View file @
6c1b5301
#include <stdio.h>
#include <stdlib.h>
#include "test_array.h"
DEFINE_SASS_ARRAY_OF
(
char
);
int
main
()
{
sass_char_array
a
=
sass_make_char_array
(
4
);
printf
(
"%ld, %ld
\n
"
,
a
.
length
,
a
.
capacity
);
sass_char_array_push
(
&
a
,
'h'
);
sass_char_array_push
(
&
a
,
'e'
);
sass_char_array_push
(
&
a
,
'l'
);
sass_char_array_push
(
&
a
,
'l'
);
printf
(
"%ld, %ld
\n
"
,
a
.
length
,
a
.
capacity
);
int
i
;
for
(
i
=
0
;
i
<
a
.
length
;
i
++
)
putchar
(
a
.
contents
[
i
]);
putchar
(
'\n'
);
sass_char_array_push
(
&
a
,
'o'
);
printf
(
"%ld, %ld
\n
"
,
a
.
length
,
a
.
capacity
);
for
(
i
=
0
;
i
<
a
.
length
;
i
++
)
putchar
(
a
.
contents
[
i
]);
putchar
(
'\n'
);
sass_char_array
b
=
sass_make_char_array
(
7
);
sass_char_array_push
(
&
a
,
' '
);
sass_char_array_push
(
&
a
,
'w'
);
sass_char_array_push
(
&
a
,
'o'
);
sass_char_array_push
(
&
a
,
'r'
);
sass_char_array_push
(
&
a
,
'l'
);
sass_char_array_push
(
&
a
,
'd'
);
sass_char_array_cat
(
&
a
,
&
b
);
printf
(
"%ld, %ld
\n
"
,
a
.
length
,
a
.
capacity
);
for
(
i
=
0
;
i
<
a
.
length
;
i
++
)
putchar
(
a
.
contents
[
i
]);
putchar
(
'\n'
);
return
0
;
}
\ No newline at end of file
test_array.h
0 → 100644
View file @
6c1b5301
#include "array.h"
DECLARE_SASS_ARRAY_OF
(
char
);
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment