Skip to content

Commit 92e19d4

Browse files
reximarcout
andcommitted
v3.10.0 - NOB_SVLIT at compile-time
Co-authored-by: arcout <arcout13579@gmail.com>
1 parent eff0516 commit 92e19d4

5 files changed

Lines changed: 34 additions & 2 deletions

File tree

nob.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const char *test_names[] = {
2020
"private_functions_inside_public_macros",
2121
"bytes_for_utf8",
2222
"sv_foreach",
23+
"svlit_at_different_storages",
2324
};
2425
#define test_names_count ARRAY_LEN(test_names)
2526

nob.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* nob - v3.9.0 - Public Domain - https://github.com/tsoding/nob.h
1+
/* nob - v3.10.0 - Public Domain - https://github.com/tsoding/nob.h
22
33
This library is the next generation of the [NoBuild](https://github.com/tsoding/nobuild) idea.
44
@@ -947,7 +947,11 @@ NOBDEF Nob_String_View nob_sv_from_parts(const char *data, size_t count);
947947
// nob_sb_to_sv() enables you to just view Nob_String_Builder as Nob_String_View
948948
#define nob_sb_to_sv(sb) nob_sv_from_parts((sb).items, (sb).count)
949949

950-
#define NOB_SVLIT(lit) nob_sv_from_parts((lit), sizeof(lit)-1)
950+
#define NOB_SVLIT(lit) (NOB_CLIT(Nob_String_View){.count = sizeof(lit)-1, .data = (lit)})
951+
// Generally majority of the C/C++ compilers will allow you to use NOB_SVLIT to construct global variables.
952+
// But there are some (specifically MSVC with /TC flag enabled) that will refuse.
953+
// For such compilers use NOB_SVLIT_STATIC instead.
954+
#define NOB_SVLIT_STATIC(lit) {.count = sizeof(lit)-1, .data = (lit)}
951955

952956
// printf macros for String_View
953957
#ifndef SV_Fmt
@@ -2956,6 +2960,7 @@ NOBDEF char *nob_temp_running_executable_path(void)
29562960
#define UNREACHABLE NOB_UNREACHABLE
29572961
#define UNREACHABLEF NOB_UNREACHABLEF
29582962
#define SVLIT NOB_SVLIT
2963+
#define SVLIT_STATIC NOB_SVLIT_STATIC
29592964
#define UNUSED NOB_UNUSED
29602965
#define ARRAY_LEN NOB_ARRAY_LEN
29612966
#define ARRAY_GET NOB_ARRAY_GET
@@ -3125,6 +3130,8 @@ NOBDEF char *nob_temp_running_executable_path(void)
31253130
/*
31263131
Revision history:
31273132
3133+
3.10.0 (2026-07-17) Make NOB_SVLIT a bit more usable at compile-time (by @Arhcout)
3134+
Add NOB_SVLIT_STATIC for when a compiler simply refuses to accept NOB_SVLIT() at compile-time (looking at you `cl.exe /TC`) (by @rexim)
31283135
3.9.0 (2026-07-15) Add NOB_TODOF() and NOB_UNREACHABLEF()
31293136
Add NOB_SVLIT()
31303137
Add nob_bytes_for_utf8[] and nob_sv_utf8_len()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define NOB_IMPLEMENTATION
2+
#include "nob.h"
3+
4+
String_View global_svlit = SVLIT_STATIC("Global SVLIT");
5+
6+
void param_svlit(String_View sv)
7+
{
8+
printf(SV_Fmt"\n", SV_Arg(sv));
9+
}
10+
11+
int main()
12+
{
13+
String_View local_svlit = SVLIT("Local SVLIT");
14+
printf(SV_Fmt"\n", SV_Arg(global_svlit));
15+
printf(SV_Fmt"\n", SV_Arg(local_svlit));
16+
param_svlit(SVLIT("Parameter SVLIT"));
17+
return 0;
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Global SVLIT
2+
Local SVLIT
3+
Parameter SVLIT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Global SVLIT
2+
Local SVLIT
3+
Parameter SVLIT

0 commit comments

Comments
 (0)