Skip to content

Commit 5bbee8a

Browse files
committed
deduplicate string compare helpers
1 parent 397d7cc commit 5bbee8a

14 files changed

Lines changed: 136 additions & 222 deletions

File tree

src/io/3mf/imp/3mf.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,6 @@ typedef struct AK3MFBlendMethods {
105105
bool cached;
106106
} AK3MFBlendMethods;
107107

108-
static
109-
bool
110-
ak_3mf_slice_eq_cstr(const char * __restrict slice,
111-
size_t sliceLen,
112-
const char * __restrict str) {
113-
size_t len;
114-
115-
if (!slice || !str)
116-
return false;
117-
118-
len = strlen(str);
119-
return len == sliceLen && memcmp(slice, str, len) == 0;
120-
}
121-
122108
AK_INLINE
123109
bool
124110
ak_3mf_space(char c) {
@@ -180,7 +166,8 @@ ak_3mf_cstr_ends_lit(const char * __restrict str,
180166
return false;
181167

182168
len = strlen(str);
183-
return len >= litLen && memcmp(str + len - litLen, lit, litLen) == 0;
169+
return len >= litLen
170+
&& ak_str_eq_fast(str + len - litLen, litLen, lit, litLen);
184171
}
185172

186173
static
@@ -299,17 +286,7 @@ ak_3mf_entry_name_eq_sz(const char * __restrict name,
299286

300287
name = ak_3mf_skip_root_slash(name, &nameLen);
301288
path = ak_3mf_skip_root_slash(path, &pathLen);
302-
return nameLen == pathLen && memcmp(name, path, nameLen) == 0;
303-
}
304-
305-
static
306-
bool
307-
ak_3mf_entry_name_eq(const char * __restrict name,
308-
size_t nameLen,
309-
const char * __restrict path) {
310-
return path
311-
? ak_3mf_entry_name_eq_sz(name, nameLen, path, strlen(path))
312-
: false;
289+
return ak_str_eq_fast(name, nameLen, path, pathLen);
313290
}
314291

315292
static
@@ -341,7 +318,7 @@ ak_3mf_xmla_local_sz(const xml_t * __restrict xml,
341318
attrName = colon + 1u;
342319
}
343320

344-
if (attrNameLen == nameLen && memcmp(attrName, name, nameLen) == 0)
321+
if (ak_str_eq_fast(attrName, attrNameLen, name, nameLen))
345322
return attr;
346323
}
347324

@@ -389,7 +366,7 @@ ak_3mf_attr_value_eq_entry(const xml_attr_t * __restrict attr,
389366

390367
value = attr->val;
391368
valueLen = attr->valsize;
392-
return ak_3mf_entry_name_eq(value, valueLen, entryName);
369+
return ak_3mf_entry_name_eq_sz(value, valueLen, entryName, strlen(entryName));
393370
}
394371

395372
static
@@ -408,7 +385,7 @@ ak_3mf_model_path_eq(const char * __restrict a,
408385
bLen = strlen(b);
409386
a = ak_3mf_skip_root_slash(a, &aLen);
410387
b = ak_3mf_skip_root_slash(b, &bLen);
411-
return aLen == bLen && memcmp(a, b, aLen) == 0;
388+
return ak_str_eq_fast(a, aLen, b, bLen);
412389
}
413390

414391
static
@@ -705,7 +682,7 @@ ak_3mf_tag_sz(const xml_t * __restrict xml,
705682
if (!tag || !ak_3mf_tag_local(xml, &xmlTag, &xmlTagSize))
706683
return false;
707684

708-
return tagSize == xmlTagSize && memcmp(xmlTag, tag, tagSize) == 0;
685+
return ak_str_eq_fast(xmlTag, xmlTagSize, tag, tagSize);
709686
}
710687

711688
static
@@ -1250,6 +1227,9 @@ ak_3mf_content_type_dup(AkDoc * __restrict doc,
12501227

12511228
ext = ak_3mf_entry_extension(entryName);
12521229
if (ext) {
1230+
size_t extLen;
1231+
1232+
extLen = strlen(ext);
12531233
for (child = contentTypesRoot->val; child; child = child->next) {
12541234
xml_attr_t *extension;
12551235
xml_attr_t *contentType;
@@ -1260,7 +1240,7 @@ ak_3mf_content_type_dup(AkDoc * __restrict doc,
12601240
extension = AK_3MF_XMLA(child, Extension);
12611241
if (!extension
12621242
|| !extension->val
1263-
|| !ak_3mf_slice_eq_cstr(extension->val, extension->valsize, ext))
1243+
|| !ak_str_eq_fast(extension->val, extension->valsize, ext, extLen))
12641244
continue;
12651245

12661246
contentType = AK_3MF_XMLA(child, ContentType);
@@ -1760,7 +1740,11 @@ ak_3mf_import_package_part_visitor(const AkZipEntryInfo * __restrict info,
17601740
return true;
17611741
if (AK_3MF_ENTRY_NAME_EQ(info->name, info->nameLen, AK_3MF_CONTENT_TYPES_PART)
17621742
|| AK_3MF_ENTRY_NAME_EQ(info->name, info->nameLen, AK_3MF_ROOT_RELS_PART)
1763-
|| ak_3mf_entry_name_eq(info->name, info->nameLen, st->modelPath))
1743+
|| (st->modelPath
1744+
&& ak_3mf_entry_name_eq_sz(info->name,
1745+
info->nameLen,
1746+
st->modelPath,
1747+
strlen(st->modelPath))))
17641748
return true;
17651749

17661750
entryName = malloc(info->nameLen + 1u);

src/io/dae/1.4/dae14.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "../../../../include/ak/assetkit.h"
1818
#include "../common.h"
1919
#include "dae14.h"
20+
#include "../../../string_fast.h"
2021

2122
static
2223
AkNewParam*
@@ -82,9 +83,7 @@ dae14_find_image_by_id_n(AkDoc * __restrict doc,
8283
const char *imageId;
8384

8485
imageId = ak_getId(image);
85-
if (imageId
86-
&& strlen(imageId) == idLen
87-
&& memcmp(imageId, id, idLen) == 0)
86+
if (ak_str_eq_cstr_fast(imageId, id, idLen))
8887
return image;
8988
}
9089

@@ -111,7 +110,10 @@ dae14_find_image_id_for_surface_source(DAEState * __restrict dst,
111110

112111
suffixLen = sizeof(suffix) - 1;
113112
if (sidLen > suffixLen
114-
&& memcmp(sid + sidLen - suffixLen, suffix, suffixLen) == 0) {
113+
&& ak_str_eq_fast(sid + sidLen - suffixLen,
114+
suffixLen,
115+
suffix,
116+
suffixLen)) {
115117
if ((image = dae14_find_image_by_id_n(doc, sid, sidLen - suffixLen)))
116118
return ak_getId(image);
117119
}

src/io/dae/exp/image.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,19 @@ dae_prepare_texture_image(DAEExpState * __restrict st,
6262
return !tex || !tex->image || dae_prepare_extra_image(st, tex->image);
6363
}
6464

65-
static
66-
bool
67-
dae_cstr_eq_token(const char * __restrict val,
68-
const char * __restrict token,
69-
size_t tokenLen) {
70-
return ak_str_eq_cstr_fast(val, token, tokenLen);
71-
}
72-
7365
static
7466
const char*
7567
dae_image_mime_ext(AkImageSource * __restrict source) {
7668
const char *mime;
7769

7870
mime = source ? source->mimeType : NULL;
79-
if (dae_cstr_eq_token(mime, "image/png", sizeof("image/png") - 1u))
71+
if (ak_str_eq_cstr_fast(mime, "image/png", sizeof("image/png") - 1u))
8072
return ".png";
81-
if (dae_cstr_eq_token(mime, "image/jpeg", sizeof("image/jpeg") - 1u))
73+
if (ak_str_eq_cstr_fast(mime, "image/jpeg", sizeof("image/jpeg") - 1u))
8274
return ".jpg";
83-
if (dae_cstr_eq_token(mime, "image/webp", sizeof("image/webp") - 1u))
75+
if (ak_str_eq_cstr_fast(mime, "image/webp", sizeof("image/webp") - 1u))
8476
return ".webp";
85-
if (dae_cstr_eq_token(mime, "image/ktx2", sizeof("image/ktx2") - 1u))
77+
if (ak_str_eq_cstr_fast(mime, "image/ktx2", sizeof("image/ktx2") - 1u))
8678
return ".ktx2";
8779

8880
return ".bin";

src/io/dae/fx/techn.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../1.4/image.h"
2424
#include "../bugfix/transp.h"
2525
#include "../../../default/material.h"
26+
#include "../../../string_fast.h"
2627

2728
#include <string.h>
2829

@@ -44,7 +45,7 @@ bool
4445
dae_xmlAttrEq(const xml_attr_t * __restrict attr,
4546
const char * __restrict value,
4647
size_t len) {
47-
return attr && attr->val && attr->valsize == len && memcmp(attr->val, value, len) == 0;
48+
return attr && ak_str_eq_fast(attr->val, attr->valsize, value, len);
4849
}
4950

5051
AK_HIDE

src/io/gltf/exp/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "../../../../include/ak/assetkit.h"
2121
#include "../../../image/export.h"
22+
#include "../../../string_fast.h"
2223

2324
#include <ds/rb.h>
2425

src/io/gltf/exp/extra.c

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ gltf_extra_type(const AkTreeNode * __restrict node) {
4444
return NULL;
4545
}
4646

47-
static
48-
bool
49-
gltf_extra_type_eq(const AkTreeNode * __restrict node,
50-
const char * __restrict type) {
51-
const char *nodeType;
52-
53-
nodeType = gltf_extra_type(node);
54-
return nodeType && strcmp(nodeType, type) == 0;
55-
}
56-
5747
AkTreeNode*
5848
gltf_extra_extensions_node(AkTreeNode * __restrict extra) {
5949
return gltf_extra_child(extra,
@@ -80,16 +70,6 @@ gltf_extra_root_required(GLTFExpState * __restrict st) {
8070
_s_gltf_extensionsRequired_len);
8171
}
8272

83-
static
84-
bool
85-
gltf_extra_value_eq(const AkTreeNode * __restrict node,
86-
const char * __restrict name,
87-
size_t nameLen) {
88-
return node
89-
&& node->val
90-
&& ak_str_eq_cstr_fast(node->val, name, nameLen);
91-
}
92-
9373
static
9474
bool
9575
gltf_extra_array_contains(const AkTreeNode * __restrict array,
@@ -98,7 +78,7 @@ gltf_extra_array_contains(const AkTreeNode * __restrict array,
9878
AkTreeNode *child;
9979

10080
for (child = array ? array->chld : NULL; child; child = child->next) {
101-
if (gltf_extra_value_eq(child, name, nameLen))
81+
if (child->val && ak_str_eq_cstr_fast(child->val, name, nameLen))
10282
return true;
10383
}
10484

@@ -154,15 +134,6 @@ gltf_extra_json_number(const char * __restrict val, size_t len) {
154134
return i == len;
155135
}
156136

157-
static
158-
bool
159-
gltf_extra_literal(const char * __restrict val,
160-
size_t len,
161-
const char * __restrict literal,
162-
size_t literalLen) {
163-
return len == literalLen && memcmp(val, literal, literalLen) == 0;
164-
}
165-
166137
static
167138
bool
168139
gltf_extra_reserved_root_child(AkTreeNode * __restrict node) {
@@ -259,9 +230,9 @@ gltf_write_extra_leaf(GLTFExpWriter * __restrict w,
259230
}
260231

261232
len = strlen(val);
262-
if (gltf_extra_literal(val, len, "true", 4)
263-
|| gltf_extra_literal(val, len, "false", 5)
264-
|| gltf_extra_literal(val, len, "null", 4)
233+
if (ak_str_eq_fast(val, len, "true", 4)
234+
|| ak_str_eq_fast(val, len, "false", 5)
235+
|| ak_str_eq_fast(val, len, "null", 4)
265236
|| gltf_extra_json_number(val, len)) {
266237
gltf_w_raw(w, val, len);
267238
return;
@@ -277,7 +248,7 @@ gltf_extra_has_json_extras(AkTreeNode * __restrict node) {
277248
if (!node)
278249
return false;
279250

280-
if (gltf_extra_type_eq(node, "array"))
251+
if (ak_str_eq_cstr_fast(gltf_extra_type(node), "array", sizeof("array") - 1u))
281252
return true;
282253

283254
if (!node->chld)
@@ -297,7 +268,11 @@ gltf_write_extra_json_extras(GLTFExpWriter * __restrict w,
297268
AkTreeNode *child;
298269
bool comma;
299270

300-
if (!node || gltf_extra_type_eq(node, "array") || !node->chld) {
271+
if (!node
272+
|| ak_str_eq_cstr_fast(gltf_extra_type(node),
273+
"array",
274+
sizeof("array") - 1u)
275+
|| !node->chld) {
301276
gltf_write_extra_json_value(w, node);
302277
return;
303278
}
@@ -370,14 +345,18 @@ gltf_write_extra_extensions_member(GLTFExpWriter * __restrict w,
370345
void
371346
gltf_write_extra_json_value(GLTFExpWriter * __restrict w,
372347
AkTreeNode * __restrict node) {
348+
const char *nodeType;
349+
373350
if (!node) {
374351
gltf_w_raw(w, "null", 4);
375352
return;
376353
}
377354

378-
if (gltf_extra_type_eq(node, "array")) {
355+
nodeType = gltf_extra_type(node);
356+
if (ak_str_eq_cstr_fast(nodeType, "array", sizeof("array") - 1u)) {
379357
gltf_write_extra_array(w, node);
380-
} else if (gltf_extra_type_eq(node, "object") || node->chld) {
358+
} else if (ak_str_eq_cstr_fast(nodeType, "object", sizeof("object") - 1u)
359+
|| node->chld) {
381360
gltf_write_extra_object(w, node);
382361
} else {
383362
gltf_write_extra_leaf(w, node);

0 commit comments

Comments
 (0)