Skip to content

Commit b4ae6e8

Browse files
committed
Formatting & comments
1 parent 6a70962 commit b4ae6e8

2 files changed

Lines changed: 14 additions & 55 deletions

File tree

extlib.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,8 +1656,8 @@ EXT_API int ext_cmd_write(const char *cmd, const void *data, size_t size);
16561656
// Puts an entry into the hashmap with custom hash and compare functions.
16571657
//
16581658
// `hash_fn` and `cmp_fn` are functions:
1659-
// size_t hash_fn(KeyType *key, size_t key_size) returns hash of the key
1660-
// int cmp_fn (KeyType *key_a, KeyType *key_b, size_t key_size) returns 0 if equal
1659+
// size_t hash_fn(KeyType *key, size_t key_size) - returns hash of the key
1660+
// int cmp_fn (KeyType *key_a, KeyType *key_b, size_t key_size) - returns 0 if equal
16611661
// Both receive a pointer to the key field (first field of the entry) along with its size
16621662
// (sizeof(KeyType)).
16631663
//
@@ -1857,12 +1857,13 @@ EXT_STATIC_ASSERT(((EXT_HMAP_INIT_CAPACITY) & (EXT_HMAP_INIT_CAPACITY - 1)) == 0
18571857
// Hidden slots stored before the entries/hashes pointers ("before-pointer" layout).
18581858
// The allocations for entries and hashes each carry EXT__HMAP_HIDDEN_SLOTS slots before
18591859
// the pointer. Raw slot indices (relative to the pointer):
1860-
// entries[EXT__HMAP_TMP_ENTRY_SLOT] (-1): tmp entry key/value staging area during probe
1860+
// entries[EXT__HMAP_TMP_ENTRY_SLOT] (-1): tmp entry - key/value staging area during probe
18611861
// entries[-2]: spare entry slot (reserved for future use)
1862-
// hashes[EXT__HMAP_TOMB_SLOT] (-2): tombstone count — incremented on delete, reset on clear/grow
1863-
// hashes[EXT__HMAP_TMP_IDX_SLOT] (-1): tmp index — probe side-channel: ext__hmap_find_ writes
1864-
// the resolved slot index here so callers can read it
1865-
// without a return value
1862+
//
1863+
// hashes[EXT__HMAP_TOMB_SLOT] (-2): tombstone count - incremented on delete, reset on clear/grow
1864+
// hashes[EXT__HMAP_TMP_IDX_SLOT] (-1): tmp index - probe side-channel: ext__hmap_find_ writes
1865+
// the resolved slot index here so callers
1866+
// can read it without a return value
18661867
#define EXT__HMAP_HIDDEN_SLOTS (2)
18671868
#define EXT__HMAP_TMP_ENTRY_SLOT (-1)
18681869
#define EXT__HMAP_TMP_IDX_SLOT (-1)
@@ -2057,9 +2058,9 @@ static inline size_t ext__hash_cstr_(const char *str) {
20572058
// Concrete hash / compare functions for the built-in key types.
20582059
//
20592060
// Both functions follow the same convention used by the _ex macros:
2060-
// hash_fn(entry, key_sz) entry points to the full entry; key_sz carries
2061-
// sizeof(key) for the bytes variant and is ignored by cstr/ss.
2062-
// cmp_fn(entry_a, entry_b, key_sz) returns 0 on a key match.
2061+
// hash_fn(entry, key_sz) - entry points to the full entry; key_sz carries
2062+
// sizeof(key) for the bytes variant and is ignored by cstr/ss.
2063+
// cmp_fn(entry_a, entry_b, key_sz) - returns 0 on a key match.
20632064

20642065
static inline size_t ext__hmap_hash_bytes_(const void *entry, size_t key_sz) {
20652066
return ext__hash_bytes_(entry, key_sz);
@@ -3329,7 +3330,7 @@ Ext_StringSlice ext_ss_extension(Ext_StringSlice path) {
33293330
Ext_StringSlice base = ext_ss_basename(path);
33303331
for(size_t i = base.size; i > 0; i--) {
33313332
if(base.data[i - 1] == '.') {
3332-
// Dotfile (e.g. ".gitignore") with no other dot no extension
3333+
// Dotfile (e.g. ".gitignore") with no other dot; no extension
33333334
if(i - 1 == 0) return (Ext_StringSlice){0, base.data + base.size};
33343335
return ext_ss_cut(base, i - 1);
33353336
}

test/test.c

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,23 +1147,18 @@ CTEST(slice, ends_with) {
11471147
CTEST(slice, strip_prefix) {
11481148
StringSlice ss = ss_from_cstr("Hello, World!");
11491149

1150-
// Prefix present
11511150
StringSlice stripped = ss_strip_prefix(ss, SS("Hello, "));
11521151
ASSERT_TRUE(ss_eq(stripped, SS("World!")));
11531152

1154-
// Prefix absent — returns original
11551153
stripped = ss_strip_prefix(ss, SS("Goodbye"));
11561154
ASSERT_TRUE(ss_eq(stripped, ss));
11571155

1158-
// Empty prefix — returns original
11591156
stripped = ss_strip_prefix(ss, SS(""));
11601157
ASSERT_TRUE(ss_eq(stripped, ss));
11611158

1162-
// Prefix == entire string
11631159
stripped = ss_strip_prefix(ss, ss);
11641160
ASSERT_TRUE(stripped.size == 0);
11651161

1166-
// cstr variant
11671162
stripped = ss_strip_prefix_cstr(ss, "Hello, ");
11681163
ASSERT_TRUE(ss_eq(stripped, SS("World!")));
11691164
stripped = ss_strip_prefix_cstr(ss, "Goodbye");
@@ -1173,23 +1168,18 @@ CTEST(slice, strip_prefix) {
11731168
CTEST(slice, strip_suffix) {
11741169
StringSlice ss = ss_from_cstr("Hello, World!");
11751170

1176-
// Suffix present
11771171
StringSlice stripped = ss_strip_suffix(ss, SS(", World!"));
11781172
ASSERT_TRUE(ss_eq(stripped, SS("Hello")));
11791173

1180-
// Suffix absent — returns original
11811174
stripped = ss_strip_suffix(ss, SS("Goodbye"));
11821175
ASSERT_TRUE(ss_eq(stripped, ss));
11831176

1184-
// Empty suffix — returns original
11851177
stripped = ss_strip_suffix(ss, SS(""));
11861178
ASSERT_TRUE(ss_eq(stripped, ss));
11871179

1188-
// Suffix == entire string
11891180
stripped = ss_strip_suffix(ss, ss);
11901181
ASSERT_TRUE(stripped.size == 0);
11911182

1192-
// cstr variant
11931183
stripped = ss_strip_suffix_cstr(ss, ", World!");
11941184
ASSERT_TRUE(ss_eq(stripped, SS("Hello")));
11951185
stripped = ss_strip_suffix_cstr(ss, "Goodbye");
@@ -1208,8 +1198,6 @@ CTEST(slice, eq_ignore_case) {
12081198
ASSERT_TRUE(ss_eq_ignore_case(SS(""), SS("")));
12091199
ASSERT_TRUE(!ss_eq_ignore_case(SS("Hello"), SS("World")));
12101200
ASSERT_TRUE(!ss_eq_ignore_case(SS("Hello"), SS("Hell")));
1211-
1212-
// Digits/punctuation unchanged
12131201
ASSERT_TRUE(ss_eq_ignore_case(SS("test-123"), SS("TEST-123")));
12141202
}
12151203

@@ -1304,6 +1292,7 @@ CTEST(slice, foreach_split_cstr) {
13041292
"o Diva",
13051293
"del Pelide Achille",
13061294
};
1295+
13071296
size_t i = 0;
13081297
ss_foreach_split_cstr(SS("Cantami, o Diva, del Pelide Achille"), ", ", word) {
13091298
ASSERT_TRUE(i < EXT_ARR_SIZE(expected));
@@ -1312,7 +1301,6 @@ CTEST(slice, foreach_split_cstr) {
13121301
}
13131302
ASSERT_TRUE(i == EXT_ARR_SIZE(expected));
13141303

1315-
// Delimiter not found — yields entire string
13161304
i = 0;
13171305
ss_foreach_split_cstr(SS("hello"), ", ", word) {
13181306
ASSERT_TRUE(ss_eq(word, SS("hello")));
@@ -1327,6 +1315,7 @@ CTEST(slice, foreach_rsplit_cstr) {
13271315
"o Diva",
13281316
"Cantami",
13291317
};
1318+
13301319
size_t i = 0;
13311320
ss_foreach_rsplit_cstr(SS("Cantami, o Diva, del Pelide Achille"), ", ", word) {
13321321
ASSERT_TRUE(i < EXT_ARR_SIZE(expected));
@@ -1335,7 +1324,6 @@ CTEST(slice, foreach_rsplit_cstr) {
13351324
}
13361325
ASSERT_TRUE(i == EXT_ARR_SIZE(expected));
13371326

1338-
// Delimiter not found — yields entire string
13391327
i = 0;
13401328
ss_foreach_rsplit_cstr(SS("hello"), ", ", word) {
13411329
ASSERT_TRUE(ss_eq(word, SS("hello")));
@@ -1346,25 +1334,19 @@ CTEST(slice, foreach_rsplit_cstr) {
13461334

13471335
CTEST(slice, find_char) {
13481336
StringSlice ss = ss_from_cstr("hello world");
1349-
13501337
ASSERT_TRUE(ss_find_char(ss, 'h', 0) == 0);
13511338
ASSERT_TRUE(ss_find_char(ss, 'o', 0) == 4);
13521339
ASSERT_TRUE(ss_find_char(ss, 'o', 5) == 7);
13531340
ASSERT_TRUE(ss_find_char(ss, 'z', 0) == -1);
1354-
1355-
// Offset past end
13561341
ASSERT_TRUE(ss_find_char(ss, 'h', ss.size) == -1);
13571342
}
13581343

13591344
CTEST(slice, rfind_char) {
13601345
StringSlice ss = ss_from_cstr("hello world");
1361-
13621346
ASSERT_TRUE(ss_rfind_char(ss, 'd', ss.size) == 10);
13631347
ASSERT_TRUE(ss_rfind_char(ss, 'o', ss.size) == 7);
13641348
ASSERT_TRUE(ss_rfind_char(ss, 'o', 5) == 4);
13651349
ASSERT_TRUE(ss_rfind_char(ss, 'z', ss.size) == -1);
1366-
1367-
// Offset at 0 — nothing to search
13681350
ASSERT_TRUE(ss_rfind_char(ss, 'h', 0) == -1);
13691351
}
13701352

@@ -1433,47 +1415,31 @@ CTEST(slice, rfind_cstr) {
14331415
}
14341416

14351417
CTEST(slice, basename) {
1436-
// Simple absolute path
14371418
ASSERT_TRUE(ss_eq(ss_basename(SS("/usr/local/bin/test")), SS("test")));
1438-
// Relative path
14391419
ASSERT_TRUE(ss_eq(ss_basename(SS("src/main.c")), SS("main.c")));
1440-
// Trailing slash
14411420
ASSERT_TRUE(ss_eq(ss_basename(SS("/usr/local/")), SS("local")));
1442-
// No separator — returns whole string
14431421
ASSERT_TRUE(ss_eq(ss_basename(SS("hello.txt")), SS("hello.txt")));
1444-
// Root
14451422
ASSERT_TRUE(ss_basename(SS("/")).size == 0);
1446-
// Multiple trailing slashes
14471423
ASSERT_TRUE(ss_eq(ss_basename(SS("/usr/local///")), SS("local")));
14481424

14491425
#ifdef EXT_WINDOWS
1450-
// Windows-style backslash separators
14511426
ASSERT_TRUE(ss_eq(ss_basename(SS("C:\\Users\\test\\file.txt")), SS("file.txt")));
14521427
ASSERT_TRUE(ss_eq(ss_basename(SS("C:\\Users\\test\\")), SS("test")));
1453-
// Mixed separators
14541428
ASSERT_TRUE(ss_eq(ss_basename(SS("C:\\Users/test/file.txt")), SS("file.txt")));
14551429
#endif
14561430
}
14571431

14581432
CTEST(slice, dirname) {
1459-
// Simple absolute path
14601433
ASSERT_TRUE(ss_eq(ss_dirname(SS("/usr/local/bin/test")), SS("/usr/local/bin")));
1461-
// Single level
14621434
ASSERT_TRUE(ss_eq(ss_dirname(SS("/test")), SS("/")));
1463-
// Trailing slash
14641435
ASSERT_TRUE(ss_eq(ss_dirname(SS("/usr/local/")), SS("/usr")));
1465-
// No separator — returns empty
14661436
ASSERT_TRUE(ss_dirname(SS("hello.txt")).size == 0);
1467-
// Root
14681437
ASSERT_TRUE(ss_eq(ss_dirname(SS("/")), SS("/")));
1469-
// Relative path
14701438
ASSERT_TRUE(ss_eq(ss_dirname(SS("src/main.c")), SS("src")));
14711439

14721440
#ifdef EXT_WINDOWS
1473-
// Windows-style backslash separators
14741441
ASSERT_TRUE(ss_eq(ss_dirname(SS("C:\\Users\\test\\file.txt")), SS("C:\\Users\\test")));
14751442
ASSERT_TRUE(ss_eq(ss_dirname(SS("C:\\file.txt")), SS("C:")));
1476-
// Mixed separators
14771443
ASSERT_TRUE(ss_eq(ss_dirname(SS("C:\\Users/test/file.txt")), SS("C:\\Users/test")));
14781444
#endif
14791445
}
@@ -1907,30 +1873,23 @@ CTEST(hmap, delete_ss) {
19071873

19081874
CTEST(hmap, get_bytes) {
19091875
HashMap(int, int) map = {0};
1910-
1911-
// Empty map must return NULL without touching a NULL entries pointer.
19121876
ASSERT_TRUE(hmap_get(&map, 1) == NULL);
19131877

19141878
for(int i = 1; i <= 22; i++) {
19151879
hmap_put(&map, i, i * 10);
19161880
}
19171881

1918-
// Hit: correct key and value, inline mutation visible on next lookup.
19191882
ASSERT_TRUE(hmap_get(&map, 5) != NULL);
19201883
ASSERT_TRUE(hmap_get(&map, 5)->key == 5);
19211884
ASSERT_TRUE(hmap_get(&map, 5)->value == 50);
19221885
hmap_get(&map, 5)->value = 99;
19231886
ASSERT_TRUE(hmap_get(&map, 5)->value == 99);
19241887

1925-
// Same key returns the same pointer (stable storage, no realloc triggered here).
1926-
// Note: two get calls must not share an expression — both write to the tmp slot.
19271888
void *p3a = hmap_get(&map, 3);
19281889
void *p3b = hmap_get(&map, 3);
19291890
ASSERT_TRUE(p3a == p3b);
19301891

1931-
// Miss on absent key.
19321892
ASSERT_TRUE(hmap_get(&map, 100) == NULL);
1933-
19341893
hmap_free(&map);
19351894
}
19361895

@@ -2078,7 +2037,6 @@ CTEST(hmap, get_ss) {
20782037
CTEST(hmap, get_default_ss) {
20792038
HashMap(StringSlice, int) map = {0};
20802039

2081-
// Word-count pattern — primary use case for get_default.
20822040
const char *words[] = {"foo", "bar", "foo", "baz", "foo", "bar"};
20832041
for(size_t i = 0; i < 6; i++) {
20842042
hmap_get_default_ss(&map, ss_from_cstr(words[i]), 0)->value++;

0 commit comments

Comments
 (0)