Skip to content

Commit 5d21603

Browse files
string list sub range helper
1 parent 438fe98 commit 5d21603

File tree

5 files changed

+126
-15
lines changed

5 files changed

+126
-15
lines changed

src/base/base_strings.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,42 @@ str8_list_copy(Arena *arena, String8List *list)
11431143
return result;
11441144
}
11451145

1146+
internal String8List
1147+
str8_list_substr(Arena *arena, String8List list, Rng1U64 range)
1148+
{
1149+
String8List result = {0};
1150+
1151+
String8Node *n = list.first;
1152+
1153+
U64 front_min = 0;
1154+
{
1155+
U64 cursor = 0;
1156+
for (; n != 0; cursor += n->string.size, n = n->next) {
1157+
if (cursor + n->string.size > range.min) {
1158+
front_min = range.min - cursor;
1159+
break;
1160+
}
1161+
}
1162+
}
1163+
1164+
if (front_min > 0) {
1165+
U64 front_max = front_min + Min(dim_1u64(range), n->string.size);
1166+
str8_list_push(arena, &result, str8_substr(n->string, r1u64(front_max, front_max)));
1167+
n = n->next;
1168+
}
1169+
1170+
for (; n != 0; n = n->next) {
1171+
if (result.total_size >= dim_1u64(range)) {
1172+
break;
1173+
}
1174+
U64 copy_max = dim_1u64(range) - result.total_size;
1175+
U64 copy_size = Min(copy_max, n->string.size);
1176+
str8_list_push(arena, &result, str8_substr(n->string, r1u64(0, copy_size)));
1177+
}
1178+
1179+
return result;
1180+
}
1181+
11461182
////////////////////////////////
11471183
//~ rjf: String Splitting & Joining
11481184

src/base/base_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ internal String8Node* str8_list_pushf(Arena *arena, String8List *list, char *fmt
269269
internal String8Node* str8_list_push_frontf(Arena *arena, String8List *list, char *fmt, ...);
270270
internal String8Node* str8_list_pop_front(String8List *list);
271271
internal String8List str8_list_copy(Arena *arena, String8List *list);
272+
internal String8List str8_list_substr(Arena *arena, String8List list, Rng1U64 range);
272273
#define str8_list_first(list) ((list)->first ? (list)->first->string : str8_zero())
273274

274275
////////////////////////////////

src/torture/torture.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ typedef struct
3939
extern U64 g_torture_test_count;
4040
extern T_Test g_torture_tests[0xffffff];
4141

42-
#define T_AddTest(name, l, ...) \
43-
T_RunResult t_##name(void); \
44-
__VA_ARGS__ void t_add_test_##name(void) \
45-
{ \
42+
#define T_AddTest(name, l, ...) \
43+
T_RunResult t_##name(void); \
44+
__VA_ARGS__ void t_add_test_##name(void) \
45+
{ \
4646
g_torture_tests[g_torture_test_count].group = T_Group; \
4747
g_torture_tests[g_torture_test_count].label = Stringify(name); \
4848
g_torture_tests[g_torture_test_count].r = &t_##name; \
@@ -52,7 +52,7 @@ g_torture_test_count += 1; \
5252

5353
#if COMPILER_MSVC
5454
#pragma section(".CRT$XCU", read)
55-
# define T_BeginTest_(name) \
55+
# define T_BeginTest_(name) \
5656
T_AddTest(name, __LINE__) \
5757
__declspec(allocate(".CRT$XCU")) void(*r_##name)(void) = t_add_test_##name; \
5858
__pragma(comment(linker, "/include:" Stringify(r_##name)))
@@ -61,20 +61,20 @@ __pragma(comment(linker, "/include:" Stringify(r_##name)))
6161
T_AddTest(name, __LINE__, __attribute__((constructor)))
6262
#endif
6363

64-
#define T_BeginTest(name) \
65-
T_BeginTest_(name) \
66-
T_RunResult t_##name(void) { \
64+
#define T_BeginTest(name) \
65+
T_BeginTest_(name) \
66+
T_RunResult t_##name(void) { \
6767
Temp scratch = scratch_begin(0,0); \
68-
T_RunResult result = { .status = T_RunStatus_Fail };
68+
T_RunResult result_ = { .status = T_RunStatus_Fail };
6969

70-
#define T_EndTest \
71-
result.status = T_RunStatus_Pass; \
72-
exit__:; \
73-
scratch_end(scratch); \
74-
return result; \
70+
#define T_EndTest \
71+
result_.status = T_RunStatus_Pass; \
72+
exit__:; \
73+
scratch_end(scratch); \
74+
return result_; \
7575
}
7676

77-
#define T_Ok(c) do { if (!(c)) { result.fail_file = __FILE__; result.fail_line = __LINE__; result.fail_cond = Stringify(c); goto exit__; } } while(0)
77+
#define T_Ok(c) do { if (!(c)) { result_.fail_file = __FILE__; result_.fail_line = __LINE__; result_.fail_cond = Stringify(c); goto exit__; } } while(0)
7878
#define T_MatchLinef(out, ...) T_Ok(t_match_linef(out, __VA_ARGS__))
7979

8080
////////////////////////////////

src/torture/torture_base.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Epic Games Tools
2+
// Licensed under the MIT license (https://opensource.org/license/mit/)
3+
4+
#define T_Group "Base"
5+
6+
T_BeginTest(str8_list_substr)
7+
{
8+
String8List zero_list = {0};
9+
10+
{
11+
String8List list = {0};
12+
str8_list_pushf(scratch.arena, &list, "a");
13+
str8_list_pushf(scratch.arena, &list, "b");
14+
str8_list_pushf(scratch.arena, &list, "c");
15+
String8List sub = str8_list_substr(scratch.arena, list, r1u64(0, 3));
16+
String8 result = str8_list_join(scratch.arena, &list, 0);
17+
T_Ok(str8_match(result, str8_lit("abc"), 0));
18+
}
19+
20+
{
21+
String8List list = {0};
22+
str8_list_pushf(scratch.arena, &list, "a");
23+
str8_list_pushf(scratch.arena, &list, "b");
24+
str8_list_pushf(scratch.arena, &list, "c");
25+
String8List sub = str8_list_substr(scratch.arena, list, r1u64(0, max_U64));
26+
String8 result = str8_list_join(scratch.arena, &list, 0);
27+
T_Ok(str8_match(result, str8_lit("abc"), 0));
28+
}
29+
30+
{
31+
32+
String8List list = {0};
33+
str8_list_pushf(scratch.arena, &list, "a");
34+
str8_list_pushf(scratch.arena, &list, "bcd");
35+
String8List sub = str8_list_substr(scratch.arena, list, r1u64(2, 3));
36+
String8 result = str8_list_join(scratch.arena, &sub, 0);
37+
T_Ok(str8_match(result, str8_lit("c"), 0));
38+
}
39+
40+
{
41+
42+
String8List list = {0};
43+
str8_list_pushf(scratch.arena, &list, "a");
44+
str8_list_pushf(scratch.arena, &list, "bcd");
45+
String8List sub = str8_list_substr(scratch.arena, list, r1u64(1, 2));
46+
String8 result = str8_list_join(scratch.arena, &sub, 0);
47+
T_Ok(str8_match(result, str8_lit("b"), 0));
48+
}
49+
50+
{
51+
String8List list = {0};
52+
str8_list_pushf(scratch.arena, &list, "ab");
53+
str8_list_pushf(scratch.arena, &list, "cd");
54+
str8_list_pushf(scratch.arena, &list, "ef");
55+
String8List sub = str8_list_substr(scratch.arena, list, r1u64(1, 5));
56+
String8 result = str8_list_join(scratch.arena, &sub, 0);
57+
T_Ok(str8_match(result, str8_lit("bcde"), 0));
58+
}
59+
60+
{
61+
String8List list = {0};
62+
str8_list_pushf(scratch.arena, &list, "abc");
63+
64+
String8List zero = str8_list_substr(scratch.arena, list, r1u64(0, 0));
65+
T_Ok(MemoryMatchStruct(&zero, &zero_list));
66+
67+
String8List out_of_bounds_range = str8_list_substr(scratch.arena, list, r1u64(max_U64/2, max_U64));
68+
T_Ok(MemoryMatchStruct(&out_of_bounds_range, &zero_list));
69+
}
70+
}
71+
T_EndTest;
72+
73+
#undef T_Group

src/torture/torture_main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "linker/thread_pool/thread_pool.c"
7373
#include "linker/codeview_ext/codeview.c"
7474
#include "torture.c"
75+
#include "torture_base.c"
7576
#include "torture_radlink.c"
7677
#include "torture_dwarf.c"
7778
#include "torture_d2r.c"

0 commit comments

Comments
 (0)