Skip to content

Commit

Permalink
Merge pull request #592 from zeux/gltf-objws
Browse files Browse the repository at this point in the history
extern: Fix fast_obj to trim trailing whitespace for names
  • Loading branch information
zeux authored Aug 14, 2023
2 parents 6ebea48 + 6e8922f commit 267f560
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions extern/fast_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,6 @@ int is_whitespace(char c)
return (c == ' ' || c == '\t' || c == '\r');
}

static
int is_end_of_name(char c)
{
return (c == '\t' || c == '\r' || c == '\n');
}

static
int is_newline(char c)
{
Expand All @@ -440,6 +434,21 @@ int is_exponent(char c)
}


static
const char* skip_name(const char* ptr)
{
const char* s = ptr;

while (!is_newline(*ptr))
ptr++;

while (ptr > s && is_whitespace(*(ptr - 1)))
ptr--;

return ptr;
}


static
const char* skip_whitespace(const char* ptr)
{
Expand Down Expand Up @@ -791,9 +800,7 @@ const char* parse_object(fastObjData* data, const char* ptr)
ptr = skip_whitespace(ptr);

s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

flush_object(data);
Expand All @@ -813,9 +820,7 @@ const char* parse_group(fastObjData* data, const char* ptr)
ptr = skip_whitespace(ptr);

s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

flush_group(data);
Expand Down Expand Up @@ -894,9 +899,7 @@ const char* parse_usemtl(fastObjData* data, const char* ptr)

/* Parse the material name */
s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

/* Find an existing material with the same name */
Expand Down Expand Up @@ -992,9 +995,7 @@ const char* read_map(fastObjData* data, const char* ptr, fastObjTexture* map)

/* Read name */
s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

name = string_copy(s, e);
Expand Down Expand Up @@ -1068,8 +1069,7 @@ int read_mtllib(fastObjData* data, void* file, const fastObjCallbacks* callbacks
p++;

s = p;
while (!is_end_of_name(*p))
p++;
p = skip_name(p);

mtl.name = string_copy(s, p);
}
Expand Down Expand Up @@ -1211,9 +1211,7 @@ const char* parse_mtllib(fastObjData* data, const char* ptr, const fastObjCallba
ptr = skip_whitespace(ptr);

s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

lib = string_concat(data->base, s, e);
Expand Down

0 comments on commit 267f560

Please sign in to comment.