sv: major refactor
- Internal data pointer is read only by default => any uses that require use of the pointer itself (freeing, mutating) must perform a cast. - refactor tests to use some new sv macros and functions, and clean them up. - slight cleanup of sv.c
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
u64 size;
|
u64 size;
|
||||||
char *data;
|
const char *data;
|
||||||
} sv_t;
|
} sv_t;
|
||||||
|
|
||||||
// String view macro constructor
|
// String view macro constructor
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ void stream_free(stream_t *stream)
|
|||||||
switch (stream->type)
|
switch (stream->type)
|
||||||
{
|
{
|
||||||
case STREAM_TYPE_STRING:
|
case STREAM_TYPE_STRING:
|
||||||
free(stream->string.data);
|
free((char *)stream->string.data);
|
||||||
break;
|
break;
|
||||||
case STREAM_TYPE_FILE:
|
case STREAM_TYPE_FILE:
|
||||||
case STREAM_TYPE_PIPE:
|
case STREAM_TYPE_PIPE:
|
||||||
|
|||||||
4
src/sv.c
4
src/sv.c
@@ -58,8 +58,6 @@ sv_t sv_till(sv_t sv, const char *reject)
|
|||||||
++offset)
|
++offset)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (offset == sv.size)
|
|
||||||
return sv;
|
|
||||||
return sv_truncate(sv, offset);
|
return sv_truncate(sv, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,8 +71,6 @@ sv_t sv_while(sv_t sv, const char *accept)
|
|||||||
++offset)
|
++offset)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (offset == sv.size)
|
|
||||||
return sv;
|
|
||||||
return sv_truncate(sv, offset);
|
return sv_truncate(sv, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ void stream_test_string(void)
|
|||||||
sv_t test_strings[] = {
|
sv_t test_strings[] = {
|
||||||
SV_AUTO("hello, world!"),
|
SV_AUTO("hello, world!"),
|
||||||
SV_AUTO("another string"),
|
SV_AUTO("another string"),
|
||||||
SV((char *)text, ARRSIZE(text) / 2),
|
sv_truncate(SV_AUTO(text), ARRSIZE(text) / 2),
|
||||||
};
|
};
|
||||||
|
|
||||||
for (u64 i = 0; i < ARRSIZE(test_strings); ++i)
|
for (u64 i = 0; i < ARRSIZE(test_strings); ++i)
|
||||||
@@ -87,7 +87,8 @@ void stream_test_string(void)
|
|||||||
"Freeing a stream does not free the underlying memory it was derived "
|
"Freeing a stream does not free the underlying memory it was derived "
|
||||||
"from");
|
"from");
|
||||||
|
|
||||||
free(copy.data);
|
// NOTE: Okay to free since we own copy.
|
||||||
|
free((void *)copy.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
stream_t stream = {0};
|
stream_t stream = {0};
|
||||||
@@ -319,7 +320,7 @@ void stream_test_substr(void)
|
|||||||
|
|
||||||
TEST(result.size == size, "Substring has right size (%lu)", result.size);
|
TEST(result.size == size, "Substring has right size (%lu)", result.size);
|
||||||
|
|
||||||
sv_t expected = SV((char *)words_text + position, size);
|
sv_t expected = sv_substr(SV_AUTO(words_text), position, size);
|
||||||
TEST(strncmp(result.data, expected.data, result.size) == 0,
|
TEST(strncmp(result.data, expected.data, result.size) == 0,
|
||||||
"Expect the substring to be the same as the data we put in");
|
"Expect the substring to be the same as the data we put in");
|
||||||
}
|
}
|
||||||
@@ -332,7 +333,7 @@ void stream_test_substr(void)
|
|||||||
|
|
||||||
TEST(result.size == size, "Substring has right size (%lu)", result.size);
|
TEST(result.size == size, "Substring has right size (%lu)", result.size);
|
||||||
|
|
||||||
sv_t expected = SV((char *)words_text, size);
|
sv_t expected = sv_truncate(SV_AUTO(words_text), size);
|
||||||
TEST(strncmp(result.data, expected.data, result.size) == 0,
|
TEST(strncmp(result.data, expected.data, result.size) == 0,
|
||||||
"Expect the substring to be the same as the data we put in");
|
"Expect the substring to be the same as the data we put in");
|
||||||
}
|
}
|
||||||
@@ -349,7 +350,7 @@ void stream_test_substr(void)
|
|||||||
|
|
||||||
TEST(result.size == size, "Substring has right size (%lu)", result.size);
|
TEST(result.size == size, "Substring has right size (%lu)", result.size);
|
||||||
|
|
||||||
sv_t expected = SV((char *)words_text + position, size);
|
sv_t expected = sv_substr(SV_AUTO(words_text), position, size);
|
||||||
TEST(strncmp(result.data, expected.data, result.size) == 0,
|
TEST(strncmp(result.data, expected.data, result.size) == 0,
|
||||||
"Expect the substring to be the same as the data we put in");
|
"Expect the substring to be the same as the data we put in");
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ void sv_copy_test(void)
|
|||||||
TEST(strncmp(word.data, copy.data, copy.size) == 0, "`%s` == `%s`",
|
TEST(strncmp(word.data, copy.data, copy.size) == 0, "`%s` == `%s`",
|
||||||
word.data, copy.data);
|
word.data, copy.data);
|
||||||
|
|
||||||
// Obviously we can't just have this lying around.
|
// NOTE: Okay to free since we own copy.
|
||||||
free(copy.data);
|
free((void *)copy.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,13 +50,13 @@ void vec_test_gen_substr(void)
|
|||||||
{0, 16},
|
{0, 16},
|
||||||
{0, 32},
|
{0, 32},
|
||||||
{32, 64},
|
{32, 64},
|
||||||
{0, ARRSIZE(text)},
|
{0, ARRSIZE(text) - 1},
|
||||||
};
|
};
|
||||||
|
|
||||||
for (u64 i = 0; i < ARRSIZE(tests); ++i)
|
for (u64 i = 0; i < ARRSIZE(tests); ++i)
|
||||||
{
|
{
|
||||||
struct Test test = tests[i];
|
struct Test test = tests[i];
|
||||||
const sv_t substr = SV((char *)text + test.start, test.size);
|
const sv_t substr = sv_substr(SV_AUTO(text), test.start, test.size);
|
||||||
const u64 size = test.size / 2;
|
const u64 size = test.size / 2;
|
||||||
|
|
||||||
lisp_t *lvec = make_vec(&system, size);
|
lisp_t *lvec = make_vec(&system, size);
|
||||||
|
|||||||
Reference in New Issue
Block a user